From 3bc8d76244813bfdbb965f1554b006c2b0bbb3b4 Mon Sep 17 00:00:00 2001 From: Alberto Donato Date: Fri, 13 Jun 2025 10:25:10 +0200 Subject: [PATCH 1/3] feat: add stacklet_platform data source ### what add a datasource to retrieve general details about the platform ### why provide general info about the deployment ### testing acceptance tests and tested in sandbox ### docs added here --- docs/data-sources/platform.md | 28 ++++++ docs/index.md | 2 +- .../stacklet_platform/data-source.tf | 2 + examples/provider/provider.tf | 2 +- .../platform_data_source_test.go | 27 ++++++ .../recordings/TestAccPlatformDataSource.json | 58 +++++++++++++ internal/api/api.go | 2 + internal/api/platform.go | 32 +++++++ internal/datasources/datasources.go | 1 + internal/datasources/platform.go | 86 +++++++++++++++++++ internal/models/platform.go | 15 ++++ 11 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 docs/data-sources/platform.md create mode 100644 examples/data-sources/stacklet_platform/data-source.tf create mode 100644 internal/acceptance_tests/platform_data_source_test.go create mode 100644 internal/acceptance_tests/recordings/TestAccPlatformDataSource.json create mode 100644 internal/api/platform.go create mode 100644 internal/datasources/platform.go create mode 100644 internal/models/platform.go diff --git a/docs/data-sources/platform.md b/docs/data-sources/platform.md new file mode 100644 index 0000000..3ae59a8 --- /dev/null +++ b/docs/data-sources/platform.md @@ -0,0 +1,28 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "stacklet_platform Data Source - terraform-provider-stacklet" +subcategory: "" +description: |- + Retrieve information about the Stacklet platform. +--- + +# stacklet_platform (Data Source) + +Retrieve information about the Stacklet platform. + +## Example Usage + +```terraform +# Fetch details about the platform +data "stacklet_platform" "example" {} +``` + + +## Schema + +### Read-Only + +- `default_role` (String) Default role for users. +- `execution_regions` (List of String) List of regions for which execution is enabled. +- `external_id` (String) The external ID for the deployment. +- `id` (String) The GraphQL Node ID. diff --git a/docs/index.md b/docs/index.md index 9a6a1a1..ad75cba 100644 --- a/docs/index.md +++ b/docs/index.md @@ -18,7 +18,7 @@ It allows managing resources like accounts, account groups, policy collections, terraform { required_providers { stacklet = { - source = "registry.terraform.io/stacklet/stacklet" + source = "stacklet/stacklet" } } } diff --git a/examples/data-sources/stacklet_platform/data-source.tf b/examples/data-sources/stacklet_platform/data-source.tf new file mode 100644 index 0000000..be445f2 --- /dev/null +++ b/examples/data-sources/stacklet_platform/data-source.tf @@ -0,0 +1,2 @@ +# Fetch details about the platform +data "stacklet_platform" "example" {} diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index ab57ad4..b9462c2 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -1,7 +1,7 @@ terraform { required_providers { stacklet = { - source = "registry.terraform.io/stacklet/stacklet" + source = "stacklet/stacklet" } } } diff --git a/internal/acceptance_tests/platform_data_source_test.go b/internal/acceptance_tests/platform_data_source_test.go new file mode 100644 index 0000000..5c19564 --- /dev/null +++ b/internal/acceptance_tests/platform_data_source_test.go @@ -0,0 +1,27 @@ +// Copyright (c) 2025 - Stacklet, Inc. + +package acceptance_tests + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccPlatformDataSource(t *testing.T) { + steps := []resource.TestStep{ + { + Config: ` + data "stacklet_platform" "test" {} + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "id"), + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "external_id"), + // at least one region is enabled + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "execution_regions.0"), + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "default_role"), + ), + }, + } + runRecordedAccTest(t, "TestAccPlatformDataSource", steps) +} diff --git a/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json new file mode 100644 index 0000000..937d2fd --- /dev/null +++ b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json @@ -0,0 +1,58 @@ +{ + "{platform{id,externalID,executionRegions,defaultRole}}": [ + { + "request": { + "query": "{platform{id,externalID,executionRegions,defaultRole}}" + }, + "response": { + "data": { + "platform": { + "defaultRole": "admin", + "executionRegions": [ + "eu-north-1", + "us-east-1" + ], + "externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2", + "id": "WyJwbGF0Zm9ybSJd" + } + } + } + }, + { + "request": { + "query": "{platform{id,externalID,executionRegions,defaultRole}}" + }, + "response": { + "data": { + "platform": { + "defaultRole": "admin", + "executionRegions": [ + "eu-north-1", + "us-east-1" + ], + "externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2", + "id": "WyJwbGF0Zm9ybSJd" + } + } + } + }, + { + "request": { + "query": "{platform{id,externalID,executionRegions,defaultRole}}" + }, + "response": { + "data": { + "platform": { + "defaultRole": "admin", + "executionRegions": [ + "eu-north-1", + "us-east-1" + ], + "externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2", + "id": "WyJwbGF0Zm9ybSJd" + } + } + } + } + ] +} \ No newline at end of file diff --git a/internal/api/api.go b/internal/api/api.go index 7051f9c..269f5da 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -14,6 +14,7 @@ type API struct { AccountGroup accountGroupAPI AccountGroupMapping accountGroupMappingAPI Binding bindingAPI + Platform platformAPI Policy policyAPI PolicyCollection policyCollectionAPI PolicyCollectionMapping policyCollectionMappingAPI @@ -28,6 +29,7 @@ func New(c *graphql.Client) *API { AccountGroup: accountGroupAPI{c}, AccountGroupMapping: accountGroupMappingAPI{c}, Binding: bindingAPI{c}, + Platform: platformAPI{c}, Policy: policyAPI{c}, PolicyCollection: policyCollectionAPI{c}, PolicyCollectionMapping: policyCollectionMappingAPI{c}, diff --git a/internal/api/platform.go b/internal/api/platform.go new file mode 100644 index 0000000..cf38e5f --- /dev/null +++ b/internal/api/platform.go @@ -0,0 +1,32 @@ +// Copyright (c) 2025 - Stacklet, Inc. + +package api + +import ( + "context" + + "github.com/hasura/go-graphql-client" +) + +// Platform is the data returned by reading platform data. +type Platform struct { + ID string + ExternalID *string `graphql:"externalID"` + ExecutionRegions []string + DefaultRole *string +} + +type platformAPI struct { + c *graphql.Client +} + +// Read returns platform data. +func (a platformAPI) Read(ctx context.Context) (*Platform, error) { + var query struct { + Platform Platform `graphql:"platform"` + } + if err := a.c.Query(ctx, &query, nil); err != nil { + return nil, NewAPIError(err) + } + return &query.Platform, nil +} diff --git a/internal/datasources/datasources.go b/internal/datasources/datasources.go index 8a98bf3..fb740f4 100644 --- a/internal/datasources/datasources.go +++ b/internal/datasources/datasources.go @@ -10,6 +10,7 @@ var DATASOURCES = []func() datasource.DataSource{ NewAccountDataSource, NewAccountGroupDataSource, NewBindingDataSource, + NewPlatformDataSource, NewPolicyCollectionDataSource, NewPolicyDataSource, NewRepositoryDataSource, diff --git a/internal/datasources/platform.go b/internal/datasources/platform.go new file mode 100644 index 0000000..3a26ccf --- /dev/null +++ b/internal/datasources/platform.go @@ -0,0 +1,86 @@ +// Copyright (c) 2025 - Stacklet, Inc. + +package datasources + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/stacklet/terraform-provider-stacklet/internal/api" + "github.com/stacklet/terraform-provider-stacklet/internal/errors" + "github.com/stacklet/terraform-provider-stacklet/internal/models" + "github.com/stacklet/terraform-provider-stacklet/internal/providerdata" + tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types" +) + +var ( + _ datasource.DataSource = &platformDataSource{} +) + +func NewPlatformDataSource() datasource.DataSource { + return &platformDataSource{} +} + +type platformDataSource struct { + api *api.API +} + +func (d *platformDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_platform" +} + +func (d *platformDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Retrieve information about the Stacklet platform.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "The GraphQL Node ID.", + Computed: true, + }, + "external_id": schema.StringAttribute{ + Description: "The external ID for the deployment.", + Computed: true, + }, + "execution_regions": schema.ListAttribute{ + Description: "List of regions for which execution is enabled.", + Computed: true, + ElementType: types.StringType, + }, + "default_role": schema.StringAttribute{ + Description: "Default role for users.", + Computed: true, + }, + }, + } +} + +func (d *platformDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if pd, err := providerdata.GetDataSourceProviderData(req); err != nil { + errors.AddDiagError(&resp.Diagnostics, err) + } else if pd != nil { + d.api = pd.API + } +} + +func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data models.PlatformDataSource + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + platform, err := d.api.Platform.Read(ctx) + if err != nil { + errors.AddDiagError(&resp.Diagnostics, err) + return + } + + data.ID = types.StringValue(platform.ID) + data.ExternalID = tftypes.NullableString(platform.ExternalID) + data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions) + data.DefaultRole = tftypes.NullableString(platform.DefaultRole) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/models/platform.go b/internal/models/platform.go new file mode 100644 index 0000000..92a8d02 --- /dev/null +++ b/internal/models/platform.go @@ -0,0 +1,15 @@ +// Copyright (c) 2025 - Stacklet, Inc. + +package models + +import ( + "github.com/hashicorp/terraform-plugin-framework/types" +) + +// PlatformDataSource is the model for the platform data source. +type PlatformDataSource struct { + ID types.String `tfsdk:"id"` + ExternalID types.String `tfsdk:"external_id"` + ExecutionRegions types.List `tfsdk:"execution_regions"` + DefaultRole types.String `tfsdk:"default_role"` +} From 05c6d893a67290d2e19f0b7a18942fc9dc02c169 Mon Sep 17 00:00:00 2001 From: Alberto Donato Date: Wed, 25 Jun 2025 16:55:14 +0200 Subject: [PATCH 2/3] add terraform modules to data source --- docs/data-sources/platform.md | 36 ++++++++ .../platform_data_source_test.go | 2 + .../recordings/TestAccPlatformDataSource.json | 52 +++++++++-- internal/api/platform.go | 22 ++++- internal/datasources/platform.go | 87 +++++++++++++++++++ internal/models/platform.go | 40 ++++++++- 6 files changed, 226 insertions(+), 13 deletions(-) diff --git a/docs/data-sources/platform.md b/docs/data-sources/platform.md index 3ae59a8..5ff69dc 100644 --- a/docs/data-sources/platform.md +++ b/docs/data-sources/platform.md @@ -22,7 +22,43 @@ data "stacklet_platform" "example" {} ### Read-Only +- `aws_account_customer_config` (Attributes) Customer configuration for AWS accounts. (see [below for nested schema](#nestedatt--aws_account_customer_config)) +- `aws_org_read_customer_config` (Attributes) Customer configuration for AWS organization read access. (see [below for nested schema](#nestedatt--aws_org_read_customer_config)) - `default_role` (String) Default role for users. - `execution_regions` (List of String) List of regions for which execution is enabled. - `external_id` (String) The external ID for the deployment. - `id` (String) The GraphQL Node ID. + + +### Nested Schema for `aws_account_customer_config` + +Read-Only: + +- `terraform_module` (Attributes) Terraform module configuration for account setup. (see [below for nested schema](#nestedatt--aws_account_customer_config--terraform_module)) + + +### Nested Schema for `aws_account_customer_config.terraform_module` + +Read-Only: + +- `repository_url` (String) Module repository URL. +- `source` (String) Module source. +- `variables_json` (String) JSON-encoded variables for module configuration. + + + + +### Nested Schema for `aws_org_read_customer_config` + +Read-Only: + +- `terraform_module` (Attributes) Terraform module configuration for organization read access setup. (see [below for nested schema](#nestedatt--aws_org_read_customer_config--terraform_module)) + + +### Nested Schema for `aws_org_read_customer_config.terraform_module` + +Read-Only: + +- `repository_url` (String) Module repository URL. +- `source` (String) Module source. +- `variables_json` (String) JSON-encoded variables for module configuration. diff --git a/internal/acceptance_tests/platform_data_source_test.go b/internal/acceptance_tests/platform_data_source_test.go index 5c19564..00fa2b5 100644 --- a/internal/acceptance_tests/platform_data_source_test.go +++ b/internal/acceptance_tests/platform_data_source_test.go @@ -20,6 +20,8 @@ func TestAccPlatformDataSource(t *testing.T) { // at least one region is enabled resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "execution_regions.0"), resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "default_role"), + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "aws_account_customer_config.terraform_module.source"), + resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "aws_org_read_customer_config.terraform_module.source"), ), }, } diff --git a/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json index 937d2fd..0bf360e 100644 --- a/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json +++ b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json @@ -1,12 +1,26 @@ { - "{platform{id,externalID,executionRegions,defaultRole}}": [ + "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}": [ { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole}}" + "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { "platform": { + "awsAccountCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//access", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_event_bus_arn\": \"arn:aws:events:eu-north-1:905418385756:event-bus/default\", \"stacklet_execution_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-execution\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\"}" + } + }, + "awsOrgReadCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//org-read", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" + } + }, "defaultRole": "admin", "executionRegions": [ "eu-north-1", @@ -20,11 +34,25 @@ }, { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole}}" + "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { "platform": { + "awsAccountCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//access", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_event_bus_arn\": \"arn:aws:events:eu-north-1:905418385756:event-bus/default\", \"stacklet_execution_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-execution\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\"}" + } + }, + "awsOrgReadCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//org-read", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" + } + }, "defaultRole": "admin", "executionRegions": [ "eu-north-1", @@ -38,11 +66,25 @@ }, { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole}}" + "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { "platform": { + "awsAccountCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//access", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_event_bus_arn\": \"arn:aws:events:eu-north-1:905418385756:event-bus/default\", \"stacklet_execution_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-execution\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\"}" + } + }, + "awsOrgReadCustomerConfig": { + "terraformModule": { + "repositoryURL": "https://github.com/stacklet/terraform-aws-onboarding", + "source": "github.com/stacklet/terraform-aws-onboarding//org-read", + "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" + } + }, "defaultRole": "admin", "executionRegions": [ "eu-north-1", @@ -55,4 +97,4 @@ } } ] -} \ No newline at end of file +} diff --git a/internal/api/platform.go b/internal/api/platform.go index cf38e5f..45a22af 100644 --- a/internal/api/platform.go +++ b/internal/api/platform.go @@ -10,10 +10,24 @@ import ( // Platform is the data returned by reading platform data. type Platform struct { - ID string - ExternalID *string `graphql:"externalID"` - ExecutionRegions []string - DefaultRole *string + ID string + ExternalID *string `graphql:"externalID"` + ExecutionRegions []string + DefaultRole *string + AWSOrgReadCustomerConfig PlatformCustomerConfig `graphql:"awsOrgReadCustomerConfig"` + AWSAccountCustomerConfig PlatformCustomerConfig `graphql:"awsAccountCustomerConfig"` +} + +// PlatformCustomerConfig is the data returned for a customer configuration. +type PlatformCustomerConfig struct { + TerraformModule TerraformModule +} + +// TerraformModule is the data returned for terraform module definitions. +type TerraformModule struct { + RepositoryURL string `graphql:"repositoryURL"` + Source string + VariablesJSON string `graphql:"variablesJSON"` } type platformAPI struct { diff --git a/internal/datasources/platform.go b/internal/datasources/platform.go index 3a26ccf..649fe19 100644 --- a/internal/datasources/platform.go +++ b/internal/datasources/platform.go @@ -7,7 +7,9 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/stacklet/terraform-provider-stacklet/internal/api" "github.com/stacklet/terraform-provider-stacklet/internal/errors" @@ -53,6 +55,54 @@ func (d *platformDataSource) Schema(_ context.Context, _ datasource.SchemaReques Description: "Default role for users.", Computed: true, }, + "aws_account_customer_config": schema.SingleNestedAttribute{ + Description: "Customer configuration for AWS accounts.", + Computed: true, + Attributes: map[string]schema.Attribute{ + "terraform_module": schema.SingleNestedAttribute{ + Description: "Terraform module configuration for account setup.", + Computed: true, + Attributes: map[string]schema.Attribute{ + "repository_url": schema.StringAttribute{ + Description: "Module repository URL.", + Computed: true, + }, + "source": schema.StringAttribute{ + Description: "Module source.", + Computed: true, + }, + "variables_json": schema.StringAttribute{ + Description: "JSON-encoded variables for module configuration.", + Computed: true, + }, + }, + }, + }, + }, + "aws_org_read_customer_config": schema.SingleNestedAttribute{ + Description: "Customer configuration for AWS organization read access.", + Computed: true, + Attributes: map[string]schema.Attribute{ + "terraform_module": schema.SingleNestedAttribute{ + Description: "Terraform module configuration for organization read access setup.", + Computed: true, + Attributes: map[string]schema.Attribute{ + "repository_url": schema.StringAttribute{ + Description: "Module repository URL.", + Computed: true, + }, + "source": schema.StringAttribute{ + Description: "Module source.", + Computed: true, + }, + "variables_json": schema.StringAttribute{ + Description: "JSON-encoded variables for module configuration.", + Computed: true, + }, + }, + }, + }, + }, }, } } @@ -82,5 +132,42 @@ func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadReques data.ExternalID = tftypes.NullableString(platform.ExternalID) data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions) data.DefaultRole = tftypes.NullableString(platform.DefaultRole) + awsAccountCustomerConfig, diags := d.getCustomerConfig(ctx, platform.AWSAccountCustomerConfig) + resp.Diagnostics.Append(diags...) + data.AWSAccountCustomerConfig = awsAccountCustomerConfig + awsOrgReadCustomerConfig, diags := d.getCustomerConfig(ctx, platform.AWSOrgReadCustomerConfig) + resp.Diagnostics.Append(diags...) + data.AWSOrgReadCustomerConfig = awsOrgReadCustomerConfig + + if resp.Diagnostics.HasError() { + return + } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } + +func (d platformDataSource) getCustomerConfig(ctx context.Context, config api.PlatformCustomerConfig) (basetypes.ObjectValue, diag.Diagnostics) { + terraformModule, diags := tftypes.ObjectValue( + ctx, + &config.TerraformModule, + func() (*models.TerraformModule, diag.Diagnostics) { + return &models.TerraformModule{ + RepositoryURL: types.StringValue(config.TerraformModule.RepositoryURL), + Source: types.StringValue(config.TerraformModule.Source), + VariablesJSON: types.StringValue(config.TerraformModule.VariablesJSON), + }, nil + }, + ) + if diags.HasError() { + return basetypes.NewObjectNull(models.PlatformCustomerConfig{}.AttributeTypes()), diags + } + + return tftypes.ObjectValue( + ctx, + &config, + func() (*models.PlatformCustomerConfig, diag.Diagnostics) { + return &models.PlatformCustomerConfig{ + TerraformModule: terraformModule, + }, nil + }, + ) +} diff --git a/internal/models/platform.go b/internal/models/platform.go index 92a8d02..c97c6a1 100644 --- a/internal/models/platform.go +++ b/internal/models/platform.go @@ -3,13 +3,45 @@ package models import ( + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" ) // PlatformDataSource is the model for the platform data source. type PlatformDataSource struct { - ID types.String `tfsdk:"id"` - ExternalID types.String `tfsdk:"external_id"` - ExecutionRegions types.List `tfsdk:"execution_regions"` - DefaultRole types.String `tfsdk:"default_role"` + ID types.String `tfsdk:"id"` + ExternalID types.String `tfsdk:"external_id"` + ExecutionRegions types.List `tfsdk:"execution_regions"` + DefaultRole types.String `tfsdk:"default_role"` + AWSAccountCustomerConfig types.Object `tfsdk:"aws_account_customer_config"` + AWSOrgReadCustomerConfig types.Object `tfsdk:"aws_org_read_customer_config"` +} + +// PlatformCustomerConfig is the model for customer config definitions. +type PlatformCustomerConfig struct { + TerraformModule types.Object `tfsdk:"terraform_module"` +} + +func (c PlatformCustomerConfig) AttributeTypes() map[string]attr.Type { + return map[string]attr.Type{ + "terraform_module": basetypes.ObjectType{ + AttrTypes: TerraformModule{}.AttributeTypes(), + }, + } +} + +// TerraformModule is the model for terraform modules definitions. +type TerraformModule struct { + RepositoryURL types.String `tfsdk:"repository_url"` + Source types.String `tfsdk:"source"` + VariablesJSON types.String `tfsdk:"variables_json"` +} + +func (c TerraformModule) AttributeTypes() map[string]attr.Type { + return map[string]attr.Type{ + "repository_url": types.StringType, + "source": types.StringType, + "variables_json": types.StringType, + } } From 8448bca0baf4d1691572ae7caed35b2bb983cb31 Mon Sep 17 00:00:00 2001 From: Alberto Donato Date: Thu, 26 Jun 2025 14:27:38 +0200 Subject: [PATCH 3/3] drop default_role --- docs/data-sources/platform.md | 1 - .../acceptance_tests/platform_data_source_test.go | 1 - .../recordings/TestAccPlatformDataSource.json | 13 +++++-------- internal/api/platform.go | 1 - internal/datasources/platform.go | 5 ----- internal/models/platform.go | 1 - 6 files changed, 5 insertions(+), 17 deletions(-) diff --git a/docs/data-sources/platform.md b/docs/data-sources/platform.md index 5ff69dc..5dcb625 100644 --- a/docs/data-sources/platform.md +++ b/docs/data-sources/platform.md @@ -24,7 +24,6 @@ data "stacklet_platform" "example" {} - `aws_account_customer_config` (Attributes) Customer configuration for AWS accounts. (see [below for nested schema](#nestedatt--aws_account_customer_config)) - `aws_org_read_customer_config` (Attributes) Customer configuration for AWS organization read access. (see [below for nested schema](#nestedatt--aws_org_read_customer_config)) -- `default_role` (String) Default role for users. - `execution_regions` (List of String) List of regions for which execution is enabled. - `external_id` (String) The external ID for the deployment. - `id` (String) The GraphQL Node ID. diff --git a/internal/acceptance_tests/platform_data_source_test.go b/internal/acceptance_tests/platform_data_source_test.go index 00fa2b5..1a5c812 100644 --- a/internal/acceptance_tests/platform_data_source_test.go +++ b/internal/acceptance_tests/platform_data_source_test.go @@ -19,7 +19,6 @@ func TestAccPlatformDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "external_id"), // at least one region is enabled resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "execution_regions.0"), - resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "default_role"), resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "aws_account_customer_config.terraform_module.source"), resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "aws_org_read_customer_config.terraform_module.source"), ), diff --git a/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json index 0bf360e..4fe49fb 100644 --- a/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json +++ b/internal/acceptance_tests/recordings/TestAccPlatformDataSource.json @@ -1,8 +1,8 @@ { - "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}": [ + "{platform{id,externalID,executionRegions,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}": [ { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" + "query": "{platform{id,externalID,executionRegions,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { @@ -21,7 +21,6 @@ "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" } }, - "defaultRole": "admin", "executionRegions": [ "eu-north-1", "us-east-1" @@ -34,7 +33,7 @@ }, { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" + "query": "{platform{id,externalID,executionRegions,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { @@ -53,7 +52,6 @@ "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" } }, - "defaultRole": "admin", "executionRegions": [ "eu-north-1", "us-east-1" @@ -66,7 +64,7 @@ }, { "request": { - "query": "{platform{id,externalID,executionRegions,defaultRole,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" + "query": "{platform{id,externalID,executionRegions,awsOrgReadCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}},awsAccountCustomerConfig{terraformModule{repositoryURL,source,variablesJSON}}}}" }, "response": { "data": { @@ -85,7 +83,6 @@ "variablesJSON": "{\"prefix\": \"stacklet\", \"stacklet_assetdb_role_arn\": \"arn:aws:iam::905418385756:role/dev-collector\", \"stacklet_external_id\": \"external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2\", \"stacklet_platform_role_arn\": \"arn:aws:iam::905418385756:role/dev-stacklet-platform-lambda\"}" } }, - "defaultRole": "admin", "executionRegions": [ "eu-north-1", "us-east-1" @@ -97,4 +94,4 @@ } } ] -} +} \ No newline at end of file diff --git a/internal/api/platform.go b/internal/api/platform.go index 45a22af..8810032 100644 --- a/internal/api/platform.go +++ b/internal/api/platform.go @@ -13,7 +13,6 @@ type Platform struct { ID string ExternalID *string `graphql:"externalID"` ExecutionRegions []string - DefaultRole *string AWSOrgReadCustomerConfig PlatformCustomerConfig `graphql:"awsOrgReadCustomerConfig"` AWSAccountCustomerConfig PlatformCustomerConfig `graphql:"awsAccountCustomerConfig"` } diff --git a/internal/datasources/platform.go b/internal/datasources/platform.go index 649fe19..ccd854c 100644 --- a/internal/datasources/platform.go +++ b/internal/datasources/platform.go @@ -51,10 +51,6 @@ func (d *platformDataSource) Schema(_ context.Context, _ datasource.SchemaReques Computed: true, ElementType: types.StringType, }, - "default_role": schema.StringAttribute{ - Description: "Default role for users.", - Computed: true, - }, "aws_account_customer_config": schema.SingleNestedAttribute{ Description: "Customer configuration for AWS accounts.", Computed: true, @@ -131,7 +127,6 @@ func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadReques data.ID = types.StringValue(platform.ID) data.ExternalID = tftypes.NullableString(platform.ExternalID) data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions) - data.DefaultRole = tftypes.NullableString(platform.DefaultRole) awsAccountCustomerConfig, diags := d.getCustomerConfig(ctx, platform.AWSAccountCustomerConfig) resp.Diagnostics.Append(diags...) data.AWSAccountCustomerConfig = awsAccountCustomerConfig diff --git a/internal/models/platform.go b/internal/models/platform.go index c97c6a1..98cd0a8 100644 --- a/internal/models/platform.go +++ b/internal/models/platform.go @@ -13,7 +13,6 @@ type PlatformDataSource struct { ID types.String `tfsdk:"id"` ExternalID types.String `tfsdk:"external_id"` ExecutionRegions types.List `tfsdk:"execution_regions"` - DefaultRole types.String `tfsdk:"default_role"` AWSAccountCustomerConfig types.Object `tfsdk:"aws_account_customer_config"` AWSOrgReadCustomerConfig types.Object `tfsdk:"aws_org_read_customer_config"` }