|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/go-tfe" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 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/schema/validator" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 19 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 20 | +) |
| 21 | + |
| 22 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 23 | +var _ resource.Resource = &resourceTFEStack{} |
| 24 | +var _ resource.ResourceWithConfigure = &resourceTFEStack{} |
| 25 | +var _ resource.ResourceWithImportState = &resourceTFEStack{} |
| 26 | + |
| 27 | +func NewStackResource() resource.Resource { |
| 28 | + return &resourceTFEStack{} |
| 29 | +} |
| 30 | + |
| 31 | +// resourceTFEStack implements the tfe_stack resource type |
| 32 | +type resourceTFEStack struct { |
| 33 | + config ConfiguredClient |
| 34 | +} |
| 35 | + |
| 36 | +func (r *resourceTFEStack) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_stack" |
| 38 | +} |
| 39 | + |
| 40 | +func (r *resourceTFEStack) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 41 | + pathVCSRepoOAuthTokenID := path.Expressions{ |
| 42 | + path.MatchRelative().AtParent().AtName("oauth_token_id"), |
| 43 | + } |
| 44 | + pathGHAInstallationID := path.Expressions{ |
| 45 | + path.MatchRelative().AtParent().AtName("github_app_installation_id"), |
| 46 | + } |
| 47 | + |
| 48 | + resp.Schema = schema.Schema{ |
| 49 | + Description: "Defines a Stack resource. Note that a Stack cannot be destroyed if it contains deployments that have underlying managed resources.", |
| 50 | + Version: 1, |
| 51 | + |
| 52 | + Blocks: map[string]schema.Block{ |
| 53 | + "vcs_repo": schema.SingleNestedBlock{ |
| 54 | + Description: "VCS repository configuration for the Stack.", |
| 55 | + Attributes: map[string]schema.Attribute{ |
| 56 | + "identifier": schema.StringAttribute{ |
| 57 | + Description: "Identifier of the VCS repository.", |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + "branch": schema.StringAttribute{ |
| 61 | + Description: "The repository branch that Terraform should use. This defaults to the respository's default branch (e.g. main).", |
| 62 | + Optional: true, |
| 63 | + }, |
| 64 | + "github_app_installation_id": schema.StringAttribute{ |
| 65 | + Description: "The installation ID of the GitHub App. This conflicts with `oauth_token_id` and can only be used if `oauth_token_id` is not used.", |
| 66 | + Optional: true, |
| 67 | + Validators: []validator.String{ |
| 68 | + stringvalidator.AtLeastOneOf(pathVCSRepoOAuthTokenID...), |
| 69 | + stringvalidator.ConflictsWith(pathVCSRepoOAuthTokenID...), |
| 70 | + }, |
| 71 | + }, |
| 72 | + "oauth_token_id": schema.StringAttribute{ |
| 73 | + Description: "The VCS Connection to use. This ID can be obtained from a `tfe_oauth_client` resource. This conflicts with `github_app_installation_id` and can only be used if `github_app_installation_id` is not used.", |
| 74 | + Optional: true, |
| 75 | + Validators: []validator.String{ |
| 76 | + stringvalidator.AtLeastOneOf(pathGHAInstallationID...), |
| 77 | + stringvalidator.ConflictsWith(pathGHAInstallationID...), |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + Attributes: map[string]schema.Attribute{ |
| 85 | + "id": schema.StringAttribute{ |
| 86 | + Description: "ID of the Stack.", |
| 87 | + Computed: true, |
| 88 | + PlanModifiers: []planmodifier.String{ |
| 89 | + stringplanmodifier.UseStateForUnknown(), |
| 90 | + }, |
| 91 | + }, |
| 92 | + "project_id": schema.StringAttribute{ |
| 93 | + Description: "ID of the project that the Stack belongs to.", |
| 94 | + Required: true, |
| 95 | + PlanModifiers: []planmodifier.String{ |
| 96 | + stringplanmodifier.RequiresReplace(), |
| 97 | + }, |
| 98 | + }, |
| 99 | + "name": schema.StringAttribute{ |
| 100 | + Description: "Name of the Stack", |
| 101 | + Required: true, |
| 102 | + }, |
| 103 | + "description": schema.StringAttribute{ |
| 104 | + Description: "Description of the Stack", |
| 105 | + Optional: true, |
| 106 | + }, |
| 107 | + "deployment_names": schema.SetAttribute{ |
| 108 | + Description: "The time when the Stack was created.", |
| 109 | + Computed: true, |
| 110 | + ElementType: types.StringType, |
| 111 | + }, |
| 112 | + "created_at": schema.StringAttribute{ |
| 113 | + Description: "The time when the stack was created.", |
| 114 | + Computed: true, |
| 115 | + PlanModifiers: []planmodifier.String{ |
| 116 | + stringplanmodifier.UseStateForUnknown(), |
| 117 | + }, |
| 118 | + }, |
| 119 | + "updated_at": schema.StringAttribute{ |
| 120 | + Description: "The time when the stack was last updated.", |
| 121 | + Computed: true, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Configure implements resource.ResourceWithConfigure |
| 128 | +func (r *resourceTFEStack) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 129 | + // Prevent panic if the provider has not been configured. |
| 130 | + if req.ProviderData == nil { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 135 | + if !ok { |
| 136 | + resp.Diagnostics.AddError( |
| 137 | + "Unexpected resource Configure type", |
| 138 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 139 | + ) |
| 140 | + } |
| 141 | + r.config = client |
| 142 | +} |
| 143 | + |
| 144 | +func (r *resourceTFEStack) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 145 | + var plan modelTFEStack |
| 146 | + |
| 147 | + // Read Terraform plan data into the model |
| 148 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 149 | + |
| 150 | + if resp.Diagnostics.HasError() { |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + if resp.Diagnostics.HasError() { |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + options := tfe.StackCreateOptions{ |
| 159 | + Name: plan.Name.ValueString(), |
| 160 | + VCSRepo: &tfe.StackVCSRepo{ |
| 161 | + Identifier: plan.VCSRepo.Identifier.ValueString(), |
| 162 | + Branch: plan.VCSRepo.Branch.ValueString(), |
| 163 | + GHAInstallationID: plan.VCSRepo.GHAInstallationID.ValueString(), |
| 164 | + OAuthTokenID: plan.VCSRepo.OAuthTokenID.ValueString(), |
| 165 | + }, |
| 166 | + Project: &tfe.Project{ |
| 167 | + ID: plan.ProjectID.ValueString(), |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + if !plan.Description.IsNull() { |
| 172 | + options.Description = tfe.String(plan.Description.ValueString()) |
| 173 | + } |
| 174 | + |
| 175 | + tflog.Debug(ctx, "Creating stack") |
| 176 | + stack, err := r.config.Client.Stacks.Create(ctx, options) |
| 177 | + if err != nil { |
| 178 | + resp.Diagnostics.AddError("Unable to create stack", err.Error()) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + result := modelFromTFEStack(stack) |
| 183 | + |
| 184 | + // Save data into Terraform state |
| 185 | + resp.Diagnostics.Append(resp.State.Set(ctx, &result)...) |
| 186 | +} |
| 187 | + |
| 188 | +func (r *resourceTFEStack) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 189 | + var state modelTFEStack |
| 190 | + |
| 191 | + // Read Terraform prior state data into the model |
| 192 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 193 | + |
| 194 | + if resp.Diagnostics.HasError() { |
| 195 | + return |
| 196 | + } |
| 197 | + |
| 198 | + tflog.Debug(ctx, fmt.Sprintf("Reading stack %q", state.ID.ValueString())) |
| 199 | + stack, err := r.config.Client.Stacks.Read(ctx, state.ID.ValueString()) |
| 200 | + if err != nil { |
| 201 | + resp.Diagnostics.AddError("Unable to read stack", err.Error()) |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + result := modelFromTFEStack(stack) |
| 206 | + |
| 207 | + // Save updated data into Terraform state |
| 208 | + resp.Diagnostics.Append(resp.State.Set(ctx, &result)...) |
| 209 | +} |
| 210 | + |
| 211 | +func (r *resourceTFEStack) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 212 | + var plan modelTFEStack |
| 213 | + var state modelTFEStack |
| 214 | + |
| 215 | + // Read Terraform plan data into the model |
| 216 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 217 | + |
| 218 | + // Read Terraform prior state data into the model |
| 219 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 220 | + |
| 221 | + if resp.Diagnostics.HasError() { |
| 222 | + return |
| 223 | + } |
| 224 | + |
| 225 | + options := tfe.StackUpdateOptions{ |
| 226 | + Name: tfe.String(plan.Name.ValueString()), |
| 227 | + Description: tfe.String(plan.Description.ValueString()), |
| 228 | + VCSRepo: &tfe.StackVCSRepo{ |
| 229 | + Identifier: plan.VCSRepo.Identifier.ValueString(), |
| 230 | + Branch: plan.VCSRepo.Branch.ValueString(), |
| 231 | + GHAInstallationID: plan.VCSRepo.GHAInstallationID.ValueString(), |
| 232 | + OAuthTokenID: plan.VCSRepo.OAuthTokenID.ValueString(), |
| 233 | + }, |
| 234 | + } |
| 235 | + |
| 236 | + tflog.Debug(ctx, "Updating stack") |
| 237 | + stack, err := r.config.Client.Stacks.Update(ctx, state.ID.ValueString(), options) |
| 238 | + if err != nil { |
| 239 | + resp.Diagnostics.AddError("Unable to update stack", err.Error()) |
| 240 | + return |
| 241 | + } |
| 242 | + |
| 243 | + result := modelFromTFEStack(stack) |
| 244 | + |
| 245 | + // Save data into Terraform state |
| 246 | + resp.Diagnostics.Append(resp.State.Set(ctx, &result)...) |
| 247 | +} |
| 248 | + |
| 249 | +func (r *resourceTFEStack) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 250 | + var state modelTFEStack |
| 251 | + |
| 252 | + // Read Terraform prior state data into the model |
| 253 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 254 | + |
| 255 | + if resp.Diagnostics.HasError() { |
| 256 | + return |
| 257 | + } |
| 258 | + |
| 259 | + tflog.Debug(ctx, "Deleting stack") |
| 260 | + err := r.config.Client.Stacks.Delete(ctx, state.ID.ValueString()) |
| 261 | + if err != nil { |
| 262 | + resp.Diagnostics.AddError("Unable to delete stack", err.Error()) |
| 263 | + return |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +func (r *resourceTFEStack) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 268 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), req.ID)...) |
| 269 | +} |
0 commit comments