|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | + |
| 7 | +"github.com/google/uuid" |
| 8 | +"github.com/hashicorp/terraform-plugin-framework/path" |
| 9 | +"github.com/hashicorp/terraform-plugin-framework/resource" |
| 10 | +"github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 11 | +"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 12 | +"github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 16 | +var_ resource.Resource=&TemplateResource{} |
| 17 | +var_ resource.ResourceWithImportState=&TemplateResource{} |
| 18 | + |
| 19 | +funcNewTemplateResource() resource.Resource { |
| 20 | +return&TemplateResource{} |
| 21 | +} |
| 22 | + |
| 23 | +// TemplateResource defines the resource implementation. |
| 24 | +typeTemplateResourcestruct { |
| 25 | +data*CoderdProviderData |
| 26 | +} |
| 27 | + |
| 28 | +// TemplateResourceModel describes the resource data model. |
| 29 | +typeTemplateResourceModelstruct { |
| 30 | +ID types.String`tfsdk:"id"` |
| 31 | + |
| 32 | +Name types.String`tfsdk:"name"` |
| 33 | +DisplayName types.String`tfsdk:"display_name"` |
| 34 | +Description types.String`tfsdk:"description"` |
| 35 | +OrganizationID types.String`tfsdk:"organization_id"` |
| 36 | + |
| 37 | +Latest*TemplateVersion`tfsdk:"latest"` |
| 38 | +} |
| 39 | + |
| 40 | +typeTemplateVersionstruct { |
| 41 | +Name types.String`tfsdk:"name"` |
| 42 | +Message types.String`tfsdk:"message"` |
| 43 | +Directory types.String`tfsdk:"directory"` |
| 44 | +DirectoryHash types.String`tfsdk:"directory_hash"` |
| 45 | +} |
| 46 | + |
| 47 | +func (r*TemplateResource)Metadata(ctx context.Context,req resource.MetadataRequest,resp*resource.MetadataResponse) { |
| 48 | +resp.TypeName=req.ProviderTypeName+"_template" |
| 49 | +} |
| 50 | + |
| 51 | +func (r*TemplateResource)Schema(ctx context.Context,req resource.SchemaRequest,resp*resource.SchemaResponse) { |
| 52 | +resp.Schema= schema.Schema{ |
| 53 | +MarkdownDescription:"A Coder template", |
| 54 | + |
| 55 | +Attributes:map[string]schema.Attribute{ |
| 56 | +"id": schema.StringAttribute{ |
| 57 | +MarkdownDescription:"The ID of the template.", |
| 58 | +Computed:true, |
| 59 | +}, |
| 60 | +"name": schema.StringAttribute{ |
| 61 | +MarkdownDescription:"The name of the template.", |
| 62 | +Required:true, |
| 63 | +}, |
| 64 | +"display_name": schema.StringAttribute{ |
| 65 | +MarkdownDescription:"The display name of the template. Defaults to the template name.", |
| 66 | +Optional:true, |
| 67 | +}, |
| 68 | +"description": schema.StringAttribute{ |
| 69 | +MarkdownDescription:"A description of the template.", |
| 70 | +Optional:true, |
| 71 | +}, |
| 72 | +// TODO: Rest of the fields |
| 73 | +"organization_id": schema.StringAttribute{ |
| 74 | +MarkdownDescription:"The ID of the organization. Defaults to the provider's default organization", |
| 75 | +Optional:true, |
| 76 | +}, |
| 77 | +"latest": schema.SingleNestedAttribute{ |
| 78 | +MarkdownDescription:"The latest version of the template.", |
| 79 | +Required:true, |
| 80 | +Attributes:map[string]schema.Attribute{ |
| 81 | +"name": schema.StringAttribute{ |
| 82 | +MarkdownDescription:"The name of the template version. Automatically generated if not provided.", |
| 83 | +Optional:true, |
| 84 | +}, |
| 85 | +"message": schema.StringAttribute{ |
| 86 | +MarkdownDescription:"A message describing the changes in this version of the template. Messages longer than 72 characters will be truncated..", |
| 87 | +Optional:true, |
| 88 | +}, |
| 89 | +"directory": schema.StringAttribute{ |
| 90 | +MarkdownDescription:"A path to the directory to create the template version from. Changes in the directory contents will trigger the creation of a new template version.", |
| 91 | +Required:true, |
| 92 | +}, |
| 93 | +"directory_hash": schema.StringAttribute{ |
| 94 | +Computed:true, |
| 95 | +PlanModifiers: []planmodifier.String{ |
| 96 | +NewDirectoryHashPlanModifier(), |
| 97 | +}, |
| 98 | +}, |
| 99 | +// TODO: Rest of the fields |
| 100 | +}, |
| 101 | +}, |
| 102 | +}, |
| 103 | +} |
| 104 | +} |
| 105 | + |
| 106 | +func (r*TemplateResource)Configure(ctx context.Context,req resource.ConfigureRequest,resp*resource.ConfigureResponse) { |
| 107 | +// Prevent panic if the provider has not been configured. |
| 108 | +ifreq.ProviderData==nil { |
| 109 | +return |
| 110 | +} |
| 111 | + |
| 112 | +data,ok:=req.ProviderData.(*CoderdProviderData) |
| 113 | + |
| 114 | +if!ok { |
| 115 | +resp.Diagnostics.AddError( |
| 116 | +"Unexpected Resource Configure Type", |
| 117 | +fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.",req.ProviderData), |
| 118 | +) |
| 119 | + |
| 120 | +return |
| 121 | +} |
| 122 | + |
| 123 | +r.data=data |
| 124 | +} |
| 125 | + |
| 126 | +func (r*TemplateResource)Create(ctx context.Context,req resource.CreateRequest,resp*resource.CreateResponse) { |
| 127 | +vardataTemplateResourceModel |
| 128 | + |
| 129 | +// Read Terraform plan data into the model |
| 130 | +resp.Diagnostics.Append(req.Plan.Get(ctx,&data)...) |
| 131 | +ifresp.Diagnostics.HasError() { |
| 132 | +return |
| 133 | +} |
| 134 | + |
| 135 | +// TODO: Placeholder |
| 136 | +data.ID=types.StringValue(uuid.New().String()) |
| 137 | +// client := r.data.Client |
| 138 | +// orgID, err := uuid.Parse(data.OrganizationID.ValueString()) |
| 139 | +// if err != nil { |
| 140 | +// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to parse supplied organization ID as UUID, got error: %s", err)) |
| 141 | +// return |
| 142 | +// } |
| 143 | + |
| 144 | +// Save data into Terraform state |
| 145 | +resp.Diagnostics.Append(resp.State.Set(ctx,&data)...) |
| 146 | +} |
| 147 | + |
| 148 | +func (r*TemplateResource)Read(ctx context.Context,req resource.ReadRequest,resp*resource.ReadResponse) { |
| 149 | +vardataTemplateResourceModel |
| 150 | + |
| 151 | +// Read Terraform prior state data into the model |
| 152 | +resp.Diagnostics.Append(req.State.Get(ctx,&data)...) |
| 153 | + |
| 154 | +ifresp.Diagnostics.HasError() { |
| 155 | +return |
| 156 | +} |
| 157 | + |
| 158 | +// client := r.data.Client |
| 159 | + |
| 160 | +// Save updated data into Terraform state |
| 161 | +resp.Diagnostics.Append(resp.State.Set(ctx,&data)...) |
| 162 | +} |
| 163 | + |
| 164 | +func (r*TemplateResource)Update(ctx context.Context,req resource.UpdateRequest,resp*resource.UpdateResponse) { |
| 165 | +vardataTemplateResourceModel |
| 166 | + |
| 167 | +// Read Terraform plan data into the model |
| 168 | +resp.Diagnostics.Append(req.Plan.Get(ctx,&data)...) |
| 169 | + |
| 170 | +ifresp.Diagnostics.HasError() { |
| 171 | +return |
| 172 | +} |
| 173 | + |
| 174 | +// client := r.data.Client |
| 175 | + |
| 176 | +// Save updated data into Terraform state |
| 177 | +resp.Diagnostics.Append(resp.State.Set(ctx,&data)...) |
| 178 | +} |
| 179 | + |
| 180 | +func (r*TemplateResource)Delete(ctx context.Context,req resource.DeleteRequest,resp*resource.DeleteResponse) { |
| 181 | +vardataTemplateResourceModel |
| 182 | + |
| 183 | +// Read Terraform prior state data into the model |
| 184 | +resp.Diagnostics.Append(req.State.Get(ctx,&data)...) |
| 185 | + |
| 186 | +ifresp.Diagnostics.HasError() { |
| 187 | +return |
| 188 | +} |
| 189 | + |
| 190 | +// client := r.data.Client |
| 191 | +} |
| 192 | + |
| 193 | +func (r*TemplateResource)ImportState(ctx context.Context,req resource.ImportStateRequest,resp*resource.ImportStateResponse) { |
| 194 | +resource.ImportStatePassthroughID(ctx,path.Root("id"),req,resp) |
| 195 | +} |
| 196 | + |
| 197 | +typedirectoryHashPlanModifierstruct{} |
| 198 | + |
| 199 | +funcNewDirectoryHashPlanModifier() planmodifier.String { |
| 200 | +return&directoryHashPlanModifier{} |
| 201 | +} |
| 202 | + |
| 203 | +// Description implements planmodifier.String. |
| 204 | +func (m*directoryHashPlanModifier)Description(context.Context)string { |
| 205 | +return"Recomputes the directory hash if the directory has changed." |
| 206 | +} |
| 207 | + |
| 208 | +// MarkdownDescription implements planmodifier.String. |
| 209 | +func (m*directoryHashPlanModifier)MarkdownDescription(ctx context.Context)string { |
| 210 | +returnm.Description(ctx) |
| 211 | +} |
| 212 | + |
| 213 | +// PlanModifyString implements planmodifier.String. |
| 214 | +func (m*directoryHashPlanModifier)PlanModifyString(ctx context.Context,req planmodifier.StringRequest,resp*planmodifier.StringResponse) { |
| 215 | +vardataTemplateResourceModel |
| 216 | +resp.Diagnostics.Append(req.Plan.Get(ctx,&data)...) |
| 217 | + |
| 218 | +ifresp.Diagnostics.HasError() { |
| 219 | +return |
| 220 | +} |
| 221 | + |
| 222 | +hash,err:=computeDirectoryHash(data.Latest.Directory.ValueString()) |
| 223 | +iferr!=nil { |
| 224 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Failed to compute directory hash: %s",err)) |
| 225 | +return |
| 226 | +} |
| 227 | + |
| 228 | +resp.PlanValue=types.StringValue(hash) |
| 229 | +} |
| 230 | + |
| 231 | +var_ planmodifier.String=&directoryHashPlanModifier{} |