|
| 1 | +// Copyright (c) Venafi, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "terraform-provider-tlspc/internal/tlspc" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + _ resource.Resource = &pluginResource{} |
| 22 | + _ resource.ResourceWithConfigure = &pluginResource{} |
| 23 | + _ resource.ResourceWithImportState = &pluginResource{} |
| 24 | +) |
| 25 | + |
| 26 | +type pluginResource struct { |
| 27 | + client *tlspc.Client |
| 28 | +} |
| 29 | + |
| 30 | +func NewPluginResource() resource.Resource { |
| 31 | + return &pluginResource{} |
| 32 | +} |
| 33 | + |
| 34 | +func (r *pluginResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 35 | + resp.TypeName = req.ProviderTypeName + "_plugin" |
| 36 | +} |
| 37 | + |
| 38 | +func (r *pluginResource) 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 | + }, |
| 44 | + "type": schema.StringAttribute{ |
| 45 | + Required: true, |
| 46 | + }, |
| 47 | + "manifest": schema.StringAttribute{ |
| 48 | + Required: true, |
| 49 | + CustomType: jsontypes.NormalizedType{}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (r *pluginResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 56 | + if req.ProviderData == nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + client, ok := req.ProviderData.(*tlspc.Client) |
| 61 | + |
| 62 | + if !ok { |
| 63 | + resp.Diagnostics.AddError( |
| 64 | + "Unexpected Data Source Configure Type", |
| 65 | + fmt.Sprintf("Expected *tlspc.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 66 | + ) |
| 67 | + |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + r.client = client |
| 72 | +} |
| 73 | + |
| 74 | +type pluginResourceModel struct { |
| 75 | + ID types.String `tfsdk:"id"` |
| 76 | + Type types.String `tfsdk:"type"` |
| 77 | + Manifest jsontypes.Normalized `tfsdk:"manifest"` |
| 78 | +} |
| 79 | + |
| 80 | +func (r *pluginResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 81 | + var plan pluginResourceModel |
| 82 | + diags := req.Plan.Get(ctx, &plan) |
| 83 | + resp.Diagnostics.Append(diags...) |
| 84 | + if resp.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + var manifest any |
| 89 | + err := json.Unmarshal([]byte(plan.Manifest.ValueString()), &manifest) |
| 90 | + if err != nil { |
| 91 | + resp.Diagnostics.AddError( |
| 92 | + "Error creating plugin", |
| 93 | + "Could not create team, invalid manifest: "+err.Error(), |
| 94 | + ) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + plugin := tlspc.Plugin{ |
| 99 | + ID: plan.ID.ValueString(), |
| 100 | + Type: plan.Type.ValueString(), |
| 101 | + Manifest: manifest, |
| 102 | + } |
| 103 | + |
| 104 | + created, err := r.client.CreatePlugin(plugin) |
| 105 | + if err != nil { |
| 106 | + resp.Diagnostics.AddError( |
| 107 | + "Error creating plugin", |
| 108 | + "Could not create plugin, unexpected error: "+err.Error(), |
| 109 | + ) |
| 110 | + return |
| 111 | + } |
| 112 | + plan.ID = types.StringValue(created.ID) |
| 113 | + diags = resp.State.Set(ctx, plan) |
| 114 | + resp.Diagnostics.Append(diags...) |
| 115 | +} |
| 116 | + |
| 117 | +func (r *pluginResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 118 | + var state pluginResourceModel |
| 119 | + |
| 120 | + diags := req.State.Get(ctx, &state) |
| 121 | + resp.Diagnostics.Append(diags...) |
| 122 | + if resp.Diagnostics.HasError() { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + plugin, err := r.client.GetPlugin(state.ID.ValueString()) |
| 127 | + if err != nil { |
| 128 | + resp.Diagnostics.AddError( |
| 129 | + "Error Reading Plugin", |
| 130 | + "Could not read plugin ID "+state.ID.ValueString()+": "+err.Error(), |
| 131 | + ) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + state.ID = types.StringValue(plugin.ID) |
| 136 | + state.Type = types.StringValue(plugin.Type) |
| 137 | + stateManifest, err := json.Marshal(plugin.Manifest) |
| 138 | + if err != nil { |
| 139 | + resp.Diagnostics.AddError( |
| 140 | + "Error Reading Plugin", |
| 141 | + "Could not read plugin manifest: "+err.Error(), |
| 142 | + ) |
| 143 | + return |
| 144 | + } |
| 145 | + state.Manifest = jsontypes.NewNormalizedValue(string(stateManifest)) |
| 146 | + |
| 147 | + diags = resp.State.Set(ctx, state) |
| 148 | + resp.Diagnostics.Append(diags...) |
| 149 | +} |
| 150 | + |
| 151 | +func (r *pluginResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 152 | + var state, plan pluginResourceModel |
| 153 | + |
| 154 | + diags := req.State.Get(ctx, &state) |
| 155 | + resp.Diagnostics.Append(diags...) |
| 156 | + if resp.Diagnostics.HasError() { |
| 157 | + return |
| 158 | + } |
| 159 | + diags = req.Plan.Get(ctx, &plan) |
| 160 | + resp.Diagnostics.Append(diags...) |
| 161 | + if resp.Diagnostics.HasError() { |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + var manifest any |
| 166 | + err := json.Unmarshal([]byte(plan.Manifest.ValueString()), &manifest) |
| 167 | + if err != nil { |
| 168 | + resp.Diagnostics.AddError( |
| 169 | + "Error Reading Plugin", |
| 170 | + "Could not read plugin manifest: "+err.Error(), |
| 171 | + ) |
| 172 | + return |
| 173 | + } |
| 174 | + plugin := tlspc.Plugin{ |
| 175 | + ID: state.ID.ValueString(), |
| 176 | + Type: plan.Type.ValueString(), |
| 177 | + Manifest: manifest, |
| 178 | + } |
| 179 | + err = r.client.UpdatePlugin(plugin) |
| 180 | + if err != nil { |
| 181 | + resp.Diagnostics.AddError( |
| 182 | + "Error updating Plugin", |
| 183 | + "Could not update plugin: "+err.Error(), |
| 184 | + ) |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + plan.ID = state.ID |
| 189 | + diags = resp.State.Set(ctx, plan) |
| 190 | + resp.Diagnostics.Append(diags...) |
| 191 | +} |
| 192 | + |
| 193 | +func (r *pluginResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 194 | + var state pluginResourceModel |
| 195 | + |
| 196 | + diags := req.State.Get(ctx, &state) |
| 197 | + resp.Diagnostics.Append(diags...) |
| 198 | + if resp.Diagnostics.HasError() { |
| 199 | + return |
| 200 | + } |
| 201 | + |
| 202 | + err := r.client.DeletePlugin(state.ID.ValueString()) |
| 203 | + if err != nil { |
| 204 | + resp.Diagnostics.AddError( |
| 205 | + "Error Deleting Plugin", |
| 206 | + "Could not delete plugin ID "+state.ID.ValueString()+": "+err.Error(), |
| 207 | + ) |
| 208 | + return |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func (r *pluginResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 213 | + // Retrieve import ID and save to id attribute |
| 214 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 215 | +} |
0 commit comments