Skip to content

Commit

Permalink
Add support for defining aws account name (#1976)
Browse files Browse the repository at this point in the history
* implemented

* update docs

* Using computed instead of plan modifier

* update acct datasource

* attempt to fix tests

* fix test

* Fix datasource

* regen docs
  • Loading branch information
thepalbi authored Jan 24, 2025
1 parent b6f981e commit 8ad5c07
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/cloud_provider_aws_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ data "grafana_cloud_provider_aws_account" "test" {
### Read-Only

- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
- `name` (String) An optional human-readable name for this AWS Account resource.
- `regions` (Set of String) A set of regions that this AWS Account resource applies to.
- `role_arn` (String) An IAM Role ARN string to represent with this AWS Account resource.
4 changes: 4 additions & 0 deletions docs/resources/cloud_provider_aws_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ resource "grafana_cloud_provider_aws_account" "test" {
- `role_arn` (String) An IAM Role ARN string to represent with this AWS Account resource.
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.

### Optional

- `name` (String) An optional human-readable name for this AWS Account resource.

### Read-Only

- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
Expand Down
3 changes: 3 additions & 0 deletions internal/common/cloudproviderapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type AWSAccount struct {

// Regions is the list of AWS regions in use for the AWS Account.
Regions []string `json:"regions"`

// Name is an optional user-defined name for the AWS account.
Name string `json:"name"`
}

func (c *Client) CreateAWSAccount(ctx context.Context, stackID string, accountData AWSAccount) (AWSAccount, error) {
Expand Down
9 changes: 9 additions & 0 deletions internal/resources/cloudprovider/data_source_aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (r datasourceAWSAccount) Schema(ctx context.Context, req datasource.SchemaR
Description: "The ID given by the Grafana Cloud Provider API to this AWS Account resource.",
Required: true,
},
"name": schema.StringAttribute{
Description: "An optional human-readable name for this AWS Account resource.",
Computed: true,
},
"role_arn": schema.StringAttribute{
Description: "An IAM Role ARN string to represent with this AWS Account resource.",
Computed: true,
Expand Down Expand Up @@ -97,6 +101,11 @@ func (r datasourceAWSAccount) Read(ctx context.Context, req datasource.ReadReque
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAccDataSourceAWSAccount(t *testing.T) {

account := cloudproviderapi.AWSAccount{
ID: testCfg.accountID,
Name: testCfg.accountName,
RoleARN: testCfg.roleARN,
Regions: []string{"us-east-1", "us-east-2", "us-west-1"},
}
Expand All @@ -29,6 +30,7 @@ func TestAccDataSourceAWSAccount(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "stack_id", testCfg.stackID),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "resource_id", account.ID),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "name", account.Name),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "role_arn", account.RoleARN),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "regions.#", strconv.Itoa(len(account.Regions))),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "regions.0", account.Regions[0]),
Expand Down
22 changes: 22 additions & 0 deletions internal/resources/cloudprovider/resource_aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -27,6 +28,7 @@ type resourceAWSAccountModel struct {
ID types.String `tfsdk:"id"`
StackID types.String `tfsdk:"stack_id"`
ResourceID types.String `tfsdk:"resource_id"`
Name types.String `tfsdk:"name"`
RoleARN types.String `tfsdk:"role_arn"`
Regions types.Set `tfsdk:"regions"`
}
Expand Down Expand Up @@ -90,6 +92,12 @@ func (r resourceAWSAccount) Schema(ctx context.Context, req resource.SchemaReque
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Description: "An optional human-readable name for this AWS Account resource.",
Optional: true,
Computed: true, // used to complete the default "" if the resource existed before this field was introduced
Default: stringdefault.StaticString(""),
},
"role_arn": schema.StringAttribute{
Description: "An IAM Role ARN string to represent with this AWS Account resource.",
Required: true,
Expand Down Expand Up @@ -134,6 +142,7 @@ func (r resourceAWSAccount) ImportState(ctx context.Context, req resource.Import
ID: types.StringValue(req.ID),
StackID: types.StringValue(stackID),
ResourceID: types.StringValue(resourceID),
Name: types.StringValue(account.Name),
RoleARN: types.StringValue(account.RoleARN),
Regions: regions,
})
Expand All @@ -149,6 +158,7 @@ func (r resourceAWSAccount) Create(ctx context.Context, req resource.CreateReque

accountData := cloudproviderapi.AWSAccount{}
accountData.RoleARN = data.RoleARN.ValueString()
accountData.Name = data.Name.ValueString()
diags = data.Regions.ElementsAs(ctx, &accountData.Regions, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -168,6 +178,7 @@ func (r resourceAWSAccount) Create(ctx context.Context, req resource.CreateReque
ID: types.StringValue(resourceAWSAccountTerraformID.Make(data.StackID.ValueString(), account.ID)),
StackID: data.StackID,
ResourceID: types.StringValue(account.ID),
Name: data.Name,
RoleARN: data.RoleARN,
Regions: data.Regions,
})
Expand Down Expand Up @@ -196,6 +207,11 @@ func (r resourceAWSAccount) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -213,6 +229,7 @@ func (r *resourceAWSAccount) Update(ctx context.Context, req resource.UpdateRequ

accountData := cloudproviderapi.AWSAccount{}
accountData.RoleARN = planData.RoleARN.ValueString()
accountData.Name = planData.Name.ValueString()
diags = planData.Regions.ElementsAs(ctx, &accountData.Regions, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -234,6 +251,11 @@ func (r *resourceAWSAccount) Update(ctx context.Context, req resource.UpdateRequ
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
4 changes: 4 additions & 0 deletions internal/resources/cloudprovider/resource_aws_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccResourceAWSAccount(t *testing.T) {

account := cloudproviderapi.AWSAccount{
RoleARN: testCfg.roleARN,
Name: testCfg.accountName,
Regions: []string{"us-east-1", "us-east-2", "us-west-1"},
}
var gotAccount cloudproviderapi.AWSAccount
Expand All @@ -34,6 +35,7 @@ func TestAccResourceAWSAccount(t *testing.T) {
checkAWSAccountResourceExists("grafana_cloud_provider_aws_account.test", testCfg.stackID, &gotAccount),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "stack_id", testCfg.stackID),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "role_arn", account.RoleARN),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "name", account.Name),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.#", strconv.Itoa(len(account.Regions))),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.0", account.Regions[0]),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.1", account.Regions[1]),
Expand Down Expand Up @@ -106,11 +108,13 @@ func awsAccountResourceData(stackID string, account cloudproviderapi.AWSAccount)
resource "grafana_cloud_provider_aws_account" "test" {
stack_id = "%[1]s"
role_arn = "%[2]s"
name = "%[4]s"
regions = [%[3]s]
}
`,
stackID,
account.RoleARN,
regionsString(account.Regions),
account.Name,
)
}
14 changes: 8 additions & 6 deletions internal/resources/cloudprovider/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

type testConfig struct {
stackID string
accountID string
roleARN string
stackID string
accountID string
accountName string
roleARN string
}

func makeTestConfig(t require.TestingT) testConfig {
Expand All @@ -36,9 +37,10 @@ func makeTestConfig(t require.TestingT) testConfig {
require.Equal(t, roleARN, gotAccount.RoleARN)

return testConfig{
stackID: stackID,
accountID: accountID,
roleARN: roleARN,
stackID: stackID,
accountID: accountID,
accountName: gotAccount.Name,
roleARN: roleARN,
}
}

Expand Down

0 comments on commit 8ad5c07

Please sign in to comment.