Skip to content

docs: API templateversions, templates, members, organizations #5546

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 26 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
15 changes: 11 additions & 4 deletions coderd/apidoc/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const docTemplate = `{
"CoderSessionToken": []
}
],
"description": "Notice: Swagger 2.0 doesn't support file upload with a ` + "`" + `content-type` + "`" + ` different than ` + "`" + `application/x-www-form-urlencoded` + "`" + `.",
"description": "Swagger notice: Swagger 2.0 doesn't support file upload with a ` + "`" + `content-type` + "`" + ` different than ` + "`" + `application/x-www-form-urlencoded` + "`" + `.",
"consumes": [
"application/x-tar"
],
Expand Down Expand Up @@ -1770,6 +1770,9 @@ const docTemplate = `{
"200": {
"description": "OK"
}
},
"x-apidocgen": {
"skip": true
}
}
},
Expand Down Expand Up @@ -3340,7 +3343,8 @@ const docTemplate = `{
"type": "object",
"properties": {
"created_at": {
"type": "string"
"type": "string",
"format": "date-time"
},
"destination_scheme": {
"type": "string",
Expand Down Expand Up @@ -3377,7 +3381,8 @@ const docTemplate = `{
]
},
"updated_at": {
"type": "string"
"type": "string",
"format": "date-time"
}
}
},
Expand Down Expand Up @@ -3454,6 +3459,7 @@ const docTemplate = `{
}
},
"codersdk.PostWorkspaceAgentVersionRequest": {
"description": "x-apidocgen:skip",
"type": "object",
"properties": {
"version": {
Expand Down Expand Up @@ -3894,7 +3900,8 @@ const docTemplate = `{
"type": "object",
"properties": {
"hash": {
"type": "string"
"type": "string",
"format": "uuid"
}
}
},
Expand Down
15 changes: 11 additions & 4 deletions coderd/apidoc/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
"CoderSessionToken": []
}
],
"description": "Notice: Swagger 2.0 doesn't support file upload with a `content-type` different than `application/x-www-form-urlencoded`.",
"description": "Swagger notice: Swagger 2.0 doesn't support file upload with a `content-type` different than `application/x-www-form-urlencoded`.",
"consumes": ["application/x-tar"],
"produces": ["application/json"],
"tags": ["Files"],
Expand Down Expand Up @@ -1548,6 +1548,9 @@
"200": {
"description": "OK"
}
},
"x-apidocgen": {
"skip": true
}
}
},
Expand Down Expand Up @@ -3013,7 +3016,8 @@
"type": "object",
"properties": {
"created_at": {
"type": "string"
"type": "string",
"format": "date-time"
},
"destination_scheme": {
"type": "string",
Expand All @@ -3039,7 +3043,8 @@
"enum": ["none", "data"]
},
"updated_at": {
"type": "string"
"type": "string",
"format": "date-time"
}
}
},
Expand Down Expand Up @@ -3109,6 +3114,7 @@
}
},
"codersdk.PostWorkspaceAgentVersionRequest": {
"description": "x-apidocgen:skip",
"type": "object",
"properties": {
"version": {
Expand Down Expand Up @@ -3539,7 +3545,8 @@
"type": "object",
"properties": {
"hash": {
"type": "string"
"type": "string",
"format": "uuid"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
)

// @Summary Upload file
// @Description Notice: Swagger 2.0 doesn't support file upload with a `content-type` different than `application/x-www-form-urlencoded`.
// @Description Swagger notice: Swagger 2.0 doesn't support file upload with a `content-type` different than `application/x-www-form-urlencoded`.
// @ID update-file
// @Security CoderSessionToken
// @Produce json
Expand Down
130 changes: 1 addition & 129 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
// @Param request body codersdk.PostWorkspaceAgentVersionRequest true "Version request"
// @Success 200
// @Router /workspaceagents/me/version [post]
// @x-apidocgen {"skip": true}
func (api *API) postWorkspaceAgentVersion(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
workspaceAgent := httpmw.WorkspaceAgent(r)
Expand Down Expand Up @@ -859,135 +860,6 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
})
}

func (api *API) workspaceAgentReportStatsWebsocket(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

api.WebsocketWaitMutex.Lock()
api.WebsocketWaitGroup.Add(1)
api.WebsocketWaitMutex.Unlock()
defer api.WebsocketWaitGroup.Done()

workspaceAgent := httpmw.WorkspaceAgent(r)
workspace, err := api.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Failed to get workspace.",
Detail: err.Error(),
})
return
}

conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Failed to accept websocket.",
Detail: err.Error(),
})
return
}
go httpapi.Heartbeat(ctx, conn)

defer conn.Close(websocket.StatusGoingAway, "")

var lastReport codersdk.AgentStatsReportResponse
latestStat, err := api.Database.GetLatestAgentStat(ctx, workspaceAgent.ID)
if err == nil {
err = json.Unmarshal(latestStat.Payload, &lastReport)
if err != nil {
api.Logger.Debug(ctx, "unmarshal stat payload", slog.Error(err))
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("unmarshal stat payload: %s", err))
return
}
}

