Skip to content

Commit 8ad5c07

Browse files
authored
Add support for defining aws account name (#1976)
* implemented * update docs * Using computed instead of plan modifier * update acct datasource * attempt to fix tests * fix test * Fix datasource * regen docs
1 parent b6f981e commit 8ad5c07

File tree

8 files changed

+53
-6
lines changed

8 files changed

+53
-6
lines changed

docs/data-sources/cloud_provider_aws_account.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ data "grafana_cloud_provider_aws_account" "test" {
4343
### Read-Only
4444

4545
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".
46+
- `name` (String) An optional human-readable name for this AWS Account resource.
4647
- `regions` (Set of String) A set of regions that this AWS Account resource applies to.
4748
- `role_arn` (String) An IAM Role ARN string to represent with this AWS Account resource.

docs/resources/cloud_provider_aws_account.md

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ resource "grafana_cloud_provider_aws_account" "test" {
4141
- `role_arn` (String) An IAM Role ARN string to represent with this AWS Account resource.
4242
- `stack_id` (String) The StackID of the Grafana Cloud instance. Part of the Terraform Resource ID.
4343

44+
### Optional
45+
46+
- `name` (String) An optional human-readable name for this AWS Account resource.
47+
4448
### Read-Only
4549

4650
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ resource_id }}".

internal/common/cloudproviderapi/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type AWSAccount struct {
5959

6060
// Regions is the list of AWS regions in use for the AWS Account.
6161
Regions []string `json:"regions"`
62+
63+
// Name is an optional user-defined name for the AWS account.
64+
Name string `json:"name"`
6265
}
6366

6467
func (c *Client) CreateAWSAccount(ctx context.Context, stackID string, accountData AWSAccount) (AWSAccount, error) {

internal/resources/cloudprovider/data_source_aws_account.go

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (r datasourceAWSAccount) Schema(ctx context.Context, req datasource.SchemaR
5656
Description: "The ID given by the Grafana Cloud Provider API to this AWS Account resource.",
5757
Required: true,
5858
},
59+
"name": schema.StringAttribute{
60+
Description: "An optional human-readable name for this AWS Account resource.",
61+
Computed: true,
62+
},
5963
"role_arn": schema.StringAttribute{
6064
Description: "An IAM Role ARN string to represent with this AWS Account resource.",
6165
Computed: true,
@@ -97,6 +101,11 @@ func (r datasourceAWSAccount) Read(ctx context.Context, req datasource.ReadReque
97101
if resp.Diagnostics.HasError() {
98102
return
99103
}
104+
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
105+
resp.Diagnostics.Append(diags...)
106+
if resp.Diagnostics.HasError() {
107+
return
108+
}
100109
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
101110
resp.Diagnostics.Append(diags...)
102111
if resp.Diagnostics.HasError() {

internal/resources/cloudprovider/data_source_aws_account_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func TestAccDataSourceAWSAccount(t *testing.T) {
1717

1818
account := cloudproviderapi.AWSAccount{
1919
ID: testCfg.accountID,
20+
Name: testCfg.accountName,
2021
RoleARN: testCfg.roleARN,
2122
Regions: []string{"us-east-1", "us-east-2", "us-west-1"},
2223
}
@@ -29,6 +30,7 @@ func TestAccDataSourceAWSAccount(t *testing.T) {
2930
Check: resource.ComposeTestCheckFunc(
3031
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "stack_id", testCfg.stackID),
3132
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "resource_id", account.ID),
33+
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "name", account.Name),
3234
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "role_arn", account.RoleARN),
3335
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "regions.#", strconv.Itoa(len(account.Regions))),
3436
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_account.test", "regions.0", account.Regions[0]),

internal/resources/cloudprovider/resource_aws_account.go

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/resource"
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1414
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
15+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
1516
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1617
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1718
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -27,6 +28,7 @@ type resourceAWSAccountModel struct {
2728
ID types.String `tfsdk:"id"`
2829
StackID types.String `tfsdk:"stack_id"`
2930
ResourceID types.String `tfsdk:"resource_id"`
31+
Name types.String `tfsdk:"name"`
3032
RoleARN types.String `tfsdk:"role_arn"`
3133
Regions types.Set `tfsdk:"regions"`
3234
}
@@ -90,6 +92,12 @@ func (r resourceAWSAccount) Schema(ctx context.Context, req resource.SchemaReque
9092
stringplanmodifier.UseStateForUnknown(),
9193
},
9294
},
95+
"name": schema.StringAttribute{
96+
Description: "An optional human-readable name for this AWS Account resource.",
97+
Optional: true,
98+
Computed: true, // used to complete the default "" if the resource existed before this field was introduced
99+
Default: stringdefault.StaticString(""),
100+
},
93101
"role_arn": schema.StringAttribute{
94102
Description: "An IAM Role ARN string to represent with this AWS Account resource.",
95103
Required: true,
@@ -134,6 +142,7 @@ func (r resourceAWSAccount) ImportState(ctx context.Context, req resource.Import
134142
ID: types.StringValue(req.ID),
135143
StackID: types.StringValue(stackID),
136144
ResourceID: types.StringValue(resourceID),
145+
Name: types.StringValue(account.Name),
137146
RoleARN: types.StringValue(account.RoleARN),
138147
Regions: regions,
139148
})
@@ -149,6 +158,7 @@ func (r resourceAWSAccount) Create(ctx context.Context, req resource.CreateReque
149158

150159
accountData := cloudproviderapi.AWSAccount{}
151160
accountData.RoleARN = data.RoleARN.ValueString()
161+
accountData.Name = data.Name.ValueString()
152162
diags = data.Regions.ElementsAs(ctx, &accountData.Regions, false)
153163
resp.Diagnostics.Append(diags...)
154164
if resp.Diagnostics.HasError() {
@@ -168,6 +178,7 @@ func (r resourceAWSAccount) Create(ctx context.Context, req resource.CreateReque
168178
ID: types.StringValue(resourceAWSAccountTerraformID.Make(data.StackID.ValueString(), account.ID)),
169179
StackID: data.StackID,
170180
ResourceID: types.StringValue(account.ID),
181+
Name: data.Name,
171182
RoleARN: data.RoleARN,
172183
Regions: data.Regions,
173184
})
@@ -196,6 +207,11 @@ func (r resourceAWSAccount) Read(ctx context.Context, req resource.ReadRequest,
196207
if resp.Diagnostics.HasError() {
197208
return
198209
}
210+
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
211+
resp.Diagnostics.Append(diags...)
212+
if resp.Diagnostics.HasError() {
213+
return
214+
}
199215
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
200216
resp.Diagnostics.Append(diags...)
201217
if resp.Diagnostics.HasError() {
@@ -213,6 +229,7 @@ func (r *resourceAWSAccount) Update(ctx context.Context, req resource.UpdateRequ
213229

214230
accountData := cloudproviderapi.AWSAccount{}
215231
accountData.RoleARN = planData.RoleARN.ValueString()
232+
accountData.Name = planData.Name.ValueString()
216233
diags = planData.Regions.ElementsAs(ctx, &accountData.Regions, false)
217234
resp.Diagnostics.Append(diags...)
218235
if resp.Diagnostics.HasError() {
@@ -234,6 +251,11 @@ func (r *resourceAWSAccount) Update(ctx context.Context, req resource.UpdateRequ
234251
if resp.Diagnostics.HasError() {
235252
return
236253
}
254+
diags = resp.State.SetAttribute(ctx, path.Root("name"), account.Name)
255+
resp.Diagnostics.Append(diags...)
256+
if resp.Diagnostics.HasError() {
257+
return
258+
}
237259
diags = resp.State.SetAttribute(ctx, path.Root("regions"), account.Regions)
238260
resp.Diagnostics.Append(diags...)
239261
if resp.Diagnostics.HasError() {

internal/resources/cloudprovider/resource_aws_account_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestAccResourceAWSAccount(t *testing.T) {
2121

2222
account := cloudproviderapi.AWSAccount{
2323
RoleARN: testCfg.roleARN,
24+
Name: testCfg.accountName,
2425
Regions: []string{"us-east-1", "us-east-2", "us-west-1"},
2526
}
2627
var gotAccount cloudproviderapi.AWSAccount
@@ -34,6 +35,7 @@ func TestAccResourceAWSAccount(t *testing.T) {
3435
checkAWSAccountResourceExists("grafana_cloud_provider_aws_account.test", testCfg.stackID, &gotAccount),
3536
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "stack_id", testCfg.stackID),
3637
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "role_arn", account.RoleARN),
38+
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "name", account.Name),
3739
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.#", strconv.Itoa(len(account.Regions))),
3840
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.0", account.Regions[0]),
3941
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_account.test", "regions.1", account.Regions[1]),
@@ -106,11 +108,13 @@ func awsAccountResourceData(stackID string, account cloudproviderapi.AWSAccount)
106108
resource "grafana_cloud_provider_aws_account" "test" {
107109
stack_id = "%[1]s"
108110
role_arn = "%[2]s"
111+
name = "%[4]s"
109112
regions = [%[3]s]
110113
}
111114
`,
112115
stackID,
113116
account.RoleARN,
114117
regionsString(account.Regions),
118+
account.Name,
115119
)
116120
}

internal/resources/cloudprovider/utils_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
)
1414

1515
type testConfig struct {
16-
stackID string
17-
accountID string
18-
roleARN string
16+
stackID string
17+
accountID string
18+
accountName string
19+
roleARN string
1920
}
2021

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

3839
return testConfig{
39-
stackID: stackID,
40-
accountID: accountID,
41-
roleARN: roleARN,
40+
stackID: stackID,
41+
accountID: accountID,
42+
accountName: gotAccount.Name,
43+
roleARN: roleARN,
4244
}
4345
}
4446

0 commit comments

Comments
 (0)