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

Skip to content

feat(coder): Add PATCH /templateversions/:templateversion endpoint #6698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(coder): Add PATCH /templateversions/:templateversion endpoint
  • Loading branch information
BrunoQuaresma committed Mar 21, 2023
commit f5bf8f9965b7cd8fc38d491e33fafecb59e080a3
33 changes: 33 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions coderd/database/dbauthz/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,13 @@ func (q *querier) UpdateTemplateScheduleByID(ctx context.Context, arg database.U
return updateWithReturn(q.log, q.auth, fetch, q.db.UpdateTemplateScheduleByID)(ctx, arg)
}

func (q *querier) UpdateTemplateVersionByID(ctx context.Context, arg database.UpdateTemplateVersionByIDParams) error {
func (q *querier) UpdateTemplateVersionByID(ctx context.Context, arg database.UpdateTemplateVersionByIDParams) (database.TemplateVersion, error) {
template, err := q.db.GetTemplateByID(ctx, arg.TemplateID.UUID)
if err != nil {
return err
return database.TemplateVersion{}, err
}
if err := q.authorizeContext(ctx, rbac.ActionUpdate, template); err != nil {
return err
return database.TemplateVersion{}, err
}
return q.db.UpdateTemplateVersionByID(ctx, arg)
}
Expand Down
9 changes: 5 additions & 4 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3413,9 +3413,9 @@ func (q *fakeQuerier) UpdateTemplateACLByID(_ context.Context, arg database.Upda
return database.Template{}, sql.ErrNoRows
}

func (q *fakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.UpdateTemplateVersionByIDParams) error {
func (q *fakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.UpdateTemplateVersionByIDParams) (database.TemplateVersion, error) {
if err := validateDatabaseType(arg); err != nil {
return err
return database.TemplateVersion{}, err
}

q.mutex.Lock()
Expand All @@ -3427,10 +3427,11 @@ func (q *fakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.
}
templateVersion.TemplateID = arg.TemplateID
templateVersion.UpdatedAt = arg.UpdatedAt
templateVersion.Name = arg.Name
q.templateVersions[index] = templateVersion
return nil
return templateVersion, nil
}
return sql.ErrNoRows
return database.TemplateVersion{}, sql.ErrNoRows
}

func (q *fakeQuerier) UpdateTemplateVersionDescriptionByJobID(_ context.Context, arg database.UpdateTemplateVersionDescriptionByJobIDParams) error {
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ INSERT INTO
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;

-- name: UpdateTemplateVersionByID :exec
-- name: UpdateTemplateVersionByID :one
UPDATE
template_versions
SET
template_id = $2,
updated_at = $3
updated_at = $3,
name = $4
WHERE
id = $1;
id = $1 RETURNING *;

-- name: UpdateTemplateVersionDescriptionByJobID :exec
UPDATE
Expand Down
2 changes: 1 addition & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque

templateAudit.New = dbTemplate

err = tx.UpdateTemplateVersionByID(ctx, database.UpdateTemplateVersionByIDParams{
_, err = tx.UpdateTemplateVersionByID(ctx, database.UpdateTemplateVersionByIDParams{
ID: templateVersion.ID,
TemplateID: uuid.NullUUID{
UUID: dbTemplate.ID,
Expand Down
29 changes: 29 additions & 0 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), user))
}

// @Summary Patch template version by ID
// @ID patch-template-version-by-id
// @Security CoderSessionToken
// @Produce json
// @Tags Templates
// @Param templateversion path string true "Template version ID" format(uuid)
// @Success 200 {object} codersdk.TemplateVersion
// @Router /templateversions/{templateversion} [patch]
func (api *API) patchTemplateVersion(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
templateVersion := httpmw.TemplateVersionParam(r)
var params codersdk.PatchTemplateVersionRequest
if !httpapi.Read(ctx, rw, r, &params) {
return
}
templateVersion, err := api.Database.UpdateTemplateVersionByID(ctx, database.UpdateTemplateVersionByIDParams{
ID: templateVersion.ID,
Name: params.Name,
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Error on patching template version.",
Detail: err.Error(),
})
return
}
httpapi.Write(ctx, rw, http.StatusNoContent, templateVersion)
}

// @Summary Cancel template version by ID
// @ID cancel-template-version-by-id
// @Security CoderSessionToken
Expand Down
2 changes: 1 addition & 1 deletion codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func ParseSSHConfigOption(opt string) (key string, value string, err error) {
return r == ' ' || r == '='
})
if idx == -1 {
return "", "", fmt.Errorf("invalid config-ssh option %q", opt)
return "", "", xerrors.New(fmt.Sprintf("invalid config-ssh option %q", opt))
}
return opt[:idx], opt[idx+1:], nil
}
Expand Down
16 changes: 16 additions & 0 deletions codersdk/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ type TemplateVersionVariable struct {
Sensitive bool `json:"sensitive"`
}

type PatchTemplateVersionRequest struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that I asked this question on slack, but it was missed.

Why in the first place we need to modify the template name? Why using the randomly generated one is bad?

Copy link
Collaborator Author

@BrunoQuaresma BrunoQuaresma Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think being able to add a custom name for the template version is way easier to identify and find it after IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need this feature considering the fact that everywhere we keep using the randomly generated. @bpmct what do you think?

Name string `json:"name"`
}

// TemplateVersion returns a template version by ID.
func (c *Client) TemplateVersion(ctx context.Context, id uuid.UUID) (TemplateVersion, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templateversions/%s", id), nil)
Expand Down Expand Up @@ -291,3 +295,15 @@ func (c *Client) PreviousTemplateVersion(ctx context.Context, organization uuid.
var version TemplateVersion
return version, json.NewDecoder(res.Body).Decode(&version)
}

func (c *Client) UpdateTemplateVersion(ctx context.Context, version, req PatchTemplateVersionRequest) error {
res, err := c.Request(ctx, http.MethodPatch, fmt.Sprintf("/api/v2/templateversions/%s", version), req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return ReadBodyAsError(res)
}
return nil
}
75 changes: 75 additions & 0 deletions docs/api/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,81 @@ curl -X GET http://coder-server:8080/api/v2/templateversions/{templateversion} \

To perform this operation, you must be authenticated. [Learn more](authentication.md).

## Patch template version by ID

### Code samples

```shell
# Example request using curl
curl -X PATCH http://coder-server:8080/api/v2/templateversions/{templateversion} \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```

`PATCH /templateversions/{templateversion}`

### Parameters

| Name | In | Type | Required | Description |
| ----------------- | ---- | ------------ | -------- | ------------------- |
| `templateversion` | path | string(uuid) | true | Template version ID |

### Example responses

> 200 Response

```json
{
"created_at": "2019-08-24T14:15:22Z",
"created_by": {
"avatar_url": "http://example.com",
"created_at": "2019-08-24T14:15:22Z",
"email": "[email protected]",
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"last_seen_at": "2019-08-24T14:15:22Z",
"organization_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"],
"roles": [
{
"display_name": "string",
"name": "string"
}
],
"status": "active",
"username": "string"
},
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"job": {
"canceled_at": "2019-08-24T14:15:22Z",
"completed_at": "2019-08-24T14:15:22Z",
"created_at": "2019-08-24T14:15:22Z",
"error": "string",
"error_code": "MISSING_TEMPLATE_PARAMETER",
"file_id": "8a0cfb4f-ddc9-436d-91bb-75133c583767",
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"started_at": "2019-08-24T14:15:22Z",
"status": "pending",
"tags": {
"property1": "string",
"property2": "string"
},
"worker_id": "ae5fa6f7-c55b-40c1-b40a-b36ac467652b"
},
"name": "string",
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"readme": "string",
"template_id": "c6d67e98-83ea-49f0-8812-e4abae2b68bc",
"updated_at": "2019-08-24T14:15:22Z"
}
```

### Responses

| Status | Meaning | Description | Schema |
| ------ | ------------------------------------------------------- | ----------- | -------------------------------------------------------------- |
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.TemplateVersion](schemas.md#codersdktemplateversion) |

To perform this operation, you must be authenticated. [Learn more](authentication.md).

## Cancel template version by ID

### Code samples
Expand Down
5 changes: 5 additions & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ export interface PatchGroupRequest {
readonly quota_allowance?: number
}

// From codersdk/templateversions.go
export interface PatchTemplateVersionRequest {
readonly name: string
}

// From codersdk/deployment.go
export interface PprofConfig {
readonly enable: boolean
Expand Down