// Allow overriding the stat interval for debugging and testing purposes.
timer := time.NewTicker(api.AgentStatsRefreshInterval)
defer timer.Stop()

go func() {
for {
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
if err != nil {
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("write report request: %s", err))
return
}

select {
case <-timer.C:
continue
case <-ctx.Done():
conn.Close(websocket.StatusNormalClosure, "")
return
}
}
}()

for {
var rep codersdk.AgentStatsReportResponse
err = wsjson.Read(ctx, conn, &rep)
if err != nil {
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("read report response: %s", err))
return
}

repJSON, err := json.Marshal(rep)
if err != nil {
api.Logger.Debug(ctx, "marshal stat json", slog.Error(err))
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("marshal stat json: %s", err))
return
}

// Avoid inserting duplicate rows to preserve DB space.
// We will see duplicate reports when on idle connections
// (e.g. web terminal left open) or when there are no connections at
// all.
// We also don't want to update the workspace last used at on duplicate
// reports.
updateDB := !reflect.DeepEqual(lastReport, rep)

api.Logger.Debug(ctx, "read stats report",
slog.F("interval", api.AgentStatsRefreshInterval),
slog.F("agent", workspaceAgent.ID),
slog.F("workspace", workspace.ID),
slog.F("update_db", updateDB),
slog.F("payload", rep),
)

if updateDB {
go activityBumpWorkspace(api.Logger.Named("activity_bump"), api.Database, workspace.ID)

lastReport = rep

_, err = api.Database.InsertAgentStat(ctx, database.InsertAgentStatParams{
ID: uuid.New(),
CreatedAt: database.Now(),
AgentID: workspaceAgent.ID,
WorkspaceID: workspace.ID,
UserID: workspace.OwnerID,
TemplateID: workspace.TemplateID,
Payload: json.RawMessage(repJSON),
})
if err != nil {
api.Logger.Debug(ctx, "insert agent stat", slog.Error(err))
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("insert agent stat: %s", err))
return
}

err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: database.Now(),
})
if err != nil {
api.Logger.Debug(ctx, "update workspace last used at", slog.Error(err))
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("update workspace last used at: %s", err))
return
}
}
}
}

// @Summary Submit workspace application health
// @ID submit-workspace-workspace-agent-health
// @Security CoderSessionToken
Expand Down
4 changes: 2 additions & 2 deletions codersdk/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type Parameter struct {
Name string `json:"name" table:"name"`
SourceScheme ParameterSourceScheme `json:"source_scheme" table:"source scheme" validate:"ne=none" enums:"none,data"`
DestinationScheme ParameterDestinationScheme `json:"destination_scheme" table:"destination scheme" validate:"ne=none" enums:"none,environment_variable,provisioner_variable"`
CreatedAt time.Time `json:"created_at" table:"created at"`
UpdatedAt time.Time `json:"updated_at" table:"updated at"`
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
UpdatedAt time.Time `json:"updated_at" table:"updated at" format:"date-time"`
}

type ParameterSchema struct {
Expand Down
1 change: 1 addition & 0 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type WorkspaceAgentConnectionInfo struct {
}

// @typescript-ignore PostWorkspaceAgentVersionRequest
// @Description x-apidocgen:skip
type PostWorkspaceAgentVersionRequest struct {
Version string `json:"version"`
}
Expand Down
2 changes: 1 addition & 1 deletion codersdk/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type WorkspaceBuild struct {
Job ProvisionerJob `json:"job"`
Reason BuildReason `db:"reason" json:"reason" enums:"initiator,autostart,autostop"`
Resources []WorkspaceResource `json:"resources"`
Deadline NullTime `json:"deadline,omitempty" swaggertype:"string" format:"date-time"`
Deadline NullTime `json:"deadline,omitempty" format:"date-time"`
Status WorkspaceStatus `json:"status" enums:"pending,starting,running,stopping,stopped,failed,canceling,canceled,deleting,deleted"`
DailyCost int32 `json:"daily_cost"`
}
Expand Down
35 changes: 0 additions & 35 deletions docs/api/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,38 +437,3 @@ curl -X POST http://coder-server:8080/api/v2/workspaceagents/me/report-stats \
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.AgentStatsResponse](schemas.md#codersdkagentstatsresponse) |

To perform this operation, you must be authenticated by means of one of the following methods: **CoderSessionToken**.

## Submit workspace agent version

### Code samples

```shell
# Example request using curl
curl -X POST http://coder-server:8080/api/v2/workspaceagents/me/version \
-H 'Content-Type: application/json' \
-H 'Coder-Session-Token: API_KEY'
```

`POST /workspaceagents/me/version`

> Body parameter

```json
{
"version": "string"
}
```

### Parameters

| Name | In | Type | Required | Description |
| ------ | ---- | ------------------------------------------------------------------------------------------------ | -------- | --------------- |
| `body` | body | [codersdk.PostWorkspaceAgentVersionRequest](schemas.md#codersdkpostworkspaceagentversionrequest) | true | Version request |

### Responses

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

To perform this operation, you must be authenticated by means of one of the following methods: **CoderSessionToken**.
Loading