Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9b42ba6

Browse files
committed
remove default org state
1 parent05655ef commit9b42ba6

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
typeGroupDataSourcestruct {
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-
vardataGroupDataSourceModel
163-
164162
// Read Terraform configuration data into the model
163+
vardataGroupDataSourceModel
165164
resp.Diagnostics.Append(req.Config.Get(ctx,&data)...)
166-
167165
ifresp.Diagnostics.HasError() {
168166
return
169167
}
170168

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

176-
client:=d.data.Client
177-
178-
ifdata.OrganizationID.IsNull() {
179-
data.OrganizationID=UUIDValue(d.data.DefaultOrganizationID)
180-
}
181-
182174
var (
183175
group codersdk.Group
184176
errerror
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
iferr!=nil {
190182
ifisNotFound(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
iferr!=nil {
203195
ifisNotFound(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
typeGroupResourcestruct {
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-
vardataGroupResourceModel
145-
146144
// Read Terraform plan data into the model
145+
vardataGroupResourceModel
147146
resp.Diagnostics.Append(req.Plan.Get(ctx,&data)...)
148-
149147
ifresp.Diagnostics.HasError() {
150148
return
151149
}
152150

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

158-
client:=r.data.Client
159-
160-
ifdata.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
ifresp.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
iferr!=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-
vardataGroupResourceModel
206-
207197
// Read Terraform prior state data into the model
198+
vardataGroupResourceModel
208199
resp.Diagnostics.Append(req.State.Get(ctx,&data)...)
209-
210200
ifresp.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
iferr!=nil {
220208
ifisNotFound(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-
vardataGroupResourceModel
248-
249235
// Read Terraform plan data into the model
236+
vardataGroupResourceModel
250237
resp.Diagnostics.Append(req.Plan.Get(ctx,&data)...)
251-
252238
ifresp.Diagnostics.HasError() {
253239
return
254240
}
255241

256-
client:=r.data.Client
257-
ifdata.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
iferr!=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
varremove []string
269251
if!data.Members.IsNull() {
270252
varplannedMembers []UUID
271-
resp.Diagnostics.Append(
272-
data.Members.ElementsAs(ctx,&plannedMembers,false)...,
273-
)
253+
resp.Diagnostics.Append(data.Members.ElementsAs(ctx,&plannedMembers,false)...)
274254
ifresp.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-
vardataGroupResourceModel
314-
315293
// Read Terraform prior state data into the model
294+
vardataGroupResourceModel
316295
resp.Diagnostics.Append(req.State.Get(ctx,&data)...)
317-
318296
ifresp.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
iferr!=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
vargroupID uuid.UUID
338-
client:=r.data.Client
339315
idParts:=strings.Split(req.ID,"/")
340316
iflen(idParts)==1 {
341317
varerrerror
@@ -345,12 +321,12 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat
345321
return
346322
}
347323
}elseiflen(idParts)==2 {
348-
org,err:=client.OrganizationByName(ctx,idParts[0])
324+
org,err:=r.Client.OrganizationByName(ctx,idParts[0])
349325
iferr!=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
iferr!=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
iferr!=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
typeCoderdProviderDatastruct {
35-
Client*codersdk.Client
36-
DefaultOrganizationID uuid.UUID
37-
Featuresmap[codersdk.FeatureName]codersdk.Feature
34+
Client*codersdk.Client
35+
Featuresmap[codersdk.FeatureName]codersdk.Feature
3836
}
3937

4038
// CoderdProviderModel describes the provider data model.
4139
typeCoderdProviderModelstruct {
4240
URL types.String`tfsdk:"url"`
4341
Token types.String`tfsdk:"token"`
44-
45-
DefaultOrganizationIDUUID`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
vardataCoderdProviderModel
81-
8272
resp.Diagnostics.Append(req.Config.Get(ctx,&data)...)
83-
8473
ifresp.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-
ifdata.DefaultOrganizationID.IsNull() {
114-
user,err:=client.User(ctx,codersdk.Me)
115-
iferr!=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
iferr!=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
typeTemplateDataSourcestruct {
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-
vardataTemplateDataSourceModel
238-
239237
// Read Terraform configuration data into the model
238+
vardataTemplateDataSourceModel
240239
resp.Diagnostics.Append(req.Config.Get(ctx,&data)...)
241-
242240
ifresp.Diagnostics.HasError() {
243241
return
244242
}
245243

246-
client:=d.data.Client
247-
248244
var (
249245
template codersdk.Template
250246
errerror
251247
)
252248
ifdata.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-
ifdata.OrganizationID.ValueUUID()==uuid.Nil {
256-
data.OrganizationID=UUIDValue(d.data.DefaultOrganizationID)
257-
}
258251
ifdata.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
iferr!=nil {
265258
ifisNotFound(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
iferr!=nil {
288281
resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Failed to get template ACL: %s",err))
289282
return

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp