Skip to content

Commit 9b42ba6

Browse files
committed
remove default org state
1 parent 05655ef commit 9b42ba6

File tree

5 files changed

+63
-147
lines changed

5 files changed

+63
-147
lines changed

internal/provider/group_data_source.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewGroupDataSource() datasource.DataSource {
2222

2323
// GroupDataSource defines the data source implementation.
2424
type GroupDataSource struct {
25-
data *CoderdProviderData
25+
*CoderdProviderData
2626
}
2727

2828
// GroupDataSourceModel describes the data source data model.
@@ -155,37 +155,29 @@ func (d *GroupDataSource) Configure(ctx context.Context, req datasource.Configur
155155
return
156156
}
157157

158-
d.data = data
158+
d.CoderdProviderData = data
159159
}
160160

161161
func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
162-
var data GroupDataSourceModel
163-
164162
// Read Terraform configuration data into the model
163+
var data GroupDataSourceModel
165164
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
166-
167165
if resp.Diagnostics.HasError() {
168166
return
169167
}
170168

171-
resp.Diagnostics.Append(CheckGroupEntitlements(ctx, d.data.Features)...)
169+
resp.Diagnostics.Append(CheckGroupEntitlements(ctx, d.Features)...)
172170
if resp.Diagnostics.HasError() {
173171
return
174172
}
175173

