Skip to content

Commit 2496d3c

Browse files
committed
First draft of resource_tfe_aws_oidc_configuration.go
1 parent 516409f commit 2496d3c

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// // Copyright (c) HashiCorp, Inc.
2+
// // SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"github.com/hashicorp/terraform-plugin-framework/path"
11+
12+
tfe "github.com/hashicorp/go-tfe"
13+
"github.com/hashicorp/terraform-plugin-framework/resource"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
15+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
16+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
17+
"github.com/hashicorp/terraform-plugin-framework/types"
18+
"github.com/hashicorp/terraform-plugin-log/tflog"
19+
)
20+
21+
var (
22+
_ resource.ResourceWithConfigure = &resourceTFEAWSOIDCConfiguration{}
23+
_ resource.ResourceWithImportState = &resourceTFEAWSOIDCConfiguration{}
24+
)
25+
26+
func NewAWSOIDCConfigurationResource() resource.Resource {
27+
return &resourceTFEAWSOIDCConfiguration{}
28+
}
29+
30+
type resourceTFEAWSOIDCConfiguration struct {
31+
config ConfiguredClient
32+
}
33+
34+
type modelTFEAWSOIDCConfiguration struct {
35+
ID types.String `tfsdk:"id"`
36+
RoleARN types.String `tfsdk:"role_arn"`
37+
}
38+
39+
func (r *resourceTFEAWSOIDCConfiguration) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
40+
// Prevent panic if the provider has not been configured.
41+
if req.ProviderData == nil {
42+
return
43+
}
44+
45+
client, ok := req.ProviderData.(ConfiguredClient)
46+
if !ok {
47+
resp.Diagnostics.AddError(
48+
"Unexpected resource Configure type",
49+
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData),
50+
)
51+
}
52+
r.config = client
53+
}
54+
55+
func (r *resourceTFEAWSOIDCConfiguration) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
56+
resp.TypeName = req.ProviderTypeName + "_aws_oidc_configuration"
57+
}
58+
59+
func (r *resourceTFEAWSOIDCConfiguration) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
60+
resp.Schema = schema.Schema{
61+
Attributes: map[string]schema.Attribute{
62+
"id": schema.StringAttribute{
63+
Description: "The ID of the AWS OIDC configuration.",
64+
Computed: true,
65+
PlanModifiers: []planmodifier.String{
66+
stringplanmodifier.UseStateForUnknown(),
67+
},
68+
},
69+
"role_arn": schema.StringAttribute{
70+
Description: "The AWS ARN of your role.",
71+
Required: true,
72+
PlanModifiers: []planmodifier.String{
73+
stringplanmodifier.RequiresReplace(),
74+
},
75+
},
76+
},
77+
Description: "Generates a new TFE AWS OIDC Configuration.",
78+
}
79+
}
80+
81+
func (r *resourceTFEAWSOIDCConfiguration) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
82+
// TODO: confirm this is right
83+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
84+
}
85+
86+
func (r *resourceTFEAWSOIDCConfiguration) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
87+
// Read Terraform plan into the model
88+
var plan modelTFEAWSOIDCConfiguration
89+
diags := req.Plan.Get(ctx, &plan)
90+
resp.Diagnostics.Append(diags...)
91+
if resp.Diagnostics.HasError() {
92+
return
93+
}
94+
95+
// Get the organization name from resource or provider config
96+
var orgName string
97+
resp.Diagnostics.Append(r.config.dataOrDefaultOrganization(ctx, req.Config, &orgName)...)
98+
if resp.Diagnostics.HasError() {
99+
return
100+
}
101+
102+
options := tfe.AWSOIDCConfigurationCreateOptions{
103+
RoleARN: plan.RoleARN.ValueString(),
104+
}
105+
106+
tflog.Debug(ctx, fmt.Sprintf("Create TFE AWS OIDC Configuration for organization %s", orgName))
107+
oidc, err := r.config.Client.AWSOIDCConfigurations.Create(ctx, orgName, options)
108+
if err != nil {
109+
resp.Diagnostics.AddError("Error creating TFE AWS OIDC Configuration", err.Error())
110+
return
111+
}
112+
result := modelFromTFEAWSOIDCConfiguration(oidc)
113+
resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
114+
}
115+
116+
func (r *resourceTFEAWSOIDCConfiguration) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
117+
// Read Terraform state into the model
118+
var state modelTFETeamToken
119+
diags := req.State.Get(ctx, &state)
120+
resp.Diagnostics.Append(diags...)
121+
if resp.Diagnostics.HasError() {
122+
return
123+
}
124+
125+
oidcID := state.ID.ValueString()
126+
tflog.Debug(ctx, fmt.Sprintf("Read AWS OIDC configuration: %s", oidcID))
127+
oidc, err := r.config.Client.AWSOIDCConfigurations.Read(ctx, state.ID.ValueString())
128+
if err != nil {
129+
if errors.Is(err, tfe.ErrResourceNotFound) {
130+
tflog.Debug(ctx, fmt.Sprintf("AWS OIDC configuration %s no longer exists", oidcID))
131+
resp.State.RemoveResource(ctx)
132+
return
133+
}
134+
resp.Diagnostics.AddError(
135+
fmt.Sprintf("Error reading AWS OIDC configuration %s", oidcID),
136+
err.Error(),
137+
)
138+
return
139+
}
140+
result := modelFromTFEAWSOIDCConfiguration(oidc)
141+
resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
142+
}
143+
144+
func (r *resourceTFEAWSOIDCConfiguration) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
145+
var plan modelTFEAWSOIDCConfiguration
146+
diags := req.Plan.Get(ctx, &plan)
147+
resp.Diagnostics.Append(diags...)
148+
149+
var state modelTFEAWSOIDCConfiguration
150+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
151+
if resp.Diagnostics.HasError() {
152+
return
153+
}
154+
155+
options := tfe.AWSOIDCConfigurationUpdateOptions{
156+
RoleARN: plan.RoleARN.ValueString(),
157+
}
158+
159+
oidcID := state.ID.ValueString()
160+
tflog.Debug(ctx, fmt.Sprintf("Update TFE AWS OIDC Configuration %s", oidcID))
161+
oidc, err := r.config.Client.AWSOIDCConfigurations.Update(ctx, oidcID, options)
162+
if err != nil {
163+
resp.Diagnostics.AddError("Error updating TFE AWS OIDC Configuration", err.Error())
164+
return
165+
}
166+
167+
result := modelFromTFEAWSOIDCConfiguration(oidc)
168+
resp.Diagnostics.Append(resp.State.Set(ctx, result)...)
169+
}
170+
171+
func (r *resourceTFEAWSOIDCConfiguration) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
172+
var state modelTFEAWSOIDCConfiguration
173+
diags := req.State.Get(ctx, &state)
174+
resp.Diagnostics.Append(diags...)
175+
if resp.Diagnostics.HasError() {
176+
return
177+
}
178+
179+
oidcID := state.ID.ValueString()
180+
tflog.Debug(ctx, fmt.Sprintf("Delete TFE AWS OIDC configuration: %s", oidcID))
181+
err := r.config.Client.AWSOIDCConfigurations.Delete(ctx, oidcID)
182+
if err != nil {
183+
if errors.Is(err, tfe.ErrResourceNotFound) {
184+
tflog.Debug(ctx, fmt.Sprintf("TFE AWS OIDC configuration %s no longer exists", oidcID))
185+
}
186+
187+
resp.Diagnostics.AddError("Error deleting TFE AWS OIDC Configuration", err.Error())
188+
return
189+
}
190+
}
191+
192+
func modelFromTFEAWSOIDCConfiguration(p *tfe.AWSOIDCConfiguration) modelTFEAWSOIDCConfiguration {
193+
return modelTFEAWSOIDCConfiguration{
194+
ID: types.StringValue(p.ID),
195+
RoleARN: types.StringValue(p.RoleARN),
196+
}
197+
}

0 commit comments

Comments
 (0)