|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/coder/coder/v2/codersdk" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 16 | +var _ resource.Resource = &WorkspaceProxyResource{} |
| 17 | + |
| 18 | +func NewWorkspaceProxyResource() resource.Resource { |
| 19 | + return &WorkspaceProxyResource{} |
| 20 | +} |
| 21 | + |
| 22 | +// WorkspaceProxyResource defines the resource implementation. |
| 23 | +type WorkspaceProxyResource struct { |
| 24 | + data *CoderdProviderData |
| 25 | +} |
| 26 | + |
| 27 | +// WorkspaceProxyResourceModel describes the resource data model. |
| 28 | +type WorkspaceProxyResourceModel struct { |
| 29 | + ID UUID `tfsdk:"id"` |
| 30 | + Name types.String `tfsdk:"name"` |
| 31 | + DisplayName types.String `tfsdk:"display_name"` |
| 32 | + Icon types.String `tfsdk:"icon"` |
| 33 | + SessionToken types.String `tfsdk:"session_token"` |
| 34 | +} |
| 35 | + |
| 36 | +func (r *WorkspaceProxyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_workspace_proxy" |
| 38 | +} |
| 39 | + |
| 40 | +func (r *WorkspaceProxyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + MarkdownDescription: "A Workspace Proxy for the Coder deployment.", |
| 43 | + |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + "id": schema.StringAttribute{ |
| 46 | + CustomType: UUIDType, |
| 47 | + Computed: true, |
| 48 | + MarkdownDescription: "Workspace Proxy ID", |
| 49 | + PlanModifiers: []planmodifier.String{ |
| 50 | + stringplanmodifier.UseStateForUnknown(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + "name": schema.StringAttribute{ |
| 54 | + MarkdownDescription: "Name of the workspace proxy.", |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + "display_name": schema.StringAttribute{ |
| 58 | + MarkdownDescription: "Display name of the workspace proxy.", |
| 59 | + Optional: true, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "icon": schema.StringAttribute{ |
| 63 | + MarkdownDescription: "Relative path or external URL that specifes an icon to be displayed in the dashboard.", |
| 64 | + Required: true, |
| 65 | + }, |
| 66 | + "session_token": schema.StringAttribute{ |
| 67 | + MarkdownDescription: "Session token for the workspace proxy.", |
| 68 | + Computed: true, |
| 69 | + PlanModifiers: []planmodifier.String{ |
| 70 | + stringplanmodifier.UseStateForUnknown(), |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (r *WorkspaceProxyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 78 | + // Prevent panic if the provider has not been configured. |
| 79 | + if req.ProviderData == nil { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + data, ok := req.ProviderData.(*CoderdProviderData) |
| 84 | + |
| 85 | + if !ok { |
| 86 | + resp.Diagnostics.AddError( |
| 87 | + "Unexpected Resource Configure Type", |
| 88 | + fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 89 | + ) |
| 90 | + |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + r.data = data |
| 95 | +} |
| 96 | + |
| 97 | +func (r *WorkspaceProxyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 98 | + var data WorkspaceProxyResourceModel |
| 99 | + |
| 100 | + // Read Terraform plan data into the model |
| 101 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 102 | + if resp.Diagnostics.HasError() { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + client := r.data.Client |
| 107 | + wsp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{ |
| 108 | + Name: data.Name.ValueString(), |
| 109 | + DisplayName: data.DisplayName.ValueString(), |
| 110 | + Icon: data.Icon.ValueString(), |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to create workspace proxy: %v", err)) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + data.ID = UUIDValue(wsp.Proxy.ID) |
| 118 | + data.Name = types.StringValue(wsp.Proxy.Name) |
| 119 | + data.DisplayName = types.StringValue(wsp.Proxy.DisplayName) |
| 120 | + data.Icon = types.StringValue(wsp.Proxy.IconURL) |
| 121 | + data.SessionToken = types.StringValue(wsp.ProxyToken) |
| 122 | + |
| 123 | + // Save data into Terraform state |
| 124 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 125 | +} |
| 126 | + |
| 127 | +func (r *WorkspaceProxyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 128 | + var data WorkspaceProxyResourceModel |
| 129 | + |
| 130 | + // Read Terraform prior state data into the model |
| 131 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 132 | + |
| 133 | + if resp.Diagnostics.HasError() { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + client := r.data.Client |
| 138 | + wsp, err := client.WorkspaceProxyByID(ctx, data.ID.ValueUUID()) |
| 139 | + if err != nil { |
| 140 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to read workspace proxy: %v", err)) |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + data.ID = UUIDValue(wsp.ID) |
| 145 | + data.Name = types.StringValue(wsp.Name) |
| 146 | + data.DisplayName = types.StringValue(wsp.DisplayName) |
| 147 | + data.Icon = types.StringValue(wsp.IconURL) |
| 148 | + |
| 149 | + // Save updated data into Terraform state |
| 150 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 151 | +} |
| 152 | + |
| 153 | +func (r *WorkspaceProxyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 154 | + var data WorkspaceProxyResourceModel |
| 155 | + |
| 156 | + // Read Terraform plan data into the model |
| 157 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 158 | + |
| 159 | + if resp.Diagnostics.HasError() { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + client := r.data.Client |
| 164 | + |
| 165 | + wsp, err := client.PatchWorkspaceProxy(ctx, codersdk.PatchWorkspaceProxy{ |
| 166 | + ID: data.ID.ValueUUID(), |
| 167 | + Name: data.Name.ValueString(), |
| 168 | + DisplayName: data.DisplayName.ValueString(), |
| 169 | + Icon: data.Icon.ValueString(), |
| 170 | + RegenerateToken: false, |
| 171 | + }) |
| 172 | + if err != nil { |
| 173 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update workspace proxy: %v", err)) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + data.Name = types.StringValue(wsp.Proxy.Name) |
| 178 | + data.DisplayName = types.StringValue(wsp.Proxy.DisplayName) |
| 179 | + data.Icon = types.StringValue(wsp.Proxy.IconURL) |
| 180 | + |
| 181 | + // Save updated data into Terraform state |
| 182 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 183 | +} |
| 184 | + |
| 185 | +func (r *WorkspaceProxyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 186 | + var data WorkspaceProxyResourceModel |
| 187 | + |
| 188 | + // Read Terraform prior state data into the model |
| 189 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 190 | + |
| 191 | + if resp.Diagnostics.HasError() { |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + client := r.data.Client |
| 196 | + err := client.DeleteWorkspaceProxyByID(ctx, data.ID.ValueUUID()) |
| 197 | + if err != nil { |
| 198 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete workspace proxy: %v", err)) |
| 199 | + return |
| 200 | + } |
| 201 | +} |
0 commit comments