176-
client := d.data.Client
177-
178-
if data.OrganizationID.IsNull() {
179-
data.OrganizationID = UUIDValue(d.data.DefaultOrganizationID)
180-
}
181-
182174
var (
183175
group codersdk.Group
184176
err error
185177
)
186178
if !data.ID.IsNull() {
187179
groupID := data.ID.ValueUUID()
188-
group, err = client.Group(ctx, groupID)
180+
group, err = r.Client.Group(ctx, groupID)
189181
if err != nil {
190182
if isNotFound(err) {
191183
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,
198190
data.Name = types.StringValue(group.Name)
199191
data.OrganizationID = UUIDValue(group.OrganizationID)
200192
} else {
201-
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
193+
group, err = r.Client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
202194
if err != nil {
203195
if isNotFound(err) {
204196
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()))

internal/provider/group_resource.go

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewGroupResource() resource.Resource {
3232

3333
// GroupResource defines the resource implementation.
3434
type GroupResource struct {
35-
data *CoderdProviderData
35+
*CoderdProviderData
3636
}
3737

3838
// GroupResourceModel describes the resource data model.
@@ -137,34 +137,26 @@ func (r *GroupResource) Configure(ctx context.Context, req resource.ConfigureReq
137137
return
138138
}
139139

140-
r.data = data
140+
r.CoderdProviderData = data
141141
}
142142

143143
func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
144-
var data GroupResourceModel
145-
146144
// Read Terraform plan data into the model
145+
var data GroupResourceModel
147146
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
148-
149147
if resp.Diagnostics.HasError() {
150148
return
151149
}
152150

153-
resp.Diagnostics.Append(CheckGroupEntitlements(ctx, r.data.Features)...)
151+
resp.Diagnostics.Append(CheckGroupEntitlements(ctx, r.Features)...)
154152
if resp.Diagnostics.HasError() {
155153
return
156154
}
157155

158-
client := r.data.Client
159-
160-
if data.OrganizationID.IsUnknown() {
161-
data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID)
162-
}
163-
164156
orgID := data.OrganizationID.ValueUUID()
165157

166158
tflog.Info(ctx, "creating group")
167-
group, err := client.CreateGroup(ctx, orgID, codersdk.CreateGroupRequest{
159+
group, err := r.Client.CreateGroup(ctx, orgID, codersdk.CreateGroupRequest{
168160
Name: data.Name.ValueString(),
169161
DisplayName: data.DisplayName.ValueString(),
170162
AvatarURL: data.AvatarURL.ValueString(),
@@ -188,7 +180,7 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest,
188180
if resp.Diagnostics.HasError() {
189181
return
190182
}
191-
group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
183+
group, err = r.Client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
192184
AddUsers: members,
193185
})
194186
if err != nil {
@@ -202,20 +194,16 @@ func (r *GroupResource) Create(ctx context.Context, req resource.CreateRequest,
202194
}
203195

204196
func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
205-
var data GroupResourceModel
206-
207197
// Read Terraform prior state data into the model
198+
var data GroupResourceModel
208199
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
209-
210200
if resp.Diagnostics.HasError() {
211201
return
212202
}
213203

214-
client := r.data.Client
215-
216204
groupID := data.ID.ValueUUID()
217205

218-
group, err := client.Group(ctx, groupID)
206+
group, err := r.Client.Group(ctx, groupID)
219207
if err != nil {
220208
if isNotFound(err) {
221209
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
244232
}
245233

246234
func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
247-
var data GroupResourceModel
248-
249235
// Read Terraform plan data into the model
236+
var data GroupResourceModel
250237
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
251-
252238
if resp.Diagnostics.HasError() {
253239
return
254240
}
255241

256-
client := r.data.Client
257-
if data.OrganizationID.IsUnknown() {
258-
data.OrganizationID = UUIDValue(r.data.DefaultOrganizationID)
259-
}
260242
groupID := data.ID.ValueUUID()
261243

262-
group, err := client.Group(ctx, groupID)
244+
group, err := r.Client.Group(ctx, groupID)
263245
if err != nil {
264246
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
265247
return
@@ -268,9 +250,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest,
268250
var remove []string
269251
if !data.Members.IsNull() {
270252
var plannedMembers []UUID
271-
resp.Diagnostics.Append(
272-
data.Members.ElementsAs(ctx, &plannedMembers, false)...,
273-
)
253+
resp.Diagnostics.Append(data.Members.ElementsAs(ctx, &plannedMembers, false)...)
274254
if resp.Diagnostics.HasError() {
275255
return
276256
}
@@ -291,7 +271,7 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest,
291271
})
292272

293273
quotaAllowance := int(data.QuotaAllowance.ValueInt32())
294-
_, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
274+
_, err = r.Client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
295275
AddUsers: add,
296276
RemoveUsers: remove,
297277
Name: data.Name.ValueString(),
@@ -310,22 +290,19 @@ func (r *GroupResource) Update(ctx context.Context, req resource.UpdateRequest,
310290
}
311291

312292
func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
313-
var data GroupResourceModel
314-
315293
// Read Terraform prior state data into the model
294+
var data GroupResourceModel
316295
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
317-
318296
if resp.Diagnostics.HasError() {
319297
return
320298
}
321299

322-
client := r.data.Client
323300
groupID := data.ID.ValueUUID()
324301

325302
tflog.Info(ctx, "deleting group", map[string]any{
326303
"id": groupID,
327304
})
328-
err := client.DeleteGroup(ctx, groupID)
305+
err := r.Client.DeleteGroup(ctx, groupID)
329306
if err != nil {
330307
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete group, got error: %s", err))
331308
return
@@ -335,7 +312,6 @@ func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest,
335312

336313
func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
337314
var groupID uuid.UUID
338-
client := r.data.Client
339315
idParts := strings.Split(req.ID, "/")
340316
if len(idParts) == 1 {
341317
var err error
@@ -345,12 +321,12 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat
345321
return
346322
}
347323
} else if len(idParts) == 2 {
348-
org, err := client.OrganizationByName(ctx, idParts[0])
324+
org, err := r.Client.OrganizationByName(ctx, idParts[0])
349325
if err != nil {
350326
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get organization with name %s: %s", idParts[0], err))
351327
return
352328
}
353-
group, err := client.GroupByOrgAndName(ctx, org.ID, idParts[1])
329+
group, err := r.Client.GroupByOrgAndName(ctx, org.ID, idParts[1])
354330
if err != nil {
355331
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get group with name %s: %s", idParts[1], err))
356332
return
@@ -360,7 +336,7 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat
360336
resp.Diagnostics.AddError("Client Error", "Invalid import ID format, expected a single UUID or `<organization-name>/<group-name>`")
361337
return
362338
}
363-
group, err := client.Group(ctx, groupID)
339+
group, err := r.Client.Group(ctx, groupID)
364340
if err != nil {
365341
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get imported group, got error: %s", err))
366342
return

internal/provider/provider.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"cdr.dev/slog"
10-
"github.com/google/uuid"
1110
"github.com/hashicorp/terraform-plugin-framework/datasource"
1211
"github.com/hashicorp/terraform-plugin-framework/function"
1312
"github.com/hashicorp/terraform-plugin-framework/provider"
@@ -32,17 +31,14 @@ type CoderdProvider struct {
3231
}
3332

3433
type CoderdProviderData struct {
35-
Client *codersdk.Client
36-
DefaultOrganizationID uuid.UUID
37-
Features map[codersdk.FeatureName]codersdk.Feature
34+
Client *codersdk.Client
35+
Features map[codersdk.FeatureName]codersdk.Feature
3836
}
3937

4038
// CoderdProviderModel describes the provider data model.
4139
type CoderdProviderModel struct {
4240
URL types.String `tfsdk:"url"`
4341
Token types.String `tfsdk:"token"`
44-
45-
DefaultOrganizationID UUID `tfsdk:"default_organization_id"`
4642
}
4743

4844
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/
6763
MarkdownDescription: "API token for communicating with the deployment. Most resource types require elevated permissions. Defaults to `$CODER_SESSION_TOKEN`.",
6864
Optional: true,
6965
},
70-
"default_organization_id": schema.StringAttribute{
71-
MarkdownDescription: "Default organization ID to use when creating resources. Defaults to the first organization the token has access to.",
72-
CustomType: UUIDType,
73-
Optional: true,
74-
},
7566
},
7667
}
7768
}
7869

7970
func (p *CoderdProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
8071
var data CoderdProviderModel
81-
8272
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
83-
8473
if resp.Diagnostics.HasError() {
8574
return
8675
}
@@ -110,23 +99,14 @@ func (p *CoderdProvider) Configure(ctx context.Context, req provider.ConfigureRe
11099
client := codersdk.New(url)
111100
client.SetLogger(slog.Make(tfslog{}).Leveled(slog.LevelDebug))
112101
client.SetSessionToken(data.Token.ValueString())
113-
if data.DefaultOrganizationID.IsNull() {
114-
user, err := client.User(ctx, codersdk.Me)
115-
if err != nil {
116-
resp.Diagnostics.AddError("default_organization_id", "failed to get default organization ID: "+err.Error())
117-
return
118-
}
119-
data.DefaultOrganizationID = UUIDValue(user.OrganizationIDs[0])
120-
}
121102
entitlements, err := client.Entitlements(ctx)
122103
if err != nil {
123104
resp.Diagnostics.AddError("Client Error", "failed to get deployment entitlements: "+err.Error())
124105
}
125106

126107
providerData := &CoderdProviderData{
127-
Client: client,
128-
DefaultOrganizationID: data.DefaultOrganizationID.ValueUUID(),
129-
Features: entitlements.Features,
108+
Client: client,
109+
Features: entitlements.Features,
130110
}
131111
resp.DataSourceData = providerData
132112
resp.ResourceData = providerData

internal/provider/template_data_source.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewTemplateDataSource() datasource.DataSource {
2424

2525
// TemplateDataSource defines the data source implementation.
2626
type TemplateDataSource struct {
27-
data *CoderdProviderData
27+
*CoderdProviderData
2828
}
2929

3030
// TemplateDataSourceModel describes the data source data model.
@@ -230,36 +230,29 @@ func (d *TemplateDataSource) Configure(ctx context.Context, req datasource.Confi
230230
return
231231
}
232232

233-
d.data = data
233+
d.CoderdProviderData = data
234234
}
235235

236236
func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
237-
var data TemplateDataSourceModel
238-
239237
// Read Terraform configuration data into the model
238+
var data TemplateDataSourceModel
240239
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
241-
242240
if resp.Diagnostics.HasError() {
243241
return
244242
}
245243

246-
client := d.data.Client
247-
248244
var (
249245
template codersdk.Template
250246
err error
251247
)
252248
if data.ID.ValueUUID() != uuid.Nil {
253-
template, err = client.Template(ctx, data.ID.ValueUUID())
249+
template, err = d.Client.Template(ctx, data.ID.ValueUUID())
254250
} else {
255-
if data.OrganizationID.ValueUUID() == uuid.Nil {
256-
data.OrganizationID = UUIDValue(d.data.DefaultOrganizationID)
257-
}
258251
if data.OrganizationID.ValueUUID() == uuid.Nil {
259252
resp.Diagnostics.AddError("Client Error", "name requires organization_id to be set")
260253
return
261254
}
262-
template, err = client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
255+
template, err = d.Client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
263256
}
264257
if err != nil {
265258
if isNotFound(err) {
@@ -283,7 +276,7 @@ func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadReques
283276
return
284277
}
285278

286-
acl, err := client.TemplateACL(ctx, template.ID)
279+
acl, err := d.Client.TemplateACL(ctx, template.ID)
287280
if err != nil {
288281
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template ACL: %s", err))
289282
return

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy