Generate API docs for intel

This commit is contained in:
Kyle Carberry 2024-05-01 15:36:52 +00:00
parent 5ccb8c6fd3
commit ef93844f56
7 changed files with 635 additions and 0 deletions

161
coderd/apidoc/docs.go generated
View File

@ -2198,6 +2198,56 @@ const docTemplate = `{
}
}
},
"/organizations/{organization}/intel/report": {
"get": {
"security": [
{
"CoderSessionToken": []
}
],
"produces": [
"application/json"
],
"tags": [
"Intel"
],
"summary": "Get intel report",
"operationId": "get-intel-report",
"parameters": [
{
"type": "string",
"format": "uuid",
"description": "Organization ID",
"name": "organization",
"in": "path",
"required": true
},
{
"type": "string",
"format": "uuid",
"description": "Cohort ID",
"name": "cohort_id",
"in": "query",
"required": true
},
{
"type": "string",
"format": "date",
"description": "Starts at",
"name": "starts_at",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/codersdk.IntelReport"
}
}
}
}
},
"/organizations/{organization}/intel/serve": {
"get": {
"security": [
@ -10158,6 +10208,117 @@ const docTemplate = `{
}
}
},
"codersdk.IntelReport": {
"type": "object",
"properties": {
"commands": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportCommand"
}
},
"git_remotes": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportGitRemote"
}
},
"invocations": {
"type": "integer"
}
}
},
"codersdk.IntelReportCommand": {
"type": "object",
"properties": {
"binary_args": {
"type": "array",
"items": {
"type": "string"
}
},
"binary_name": {
"type": "string"
},
"binary_paths": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"exit_codes": {
"description": "ExitCodes maps exit codes to the number of invocations.",
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"git_remote_urls": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"intervals": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportInvocationInterval"
}
},
"invocations": {
"type": "integer"
},
"working_directories": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
}
}
},
"codersdk.IntelReportGitRemote": {
"type": "object",
"properties": {
"external_auth_provider_id": {
"type": "string"
},
"intervals": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportInvocationInterval"
}
},
"invocations": {
"type": "integer"
},
"url": {
"type": "string"
}
}
},
"codersdk.IntelReportInvocationInterval": {
"type": "object",
"properties": {
"cohort_id": {
"type": "string",
"format": "uuid"
},
"ends_at": {
"type": "string",
"format": "date-time"
},
"invocations": {
"type": "integer"
},
"median_duration_ms": {
"type": "number"
},
"starts_at": {
"type": "string",
"format": "date-time"
}
}
},
"codersdk.IssueReconnectingPTYSignedTokenRequest": {
"type": "object",
"required": [

View File

@ -1927,6 +1927,52 @@
}
}
},
"/organizations/{organization}/intel/report": {
"get": {
"security": [
{
"CoderSessionToken": []
}
],
"produces": ["application/json"],
"tags": ["Intel"],
"summary": "Get intel report",
"operationId": "get-intel-report",
"parameters": [
{
"type": "string",
"format": "uuid",
"description": "Organization ID",
"name": "organization",
"in": "path",
"required": true
},
{
"type": "string",
"format": "uuid",
"description": "Cohort ID",
"name": "cohort_id",
"in": "query",
"required": true
},
{
"type": "string",
"format": "date",
"description": "Starts at",
"name": "starts_at",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/codersdk.IntelReport"
}
}
}
}
},
"/organizations/{organization}/intel/serve": {
"get": {
"security": [
@ -9140,6 +9186,117 @@
}
}
},
"codersdk.IntelReport": {
"type": "object",
"properties": {
"commands": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportCommand"
}
},
"git_remotes": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportGitRemote"
}
},
"invocations": {
"type": "integer"
}
}
},
"codersdk.IntelReportCommand": {
"type": "object",
"properties": {
"binary_args": {
"type": "array",
"items": {
"type": "string"
}
},
"binary_name": {
"type": "string"
},
"binary_paths": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"exit_codes": {
"description": "ExitCodes maps exit codes to the number of invocations.",
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"git_remote_urls": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
},
"intervals": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportInvocationInterval"
}
},
"invocations": {
"type": "integer"
},
"working_directories": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
}
}
},
"codersdk.IntelReportGitRemote": {
"type": "object",
"properties": {
"external_auth_provider_id": {
"type": "string"
},
"intervals": {
"type": "array",
"items": {
"$ref": "#/definitions/codersdk.IntelReportInvocationInterval"
}
},
"invocations": {
"type": "integer"
},
"url": {
"type": "string"
}
}
},
"codersdk.IntelReportInvocationInterval": {
"type": "object",
"properties": {
"cohort_id": {
"type": "string",
"format": "uuid"
},
"ends_at": {
"type": "string",
"format": "date-time"
},
"invocations": {
"type": "integer"
},
"median_duration_ms": {
"type": "number"
},
"starts_at": {
"type": "string",
"format": "date-time"
}
}
},
"codersdk.IssueReconnectingPTYSignedTokenRequest": {
"type": "object",
"required": ["agentID", "url"],

View File

@ -29,6 +29,18 @@ import (
"github.com/coder/coder/v2/inteld/proto"
)
// intelReport returns a report of invocations for a set of cohorts.
//
// @Summary Get intel report
// @ID get-intel-report
// @Security CoderSessionToken
// @Produce json
// @Tags Intel
// @Param organization path string true "Organization ID" format(uuid)
// @Param cohort_id query string true "Cohort ID" format(uuid)
// @Param starts_at query string false "Starts at" format(date)
// @Success 200 {object} codersdk.IntelReport
// @Router /organizations/{organization}/intel/report [get]
func (api *API) intelReport(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

View File

@ -2,8 +2,10 @@ package coderd_test
import (
"context"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/coderdtest"
@ -86,3 +88,23 @@ func TestIntelCohorts(t *testing.T) {
require.Equal(t, cohort.Name, "example")
})
}
func TestIntelReport(t *testing.T) {
t.Parallel()
t.Run("Empty", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
ctx := context.Background()
cohort, err := client.CreateIntelCohort(ctx, user.OrganizationID, codersdk.CreateIntelCohortRequest{
Name: "example",
})
require.NoError(t, err)
report, err := client.IntelReport(ctx, user.OrganizationID, codersdk.IntelReportRequest{
CohortIDs: []uuid.UUID{cohort.ID},
})
require.NoError(t, err)
// require.Equal(t, )
fmt.Printf("%+v\n", report)
})
}

