|
| 1 | +// Copyright (c) Venafi, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "terraform-provider-tlspc/internal/tlspc" |
| 11 | + |
| 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/types" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + _ resource.Resource = &cloudProviderGCPResource{} |
| 22 | + _ resource.ResourceWithConfigure = &cloudProviderGCPResource{} |
| 23 | + _ resource.ResourceWithImportState = &cloudProviderGCPResource{} |
| 24 | +) |
| 25 | + |
| 26 | +type cloudProviderGCPResource struct { |
| 27 | + client *tlspc.Client |
| 28 | +} |
| 29 | + |
| 30 | +func NewCloudProviderGCPResource() resource.Resource { |
| 31 | + return &cloudProviderGCPResource{} |
| 32 | +} |
| 33 | + |
| 34 | +func (r *cloudProviderGCPResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 35 | + resp.TypeName = req.ProviderTypeName + "_cloudprovider_gcp" |
| 36 | +} |
| 37 | + |
| 38 | +func (r *cloudProviderGCPResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 39 | + resp.Schema = schema.Schema{ |
| 40 | + Attributes: map[string]schema.Attribute{ |
| 41 | + "id": schema.StringAttribute{ |
| 42 | + Computed: true, |
| 43 | + PlanModifiers: []planmodifier.String{ |
| 44 | + stringplanmodifier.UseStateForUnknown(), |
| 45 | + }, |
| 46 | + }, |
| 47 | + "issuer_url": schema.StringAttribute{ |
| 48 | + Computed: true, |
| 49 | + PlanModifiers: []planmodifier.String{ |
| 50 | + stringplanmodifier.UseStateForUnknown(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + "name": schema.StringAttribute{ |
| 54 | + Required: true, |
| 55 | + }, |
| 56 | + "team": schema.StringAttribute{ |
| 57 | + Required: true, |
| 58 | + }, |
| 59 | + "service_account_email": schema.StringAttribute{ |
| 60 | + Required: true, |
| 61 | + }, |
| 62 | + "project_number": schema.Int64Attribute{ |
| 63 | + Required: true, |
| 64 | + }, |
| 65 | + "workload_identity_pool_id": schema.StringAttribute{ |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + "workload_identity_pool_provider_id": schema.StringAttribute{ |
| 69 | + Required: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (r *cloudProviderGCPResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 76 | + if req.ProviderData == nil { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + client, ok := req.ProviderData.(*tlspc.Client) |
| 81 | + |
| 82 | + if !ok { |
| 83 | + resp.Diagnostics.AddError( |
| 84 | + "Unexpected Data Source Configure Type", |
| 85 | + fmt.Sprintf("Expected *tlspc.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 86 | + ) |
| 87 | + |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + r.client = client |
| 92 | +} |
| 93 | + |
| 94 | +type cloudProviderGCPResourceModel struct { |
| 95 | + ID types.String `tfsdk:"id"` |
| 96 | + IssuerUrl types.String `tfsdk:"issuer_url"` |
| 97 | + Name types.String `tfsdk:"name"` |
| 98 | + Team types.String `tfsdk:"team"` |
| 99 | + ServiceAccountEmail types.String `tfsdk:"service_account_email"` |
| 100 | + ProjectNumber types.Int64 `tfsdk:"project_number"` |
| 101 | + WorkloadIdentityPoolId types.String `tfsdk:"workload_identity_pool_id"` |
| 102 | + WorkloadIdentityPoolProviderId types.String `tfsdk:"workload_identity_pool_provider_id"` |
| 103 | +} |
| 104 | + |
| 105 | +func (r *cloudProviderGCPResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 106 | + var plan cloudProviderGCPResourceModel |
| 107 | + diags := req.Plan.Get(ctx, &plan) |
| 108 | + resp.Diagnostics.Append(diags...) |
| 109 | + if resp.Diagnostics.HasError() { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + p := tlspc.CloudProviderGCP{ |
| 114 | + Name: plan.Name.ValueString(), |
| 115 | + Team: plan.Team.ValueString(), |
| 116 | + ServiceAccountEmail: plan.ServiceAccountEmail.ValueString(), |
| 117 | + ProjectNumber: plan.ProjectNumber.ValueInt64(), |
| 118 | + WorkloadIdentityPoolId: plan.WorkloadIdentityPoolId.ValueString(), |
| 119 | + WorkloadIdentityPoolProviderId: plan.WorkloadIdentityPoolProviderId.ValueString(), |
| 120 | + } |
| 121 | + |
| 122 | + created, err := r.client.CreateCloudProviderGCP(ctx, p) |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + resp.Diagnostics.AddError( |
| 126 | + "Error creating GCP Cloud Provider", |
| 127 | + "Could not create GCP Cloud Provider: "+err.Error(), |
| 128 | + ) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + plan.ID = types.StringValue(created.ID) |
| 133 | + plan.IssuerUrl = types.StringValue(created.IssuerUrl) |
| 134 | + |
| 135 | + diags = resp.State.Set(ctx, plan) |
| 136 | + resp.Diagnostics.Append(diags...) |
| 137 | +} |
| 138 | + |
| 139 | +func (r *cloudProviderGCPResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 140 | + var state cloudProviderGCPResourceModel |
| 141 | + |
| 142 | + diags := req.State.Get(ctx, &state) |
| 143 | + resp.Diagnostics.Append(diags...) |
| 144 | + if resp.Diagnostics.HasError() { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + cp, err := r.client.GetCloudProviderGCP(ctx, state.ID.ValueString()) |
| 149 | + if err != nil { |
| 150 | + resp.Diagnostics.AddError( |
| 151 | + "Error retrieving GCP Cloud Provider", |
| 152 | + "Could not find GCP Cloud Provider: "+err.Error(), |
| 153 | + ) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + state.IssuerUrl = types.StringValue(cp.IssuerUrl) |
| 158 | + state.Name = types.StringValue(cp.Name) |
| 159 | + state.Team = types.StringValue(cp.Team) |
| 160 | + state.ServiceAccountEmail = types.StringValue(cp.ServiceAccountEmail) |
| 161 | + state.ProjectNumber = types.Int64Value(cp.ProjectNumber) |
| 162 | + state.WorkloadIdentityPoolId = types.StringValue(cp.WorkloadIdentityPoolId) |
| 163 | + state.WorkloadIdentityPoolProviderId = types.StringValue(cp.WorkloadIdentityPoolProviderId) |
| 164 | + |
| 165 | + diags = resp.State.Set(ctx, state) |
| 166 | + resp.Diagnostics.Append(diags...) |
| 167 | +} |
| 168 | + |
| 169 | +func (r *cloudProviderGCPResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 170 | + var state, plan cloudProviderGCPResourceModel |
| 171 | + |
| 172 | + diags := req.State.Get(ctx, &state) |
| 173 | + resp.Diagnostics.Append(diags...) |
| 174 | + if resp.Diagnostics.HasError() { |
| 175 | + return |
| 176 | + } |
| 177 | + diags = req.Plan.Get(ctx, &plan) |
| 178 | + resp.Diagnostics.Append(diags...) |
| 179 | + if resp.Diagnostics.HasError() { |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + cp := tlspc.CloudProviderGCP{ |
| 184 | + ID: state.ID.ValueString(), |
| 185 | + Name: plan.Name.ValueString(), |
| 186 | + Team: plan.Team.ValueString(), |
| 187 | + ServiceAccountEmail: plan.ServiceAccountEmail.ValueString(), |
| 188 | + ProjectNumber: plan.ProjectNumber.ValueInt64(), |
| 189 | + WorkloadIdentityPoolId: plan.WorkloadIdentityPoolId.ValueString(), |
| 190 | + WorkloadIdentityPoolProviderId: plan.WorkloadIdentityPoolProviderId.ValueString(), |
| 191 | + } |
| 192 | + |
| 193 | + updated, err := r.client.UpdateCloudProviderGCP(ctx, cp) |
| 194 | + |
| 195 | + if err != nil { |
| 196 | + resp.Diagnostics.AddError( |
| 197 | + "Error updating GCP Cloud Provider", |
| 198 | + "Could not update GCP Cloud Provider, unexpected error: "+err.Error(), |
| 199 | + ) |
| 200 | + return |
| 201 | + } |
| 202 | + plan.IssuerUrl = types.StringValue(updated.IssuerUrl) |
| 203 | + |
| 204 | + diags = resp.State.Set(ctx, plan) |
| 205 | + resp.Diagnostics.Append(diags...) |
| 206 | +} |
| 207 | + |
| 208 | +func (r *cloudProviderGCPResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 209 | + var state cloudProviderGCPResourceModel |
| 210 | + |
| 211 | + diags := req.State.Get(ctx, &state) |
| 212 | + resp.Diagnostics.Append(diags...) |
| 213 | + if resp.Diagnostics.HasError() { |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + err := r.client.DeleteCloudProviderGCP(ctx, state.ID.ValueString()) |
| 218 | + if err != nil { |
| 219 | + resp.Diagnostics.AddError( |
| 220 | + "Error updating GCP Cloud Provider", |
| 221 | + "Could not updating GCP Cloud Provider: "+err.Error(), |
| 222 | + ) |
| 223 | + return |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +func (r *cloudProviderGCPResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 228 | + // Retrieve import ID and save to id attribute |
| 229 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 230 | +} |
0 commit comments