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

Commit52bf5aa

Browse files
committed
chore: creating workspaces and templates to work with orgs
1 parent2a297b0 commit52bf5aa

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

‎cli/create.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func (r *RootCmd) create() *serpent.Command {
2929
parameterFlagsworkspaceParameterFlags
3030
autoUpdatesstring
3131
copyParametersFromstring
32-
orgContext=NewOrganizationContext()
32+
// Organization context is only required if more than 1 template
33+
// shares the same name across multiple organizations.
34+
orgContext=NewOrganizationContext()
3335
)
3436
client:=new(codersdk.Client)
3537
cmd:=&serpent.Command{
@@ -44,11 +46,7 @@ func (r *RootCmd) create() *serpent.Command {
4446
),
4547
Middleware:serpent.Chain(r.InitClient(client)),
4648
Handler:func(inv*serpent.Invocation)error {
47-
organization,err:=orgContext.Selected(inv,client)
48-
iferr!=nil {
49-
returnerr
50-
}
51-
49+
varerrerror
5250
workspaceOwner:=codersdk.Me
5351
iflen(inv.Args)>=1 {
5452
workspaceOwner,workspaceName,err=splitNamedWorkspace(inv.Args[0])
@@ -99,7 +97,7 @@ func (r *RootCmd) create() *serpent.Command {
9997
iftemplateName=="" {
10098
_,_=fmt.Fprintln(inv.Stdout,pretty.Sprint(cliui.DefaultStyles.Wrap,"Select a template below to preview the provisioned infrastructure:"))
10199

102-
templates,err:=client.TemplatesByOrganization(inv.Context(),organization.ID)
100+
templates,err:=client.Templates(inv.Context(),codersdk.TemplateFilter{})
103101
iferr!=nil {
104102
returnerr
105103
}
@@ -145,10 +143,34 @@ func (r *RootCmd) create() *serpent.Command {
145143
}
146144
templateVersionID=sourceWorkspace.LatestBuild.TemplateVersionID
147145
}else {
148-
template,err=client.TemplateByName(inv.Context(),organization.ID,templateName)
146+
templates,err:=client.Templates(inv.Context(), codersdk.TemplateFilter{
147+
ExactName:templateName,
148+
})
149149
iferr!=nil {
150150
returnxerrors.Errorf("get template by name: %w",err)
151151
}
152+
iflen(templates)==0 {
153+
returnxerrors.Errorf("no template found with the name %q",templateName)
154+
}
155+
156+
iflen(templates)>1 {
157+
selectedOrg,err:=orgContext.Selected(inv,client)
158+
iferr!=nil {
159+
returnxerrors.Errorf("multiple templates found with the name %q, use `--org=<organization_name>` to specify which template by that name to use",templateName)
160+
}
161+
162+
index:=slices.IndexFunc(templates,func(i codersdk.Template)bool {
163+
returni.OrganizationID==selectedOrg.ID
164+
})
165+
ifindex==-1 {
166+
returnxerrors.Errorf("no templates found with the name %q in the organization %q",templateName,selectedOrg.Name)
167+
}
168+
169+
// remake the list with the only template selected
170+
templates= []codersdk.Template{templates[index]}
171+
}
172+
173+
template=templates[0]
152174
templateVersionID=template.ActiveVersionID
153175
}
154176

@@ -207,7 +229,7 @@ func (r *RootCmd) create() *serpent.Command {
207229
ttlMillis=ptr.Ref(stopAfter.Milliseconds())
208230
}
209231

210-
workspace,err:=client.CreateWorkspace(inv.Context(),organization.ID,workspaceOwner, codersdk.CreateWorkspaceRequest{
232+
workspace,err:=client.CreateWorkspace(inv.Context(),template.OrganizationID,workspaceOwner, codersdk.CreateWorkspaceRequest{
211233
TemplateVersionID:templateVersionID,
212234
Name:workspaceName,
213235
AutostartSchedule:schedSpec,

‎cli/templatecreate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
160160
RequireActiveVersion:requireActiveVersion,
161161
}
162162

163-
_,err=client.CreateTemplate(inv.Context(),organization.ID,createReq)
163+
template,err:=client.CreateTemplate(inv.Context(),organization.ID,createReq)
164164
iferr!=nil {
165165
returnerr
166166
}
@@ -171,7 +171,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
171171
pretty.Sprint(cliui.DefaultStyles.DateTimeStamp,time.Now().Format(time.Stamp))+"! "+
172172
"Developers can provision a workspace with this template using:")+"\n")
173173

174-
_,_=fmt.Fprintln(inv.Stdout," "+pretty.Sprint(cliui.DefaultStyles.Code,fmt.Sprintf("coder create --template=%q [workspace name]",templateName)))
174+
_,_=fmt.Fprintln(inv.Stdout," "+pretty.Sprint(cliui.DefaultStyles.Code,fmt.Sprintf("coder create --template=%q--org=%q[workspace name]",templateName,template.OrganizationName)))
175175
_,_=fmt.Fprintln(inv.Stdout)
176176

177177
returnnil
@@ -244,6 +244,7 @@ func (r *RootCmd) templateCreate() *serpent.Command {
244244

245245
cliui.SkipPromptOption(),
246246
}
247+
orgContext.AttachOptions(cmd)
247248
cmd.Options=append(cmd.Options,uploadFlags.options()...)
248249
returncmd
249250
}

‎coderd/searchquery/search.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ func Templates(ctx context.Context, db database.Store, query string) (database.G
198198

199199
parser:=httpapi.NewQueryParamParser()
200200
filter:= database.GetTemplatesWithFilterParams{
201-
Deleted:parser.Boolean(values,false,"deleted"),
202-
// TODO: Should name be a fuzzy search?
203-
ExactName:parser.String(values,"","name"),
201+
Deleted:parser.Boolean(values,false,"deleted"),
202+
ExactName:parser.String(values,"","exact_name"),
204203
IDs:parser.UUIDs(values, []uuid.UUID{},"ids"),
205204
Deprecated:parser.NullableBoolean(values, sql.NullBool{},"deprecated"),
206205
}

‎codersdk/organizations.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ func (c *Client) TemplatesByOrganization(ctx context.Context, organizationID uui
365365

366366
typeTemplateFilterstruct {
367367
OrganizationID uuid.UUID
368+
ExactNamestring
368369
}
369370

370371
// asRequestOption returns a function that can be used in (*Client).Request.
@@ -378,6 +379,10 @@ func (f TemplateFilter) asRequestOption() RequestOption {
378379
params=append(params,fmt.Sprintf("organization:%q",f.OrganizationID.String()))
379380
}
380381

382+
iff.ExactName!="" {
383+
params=append(params,fmt.Sprintf("exact_name:%q",f.ExactName))
384+
}
385+
381386
q:=r.URL.Query()
382387
q.Set("q",strings.Join(params," "))
383388
r.URL.RawQuery=q.Encode()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp