Skip to content

Commit 145a888

Browse files
committed
resource_tfe_aws_oidc_configuration.go and basic test
1 parent 2496d3c commit 145a888

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

internal/provider/resource_tfe_aws_oidc_configuration.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ type resourceTFEAWSOIDCConfiguration struct {
3232
}
3333

3434
type modelTFEAWSOIDCConfiguration struct {
35-
ID types.String `tfsdk:"id"`
36-
RoleARN types.String `tfsdk:"role_arn"`
35+
ID types.String `tfsdk:"id"`
36+
RoleARN types.String `tfsdk:"role_arn"`
37+
Organization types.String `tfsdk:"organization"`
3738
}
3839

3940
func (r *resourceTFEAWSOIDCConfiguration) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
@@ -73,13 +74,20 @@ func (r *resourceTFEAWSOIDCConfiguration) Schema(_ context.Context, _ resource.S
7374
stringplanmodifier.RequiresReplace(),
7475
},
7576
},
77+
"organization": schema.StringAttribute{
78+
Description: "Name of the organization to which the TFE AWS OIDC configuration belongs.",
79+
Optional: true,
80+
Computed: true,
81+
PlanModifiers: []planmodifier.String{
82+
stringplanmodifier.RequiresReplace(),
83+
},
84+
},
7685
},
7786
Description: "Generates a new TFE AWS OIDC Configuration.",
7887
}
7988
}
8089

8190
func (r *resourceTFEAWSOIDCConfiguration) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
82-
// TODO: confirm this is right
8391
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
8492
}
8593

@@ -115,7 +123,7 @@ func (r *resourceTFEAWSOIDCConfiguration) Create(ctx context.Context, req resour
115123

116124
func (r *resourceTFEAWSOIDCConfiguration) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
117125
// Read Terraform state into the model
118-
var state modelTFETeamToken
126+
var state modelTFEAWSOIDCConfiguration
119127
diags := req.State.Get(ctx, &state)
120128
resp.Diagnostics.Append(diags...)
121129
if resp.Diagnostics.HasError() {
@@ -191,7 +199,8 @@ func (r *resourceTFEAWSOIDCConfiguration) Delete(ctx context.Context, req resour
191199

192200
func modelFromTFEAWSOIDCConfiguration(p *tfe.AWSOIDCConfiguration) modelTFEAWSOIDCConfiguration {
193201
return modelTFEAWSOIDCConfiguration{
194-
ID: types.StringValue(p.ID),
195-
RoleARN: types.StringValue(p.RoleARN),
202+
ID: types.StringValue(p.ID),
203+
RoleARN: types.StringValue(p.RoleARN),
204+
Organization: types.StringValue(p.Organization.Name),
196205
}
197206
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
)
11+
12+
func TestAccTFEAWSOIDCConfiguration_basic(t *testing.T) {
13+
orgName := os.Getenv("HYOK_ORGANIZATION_NAME")
14+
15+
if orgName == "" {
16+
t.Skip("Test skipped: HYOK_ORGANIZATION_NAME environment variable is not set")
17+
}
18+
19+
originalRoleARN := "arn:aws:iam::123456789012:role/terraform-provider-tfe-example-1"
20+
newRoleARN := "arn:aws:iam::123456789012:role/terraform-provider-tfe-example-2"
21+
22+
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { testAccPreCheck(t) },
24+
ProtoV5ProviderFactories: testAccMuxedProviders,
25+
CheckDestroy: testAccCheckTFEAWSOIDCConfigurationDestroy,
26+
Steps: []resource.TestStep{
27+
{
28+
Config: testAccTFEAWSOIDCConfigurationConfig(orgName, originalRoleARN),
29+
Check: resource.ComposeAggregateTestCheckFunc(
30+
resource.TestCheckResourceAttrSet("tfe_aws_oidc_configuration.test", "id"),
31+
resource.TestCheckResourceAttr("tfe_aws_oidc_configuration.test", "role_arn", originalRoleARN),
32+
),
33+
},
34+
// Import
35+
{
36+
ResourceName: "tfe_aws_oidc_configuration.test",
37+
ImportState: true,
38+
ImportStateVerify: true,
39+
},
40+
// Update role ARN
41+
{
42+
Config: testAccTFEAWSOIDCConfigurationConfig(orgName, newRoleARN),
43+
Check: resource.ComposeAggregateTestCheckFunc(
44+
resource.TestCheckResourceAttrSet("tfe_aws_oidc_configuration.test", "id"),
45+
resource.TestCheckResourceAttr("tfe_aws_oidc_configuration.test", "role_arn", newRoleARN),
46+
),
47+
},
48+
},
49+
})
50+
}
51+
52+
func testAccTFEAWSOIDCConfigurationConfig(orgName string, roleARN string) string {
53+
return fmt.Sprintf(`
54+
resource "tfe_aws_oidc_configuration" "test" {
55+
role_arn = "%s"
56+
organization = "%s"
57+
}
58+
`, roleARN, orgName)
59+
}
60+
61+
func testAccCheckTFEAWSOIDCConfigurationDestroy(s *terraform.State) error {
62+
for _, rs := range s.RootModule().Resources {
63+
if rs.Type != "tfe_aws_oidc_configuration" {
64+
continue
65+
}
66+
67+
if rs.Primary.ID == "" {
68+
return fmt.Errorf("no instance ID is set")
69+
}
70+
71+
_, err := testAccConfiguredClient.Client.AWSOIDCConfigurations.Read(ctx, rs.Primary.ID)
72+
if err == nil {
73+
return fmt.Errorf("TFE AWS OIDC Configuration %s still exists", rs.Primary.ID)
74+
}
75+
}
76+
77+
return nil
78+
}

0 commit comments

Comments
 (0)