From 9b42ba62f09d2ac4cf9673c007ed0c32d648929e Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Fri, 8 Nov 2024 23:14:42 +0000 Subject: [PATCH 1/3] remove default org state --- internal/provider/group_data_source.go | 20 ++---- internal/provider/group_resource.go | 58 +++++----------- internal/provider/provider.go | 28 ++------ internal/provider/template_data_source.go | 19 ++--- internal/provider/template_resource.go | 85 ++++++++--------------- 5 files changed, 63 insertions(+), 147 deletions(-) diff --git a/internal/provider/group_data_source.go b/internal/provider/group_data_source.go index 007abaa..5389489 100644 --- a/internal/provider/group_data_source.go +++ b/internal/provider/group_data_source.go @@ -22,7 +22,7 @@ func NewGroupDataSource() datasource.DataSource { // GroupDataSource defines the data source implementation. type GroupDataSource struct { - data *CoderdProviderData + *CoderdProviderData } // GroupDataSourceModel describes the data source data model. @@ -155,37 +155,29 @@ func (d *GroupDataSource) Configure(ctx context.Context, req datasource.Configur return } - d.data = data + d.CoderdProviderData = data } func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data GroupDataSourceModel - // Read Terraform configuration data into the model + var data GroupDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - resp.Diagnostics.Append(CheckGroupEntitlements(ctx, d.data.Features)...) + resp.Diagnostics.Append(CheckGroupEntitlements(ctx, d.Features)...) if resp.Diagnostics.HasError() { return } - client := d.data.Client - - if data.OrganizationID.IsNull() { - data.OrganizationID = UUIDValue(d.data.DefaultOrganizationID) - } - var ( group codersdk.Group err error ) if !data.ID.IsNull() { groupID := data.ID.ValueUUID() - group, err = client.Group(ctx, groupID) + group, err = r.Client.Group(ctx, groupID) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String())) @@ -198,7 +190,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, data.Name = types.StringValue(group.Name) data.OrganizationID = UUIDValue(group.OrganizationID) } else { - group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) + group, err = r.Client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString())) diff --git a/internal/provider/group_resource.go b/internal/provider/group_resource.go index fa370a0..d6ddcf7 100644 --- a/internal/provider/group_resource.go +++ b/internal/provider/group_resource.go @@ -32,7 +32,7 @@ func NewGroupResource() resource.Resource { // GroupResource defines the resource implementation. type GroupResource struct { - data *CoderdProviderData + *CoderdProviderData } // GroupResourceModel describes the resource data model. @@ -137,34 +137,26 @@ func (r *GroupResource) Configure(ctx context.Context, req resource.ConfigureReq return } - r.data = data + r.CoderdProviderData = data } func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data GroupResourceModel - // Read Terraform plan data into the model + var data GroupResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - resp.Diagnostics.Append(CheckGroupEntitlements(ctx, r.data.Features)...) + resp.Diagnostics.Append(CheckGroupEntitlements(ctx, r.Features)...) if resp.Diagnostics.HasError() { return } - client := r.data.Client - - if data.OrganizationID.IsUnknown() { - data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID) - } - orgID := data.OrganizationID.ValueUUID() tflog.Info(ctx, "creating group") - group, err := client.CreateGroup(ctx, orgID, codersdk.CreateGroupRequest{ + group, err := r.Client.CreateGroup(ctx, orgID, codersdk.CreateGroupRequest{ Name: data.Name.ValueString(), DisplayName: data.DisplayName.ValueString(), AvatarURL: data.AvatarURL.ValueString(), @@ -188,7 +180,7 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest, if resp.Diagnostics.HasError() { return } - group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{ + group, err = r.Client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{ AddUsers: members, }) if err != nil { @@ -202,20 +194,16 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest, } func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data GroupResourceModel - // Read Terraform prior state data into the model + var data GroupResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - client := r.data.Client - groupID := data.ID.ValueUUID() - group, err := client.Group(ctx, groupID) + group, err := r.Client.Group(ctx, groupID) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String())) @@ -244,22 +232,16 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp } func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data GroupResourceModel - // Read Terraform plan data into the model + var data GroupResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - client := r.data.Client - if data.OrganizationID.IsUnknown() { - data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID) - } groupID := data.ID.ValueUUID() - group, err := client.Group(ctx, groupID) + group, err := r.Client.Group(ctx, groupID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err)) return @@ -268,9 +250,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest, var remove []string if !data.Members.IsNull() { var plannedMembers []UUID - resp.Diagnostics.Append( - data.Members.ElementsAs(ctx, &plannedMembers, false)..., - ) + resp.Diagnostics.Append(data.Members.ElementsAs(ctx, &plannedMembers, false)...) if resp.Diagnostics.HasError() { return } @@ -291,7 +271,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest, }) quotaAllowance := int(data.QuotaAllowance.ValueInt32()) - _, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{ + _, err = r.Client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{ AddUsers: add, RemoveUsers: remove, Name: data.Name.ValueString(), @@ -310,22 +290,19 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest, } func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data GroupResourceModel - // Read Terraform prior state data into the model + var data GroupResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - client := r.data.Client groupID := data.ID.ValueUUID() tflog.Info(ctx, "deleting group", map[string]any{ "id": groupID, }) - err := client.DeleteGroup(ctx, groupID) + err := r.Client.DeleteGroup(ctx, groupID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete group, got error: %s", err)) return @@ -335,7 +312,6 @@ func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest, func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { var groupID uuid.UUID - client := r.data.Client idParts := strings.Split(req.ID, "/") if len(idParts) == 1 { var err error @@ -345,12 +321,12 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat return } } else if len(idParts) == 2 { - org, err := client.OrganizationByName(ctx, idParts[0]) + org, err := r.Client.OrganizationByName(ctx, idParts[0]) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get organization with name %s: %s", idParts[0], err)) return } - group, err := client.GroupByOrgAndName(ctx, org.ID, idParts[1]) + group, err := r.Client.GroupByOrgAndName(ctx, org.ID, idParts[1]) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get group with name %s: %s", idParts[1], err)) return @@ -360,7 +336,7 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat resp.Diagnostics.AddError("Client Error", "Invalid import ID format, expected a single UUID or `/`") return } - group, err := client.Group(ctx, groupID) + group, err := r.Client.Group(ctx, groupID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get imported group, got error: %s", err)) return diff --git a/internal/provider/provider.go b/internal/provider/provider.go index bfeea5e..df11211 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -7,7 +7,6 @@ import ( "strings" "cdr.dev/slog" - "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/function" "github.com/hashicorp/terraform-plugin-framework/provider" @@ -32,17 +31,14 @@ type CoderdProvider struct { } type CoderdProviderData struct { - Client *codersdk.Client - DefaultOrganizationID uuid.UUID - Features map[codersdk.FeatureName]codersdk.Feature + Client *codersdk.Client + Features map[codersdk.FeatureName]codersdk.Feature } // CoderdProviderModel describes the provider data model. type CoderdProviderModel struct { URL types.String `tfsdk:"url"` Token types.String `tfsdk:"token"` - - DefaultOrganizationID UUID `tfsdk:"default_organization_id"` } func (p *CoderdProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { @@ -67,20 +63,13 @@ This provider is only compatible with Coder version [2.10.1](https://github.com/ MarkdownDescription: "API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to `$CODER_SESSION_TOKEN`.", Optional: true, }, - "default_organization_id": schema.StringAttribute{ - MarkdownDescription: "Default organization ID to use when creating resources. Defaults to the first organization the token has access to.", - CustomType: UUIDType, - Optional: true, - }, }, } } func (p *CoderdProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { var data CoderdProviderModel - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } @@ -110,23 +99,14 @@ func (p *CoderdProvider) Configure(ctx context.Context, req provider.ConfigureRe client := codersdk.New(url) client.SetLogger(slog.Make(tfslog{}).Leveled(slog.LevelDebug)) client.SetSessionToken(data.Token.ValueString()) - if data.DefaultOrganizationID.IsNull() { - user, err := client.User(ctx, codersdk.Me) - if err != nil { - resp.Diagnostics.AddError("default_organization_id", "failed to get default organization ID: "+err.Error()) - return - } - data.DefaultOrganizationID = UUIDValue(user.OrganizationIDs[0]) - } entitlements, err := client.Entitlements(ctx) if err != nil { resp.Diagnostics.AddError("Client Error", "failed to get deployment entitlements: "+err.Error()) } providerData := &CoderdProviderData{ - Client: client, - DefaultOrganizationID: data.DefaultOrganizationID.ValueUUID(), - Features: entitlements.Features, + Client: client, + Features: entitlements.Features, } resp.DataSourceData = providerData resp.ResourceData = providerData diff --git a/internal/provider/template_data_source.go b/internal/provider/template_data_source.go index 3a5f59a..0c51e5f 100644 --- a/internal/provider/template_data_source.go +++ b/internal/provider/template_data_source.go @@ -24,7 +24,7 @@ func NewTemplateDataSource() datasource.DataSource { // TemplateDataSource defines the data source implementation. type TemplateDataSource struct { - data *CoderdProviderData + *CoderdProviderData } // TemplateDataSourceModel describes the data source data model. @@ -230,36 +230,29 @@ func (d *TemplateDataSource) Configure(ctx context.Context, req datasource.Confi return } - d.data = data + d.CoderdProviderData = data } func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data TemplateDataSourceModel - // Read Terraform configuration data into the model + var data TemplateDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - client := d.data.Client - var ( template codersdk.Template err error ) if data.ID.ValueUUID() != uuid.Nil { - template, err = client.Template(ctx, data.ID.ValueUUID()) + template, err = d.Client.Template(ctx, data.ID.ValueUUID()) } else { - if data.OrganizationID.ValueUUID() == uuid.Nil { - data.OrganizationID = UUIDValue(d.data.DefaultOrganizationID) - } if data.OrganizationID.ValueUUID() == uuid.Nil { resp.Diagnostics.AddError("Client Error", "name requires organization_id to be set") return } - template, err = client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) + template, err = d.Client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) } if err != nil { if isNotFound(err) { @@ -283,7 +276,7 @@ func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadReques return } - acl, err := client.TemplateACL(ctx, template.ID) + acl, err := d.Client.TemplateACL(ctx, template.ID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template ACL: %s", err)) return diff --git a/internal/provider/template_resource.go b/internal/provider/template_resource.go index a5834db..dc3ca6c 100644 --- a/internal/provider/template_resource.go +++ b/internal/provider/template_resource.go @@ -47,7 +47,7 @@ func NewTemplateResource() resource.Resource { // TemplateResource defines the resource implementation. type TemplateResource struct { - data *CoderdProviderData + *CoderdProviderData } // TemplateResourceModel describes the resource data model. @@ -277,10 +277,8 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques Default: stringdefault.StaticString(""), }, "organization_id": schema.StringAttribute{ - MarkdownDescription: "The ID of the organization. Defaults to the provider's default organization", + MarkdownDescription: "The ID of the organization that this template belongs to.", CustomType: UUIDType, - Optional: true, - Computed: true, PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplaceIfConfigured(), }, @@ -476,32 +474,26 @@ func (r *TemplateResource) Configure(ctx context.Context, req resource.Configure return } - r.data = data + r.CoderdProviderData = data } func (r *TemplateResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data TemplateResourceModel - // Read Terraform plan data into the model + var data TemplateResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } - if data.OrganizationID.IsUnknown() { - data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID) - } - if data.DisplayName.IsUnknown() { data.DisplayName = data.Name } - resp.Diagnostics.Append(data.CheckEntitlements(ctx, r.data.Features)...) + resp.Diagnostics.Append(data.CheckEntitlements(ctx, r.Features)...) if resp.Diagnostics.HasError() { return } - client := r.data.Client orgID := data.OrganizationID.ValueUUID() var templateResp codersdk.Template for idx, version := range data.Versions { @@ -512,7 +504,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques if idx > 0 { newVersionRequest.TemplateID = &templateResp.ID } - versionResp, err, logs := newVersion(ctx, client, newVersionRequest) + versionResp, err, logs := newVersion(ctx, r.Client, newVersionRequest) if err != nil { resp.Diagnostics.AddError("Provisioner Error", formatLogs(err, logs)) return @@ -523,7 +515,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques if resp.Diagnostics.HasError() { return } - templateResp, err = client.CreateTemplate(ctx, orgID, *createReq) + templateResp, err = r.Client.CreateTemplate(ctx, orgID, *createReq) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to create template: %s", err)) return @@ -548,7 +540,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques if resp.Diagnostics.HasError() { return } - err = client.UpdateTemplateACL(ctx, templateResp.ID, convertACLToRequest(codersdk.TemplateACL{}, acl)) + err = r.Client.UpdateTemplateACL(ctx, templateResp.ID, convertACLToRequest(codersdk.TemplateACL{}, acl)) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to create template ACL: %s", err)) return @@ -557,7 +549,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques } } if version.Active.ValueBool() { - err := markActive(ctx, client, templateResp.ID, versionResp.ID) + err := markActive(ctx, r.Client, templateResp.ID, versionResp.ID) if err != nil { resp.Diagnostics.AddError("Client Error", err.Error()) return @@ -578,7 +570,7 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques if resp.Diagnostics.HasError() { return } - mpslResp, err := client.UpdateTemplateMeta(ctx, data.ID.ValueUUID(), *mpslReq) + mpslResp, err := r.Client.UpdateTemplateMeta(ctx, data.ID.ValueUUID(), *mpslReq) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to set max port share level via update: %s", err)) return @@ -596,19 +588,16 @@ func (r *TemplateResource) Create(ctx context.Context, req resource.CreateReques } func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data TemplateResourceModel - // Read Terraform prior state data into the model + var data TemplateResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } - client := r.data.Client - templateID := data.ID.ValueUUID() - template, err := client.Template(ctx, templateID) + template, err := r.Client.Template(ctx, templateID) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Template with ID %s not found. Marking as deleted.", templateID.String())) @@ -628,7 +617,7 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r if !data.ACL.IsNull() { tflog.Info(ctx, "reading template ACL") - acl, err := client.TemplateACL(ctx, templateID) + acl, err := r.Client.TemplateACL(ctx, templateID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template ACL: %s", err)) return @@ -645,7 +634,7 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r for idx, version := range data.Versions { versionID := version.ID.ValueUUID() - versionResp, err := client.TemplateVersion(ctx, versionID) + versionResp, err := r.Client.TemplateVersion(ctx, versionID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template version: %s", err)) return @@ -664,41 +653,32 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r } func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var newState TemplateResourceModel - var curState TemplateResourceModel - // Read Terraform plan data into the model + var newState TemplateResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &newState)...) - if resp.Diagnostics.HasError() { return } // Read Terraform prior state data into the model + var curState TemplateResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &curState)...) if resp.Diagnostics.HasError() { return } - if newState.OrganizationID.IsUnknown() { - newState.OrganizationID = UUIDValue(r.data.DefaultOrganizationID) - } - if newState.DisplayName.IsUnknown() { newState.DisplayName = newState.Name } - resp.Diagnostics.Append(newState.CheckEntitlements(ctx, r.data.Features)...) + resp.Diagnostics.Append(newState.CheckEntitlements(ctx, r.Features)...) if resp.Diagnostics.HasError() { return } orgID := newState.OrganizationID.ValueUUID() - templateID := newState.ID.ValueUUID() - client := r.data.Client - // TODO(ethanndickson): Remove this once the provider requires a Coder // deployment running `v2.15.0` or later. if newState.MaxPortShareLevel.IsUnknown() { @@ -712,7 +692,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques if resp.Diagnostics.HasError() { return } - _, err := client.UpdateTemplateMeta(ctx, templateID, *updateReq) + _, err := r.Client.UpdateTemplateMeta(ctx, templateID, *updateReq) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template metadata: %s", err)) return @@ -729,13 +709,13 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques if resp.Diagnostics.HasError() { return } - curACL, err := client.TemplateACL(ctx, templateID) + curACL, err := r.Client.TemplateACL(ctx, templateID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template ACL: %s", err)) return } - err = client.UpdateTemplateACL(ctx, templateID, convertACLToRequest(curACL, acl)) + err = r.Client.UpdateTemplateACL(ctx, templateID, convertACLToRequest(curACL, acl)) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to update template ACL: %s", err)) return @@ -746,7 +726,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques for idx := range newState.Versions { if newState.Versions[idx].ID.IsUnknown() { tflog.Info(ctx, "discovered a new or modified template version") - uploadResp, err, logs := newVersion(ctx, client, newVersionRequest{ + uploadResp, err, logs := newVersion(ctx, r.Client, newVersionRequest{ Version: &newState.Versions[idx], OrganizationID: orgID, TemplateID: &templateID, @@ -755,7 +735,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques resp.Diagnostics.AddError("Provisioner Error", formatLogs(err, logs)) return } - versionResp, err := client.TemplateVersion(ctx, uploadResp.ID) + versionResp, err := r.Client.TemplateVersion(ctx, uploadResp.ID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template version: %s", err)) return @@ -763,7 +743,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques newState.Versions[idx].ID = UUIDValue(versionResp.ID) newState.Versions[idx].Name = types.StringValue(versionResp.Name) if newState.Versions[idx].Active.ValueBool() { - err := markActive(ctx, client, templateID, newState.Versions[idx].ID.ValueUUID()) + err := markActive(ctx, r.Client, templateID, newState.Versions[idx].ID.ValueUUID()) if err != nil { resp.Diagnostics.AddError("Client Error", err.Error()) return @@ -779,7 +759,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques return } if !curVersion.Name.Equal(newState.Versions[idx].Name) { - _, err := client.UpdateTemplateVersion(ctx, newState.Versions[idx].ID.ValueUUID(), codersdk.PatchTemplateVersionRequest{ + _, err := r.Client.UpdateTemplateVersion(ctx, newState.Versions[idx].ID.ValueUUID(), codersdk.PatchTemplateVersionRequest{ Name: newState.Versions[idx].Name.ValueString(), Message: newState.Versions[idx].Message.ValueStringPointer(), }) @@ -789,7 +769,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques } } if newState.Versions[idx].Active.ValueBool() && !curVersion.Active.ValueBool() { - err := markActive(ctx, client, templateID, newState.Versions[idx].ID.ValueUUID()) + err := markActive(ctx, r.Client, templateID, newState.Versions[idx].ID.ValueUUID()) if err != nil { resp.Diagnostics.AddError("Client Error", err.Error()) return @@ -799,7 +779,7 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques } // TODO(ethanndickson): Remove this once the provider requires a Coder // deployment running `v2.15.0` or later. - templateResp, err := client.Template(ctx, templateID) + templateResp, err := r.Client.Template(ctx, templateID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template: %s", err)) return @@ -816,21 +796,17 @@ func (r *TemplateResource) Update(ctx context.Context, req resource.UpdateReques } func (r *TemplateResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data TemplateResourceModel - // Read Terraform prior state data into the model + var data TemplateResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } - client := r.data.Client - templateID := data.ID.ValueUUID() tflog.Info(ctx, "deleting template") - err := client.DeleteTemplate(ctx, templateID) + err := r.Client.DeleteTemplate(ctx, templateID) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to delete template: %s", err)) return @@ -843,13 +819,12 @@ func (r *TemplateResource) ImportState(ctx context.Context, req resource.ImportS resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) return } else if len(idParts) == 2 { - client := r.data.Client - org, err := client.OrganizationByName(ctx, idParts[0]) + org, err := r.Client.OrganizationByName(ctx, idParts[0]) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get organization with name %s: %s", idParts[0], err)) return } - template, err := client.TemplateByName(ctx, org.ID, idParts[1]) + template, err := r.Client.TemplateByName(ctx, org.ID, idParts[1]) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template with name %s: %s", idParts[1], err)) return From 38ada6453f4942a7f3ac2e4d6889785b480361bf Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Fri, 8 Nov 2024 23:21:30 +0000 Subject: [PATCH 2/3] :) --- internal/provider/group_data_source.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/provider/group_data_source.go b/internal/provider/group_data_source.go index 5389489..8a291b5 100644 --- a/internal/provider/group_data_source.go +++ b/internal/provider/group_data_source.go @@ -177,7 +177,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, ) if !data.ID.IsNull() { groupID := data.ID.ValueUUID() - group, err = r.Client.Group(ctx, groupID) + group, err = d.Client.Group(ctx, groupID) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String())) @@ -190,7 +190,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, data.Name = types.StringValue(group.Name) data.OrganizationID = UUIDValue(group.OrganizationID) } else { - group, err = r.Client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) + group, err = d.Client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) if err != nil { if isNotFound(err) { resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString())) From 6b8b915f619af9122f87a6a082e5bbd057c27d7b Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Fri, 8 Nov 2024 23:32:05 +0000 Subject: [PATCH 3/3] woot --- docs/index.md | 1 - docs/resources/template.md | 2 +- internal/provider/template_resource.go | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/index.md b/docs/index.md index 3d9be49..b580aff 100644 --- a/docs/index.md +++ b/docs/index.md @@ -84,6 +84,5 @@ resource "coderd_template" "example" { ### Optional -- `default_organization_id` (String) Default organization ID to use when creating resources. Defaults to the first organization the token has access to. - `token` (String) API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to `$CODER_SESSION_TOKEN`. - `url` (String) URL to the Coder deployment. Defaults to `$CODER_URL`. diff --git a/docs/resources/template.md b/docs/resources/template.md index c0efc4b..3266231 100644 --- a/docs/resources/template.md +++ b/docs/resources/template.md @@ -79,7 +79,7 @@ resource "coderd_template" "ubuntu-main" { - `failure_ttl_ms` (Number) (Enterprise) The max lifetime before Coder stops all resources for failed workspaces created from this template, in milliseconds. - `icon` (String) Relative path or external URL that specifes an icon to be displayed in the dashboard. - `max_port_share_level` (String) (Enterprise) The maximum port share level for workspaces created from this template. Defaults to `owner` on an Enterprise deployment, or `public` otherwise. -- `organization_id` (String) The ID of the organization. Defaults to the provider's default organization +- `organization_id` (String) The ID of the organization that this template belongs to. - `require_active_version` (Boolean) (Enterprise) Whether workspaces must be created from the active version of this template. Defaults to false. - `time_til_dormant_autodelete_ms` (Number) (Enterprise) The max lifetime before Coder permanently deletes dormant workspaces created from this template. - `time_til_dormant_ms` (Number) (Enterprise) The max lifetime before Coder locks inactive workspaces created from this template, in milliseconds. diff --git a/internal/provider/template_resource.go b/internal/provider/template_resource.go index dc3ca6c..386d48c 100644 --- a/internal/provider/template_resource.go +++ b/internal/provider/template_resource.go @@ -278,6 +278,8 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques }, "organization_id": schema.StringAttribute{ MarkdownDescription: "The ID of the organization that this template belongs to.", + Computed: true, + Optional: true, CustomType: UUIDType, PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplaceIfConfigured(),