Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

feat: Added lightsail alarms #1242

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client/mocks/mock_lightsail.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ type LightsailClient interface {
GetInstances(ctx context.Context, params *lightsail.GetInstancesInput, optFns ...func(*lightsail.Options)) (*lightsail.GetInstancesOutput, error)
GetBuckets(ctx context.Context, params *lightsail.GetBucketsInput, optFns ...func(*lightsail.Options)) (*lightsail.GetBucketsOutput, error)
GetBucketAccessKeys(ctx context.Context, params *lightsail.GetBucketAccessKeysInput, optFns ...func(*lightsail.Options)) (*lightsail.GetBucketAccessKeysOutput, error)
GetAlarms(ctx context.Context, params *lightsail.GetAlarmsInput, optFns ...func(*lightsail.Options)) (*lightsail.GetAlarmsOutput, error)
}

//go:generate mockgen -package=mocks -destination=./mocks/mock_mq.go . MQClient
Expand Down
31 changes: 31 additions & 0 deletions docs/tables/aws_lightsail_alarms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Table: aws_lightsail_alarms
Describes an alarm
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|account_id|text|The AWS Account ID of the resource.|
|region|text|The AWS Region of the resource.|
|arn|text|The Amazon Resource Name (ARN) of the alarm|
|comparison_operator|text|The arithmetic operation used when comparing the specified statistic and threshold|
|contact_protocols|text[]|The contact protocols for the alarm, such as Email, SMS (text messaging), or both|
|created_at|timestamp without time zone|The timestamp when the alarm was created|
|datapoints_to_alarm|integer|The number of data points that must not within the specified threshold to trigger the alarm|
|evaluation_periods|integer|The number of periods over which data is compared to the specified threshold|
|location_availability_zone|text|The Availability Zone|
|location_region_name|text|The AWS Region name|
|metric_name|text|The name of the metric associated with the alarm|
|monitored_resource_info_arn|text|The Amazon Resource Name (ARN) of the resource being monitored|
|monitored_resource_info_name|text|The name of the Lightsail resource being monitored|
|monitored_resource_info_resource_type|text|The Lightsail resource type of the resource being monitored|
|name|text|The name of the alarm|
|notification_enabled|boolean|Indicates whether the alarm is enabled|
|notification_triggers|text[]|The alarm states that trigger a notification|
|period|integer|The period, in seconds, over which the statistic is applied|
|resource_type|text|The Lightsail resource type (eg, Alarm)|
|state|text|The current state of the alarm|
|statistic|text|The statistic for the metric associated with the alarm|
|support_code|text|The support code|
|threshold|float|The value against which the specified statistic is compared|
|treat_missing_data|text|Specifies how the alarm handles missing data points|
|unit|text|The unit of the metric associated with the alarm|
1 change: 1 addition & 0 deletions resources/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func Provider() *provider.Provider {
"lambda.functions": lambda.Functions(),
"lambda.layers": lambda.LambdaLayers(),
"lambda.runtimes": lambda.LambdaRuntimes(),
"lightsail.alarms": lightsail.Alarms(),
"lightsail.buckets": lightsail.Buckets(),
"lightsail.instances": lightsail.Instances(),
"mq.brokers": mq.Brokers(),
Expand Down
183 changes: 183 additions & 0 deletions resources/services/lightsail/alarms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package lightsail

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

//go:generate cq-gen --resource alarms --config gen.hcl --output .
func Alarms() *schema.Table {
return &schema.Table{
Name: "aws_lightsail_alarms",
Description: "Describes an alarm",
Resolver: fetchLightsailAlarms,
Multiplex: client.ServiceAccountRegionMultiplexer("lightsail"),
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
IgnoreInTests: true, // it can't be included into e2e test, because there is no way to deploy it using terraform
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the alarm",
Type: schema.TypeString,
},
{
Name: "comparison_operator",
Description: "The arithmetic operation used when comparing the specified statistic and threshold",
Type: schema.TypeString,
},
{
Name: "contact_protocols",
Description: "The contact protocols for the alarm, such as Email, SMS (text messaging), or both",
Type: schema.TypeStringArray,
},
{
Name: "created_at",
Description: "The timestamp when the alarm was created",
Type: schema.TypeTimestamp,
},
{
Name: "datapoints_to_alarm",
Description: "The number of data points that must not within the specified threshold to trigger the alarm",
Type: schema.TypeInt,
},
{
Name: "evaluation_periods",
Description: "The number of periods over which data is compared to the specified threshold",
Type: schema.TypeInt,
},
{
Name: "location_availability_zone",
Description: "The Availability Zone",
Type: schema.TypeString,
Resolver: schema.PathResolver("Location.AvailabilityZone"),
},
{
Name: "location_region_name",
Description: "The AWS Region name",
Type: schema.TypeString,
Resolver: schema.PathResolver("Location.RegionName"),
},
amanenk marked this conversation as resolved.
Show resolved Hide resolved
{
Name: "metric_name",
Description: "The name of the metric associated with the alarm",
Type: schema.TypeString,
},
{
Name: "monitored_resource_info_arn",
Description: "The Amazon Resource Name (ARN) of the resource being monitored",
Type: schema.TypeString,
Resolver: schema.PathResolver("MonitoredResourceInfo.Arn"),
},
{
Name: "monitored_resource_info_name",
amanenk marked this conversation as resolved.
Show resolved Hide resolved
Description: "The name of the Lightsail resource being monitored",
Type: schema.TypeString,
Resolver: schema.PathResolver("MonitoredResourceInfo.Name"),
},
{
Name: "monitored_resource_info_resource_type",
amanenk marked this conversation as resolved.
Show resolved Hide resolved
Description: "The Lightsail resource type of the resource being monitored",
Type: schema.TypeString,
Resolver: schema.PathResolver("MonitoredResourceInfo.ResourceType"),
},
{
Name: "name",
Description: "The name of the alarm",
Type: schema.TypeString,
},
{
Name: "notification_enabled",
Description: "Indicates whether the alarm is enabled",
Type: schema.TypeBool,
},
{
Name: "notification_triggers",
Description: "The alarm states that trigger a notification",
Type: schema.TypeStringArray,
},
{
Name: "period",
Description: "The period, in seconds, over which the statistic is applied",
Type: schema.TypeInt,
},
{
Name: "resource_type",
Description: "The Lightsail resource type (eg, Alarm)",
Type: schema.TypeString,
},
{
Name: "state",
Description: "The current state of the alarm",
Type: schema.TypeString,
},
{
Name: "statistic",
Description: "The statistic for the metric associated with the alarm",
Type: schema.TypeString,
},
{
Name: "support_code",
Description: "The support code",
Type: schema.TypeString,
},
{
Name: "threshold",
Description: "The value against which the specified statistic is compared",
Type: schema.TypeFloat,
},
{
Name: "treat_missing_data",
Description: "Specifies how the alarm handles missing data points",
Type: schema.TypeString,
},
{
Name: "unit",
Description: "The unit of the metric associated with the alarm",
Type: schema.TypeString,
},
},
}
}

// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================

func fetchLightsailAlarms(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
var input lightsail.GetAlarmsInput
c := meta.(*client.Client)
svc := c.Services().Lightsail
for {
response, err := svc.GetAlarms(ctx, &input, func(options *lightsail.Options) {
options.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
res <- response.Alarms
if aws.ToString(response.NextPageToken) == "" {
break
}
input.PageToken = response.NextPageToken
}
return nil
}
32 changes: 32 additions & 0 deletions resources/services/lightsail/alarms_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lightsail

import (
"testing"

"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
"github.com/cloudquery/faker/v3"
"github.com/golang/mock/gomock"
)

func buildAlarmsMock(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockLightsailClient(ctrl)

b := lightsail.GetAlarmsOutput{}
err := faker.FakeData(&b)
if err != nil {
t.Fatal(err)
}
b.NextPageToken = nil
m.EXPECT().GetAlarms(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&b, nil)

return client.Services{
Lightsail: m,
}
}

func TestAlarms(t *testing.T) {
client.AwsMockTestHelper(t, Alarms(), buildAlarmsMock, client.TestOptions{})
}
2 changes: 1 addition & 1 deletion resources/services/lightsail/buckets_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func buildBucketsMock(t *testing.T, ctrl *gomock.Controller) client.Services {
}
}

func TestLambdaFunctions(t *testing.T) {
func TestBuckets(t *testing.T) {
client.AwsMockTestHelper(t, Buckets(), buildBucketsMock, client.TestOptions{})
}
37 changes: 35 additions & 2 deletions resources/services/lightsail/gen.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ resource "aws" "lightsail" "instances" {
}
}



resource "aws" "lightsail" "buckets" {
path = "github.com/aws/aws-sdk-go-v2/service/lightsail/types.Bucket"
ignoreError "IgnoreAccessDenied" {
Expand Down Expand Up @@ -119,4 +117,39 @@ resource "aws" "lightsail" "buckets" {
user_relation "aws" "lightsail" "access_keys" {
path = "github.com/aws/aws-sdk-go-v2/service/lightsail/types.AccessKey"
}
}


resource "aws" "lightsail" "alarms" {
path = "github.com/aws/aws-sdk-go-v2/service/lightsail/types.Alarm"
ignoreError "IgnoreAccessDenied" {
path = "github.com/cloudquery/cq-provider-aws/client.IgnoreAccessDeniedServiceDisabled"
}
multiplex "AwsAccountRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ServiceAccountRegionMultiplexer"
params = ["lightsail"]
}
deleteFilter "AccountRegionFilter" {
path = "github.com/cloudquery/cq-provider-aws/client.DeleteAccountRegionFilter"
}

options {
primary_keys = [
"arn"
]
}
userDefinedColumn "account_id" {
type = "string"
description = "The AWS Account ID of the resource."
resolver "resolveAWSAccount" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSAccount"
}
}
userDefinedColumn "region" {
type = "string"
description = "The AWS Region of the resource."
resolver "resolveAWSRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSRegion"
}
}
}