Skip to content

Commit 64b70d4

Browse files
committed
modified the implementation to include enabled argument and added import
1 parent 0ad4d5f commit 64b70d4

File tree

3 files changed

+92
-79
lines changed

3 files changed

+92
-79
lines changed

internal/service/iam/outbound_web_identity_federation.go

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
1212
"github.com/hashicorp/terraform-plugin-framework/resource"
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1415
"github.com/hashicorp/terraform-plugin-framework/types"
1516
"github.com/hashicorp/terraform-provider-aws/internal/errs"
1617
"github.com/hashicorp/terraform-provider-aws/internal/framework"
1718
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
1819
"github.com/hashicorp/terraform-provider-aws/internal/smerr"
20+
"github.com/hashicorp/terraform-provider-aws/names"
1921
)
2022

2123
// @FrameworkResource("aws_iam_outbound_web_identity_federation", name="Outbound Web Identity Federation")
@@ -31,16 +33,19 @@ const (
3133

3234
type resourceOutboundWebIdentityFederation struct {
3335
framework.ResourceWithModel[resourceOutboundWebIdentityFederationModel]
34-
framework.WithNoUpdate
36+
framework.WithImportByID
3537
}
3638

3739
func (r *resourceOutboundWebIdentityFederation) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
3840
resp.Schema = schema.Schema{
3941
Attributes: map[string]schema.Attribute{
40-
"issuer_identifier": schema.StringAttribute{
42+
names.AttrEnabled: schema.BoolAttribute{
43+
Optional: true,
4144
Computed: true,
45+
Default: booldefault.StaticBool(true),
4246
},
43-
"jwt_vending_enabled": schema.BoolAttribute{
47+
names.AttrID: framework.IDAttribute(),
48+
"issuer_identifier": schema.StringAttribute{
4449
Computed: true,
4550
},
4651
},
@@ -55,24 +60,8 @@ func (r *resourceOutboundWebIdentityFederation) Create(ctx context.Context, req
5560
if resp.Diagnostics.HasError() {
5661
return
5762
}
58-
59-
out, err := conn.EnableOutboundWebIdentityFederation(ctx, &iam.EnableOutboundWebIdentityFederationInput{})
60-
if errs.IsA[*awstypes.FeatureEnabledException](err) {
61-
// Feature is already enabled, adopt existing state
62-
outAlreadyEnabled, err := getOutboundWebIdentityFederation(ctx, conn)
63-
if err != nil {
64-
smerr.AddError(ctx, &resp.Diagnostics, err)
65-
return
66-
}
67-
if outAlreadyEnabled == nil {
68-
smerr.AddError(ctx, &resp.Diagnostics, fmt.Errorf("expected non-nil response from GetOutboundWebIdentityFederationInfo"))
69-
return
70-
}
71-
smerr.AddEnrich(ctx, &resp.Diagnostics, flex.Flatten(ctx, outAlreadyEnabled, &plan))
72-
if resp.Diagnostics.HasError() {
73-
return
74-
}
75-
} else {
63+
if plan.Enabled.ValueBool() {
64+
out, err := conn.EnableOutboundWebIdentityFederation(ctx, &iam.EnableOutboundWebIdentityFederationInput{})
7665
if err != nil {
7766
smerr.AddError(ctx, &resp.Diagnostics, err)
7867
return
@@ -81,12 +70,15 @@ func (r *resourceOutboundWebIdentityFederation) Create(ctx context.Context, req
8170
smerr.AddError(ctx, &resp.Diagnostics, fmt.Errorf("expected non-nil response from EnableOutboundWebIdentityFederation"))
8271
return
8372
}
84-
plan.JwtVendingEnabled = types.BoolValue(true)
73+
plan.Enabled = types.BoolValue(true)
8574
smerr.AddEnrich(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &plan))
8675
if resp.Diagnostics.HasError() {
8776
return
8877
}
78+
} else {
79+
plan.Enabled = types.BoolValue(false)
8980
}
81+
plan.AccountId = types.StringValue(r.Meta().AccountID(ctx))
9082
smerr.AddEnrich(ctx, &resp.Diagnostics, resp.State.Set(ctx, plan))
9183
}
9284

@@ -104,13 +96,52 @@ func (r *resourceOutboundWebIdentityFederation) Read(ctx context.Context, req re
10496
smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID)
10597
return
10698
}
99+
if out != nil {
100+
smerr.AddEnrich(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &state))
101+
if resp.Diagnostics.HasError() {
102+
return
103+
}
104+
}
105+
if state.Enabled.IsNull() || state.Enabled.IsUnknown() {
106+
state.Enabled = types.BoolValue(out != nil)
107+
}
108+
109+
smerr.AddEnrich(ctx, &resp.Diagnostics, resp.State.Set(ctx, &state))
110+
}
111+
112+
func (r *resourceOutboundWebIdentityFederation) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
113+
conn := r.Meta().IAMClient(ctx)
114+
115+
var plan resourceOutboundWebIdentityFederationModel
116+
smerr.AddEnrich(ctx, &resp.Diagnostics, req.Plan.Get(ctx, &plan))
117+
if resp.Diagnostics.HasError() {
118+
return
119+
}
107120

108-
smerr.AddEnrich(ctx, &resp.Diagnostics, flex.Flatten(ctx, out, &state))
121+
var state resourceOutboundWebIdentityFederationModel
122+
smerr.AddEnrich(ctx, &resp.Diagnostics, req.State.Get(ctx, &state))
109123
if resp.Diagnostics.HasError() {
110124
return
111125
}
112126

113-
smerr.AddEnrich(ctx, &resp.Diagnostics, resp.State.Set(ctx, &state))
127+
if plan.Enabled.ValueBool() != state.Enabled.ValueBool() {
128+
if plan.Enabled.ValueBool() {
129+
_, err := conn.EnableOutboundWebIdentityFederation(ctx, &iam.EnableOutboundWebIdentityFederationInput{})
130+
if err != nil && !errs.IsA[*awstypes.FeatureEnabledException](err) {
131+
smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID)
132+
return
133+
}
134+
} else {
135+
_, err := conn.DisableOutboundWebIdentityFederation(ctx, &iam.DisableOutboundWebIdentityFederationInput{})
136+
if err != nil && !errs.IsA[*awstypes.FeatureDisabledException](err) {
137+
smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID)
138+
return
139+
}
140+
plan.IssuerIdentifier = types.StringNull()
141+
}
142+
}
143+
144+
smerr.AddEnrich(ctx, &resp.Diagnostics, resp.State.Set(ctx, &plan))
114145
}
115146

116147
func (r *resourceOutboundWebIdentityFederation) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
@@ -133,8 +164,9 @@ func (r *resourceOutboundWebIdentityFederation) Delete(ctx context.Context, req
133164
}
134165

135166
type resourceOutboundWebIdentityFederationModel struct {
136-
JwtVendingEnabled types.Bool `tfsdk:"jwt_vending_enabled"`
137-
IssuerIdentifier types.String `tfsdk:"issuer_identifier"`
167+
Enabled types.Bool `tfsdk:"enabled"`
168+
AccountId types.String `tfsdk:"id"`
169+
IssuerIdentifier types.String `tfsdk:"issuer_identifier"`
138170
}
139171

140172
func getOutboundWebIdentityFederation(ctx context.Context, conn *iam.Client) (*iam.GetOutboundWebIdentityFederationInfoOutput, error) {

internal/service/iam/outbound_web_identity_federation_test.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"testing"
1010

11-
"github.com/aws/aws-sdk-go-v2/service/iam"
1211
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1312
"github.com/hashicorp/terraform-plugin-testing/terraform"
1413
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
@@ -17,18 +16,7 @@ import (
1716
"github.com/hashicorp/terraform-provider-aws/names"
1817
)
1918

20-
func TestAccIAMOutboundWebIdentityFederation_serial(t *testing.T) {
21-
t.Helper()
22-
23-
testCases := map[string]func(t *testing.T){
24-
acctest.CtBasic: testAccIAMOutboundWebIdentityFederation_basic,
25-
"alreadyEnabled": testAccIAMOutboundWebIdentityFederation_alreadyEnabled,
26-
}
27-
28-
acctest.RunSerialTests1Level(t, testCases, 0)
29-
}
30-
31-
func testAccIAMOutboundWebIdentityFederation_basic(t *testing.T) {
19+
func TestAccIAMOutboundWebIdentityFederation_basic(t *testing.T) {
3220
ctx := acctest.Context(t)
3321

3422
resourceName := "aws_iam_outbound_web_identity_federation.test"
@@ -43,43 +31,21 @@ func testAccIAMOutboundWebIdentityFederation_basic(t *testing.T) {
4331
CheckDestroy: testAccCheckOutboundWebIdentityFederationDestroy(ctx),
4432
Steps: []resource.TestStep{
4533
{
46-
Config: testAccOutboundWebIdentityFederationConfig_basic(),
34+
Config: testAccOutboundWebIdentityFederationConfig_basic(true),
4735
Check: resource.ComposeAggregateTestCheckFunc(
48-
resource.TestCheckResourceAttr(resourceName, "jwt_vending_enabled", acctest.CtTrue),
36+
resource.TestCheckResourceAttr(resourceName, "enabled", acctest.CtTrue),
4937
resource.TestCheckResourceAttrSet(resourceName, "issuer_identifier"),
5038
),
5139
},
52-
},
53-
})
54-
}
55-
56-
func testAccIAMOutboundWebIdentityFederation_alreadyEnabled(t *testing.T) {
57-
ctx := acctest.Context(t)
58-
59-
resourceName := "aws_iam_outbound_web_identity_federation.test"
60-
61-
resource.Test(t, resource.TestCase{
62-
PreCheck: func() {
63-
acctest.PreCheck(ctx, t)
64-
testAccPreCheck(ctx, t)
65-
},
66-
ErrorCheck: acctest.ErrorCheck(t, names.IAMServiceID),
67-
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
68-
CheckDestroy: testAccCheckOutboundWebIdentityFederationDestroy(ctx),
69-
Steps: []resource.TestStep{
7040
{
71-
PreConfig: func() {
72-
conn := acctest.Provider.Meta().(*conns.AWSClient).IAMClient(ctx)
73-
74-
_, err := conn.EnableOutboundWebIdentityFederation(ctx, &iam.EnableOutboundWebIdentityFederationInput{})
75-
if err != nil {
76-
t.Fatalf("error enabling outbound web identity federation: %s", err)
77-
}
78-
},
79-
Config: testAccOutboundWebIdentityFederationConfig_basic(),
41+
ResourceName: resourceName,
42+
ImportState: true,
43+
ImportStateVerify: true,
44+
},
45+
{
46+
Config: testAccOutboundWebIdentityFederationConfig_basic(false),
8047
Check: resource.ComposeAggregateTestCheckFunc(
81-
resource.TestCheckResourceAttr(resourceName, "jwt_vending_enabled", acctest.CtTrue),
82-
resource.TestCheckResourceAttrSet(resourceName, "issuer_identifier"),
48+
resource.TestCheckResourceAttr(resourceName, "enabled", acctest.CtFalse),
8349
),
8450
},
8551
},
@@ -125,9 +91,10 @@ func testAccPreCheck(ctx context.Context, t *testing.T) {
12591
}
12692
}
12793

128-
func testAccOutboundWebIdentityFederationConfig_basic() string {
129-
return `
94+
func testAccOutboundWebIdentityFederationConfig_basic(enabled bool) string {
95+
return fmt.Sprintf(`
13096
resource "aws_iam_outbound_web_identity_federation" "test" {
97+
enabled = %[1]t
13198
}
132-
`
99+
`, enabled)
133100
}

website/docs/r/iam_outbound_web_identity_federation.html.markdown

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,41 @@ description: |-
1010

1111
Manages an AWS IAM (Identity & Access Management) Outbound Web Identity Federation.
1212

13-
~> **NOTE:** This resource will enable IAM Outbound Web Identity Federation on the account when created and disable when destroyed.
13+
~> **NOTE:** Removing this Terraform resource disables IAM Outbound Web Identity Federation.
1414

1515
## Example Usage
1616

1717
```terraform
18-
resource "aws_iam_outbound_web_identity_federation" "example" {}
18+
resource "aws_iam_outbound_web_identity_federation" "example" {
19+
enabled = true
20+
}
1921
```
2022

2123
## Argument Reference
2224

23-
This resource does not support any arguments.
25+
* `enabled` - (Optional) Whether or not Outbound Web Identity Federation is enabled. Valid values are `true` or `false`. Defaults to `true`.
2426

2527
## Attribute Reference
2628

2729
This resource exports the following attributes in addition to the arguments above:
2830

31+
* `id` - Unique identifier for the account registration. Since registration is applied globaly, this will be the Account ID.
2932
* `issuer_identifier` - A unique issuer URL for your AWS account that hosts the OpenID Connect (OIDC) discovery endpoints.
30-
* `jwt_vending_enabled` - Indicates whether outbound identity federation is currently enabled for your AWS account.
33+
3134

3235
## Import
3336

34-
You cannot import this resource.
37+
In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import IAM Outbound Web Identity Federation resources using the `id`. For example:
38+
39+
```terraform
40+
import {
41+
to = aws_iam_outbound_web_identity_federation.example
42+
id = "123456789012"
43+
}
44+
```
45+
46+
Using `terraform import`, import IAM Outbound Web Identity Federation resources using the `id`. For example:
3547

36-
~> **NOTE:** This resource will adopt the IAM Outbound Web Identity Federation setting in the account if this setting is already enabled.
48+
```console
49+
% terraform import aws_iam_outbound_web_identity_federation.example 123456789012
50+
```

0 commit comments

Comments
 (0)