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

Commit

Permalink
feat: Add support for Lightsail Instances (#1138)
Browse files Browse the repository at this point in the history
* Add support for Lightsail Instances, along with tests and documentation
* Fix a bug in TagIntoMap that dropped key-only tags
  • Loading branch information
hermanschaaf authored Jul 6, 2022
1 parent 425b8cf commit bcfb724
Show file tree
Hide file tree
Showing 17 changed files with 1,170 additions and 13 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/iot"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/aws/aws-sdk-go-v2/service/mq"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/qldb"
Expand Down Expand Up @@ -153,6 +154,7 @@ type Services struct {
IOT IOTClient
KMS KmsClient
Lambda LambdaClient
Lightsail LightsailClient
MQ MQClient
Organizations OrganizationsClient
QLDB QLDBClient
Expand Down Expand Up @@ -624,6 +626,7 @@ func initServices(region string, c aws.Config) Services {
IAM: iam.NewFromConfig(awsCfg),
KMS: kms.NewFromConfig(awsCfg),
Lambda: lambda.NewFromConfig(awsCfg),
Lightsail: lightsail.NewFromConfig(awsCfg),
MQ: mq.NewFromConfig(awsCfg),
Organizations: organizations.NewFromConfig(awsCfg),
QLDB: qldb.NewFromConfig(awsCfg),
Expand Down
12 changes: 9 additions & 3 deletions client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ func TagsIntoMap(tagSlice interface{}, dst map[string]string) {
panic("field is not string or *string")
}

if v.IsNil() {
// return empty string if string pointer is nil
return ""
}

return v.Elem().String()
}

Expand All @@ -354,13 +359,14 @@ func TagsIntoMap(tagSlice interface{}, dst map[string]string) {
panic("slice member is not struct: " + k.String())
}

// key cannot be nil, but value can in the case of key-only tags
keyField, valField := val.FieldByName("Key"), val.FieldByName("Value")
if (keyField.Type().Kind() == reflect.Ptr && keyField.IsNil()) || (valField.Type().Kind() == reflect.Ptr && valField.IsNil()) {
if keyField.Type().Kind() == reflect.Ptr && keyField.IsNil() {
continue
}

if keyField.IsZero() || valField.IsZero() {
panic("slice member is missing Key or Value fields")
if keyField.IsZero() {
panic("slice member is missing Key field")
}

dst[stringify(keyField)] = stringify(valField)
Expand Down
2 changes: 1 addition & 1 deletion client/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestTagsToMap(t *testing.T) {
Value: nil,
},
},
Expected: map[string]string{"k": "v"},
Expected: map[string]string{"k": "v", "emptyvalue": ""},
},
{
Input: []ttypes.Tag{
Expand Down
56 changes: 56 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.

6 changes: 6 additions & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/iot"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/aws/aws-sdk-go-v2/service/mq"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/qldb"
Expand Down Expand Up @@ -369,6 +370,11 @@ type KmsClient interface {
ListResourceTags(ctx context.Context, params *kms.ListResourceTagsInput, optFns ...func(*kms.Options)) (*kms.ListResourceTagsOutput, error)
}

//go:generate mockgen -package=mocks -destination=./mocks/mock_lightsail.go . LightsailClient
type LightsailClient interface {
GetInstances(ctx context.Context, params *lightsail.GetInstancesInput, optFns ...func(*lightsail.Options)) (*lightsail.GetInstancesOutput, error)
}

//go:generate mockgen -package=mocks -destination=./mocks/mock_mq.go . MQClient
type MQClient interface {
DescribeBroker(ctx context.Context, params *mq.DescribeBrokerInput, optFns ...func(*mq.Options)) (*mq.DescribeBrokerOutput, error)
Expand Down
6 changes: 3 additions & 3 deletions docs/contributing/adding_a_new_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ aws configure sso
To run an integration test for a specific table:

```bash
go test -run="TestIntegration/ROOT_TABLE_NAME" -tags=integration ./...
go test -run="TestIntegration/RESOURCE_NAME" -tags=integration ./...
# For example
go test -run="TestIntegration/aws_lambda_functions" -tags=integration ./...
```
Expand All @@ -87,7 +87,7 @@ To run all integration tests:
go test -run=TestIntegration -tags=integration ./...
```

>**Important** When adding a single resource, it's more common to only run the test for a specific table. You'll need to ensure your resource has the relevant Terraform service deployed.
>**Important** When adding a single resource, it's more common to only run the integration tests for that specific resource. You'll need to ensure your resource has the relevant Terraform service deployed.
#### Adding new Terraform Files Guidelines

Expand All @@ -112,5 +112,5 @@ cd terraform/YOUR_SERVICE_NAME/local
terraform init
# Replace AB with your own initials so multiple team members can work on the same account without conflicting resources
terraform apply -var="prefix=AB"
go test -run="TestIntegration/ROOT_TABLE_NAME" -tags=integration ./...
go test -run="TestIntegration/RESOURCE_NAME" -tags=integration ./...
```
11 changes: 11 additions & 0 deletions docs/tables/aws_lightsail_instance_add_ons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Table: aws_lightsail_instance_add_ons
Describes an add-on that is enabled for an Amazon Lightsail resource.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|instance_cq_id|uuid|Unique CloudQuery ID of aws_lightsail_instances table (FK)|
|name|text|The name of the add-on.|
|next_snapshot_time_of_day|text|The next daily time an automatic snapshot will be created|
|snapshot_time_of_day|text|The daily time when an automatic snapshot is created|
|status|text|The status of the add-on.|
11 changes: 11 additions & 0 deletions docs/tables/aws_lightsail_instance_hardware_disk_add_ons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Table: aws_lightsail_instance_hardware_disk_add_ons
Describes an add-on that is enabled for an Amazon Lightsail resource.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|instance_hardware_disk_cq_id|uuid|Unique CloudQuery ID of aws_lightsail_instance_hardware_disks table (FK)|
|name|text|The name of the add-on.|
|next_snapshot_time_of_day|text|The next daily time an automatic snapshot will be created|
|snapshot_time_of_day|text|The daily time when an automatic snapshot is created|
|status|text|The status of the add-on.|
22 changes: 22 additions & 0 deletions docs/tables/aws_lightsail_instance_hardware_disks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Table: aws_lightsail_instance_hardware_disks
Describes a block storage disk.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|instance_cq_id|uuid|Unique CloudQuery ID of aws_lightsail_instances table (FK)|
|arn|text|The Amazon Resource Name (ARN) of the disk.|
|attached_to|text|The resources to which the disk is attached.|
|created_at|timestamp without time zone|The date when the disk was created.|
|iops|integer|The input/output operations per second (IOPS) of the disk.|
|is_attached|boolean|A Boolean value indicating whether the disk is attached.|
|is_system_disk|boolean|A Boolean value indicating whether this disk is a system disk (has an operating system loaded on it).|
|location_availability_zone|text|The Availability Zone|
|location_region_name|text|The AWS Region name.|
|name|text|The unique name of the disk.|
|path|text|The disk path.|
|resource_type|text|The Lightsail resource type (e.g., Disk).|
|size_in_gb|integer|The size of the disk in GB.|
|state|text|Describes the status of the disk.|
|support_code|text|The support code|
|tags|jsonb|The tag keys and optional values for the resource|
17 changes: 17 additions & 0 deletions docs/tables/aws_lightsail_instance_networking_ports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# Table: aws_lightsail_instance_networking_ports
Describes information about ports for an Amazon Lightsail instance.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|instance_cq_id|uuid|Unique CloudQuery ID of aws_lightsail_instances table (FK)|
|access_direction|text|The access direction (inbound or outbound)|
|access_from|text|The location from which access is allowed|
|access_type|text|The type of access (Public or Private).|
|cidr_list_aliases|text[]|An alias that defines access for a preconfigured range of IP addresses|
|cidrs|text[]|The IPv4 address, or range of IPv4 addresses (in CIDR notation) that are allowed to connect to an instance through the ports, and the protocol|
|common_name|text|The common name of the port information.|
|from_port|integer|The first port in a range of open ports on an instance|
|ipv6_cidrs|text[]|The IPv6 address, or range of IPv6 addresses (in CIDR notation) that are allowed to connect to an instance through the ports, and the protocol|
|protocol|text|The IP protocol name|
|to_port|integer|The last port in a range of open ports on an instance|
31 changes: 31 additions & 0 deletions docs/tables/aws_lightsail_instances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Table: aws_lightsail_instances
Describes an instance (a virtual private server).
## 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 instance (e.g., arn:aws:lightsail:us-east-2:123456789101:Instance/244ad76f-8aad-4741-809f-12345EXAMPLE).|
|blueprint_id|text|The blueprint ID (e.g., os_amlinux_2016_03).|
|blueprint_name|text|The friendly name of the blueprint (e.g., Amazon Linux).|
|bundle_id|text|The bundle for the instance (e.g., micro_1_0).|
|created_at|timestamp without time zone|The timestamp when the instance was created (e.g., 1479734909.17) in Unix time format.|
|hardware_cpu_count|integer|The number of vCPUs the instance has.|
|hardware_ram_size_in_gb|float|The amount of RAM in GB on the instance (e.g., 1.0).|
|ip_address_type|text|The IP address type of the instance|
|ipv6_addresses|text[]|The IPv6 addresses of the instance.|
|is_static_ip|boolean|A Boolean value indicating whether this instance has a static IP assigned to it.|
|location_availability_zone|text|The Availability Zone|
|location_region_name|text|The AWS Region name.|
|name|text|The name the user gave the instance (e.g., Amazon_Linux-1GB-Ohio-1).|
|networking_monthly_transfer_gb_per_month_allocated|integer|The amount allocated per month (in GB).|
|private_ip_address|text|The private IP address of the instance.|
|public_ip_address|text|The public IP address of the instance.|
|resource_type|text|The type of resource (usually Instance).|
|ssh_key_name|text|The name of the SSH key being used to connect to the instance (e.g., LightsailDefaultKeyPair).|
|state_code|integer|The status code for the instance.|
|state_name|text|The state of the instance (e.g., running or pending).|
|support_code|text|The support code|
|tags|jsonb|The tag keys and optional values for the resource|
|username|text|The user name for connecting to the instance (e.g., ec2-user).|
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/iot v1.25.4
github.com/aws/aws-sdk-go-v2/service/kms v1.17.4
github.com/aws/aws-sdk-go-v2/service/lambda v1.23.3
github.com/aws/aws-sdk-go-v2/service/lightsail v1.22.2
github.com/aws/aws-sdk-go-v2/service/mq v1.13.3
github.com/aws/aws-sdk-go-v2/service/organizations v1.16.3
github.com/aws/aws-sdk-go-v2/service/qldb v1.14.8
Expand Down Expand Up @@ -76,6 +77,8 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cast v1.5.0
github.com/stretchr/testify v1.8.0
github.com/thoas/go-funk v0.9.2
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
)

require (
Expand Down Expand Up @@ -118,7 +121,7 @@ require (
github.com/getkin/kin-openapi v0.83.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -139,9 +142,10 @@ require (
github.com/jackc/puddle v1.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand All @@ -152,7 +156,6 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/thoas/go-funk v0.9.2
github.com/vektah/gqlparser/v2 v2.2.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand All @@ -161,7 +164,6 @@ require (
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.11 // indirect
Expand Down
Loading

0 comments on commit bcfb724

Please sign in to comment.