Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52bf5aa

Browse files
committed
chore: creating workspaces and templates to work with orgs
1 parent 2a297b0 commit 52bf5aa

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
parameterFlags workspaceParameterFlags
3030
autoUpdates string
3131
copyParametersFrom string
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-
if err != nil {
49-
return err
50-
}
51-
49+
var err error
5250
workspaceOwner := codersdk.Me
5351
if len(inv.Args) >= 1 {
5452
workspaceOwner, workspaceName, err = splitNamedWorkspace(inv.Args[0])
@@ -99,7 +97,7 @@ func (r *RootCmd) create() *serpent.Command {
9997
if templateName == "" {
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
if err != nil {
104102
return err
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
if err != nil {
150150
return xerrors.Errorf("get template by name: %w", err)
151151
}
152+
if len(templates) == 0 {
153+
return xerrors.Errorf("no template found with the name %q", templateName)
154+
}
155+
156+
if len(templates) > 1 {
157+
selectedOrg, err := orgContext.Selected(inv, client)
158+
if err != nil {
159+
return xerrors.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+
return i.OrganizationID == selectedOrg.ID
164+
})
165+
if index == -1 {
166+
return xerrors.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
if err != nil {
165165
return err
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
return nil
@@ -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
return cmd
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
type TemplateFilter struct {
367367
OrganizationID uuid.UUID
368+
ExactName string
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+
if f.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)