View File

@ -284,3 +284,33 @@ type IntelReportCommand struct {
WorkingDirectories map[string]int64 `json:"working_directories"`
BinaryPaths map[string]int64 `json:"binary_paths"`
}
// IntelReport returns a report of invocations for a cohort.
func (c *Client) IntelReport(ctx context.Context, organizationID uuid.UUID, req IntelReportRequest) (IntelReport, error) {
orgParam := organizationID.String()
if organizationID == uuid.Nil {
orgParam = DefaultOrganization
}
serverURL, err := c.URL.Parse(fmt.Sprintf("/api/v2/organizations/%s/intel/report", orgParam))
if err != nil {
return IntelReport{}, xerrors.Errorf("parse url: %w", err)
}
q := serverURL.Query()
if !req.StartsAt.IsZero() {
q.Set("starts_at", req.StartsAt.Format(time.DateOnly))
}
for _, cohortID := range req.CohortIDs {
q.Add("cohort_id", cohortID.String())
}
serverURL.RawQuery = q.Encode()
res, err := c.Request(ctx, http.MethodGet, serverURL.String(), req)
if err != nil {
return IntelReport{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return IntelReport{}, ReadBodyAsError(res)
}
var report IntelReport
return report, json.NewDecoder(res.Body).Decode(&report)
}

87
docs/api/intel.md generated
View File

@ -111,6 +111,93 @@ curl -X POST http://coder-server:8080/api/v2/organizations/{organization}/intel/
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get intel report
### Code samples
```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/v2/organizations/{organization}/intel/report?cohort_id=497f6eca-6276-4993-bfeb-53cbbbba6f08 \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /organizations/{organization}/intel/report`
### Parameters
| Name | In | Type | Required | Description |
| -------------- | ----- | ------------ | -------- | --------------- |
| `organization` | path | string(uuid) | true | Organization ID |
| `cohort_id` | query | string(uuid) | true | Cohort ID |
| `starts_at` | query | string(date) | false | Starts at |
### Example responses
> 200 Response
```json
{
"commands": [
{
"binary_args": ["string"],
"binary_name": "string",
"binary_paths": {
"property1": 0,
"property2": 0
},
"exit_codes": {
"property1": 0,
"property2": 0
},
"git_remote_urls": {
"property1": 0,
"property2": 0
},
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"working_directories": {
"property1": 0,
"property2": 0
}
}
],
"git_remotes": [
{
"external_auth_provider_id": "string",
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"url": "string"
}
],
"invocations": 0
}
```
### Responses
| Status | Meaning | Description | Schema |
| ------ | ------------------------------------------------------- | ----------- | ------------------------------------------------------ |
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.IntelReport](schemas.md#codersdkintelreport) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Serve intel daemon
### Code samples

166
docs/api/schemas.md generated
View File

@ -3198,6 +3198,172 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o
| `count` | integer | false | | |
| `intel_machines` | array of [codersdk.IntelMachine](#codersdkintelmachine) | false | | |
## codersdk.IntelReport
```json
{
"commands": [
{
"binary_args": ["string"],
"binary_name": "string",
"binary_paths": {
"property1": 0,
"property2": 0
},
"exit_codes": {
"property1": 0,
"property2": 0
},
"git_remote_urls": {
"property1": 0,
"property2": 0
},
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"working_directories": {
"property1": 0,
"property2": 0
}
}
],
"git_remotes": [
{
"external_auth_provider_id": "string",
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"url": "string"
}
],
"invocations": 0
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
| ------------- | ----------------------------------------------------------------------- | -------- | ------------ | ----------- |
| `commands` | array of [codersdk.IntelReportCommand](#codersdkintelreportcommand) | false | | |
| `git_remotes` | array of [codersdk.IntelReportGitRemote](#codersdkintelreportgitremote) | false | | |
| `invocations` | integer | false | | |
## codersdk.IntelReportCommand
```json
{
"binary_args": ["string"],
"binary_name": "string",
"binary_paths": {
"property1": 0,
"property2": 0
},
"exit_codes": {
"property1": 0,
"property2": 0
},
"git_remote_urls": {
"property1": 0,
"property2": 0
},
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"working_directories": {
"property1": 0,
"property2": 0
}
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
| --------------------- | ----------------------------------------------------------------------------------------- | -------- | ------------ | -------------------------------------------------------- |
| `binary_args` | array of string | false | | |
| `binary_name` | string | false | | |
| `binary_paths` | object | false | | |
| » `[any property]` | integer | false | | |
| `exit_codes` | object | false | | Exit codes maps exit codes to the number of invocations. |
| » `[any property]` | integer | false | | |
| `git_remote_urls` | object | false | | |
| » `[any property]` | integer | false | | |
| `intervals` | array of [codersdk.IntelReportInvocationInterval](#codersdkintelreportinvocationinterval) | false | | |
| `invocations` | integer | false | | |
| `working_directories` | object | false | | |
| » `[any property]` | integer | false | | |
## codersdk.IntelReportGitRemote
```json
{
"external_auth_provider_id": "string",
"intervals": [
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
],
"invocations": 0,
"url": "string"
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
| --------------------------- | ----------------------------------------------------------------------------------------- | -------- | ------------ | ----------- |
| `external_auth_provider_id` | string | false | | |
| `intervals` | array of [codersdk.IntelReportInvocationInterval](#codersdkintelreportinvocationinterval) | false | | |
| `invocations` | integer | false | | |
| `url` | string | false | | |
## codersdk.IntelReportInvocationInterval
```json
{
"cohort_id": "123f4084-d082-4664-a07c-0daa0f13a82f",
"ends_at": "2019-08-24T14:15:22Z",
"invocations": 0,
"median_duration_ms": 0,
"starts_at": "2019-08-24T14:15:22Z"
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
| -------------------- | ------- | -------- | ------------ | ----------- |
| `cohort_id` | string | false | | |
| `ends_at` | string | false | | |
| `invocations` | integer | false | | |
| `median_duration_ms` | number | false | | |
| `starts_at` | string | false | | |
## codersdk.IssueReconnectingPTYSignedTokenRequest
```json