From ad9a31a2caa2a34cdbb9f5f391812b6515507818 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Tue, 20 Dec 2022 10:37:00 +0200 Subject: [PATCH 1/5] more aws elasticsearch resources --- .../aws/codegen/recipes/elasticsearch.go | 52 ++++++++++-- plugins/source/aws/docs/tables/README.md | 3 + .../docs/tables/aws_elasticsearch_domains.md | 3 + .../docs/tables/aws_elasticsearch_packages.md | 25 ++++++ .../docs/tables/aws_elasticsearch_versions.md | 18 ++++ .../tables/aws_elasticsearch_vpc_endpoints.md | 22 +++++ plugins/source/aws/resources/plugin/tables.go | 3 + .../services/elasticsearch/domains.go | 15 ++-- .../services/elasticsearch/domains_fetch.go | 53 +++++++++--- .../elasticsearch/domains_mock_test.go | 37 +++++---- .../services/elasticsearch/packages.go | 83 +++++++++++++++++++ .../services/elasticsearch/packages_fetch.go | 25 ++++++ .../elasticsearch/packages_mock_test.go | 33 ++++++++ .../services/elasticsearch/tags_fetch.go | 35 -------- .../elasticsearch/tags_fetch_mock_test.go | 18 ---- .../services/elasticsearch/versions.go | 48 +++++++++++ .../services/elasticsearch/versions_fetch.go | 54 ++++++++++++ .../elasticsearch/versions_mock_test.go | 44 ++++++++++ .../services/elasticsearch/vpc_endpoints.go | 62 ++++++++++++++ .../elasticsearch/vpc_endpoints_fetch.go | 58 +++++++++++++ .../elasticsearch/vpc_endpoints_mock_test.go | 46 ++++++++++ .../pages/docs/plugins/sources/aws/tables.md | 3 + 22 files changed, 649 insertions(+), 91 deletions(-) create mode 100644 plugins/source/aws/docs/tables/aws_elasticsearch_packages.md create mode 100644 plugins/source/aws/docs/tables/aws_elasticsearch_versions.md create mode 100644 plugins/source/aws/docs/tables/aws_elasticsearch_vpc_endpoints.md create mode 100644 plugins/source/aws/resources/services/elasticsearch/packages.go create mode 100755 plugins/source/aws/resources/services/elasticsearch/packages_fetch.go create mode 100644 plugins/source/aws/resources/services/elasticsearch/packages_mock_test.go delete mode 100644 plugins/source/aws/resources/services/elasticsearch/tags_fetch.go delete mode 100644 plugins/source/aws/resources/services/elasticsearch/tags_fetch_mock_test.go create mode 100644 plugins/source/aws/resources/services/elasticsearch/versions.go create mode 100755 plugins/source/aws/resources/services/elasticsearch/versions_fetch.go create mode 100644 plugins/source/aws/resources/services/elasticsearch/versions_mock_test.go create mode 100644 plugins/source/aws/resources/services/elasticsearch/vpc_endpoints.go create mode 100755 plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go create mode 100644 plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_mock_test.go diff --git a/plugins/source/aws/codegen/recipes/elasticsearch.go b/plugins/source/aws/codegen/recipes/elasticsearch.go index b7710d4eb93664..caa25864d4f5e1 100644 --- a/plugins/source/aws/codegen/recipes/elasticsearch.go +++ b/plugins/source/aws/codegen/recipes/elasticsearch.go @@ -9,18 +9,58 @@ import ( func ElasticsearchResources() []*Resource { resources := []*Resource{ { - SubService: "domains", - Struct: &types.ElasticsearchDomainStatus{}, - PreResourceResolver: "getDomain", - PKColumns: []string{"arn"}, - ExtraColumns: append(defaultRegionalColumns, + SubService: "domains", + Struct: new(types.ElasticsearchDomainStatus), + Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html", + PKColumns: []string{"arn"}, + ExtraColumns: append( + defaultRegionalColumns, + codegen.ColumnDefinition{ + Name: "authorized_principals", + Type: schema.TypeJSON, + Resolver: `resolveAuthorizedPrincipals`, + }, codegen.ColumnDefinition{ Name: "tags", Type: schema.TypeJSON, - Resolver: `resolveTags`, + Resolver: `resolveDomainTags`, }, ), }, + { + SubService: "packages", + Struct: new(types.PackageDetails), + Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_PackageDetails.html", + NameTransformer: CreateReplaceTransformer(map[string]string{"package_id": "id"}), + PKColumns: []string{"id"}, + ExtraColumns: defaultRegionalColumnsPK, + }, + { + SubService: "versions", + Struct: new(struct{}), + Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_ListVersions.html", + PKColumns: []string{"version"}, + ExtraColumns: append(defaultRegionalColumnsPK, + codegen.ColumnDefinition{ + Name: "version", + Type: schema.TypeString, + Resolver: `resolveVersion`, + }, + codegen.ColumnDefinition{ + Name: "instance_types", + Type: schema.TypeJSON, + Resolver: `resolveInstanceTypes`, + }, + ), + }, + { + SubService: "vpc_endpoints", + Struct: new(types.VpcEndpoint), + Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_VpcEndpoint.html", + NameTransformer: CreateReplaceTransformer(map[string]string{"vpc_endpoint_id": "id"}), + PKColumns: []string{"id"}, + ExtraColumns: defaultRegionalColumns, + }, } // set default values diff --git a/plugins/source/aws/docs/tables/README.md b/plugins/source/aws/docs/tables/README.md index ca91558b7bd68c..d0e11b997e83e8 100644 --- a/plugins/source/aws/docs/tables/README.md +++ b/plugins/source/aws/docs/tables/README.md @@ -195,6 +195,9 @@ - [aws_elasticbeanstalk_configuration_settings](aws_elasticbeanstalk_configuration_settings.md) - [aws_elasticbeanstalk_configuration_options](aws_elasticbeanstalk_configuration_options.md) - [aws_elasticsearch_domains](aws_elasticsearch_domains.md) +- [aws_elasticsearch_packages](aws_elasticsearch_packages.md) +- [aws_elasticsearch_versions](aws_elasticsearch_versions.md) +- [aws_elasticsearch_vpc_endpoints](aws_elasticsearch_vpc_endpoints.md) - [aws_elastictranscoder_pipelines](aws_elastictranscoder_pipelines.md) - [aws_elastictranscoder_pipeline_jobs](aws_elastictranscoder_pipeline_jobs.md) - [aws_elastictranscoder_presets](aws_elastictranscoder_presets.md) diff --git a/plugins/source/aws/docs/tables/aws_elasticsearch_domains.md b/plugins/source/aws/docs/tables/aws_elasticsearch_domains.md index 352aa8ac912078..56fe7b616f5e3f 100644 --- a/plugins/source/aws/docs/tables/aws_elasticsearch_domains.md +++ b/plugins/source/aws/docs/tables/aws_elasticsearch_domains.md @@ -1,5 +1,7 @@ # Table: aws_elasticsearch_domains +https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html + The primary key for this table is **arn**. ## Columns @@ -12,6 +14,7 @@ The primary key for this table is **arn**. |_cq_parent_id|UUID| |account_id|String| |region|String| +|authorized_principals|JSON| |tags|JSON| |arn (PK)|String| |domain_id|String| diff --git a/plugins/source/aws/docs/tables/aws_elasticsearch_packages.md b/plugins/source/aws/docs/tables/aws_elasticsearch_packages.md new file mode 100644 index 00000000000000..24ac957a539793 --- /dev/null +++ b/plugins/source/aws/docs/tables/aws_elasticsearch_packages.md @@ -0,0 +1,25 @@ +# Table: aws_elasticsearch_packages + +https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_PackageDetails.html + +The composite primary key for this table is (**account_id**, **region**, **id**). + +## Columns + +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id|UUID| +|_cq_parent_id|UUID| +|account_id (PK)|String| +|region (PK)|String| +|available_package_version|String| +|created_at|Timestamp| +|error_details|JSON| +|last_updated_at|Timestamp| +|package_description|String| +|id (PK)|String| +|package_name|String| +|package_status|String| +|package_type|String| \ No newline at end of file diff --git a/plugins/source/aws/docs/tables/aws_elasticsearch_versions.md b/plugins/source/aws/docs/tables/aws_elasticsearch_versions.md new file mode 100644 index 00000000000000..db0fffa3cf6339 --- /dev/null +++ b/plugins/source/aws/docs/tables/aws_elasticsearch_versions.md @@ -0,0 +1,18 @@ +# Table: aws_elasticsearch_versions + +https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_ListVersions.html + +The composite primary key for this table is (**account_id**, **region**, **version**). + +## Columns + +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id|UUID| +|_cq_parent_id|UUID| +|account_id (PK)|String| +|region (PK)|String| +|version (PK)|String| +|instance_types|JSON| \ No newline at end of file diff --git a/plugins/source/aws/docs/tables/aws_elasticsearch_vpc_endpoints.md b/plugins/source/aws/docs/tables/aws_elasticsearch_vpc_endpoints.md new file mode 100644 index 00000000000000..ed413a3ba7e7aa --- /dev/null +++ b/plugins/source/aws/docs/tables/aws_elasticsearch_vpc_endpoints.md @@ -0,0 +1,22 @@ +# Table: aws_elasticsearch_vpc_endpoints + +https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_VpcEndpoint.html + +The primary key for this table is **id**. + +## Columns + +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id|UUID| +|_cq_parent_id|UUID| +|account_id|String| +|region|String| +|domain_arn|String| +|endpoint|String| +|status|String| +|id (PK)|String| +|vpc_endpoint_owner|String| +|vpc_options|JSON| \ No newline at end of file diff --git a/plugins/source/aws/resources/plugin/tables.go b/plugins/source/aws/resources/plugin/tables.go index cee4a21102c2a3..61b8dba5117436 100644 --- a/plugins/source/aws/resources/plugin/tables.go +++ b/plugins/source/aws/resources/plugin/tables.go @@ -227,6 +227,9 @@ func tables() []*schema.Table { elasticbeanstalk.Applications(), elasticbeanstalk.Environments(), elasticsearch.Domains(), + elasticsearch.Packages(), + elasticsearch.Versions(), + elasticsearch.VpcEndpoints(), elastictranscoder.Pipelines(), elastictranscoder.Presets(), elbv1.LoadBalancers(), diff --git a/plugins/source/aws/resources/services/elasticsearch/domains.go b/plugins/source/aws/resources/services/elasticsearch/domains.go index 58a50aa512dbce..a7ace89f00c761 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains.go @@ -9,10 +9,10 @@ import ( func Domains() *schema.Table { return &schema.Table{ - Name: "aws_elasticsearch_domains", - Resolver: fetchElasticsearchDomains, - PreResourceResolver: getDomain, - Multiplex: client.ServiceAccountRegionMultiplexer("es"), + Name: "aws_elasticsearch_domains", + Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html`, + Resolver: fetchElasticsearchDomains, + Multiplex: client.ServiceAccountRegionMultiplexer("es"), Columns: []schema.Column{ { Name: "account_id", @@ -24,10 +24,15 @@ func Domains() *schema.Table { Type: schema.TypeString, Resolver: client.ResolveAWSRegion, }, + { + Name: "authorized_principals", + Type: schema.TypeJSON, + Resolver: resolveAuthorizedPrincipals, + }, { Name: "tags", Type: schema.TypeJSON, - Resolver: resolveTags, + Resolver: resolveDomainTags, }, { Name: "arn", diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go index 19dcd49a7a7646..3fccc36d2e79d7 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go @@ -10,28 +10,55 @@ import ( ) func fetchElasticsearchDomains(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { - c := meta.(*client.Client) - svc := c.Services().Elasticsearchservice - out, err := svc.ListDomainNames(ctx, &elasticsearchservice.ListDomainNamesInput{}) + svc := meta.(*client.Client).Services().Elasticsearchservice + + out, err := svc.DescribeElasticsearchDomains(ctx, nil) if err != nil { return err } - res <- out.DomainNames + res <- out.DomainStatusList + return nil } -func getDomain(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource) error { - c := meta.(*client.Client) - svc := c.Services().Elasticsearchservice +func resolveDomainTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + svc := meta.(*client.Client).Services().Elasticsearchservice - info := resource.Item.(types.DomainInfo) - - domainOutput, err := svc.DescribeElasticsearchDomain(ctx, &elasticsearchservice.DescribeElasticsearchDomainInput{DomainName: info.DomainName}) + tagsOutput, err := svc.ListTags(ctx, + &elasticsearchservice.ListTagsInput{ + ARN: resource.Item.(types.ElasticsearchDomainStatus).ARN, + }, + ) if err != nil { - return nil + return err } - resource.Item = domainOutput.DomainStatus - return nil + return resource.Set(c.Name, client.TagsToMap(tagsOutput.TagList)) +} + +func resolveAuthorizedPrincipals(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + + input := &elasticsearchservice.ListVpcEndpointAccessInput{ + DomainName: resource.Item.(types.ElasticsearchDomainStatus).DomainName, + } + + var principals []types.AuthorizedPrincipal + for { + out, err := svc.ListVpcEndpointAccess(ctx, input) + if err != nil { + return err + } + + principals = append(principals, out.AuthorizedPrincipalList...) + + if out.NextToken == nil { + break + } + + input.NextToken = out.NextToken + } + + return resource.Set(c.Name, principals) } diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go index efff6298b0d8a9..4c90f7431d16fb 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go @@ -14,24 +14,33 @@ import ( func buildElasticSearchDomains(t *testing.T, ctrl *gomock.Controller) client.Services { m := mocks.NewMockElasticsearchserviceClient(ctrl) - var di types.DomainInfo - if err := faker.FakeObject(&di); err != nil { - t.Fatal(err) - } - m.EXPECT().ListDomainNames(gomock.Any(), &elasticsearchservice.ListDomainNamesInput{}, gomock.Any()).Return( - &elasticsearchservice.ListDomainNamesOutput{DomainNames: []types.DomainInfo{di}}, nil) - var ds types.ElasticsearchDomainStatus if err := faker.FakeObject(&ds); err != nil { t.Fatal(err) } - m.EXPECT().DescribeElasticsearchDomain( - gomock.Any(), - &elasticsearchservice.DescribeElasticsearchDomainInput{DomainName: di.DomainName}, - gomock.Any(), - ).Return(&elasticsearchservice.DescribeElasticsearchDomainOutput{DomainStatus: &ds}, nil) - - addTagsCall(t, m) + m.EXPECT().DescribeElasticsearchDomains(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.DescribeElasticsearchDomainsOutput{ + DomainStatusList: []types.ElasticsearchDomainStatus{ds}, + }, + nil, + ) + + var principal types.AuthorizedPrincipal + if err := faker.FakeObject(&principal); err != nil { + t.Fatal(err) + } + m.EXPECT().ListVpcEndpointAccess(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.ListVpcEndpointAccessOutput{ + AuthorizedPrincipalList: []types.AuthorizedPrincipal{principal}, + }, + nil, + ) + + var tags elasticsearchservice.ListTagsOutput + if err := faker.FakeObject(&tags); err != nil { + t.Fatal(err) + } + m.EXPECT().ListTags(gomock.Any(), gomock.Any()).Return(&tags, nil) return client.Services{Elasticsearchservice: m} } diff --git a/plugins/source/aws/resources/services/elasticsearch/packages.go b/plugins/source/aws/resources/services/elasticsearch/packages.go new file mode 100644 index 00000000000000..91c883f02461aa --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/packages.go @@ -0,0 +1,83 @@ +// Code generated by codegen; DO NOT EDIT. + +package elasticsearch + +import ( + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func Packages() *schema.Table { + return &schema.Table{ + Name: "aws_elasticsearch_packages", + Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_PackageDetails.html`, + Resolver: fetchElasticsearchPackages, + Multiplex: client.ServiceAccountRegionMultiplexer("es"), + Columns: []schema.Column{ + { + Name: "account_id", + Type: schema.TypeString, + Resolver: client.ResolveAWSAccount, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "region", + Type: schema.TypeString, + Resolver: client.ResolveAWSRegion, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "available_package_version", + Type: schema.TypeString, + Resolver: schema.PathResolver("AvailablePackageVersion"), + }, + { + Name: "created_at", + Type: schema.TypeTimestamp, + Resolver: schema.PathResolver("CreatedAt"), + }, + { + Name: "error_details", + Type: schema.TypeJSON, + Resolver: schema.PathResolver("ErrorDetails"), + }, + { + Name: "last_updated_at", + Type: schema.TypeTimestamp, + Resolver: schema.PathResolver("LastUpdatedAt"), + }, + { + Name: "package_description", + Type: schema.TypeString, + Resolver: schema.PathResolver("PackageDescription"), + }, + { + Name: "id", + Type: schema.TypeString, + Resolver: schema.PathResolver("PackageID"), + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "package_name", + Type: schema.TypeString, + Resolver: schema.PathResolver("PackageName"), + }, + { + Name: "package_status", + Type: schema.TypeString, + Resolver: schema.PathResolver("PackageStatus"), + }, + { + Name: "package_type", + Type: schema.TypeString, + Resolver: schema.PathResolver("PackageType"), + }, + }, + } +} diff --git a/plugins/source/aws/resources/services/elasticsearch/packages_fetch.go b/plugins/source/aws/resources/services/elasticsearch/packages_fetch.go new file mode 100755 index 00000000000000..8e10ee4491facb --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/packages_fetch.go @@ -0,0 +1,25 @@ +package elasticsearch + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func fetchElasticsearchPackages(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + + p := elasticsearchservice.NewDescribePackagesPaginator(svc, nil) + for p.HasMorePages() { + out, err := p.NextPage(ctx) + if err != nil { + return err + } + + res <- out.PackageDetailsList + } + + return nil +} diff --git a/plugins/source/aws/resources/services/elasticsearch/packages_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/packages_mock_test.go new file mode 100644 index 00000000000000..f234e8b8354d64 --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/packages_mock_test.go @@ -0,0 +1,33 @@ +package elasticsearch + +import ( + "testing" + + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks" + "github.com/cloudquery/plugin-sdk/faker" + "github.com/golang/mock/gomock" +) + +func buildElasticSearchPackages(t *testing.T, ctrl *gomock.Controller) client.Services { + m := mocks.NewMockElasticsearchserviceClient(ctrl) + + var pkg types.PackageDetails + if err := faker.FakeObject(&pkg); err != nil { + t.Fatal(err) + } + m.EXPECT().DescribePackages(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.DescribePackagesOutput{ + PackageDetailsList: []types.PackageDetails{pkg}, + }, + nil, + ) + + return client.Services{Elasticsearchservice: m} +} + +func TestElasticSearchPackages(t *testing.T) { + client.AwsMockTestHelper(t, Packages(), buildElasticSearchPackages, client.TestOptions{}) +} diff --git a/plugins/source/aws/resources/services/elasticsearch/tags_fetch.go b/plugins/source/aws/resources/services/elasticsearch/tags_fetch.go deleted file mode 100644 index 6da9a757aec8b0..00000000000000 --- a/plugins/source/aws/resources/services/elasticsearch/tags_fetch.go +++ /dev/null @@ -1,35 +0,0 @@ -package elasticsearch - -import ( - "context" - "fmt" - - "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" - "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice/types" - "github.com/cloudquery/cloudquery/plugins/source/aws/client" - "github.com/cloudquery/plugin-sdk/schema" -) - -func resolveTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { - region := meta.(*client.Client).Region - svc := meta.(*client.Client).Services().Elasticsearchservice - - var arn *string - switch typed := resource.Item.(type) { - case *types.ElasticsearchDomainStatus: - arn = typed.ARN - default: - return fmt.Errorf("unsupported type for resolveTags: %T", typed) - } - - tagsOutput, err := svc.ListTags(ctx, &elasticsearchservice.ListTagsInput{ - ARN: arn, - }, func(o *elasticsearchservice.Options) { - o.Region = region - }) - if err != nil { - return err - } - - return resource.Set(c.Name, client.TagsToMap(tagsOutput.TagList)) -} diff --git a/plugins/source/aws/resources/services/elasticsearch/tags_fetch_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/tags_fetch_mock_test.go deleted file mode 100644 index 87fe8181eb2ba3..00000000000000 --- a/plugins/source/aws/resources/services/elasticsearch/tags_fetch_mock_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package elasticsearch - -import ( - "testing" - - "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" - "github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks" - "github.com/cloudquery/plugin-sdk/faker" - "github.com/golang/mock/gomock" -) - -func addTagsCall(t *testing.T, m *mocks.MockElasticsearchserviceClient) { - var tags elasticsearchservice.ListTagsOutput - if err := faker.FakeObject(&tags); err != nil { - t.Fatal(err) - } - m.EXPECT().ListTags(gomock.Any(), gomock.Any(), gomock.Any()).Return(&tags, nil) -} diff --git a/plugins/source/aws/resources/services/elasticsearch/versions.go b/plugins/source/aws/resources/services/elasticsearch/versions.go new file mode 100644 index 00000000000000..e92597ac79f5e2 --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/versions.go @@ -0,0 +1,48 @@ +// Code generated by codegen; DO NOT EDIT. + +package elasticsearch + +import ( + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func Versions() *schema.Table { + return &schema.Table{ + Name: "aws_elasticsearch_versions", + Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_ListVersions.html`, + Resolver: fetchElasticsearchVersions, + Multiplex: client.ServiceAccountRegionMultiplexer("es"), + Columns: []schema.Column{ + { + Name: "account_id", + Type: schema.TypeString, + Resolver: client.ResolveAWSAccount, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "region", + Type: schema.TypeString, + Resolver: client.ResolveAWSRegion, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "version", + Type: schema.TypeString, + Resolver: resolveVersion, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "instance_types", + Type: schema.TypeJSON, + Resolver: resolveInstanceTypes, + }, + }, + } +} diff --git a/plugins/source/aws/resources/services/elasticsearch/versions_fetch.go b/plugins/source/aws/resources/services/elasticsearch/versions_fetch.go new file mode 100755 index 00000000000000..723c17c297afc3 --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/versions_fetch.go @@ -0,0 +1,54 @@ +package elasticsearch + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func fetchElasticsearchVersions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + + p := elasticsearchservice.NewListElasticsearchVersionsPaginator(svc, + &elasticsearchservice.ListElasticsearchVersionsInput{MaxResults: 100}, + ) + for p.HasMorePages() { + out, err := p.NextPage(ctx) + if err != nil { + return err + } + + res <- out.ElasticsearchVersions + } + + return nil +} + +func resolveVersion(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + return resource.Set(c.Name, resource.Item.(string)) +} + +func resolveInstanceTypes(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + + var instanceTypes []types.ESPartitionInstanceType + p := elasticsearchservice.NewListElasticsearchInstanceTypesPaginator(svc, + &elasticsearchservice.ListElasticsearchInstanceTypesInput{ + ElasticsearchVersion: aws.String(resource.Item.(string)), + }, + ) + for p.HasMorePages() { + out, err := p.NextPage(ctx) + if err != nil { + return err + } + + instanceTypes = append(instanceTypes, out.ElasticsearchInstanceTypes...) + } + + return resource.Set(c.Name, instanceTypes) +} diff --git a/plugins/source/aws/resources/services/elasticsearch/versions_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/versions_mock_test.go new file mode 100644 index 00000000000000..bb75bfd998af9a --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/versions_mock_test.go @@ -0,0 +1,44 @@ +package elasticsearch + +import ( + "testing" + + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks" + "github.com/cloudquery/plugin-sdk/faker" + "github.com/golang/mock/gomock" +) + +func buildElasticSearchVersions(t *testing.T, ctrl *gomock.Controller) client.Services { + m := mocks.NewMockElasticsearchserviceClient(ctrl) + + var versions []string + if err := faker.FakeObject(&versions); err != nil { + t.Fatal(err) + } + m.EXPECT().ListElasticsearchVersions(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.ListElasticsearchVersionsOutput{ + ElasticsearchVersions: versions, + }, + nil, + ) + + var instanceTypes []types.ESPartitionInstanceType + if err := faker.FakeObject(&versions); err != nil { + t.Fatal(err) + } + m.EXPECT().ListElasticsearchInstanceTypes(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.ListElasticsearchInstanceTypesOutput{ + ElasticsearchInstanceTypes: instanceTypes, + }, + nil, + ) + + return client.Services{Elasticsearchservice: m} +} + +func TestElasticSearchVersions(t *testing.T) { + client.AwsMockTestHelper(t, Versions(), buildElasticSearchVersions, client.TestOptions{}) +} diff --git a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints.go b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints.go new file mode 100644 index 00000000000000..f8798e41105c79 --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints.go @@ -0,0 +1,62 @@ +// Code generated by codegen; DO NOT EDIT. + +package elasticsearch + +import ( + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func VpcEndpoints() *schema.Table { + return &schema.Table{ + Name: "aws_elasticsearch_vpc_endpoints", + Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_VpcEndpoint.html`, + Resolver: fetchElasticsearchVpcEndpoints, + Multiplex: client.ServiceAccountRegionMultiplexer("es"), + Columns: []schema.Column{ + { + Name: "account_id", + Type: schema.TypeString, + Resolver: client.ResolveAWSAccount, + }, + { + Name: "region", + Type: schema.TypeString, + Resolver: client.ResolveAWSRegion, + }, + { + Name: "domain_arn", + Type: schema.TypeString, + Resolver: schema.PathResolver("DomainArn"), + }, + { + Name: "endpoint", + Type: schema.TypeString, + Resolver: schema.PathResolver("Endpoint"), + }, + { + Name: "status", + Type: schema.TypeString, + Resolver: schema.PathResolver("Status"), + }, + { + Name: "id", + Type: schema.TypeString, + Resolver: schema.PathResolver("VpcEndpointId"), + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "vpc_endpoint_owner", + Type: schema.TypeString, + Resolver: schema.PathResolver("VpcEndpointOwner"), + }, + { + Name: "vpc_options", + Type: schema.TypeJSON, + Resolver: schema.PathResolver("VpcOptions"), + }, + }, + } +} diff --git a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go new file mode 100755 index 00000000000000..40949563d858bd --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go @@ -0,0 +1,58 @@ +package elasticsearch + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/cloudquery/plugins/source/aws/client/services" + "github.com/cloudquery/plugin-sdk/schema" +) + +func fetchElasticsearchVpcEndpoints(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + + listInput := new(elasticsearchservice.ListVpcEndpointsInput) + var vpcEndpointIDs []string + // get the IDs first + for { + out, err := svc.ListVpcEndpoints(ctx, listInput) + if err != nil { + return err + } + + for _, summary := range out.VpcEndpointSummaryList { + vpcEndpointIDs = append(vpcEndpointIDs, *summary.VpcEndpointId) + } + + if out.NextToken == nil { + break + } + + listInput.NextToken = out.NextToken + } + + return describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs) +} + +func describeVPCEndpoints(ctx context.Context, svc services.ElasticsearchserviceClient, res chan<- any, vpcEndpointIDs []string) error { + // DescribeVpcEndpoints supports amounts [5, 100], so, if we have > 100 endpoints to fetch, split in halves + const maxLen = 100 + if l := len(vpcEndpointIDs); l > maxLen { + if err := describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs[:l/2]); err != nil { + return err + } + return describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs[l/2:]) + } + + out, err := svc.DescribeVpcEndpoints(ctx, + &elasticsearchservice.DescribeVpcEndpointsInput{VpcEndpointIds: vpcEndpointIDs}, + ) + if err != nil { + return err + } + + res <- out.VpcEndpoints + + return nil +} diff --git a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_mock_test.go new file mode 100644 index 00000000000000..420acbdc09336e --- /dev/null +++ b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_mock_test.go @@ -0,0 +1,46 @@ +package elasticsearch + +import ( + "testing" + + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" + "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks" + "github.com/cloudquery/plugin-sdk/faker" + "github.com/golang/mock/gomock" +) + +func buildElasticSearchVpcEndpoints(t *testing.T, ctrl *gomock.Controller) client.Services { + m := mocks.NewMockElasticsearchserviceClient(ctrl) + + var summary types.VpcEndpointSummary + if err := faker.FakeObject(&summary); err != nil { + t.Fatal(err) + } + m.EXPECT().ListVpcEndpoints(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.ListVpcEndpointsOutput{ + VpcEndpointSummaryList: []types.VpcEndpointSummary{summary}, + }, + nil, + ) + + var endpoint types.VpcEndpoint + if err := faker.FakeObject(&endpoint); err != nil { + t.Fatal(err) + } + endpoint.VpcEndpointId = summary.VpcEndpointId + + m.EXPECT().DescribeVpcEndpoints(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.DescribeVpcEndpointsOutput{ + VpcEndpoints: []types.VpcEndpoint{endpoint}, + }, + nil, + ) + + return client.Services{Elasticsearchservice: m} +} + +func TestElasticSearchVpcEndpoints(t *testing.T) { + client.AwsMockTestHelper(t, VpcEndpoints(), buildElasticSearchVpcEndpoints, client.TestOptions{}) +} diff --git a/website/pages/docs/plugins/sources/aws/tables.md b/website/pages/docs/plugins/sources/aws/tables.md index f275eb0f701ce9..8d1ea7eadcf3b4 100644 --- a/website/pages/docs/plugins/sources/aws/tables.md +++ b/website/pages/docs/plugins/sources/aws/tables.md @@ -195,6 +195,9 @@ - [aws_elasticbeanstalk_configuration_settings](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticbeanstalk_configuration_settings.md) - [aws_elasticbeanstalk_configuration_options](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticbeanstalk_configuration_options.md) - [aws_elasticsearch_domains](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticsearch_domains.md) +- [aws_elasticsearch_packages](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticsearch_packages.md) +- [aws_elasticsearch_versions](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticsearch_versions.md) +- [aws_elasticsearch_vpc_endpoints](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elasticsearch_vpc_endpoints.md) - [aws_elastictranscoder_pipelines](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elastictranscoder_pipelines.md) - [aws_elastictranscoder_pipeline_jobs](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elastictranscoder_pipeline_jobs.md) - [aws_elastictranscoder_presets](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_elastictranscoder_presets.md) From 4c6f46fedca058ac709a804b904dda08f6ef20f9 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Tue, 20 Dec 2022 19:40:58 +0200 Subject: [PATCH 2/5] fix fetchers --- .../aws/resources/services/elasticsearch/domains_fetch.go | 4 +++- .../resources/services/elasticsearch/vpc_endpoints_fetch.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go index 3fccc36d2e79d7..43a8352ec7d763 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go @@ -12,7 +12,9 @@ import ( func fetchElasticsearchDomains(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { svc := meta.(*client.Client).Services().Elasticsearchservice - out, err := svc.DescribeElasticsearchDomains(ctx, nil) + out, err := svc.DescribeElasticsearchDomains(ctx, + &elasticsearchservice.DescribeElasticsearchDomainsInput{DomainNames: []string{}}, + ) if err != nil { return err } diff --git a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go index 40949563d858bd..64973c3c126c70 100755 --- a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go @@ -32,6 +32,10 @@ func fetchElasticsearchVpcEndpoints(ctx context.Context, meta schema.ClientMeta, listInput.NextToken = out.NextToken } + if len(vpcEndpointIDs) == 0 { + return nil + } + return describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs) } From 4747c267f50f95abf851855b2a9d47cc8adb7c85 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Wed, 21 Dec 2022 11:41:19 +0200 Subject: [PATCH 3/5] don't do recursion --- .../elasticsearch/vpc_endpoints_fetch.go | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go index 64973c3c126c70..62043f1967b567 100755 --- a/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/vpc_endpoints_fetch.go @@ -5,7 +5,6 @@ import ( "github.com/aws/aws-sdk-go-v2/service/elasticsearchservice" "github.com/cloudquery/cloudquery/plugins/source/aws/client" - "github.com/cloudquery/cloudquery/plugins/source/aws/client/services" "github.com/cloudquery/plugin-sdk/schema" ) @@ -32,31 +31,25 @@ func fetchElasticsearchVpcEndpoints(ctx context.Context, meta schema.ClientMeta, listInput.NextToken = out.NextToken } - if len(vpcEndpointIDs) == 0 { - return nil - } - - return describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs) -} - -func describeVPCEndpoints(ctx context.Context, svc services.ElasticsearchserviceClient, res chan<- any, vpcEndpointIDs []string) error { - // DescribeVpcEndpoints supports amounts [5, 100], so, if we have > 100 endpoints to fetch, split in halves + // slice in parts const maxLen = 100 - if l := len(vpcEndpointIDs); l > maxLen { - if err := describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs[:l/2]); err != nil { + for len(vpcEndpointIDs) > 0 { + var part []string + if len(vpcEndpointIDs) > maxLen { + part, vpcEndpointIDs = vpcEndpointIDs[:maxLen], vpcEndpointIDs[maxLen:] + } else { + part, vpcEndpointIDs = vpcEndpointIDs, nil + } + + out, err := svc.DescribeVpcEndpoints(ctx, + &elasticsearchservice.DescribeVpcEndpointsInput{VpcEndpointIds: part}, + ) + if err != nil { return err } - return describeVPCEndpoints(ctx, svc, res, vpcEndpointIDs[l/2:]) - } - out, err := svc.DescribeVpcEndpoints(ctx, - &elasticsearchservice.DescribeVpcEndpointsInput{VpcEndpointIds: vpcEndpointIDs}, - ) - if err != nil { - return err + res <- out.VpcEndpoints } - res <- out.VpcEndpoints - return nil } From 471b9887732b9b7e8b8b39744c1ae0e25419c122 Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Wed, 21 Dec 2022 12:34:06 +0200 Subject: [PATCH 4/5] fix fetch --- .../aws/codegen/recipes/elasticsearch.go | 9 +++---- .../services/elasticsearch/domains.go | 9 +++---- .../services/elasticsearch/domains_fetch.go | 24 +++++++++++++++---- .../elasticsearch/domains_mock_test.go | 17 ++++++++++--- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/plugins/source/aws/codegen/recipes/elasticsearch.go b/plugins/source/aws/codegen/recipes/elasticsearch.go index caa25864d4f5e1..3edcc84131cf95 100644 --- a/plugins/source/aws/codegen/recipes/elasticsearch.go +++ b/plugins/source/aws/codegen/recipes/elasticsearch.go @@ -9,10 +9,11 @@ import ( func ElasticsearchResources() []*Resource { resources := []*Resource{ { - SubService: "domains", - Struct: new(types.ElasticsearchDomainStatus), - Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html", - PKColumns: []string{"arn"}, + SubService: "domains", + Struct: new(types.ElasticsearchDomainStatus), + Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html", + PreResourceResolver: "describeElasticsearchDomain", + PKColumns: []string{"arn"}, ExtraColumns: append( defaultRegionalColumns, codegen.ColumnDefinition{ diff --git a/plugins/source/aws/resources/services/elasticsearch/domains.go b/plugins/source/aws/resources/services/elasticsearch/domains.go index a7ace89f00c761..cb64a54ec5c59a 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains.go @@ -9,10 +9,11 @@ import ( func Domains() *schema.Table { return &schema.Table{ - Name: "aws_elasticsearch_domains", - Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html`, - Resolver: fetchElasticsearchDomains, - Multiplex: client.ServiceAccountRegionMultiplexer("es"), + Name: "aws_elasticsearch_domains", + Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html`, + Resolver: fetchElasticsearchDomains, + PreResourceResolver: describeElasticsearchDomain, + Multiplex: client.ServiceAccountRegionMultiplexer("es"), Columns: []schema.Column{ { Name: "account_id", diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go index 43a8352ec7d763..679373d59093c6 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go @@ -12,14 +12,28 @@ import ( func fetchElasticsearchDomains(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { svc := meta.(*client.Client).Services().Elasticsearchservice - out, err := svc.DescribeElasticsearchDomains(ctx, - &elasticsearchservice.DescribeElasticsearchDomainsInput{DomainNames: []string{}}, + domainNames, err := svc.ListDomainNames(ctx, nil) + if err != nil { + return err + } + + res <- domainNames.DomainNames + + return nil +} + +func describeElasticsearchDomain(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource) error { + svc := meta.(*client.Client).Services().Elasticsearchservice + name := resource.Item.(types.DomainInfo).DomainName + + out, err := svc.DescribeElasticsearchDomain(ctx, + &elasticsearchservice.DescribeElasticsearchDomainInput{DomainName: name}, ) if err != nil { return err } - res <- out.DomainStatusList + resource.SetItem(out.DomainStatus) return nil } @@ -29,7 +43,7 @@ func resolveDomainTags(ctx context.Context, meta schema.ClientMeta, resource *sc tagsOutput, err := svc.ListTags(ctx, &elasticsearchservice.ListTagsInput{ - ARN: resource.Item.(types.ElasticsearchDomainStatus).ARN, + ARN: resource.Item.(*types.ElasticsearchDomainStatus).ARN, }, ) if err != nil { @@ -43,7 +57,7 @@ func resolveAuthorizedPrincipals(ctx context.Context, meta schema.ClientMeta, re svc := meta.(*client.Client).Services().Elasticsearchservice input := &elasticsearchservice.ListVpcEndpointAccessInput{ - DomainName: resource.Item.(types.ElasticsearchDomainStatus).DomainName, + DomainName: resource.Item.(*types.ElasticsearchDomainStatus).DomainName, } var principals []types.AuthorizedPrincipal diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go b/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go index 4c90f7431d16fb..707a3c6326b084 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_mock_test.go @@ -14,13 +14,24 @@ import ( func buildElasticSearchDomains(t *testing.T, ctrl *gomock.Controller) client.Services { m := mocks.NewMockElasticsearchserviceClient(ctrl) + var info types.DomainInfo + if err := faker.FakeObject(&info); err != nil { + t.Fatal(err) + } + m.EXPECT().ListDomainNames(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.ListDomainNamesOutput{ + DomainNames: []types.DomainInfo{info}, + }, + nil, + ) + var ds types.ElasticsearchDomainStatus if err := faker.FakeObject(&ds); err != nil { t.Fatal(err) } - m.EXPECT().DescribeElasticsearchDomains(gomock.Any(), gomock.Any()).Return( - &elasticsearchservice.DescribeElasticsearchDomainsOutput{ - DomainStatusList: []types.ElasticsearchDomainStatus{ds}, + m.EXPECT().DescribeElasticsearchDomain(gomock.Any(), gomock.Any()).Return( + &elasticsearchservice.DescribeElasticsearchDomainOutput{ + DomainStatus: &ds, }, nil, ) From 435562807048a2ab69159da5a71cd4a09df4a14a Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Wed, 21 Dec 2022 12:43:02 +0200 Subject: [PATCH 5/5] less diff --- plugins/source/aws/codegen/recipes/elasticsearch.go | 2 +- .../aws/resources/services/elasticsearch/domains.go | 2 +- .../resources/services/elasticsearch/domains_fetch.go | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/plugins/source/aws/codegen/recipes/elasticsearch.go b/plugins/source/aws/codegen/recipes/elasticsearch.go index 3edcc84131cf95..a590083ca90581 100644 --- a/plugins/source/aws/codegen/recipes/elasticsearch.go +++ b/plugins/source/aws/codegen/recipes/elasticsearch.go @@ -12,7 +12,7 @@ func ElasticsearchResources() []*Resource { SubService: "domains", Struct: new(types.ElasticsearchDomainStatus), Description: "https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html", - PreResourceResolver: "describeElasticsearchDomain", + PreResourceResolver: "getDomain", PKColumns: []string{"arn"}, ExtraColumns: append( defaultRegionalColumns, diff --git a/plugins/source/aws/resources/services/elasticsearch/domains.go b/plugins/source/aws/resources/services/elasticsearch/domains.go index cb64a54ec5c59a..9aca73aecf8306 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains.go @@ -12,7 +12,7 @@ func Domains() *schema.Table { Name: "aws_elasticsearch_domains", Description: `https://docs.aws.amazon.com/opensearch-service/latest/APIReference/API_DomainStatus.html`, Resolver: fetchElasticsearchDomains, - PreResourceResolver: describeElasticsearchDomain, + PreResourceResolver: getDomain, Multiplex: client.ServiceAccountRegionMultiplexer("es"), Columns: []schema.Column{ { diff --git a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go index 679373d59093c6..4b5b164d2c3dbc 100644 --- a/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go +++ b/plugins/source/aws/resources/services/elasticsearch/domains_fetch.go @@ -12,22 +12,23 @@ import ( func fetchElasticsearchDomains(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { svc := meta.(*client.Client).Services().Elasticsearchservice - domainNames, err := svc.ListDomainNames(ctx, nil) + out, err := svc.ListDomainNames(ctx, nil) if err != nil { return err } - res <- domainNames.DomainNames + res <- out.DomainNames return nil } -func describeElasticsearchDomain(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource) error { +func getDomain(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource) error { svc := meta.(*client.Client).Services().Elasticsearchservice - name := resource.Item.(types.DomainInfo).DomainName out, err := svc.DescribeElasticsearchDomain(ctx, - &elasticsearchservice.DescribeElasticsearchDomainInput{DomainName: name}, + &elasticsearchservice.DescribeElasticsearchDomainInput{ + DomainName: resource.Item.(types.DomainInfo).DomainName, + }, ) if err != nil { return err