mirror of
https://github.com/coder/coder.git
synced 2025-06-28 04:33:02 +00:00
* ci: Update DataDog GitHub branch to fallback to GITHUB_REF This was detecting branches, but not our "main" branch before. Hopefully this fixes it! * Add basic Terraform Provider * Rename post files to upload * Add tests for resources * Skip instance identity test * Add tests for ensuring agent get's passed through properly * Fix linting errors * Add echo path * Fix agent authentication * fix: Convert all jobs to use a common resource and agent type This enables a consistent API for project import and provisioned resources.
96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/coderd"
|
|
)
|
|
|
|
// CreateProjectImportJob creates a new import job in the organization provided.
|
|
// ProjectImportJob is not associated with a project by default. Projects
|
|
// are created from import.
|
|
func (c *Client) CreateProjectImportJob(ctx context.Context, organization string, req coderd.CreateProjectImportJobRequest) (coderd.ProvisionerJob, error) {
|
|
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/projectimport/%s", organization), req)
|
|
if err != nil {
|
|
return coderd.ProvisionerJob{}, err
|
|
}
|
|
if res.StatusCode != http.StatusCreated {
|
|
defer res.Body.Close()
|
|
return coderd.ProvisionerJob{}, readBodyAsError(res)
|
|
}
|
|
var job coderd.ProvisionerJob
|
|
return job, json.NewDecoder(res.Body).Decode(&job)
|
|
}
|
|
|
|
// ProjectImportJob returns an import job by ID.
|
|
func (c *Client) ProjectImportJob(ctx context.Context, organization string, job uuid.UUID) (coderd.ProvisionerJob, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s", organization, job), nil)
|
|
if err != nil {
|
|
return coderd.ProvisionerJob{}, nil
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return coderd.ProvisionerJob{}, readBodyAsError(res)
|
|
}
|
|
var resp coderd.ProvisionerJob
|
|
return resp, json.NewDecoder(res.Body).Decode(&resp)
|
|
}
|
|
|
|
// ProjectImportJobLogsBefore returns logs that occurred before a specific time.
|
|
func (c *Client) ProjectImportJobLogsBefore(ctx context.Context, organization string, job uuid.UUID, before time.Time) ([]coderd.ProvisionerJobLog, error) {
|
|
return c.provisionerJobLogsBefore(ctx, "projectimport", organization, job, before)
|
|
}
|
|
|
|
// ProjectImportJobLogsAfter streams logs for a project import operation that occurred after a specific time.
|
|
func (c *Client) ProjectImportJobLogsAfter(ctx context.Context, organization string, job uuid.UUID, after time.Time) (<-chan coderd.ProvisionerJobLog, error) {
|
|
return c.provisionerJobLogsAfter(ctx, "projectimport", organization, job, after)
|
|
}
|
|
|
|
// ProjectImportJobSchemas returns schemas for an import job by ID.
|
|
func (c *Client) ProjectImportJobSchemas(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ParameterSchema, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/schemas", organization, job), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, readBodyAsError(res)
|
|
}
|
|
var params []coderd.ParameterSchema
|
|
return params, json.NewDecoder(res.Body).Decode(¶ms)
|
|
}
|
|
|
|
// ProjectImportJobParameters returns computed parameters for a project import job.
|
|
func (c *Client) ProjectImportJobParameters(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ComputedParameterValue, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/parameters", organization, job), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, readBodyAsError(res)
|
|
}
|
|
var params []coderd.ComputedParameterValue
|
|
return params, json.NewDecoder(res.Body).Decode(¶ms)
|
|
}
|
|
|
|
// ProjectImportJobResources returns resources for a project import job.
|
|
func (c *Client) ProjectImportJobResources(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ProvisionerJobResource, error) {
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/resources", organization, job), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, readBodyAsError(res)
|
|
}
|
|
var resources []coderd.ProvisionerJobResource
|
|
return resources, json.NewDecoder(res.Body).Decode(&resources)
|
|
}
|