feat: Add streaming endpoint for workspace history (#157)

* feat: Add parameter querying to the API

* feat: Add streaming endpoint for workspace history

Enables a buildlog-like flow for reading job output.

* Fix empty parameter source and destination

* Add comment for usage of workspace history logs endpoint
This commit is contained in:
Kyle Carberry
2022-02-04 13:36:43 -06:00
committed by GitHub
parent 430341b7da
commit 65de6eef9c
22 changed files with 688 additions and 115 deletions

View File

@ -31,6 +31,27 @@ type ProjectHistory struct {
Import ProvisionerJob `json:"import"`
}
// ProjectParameter represents a parameter parsed from project history source on creation.
type ProjectParameter struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
ProjectHistoryID uuid.UUID `json:"project_history_id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
DefaultSourceScheme database.ParameterSourceScheme `json:"default_source_scheme,omitempty"`
DefaultSourceValue string `json:"default_source_value,omitempty"`
AllowOverrideSource bool `json:"allow_override_source"`
DefaultDestinationScheme database.ParameterDestinationScheme `json:"default_destination_scheme,omitempty"`
DefaultDestinationValue string `json:"default_destination_value,omitempty"`
AllowOverrideDestination bool `json:"allow_override_destination"`
DefaultRefresh string `json:"default_refresh"`
RedisplayValue bool `json:"redisplay_value"`
ValidationError string `json:"validation_error,omitempty"`
ValidationCondition string `json:"validation_condition,omitempty"`
ValidationTypeSystem database.ParameterTypeSystem `json:"validation_type_system,omitempty"`
ValidationValueType string `json:"validation_value_type,omitempty"`
}
// CreateProjectHistoryRequest enables callers to create a new Project Version.
type CreateProjectHistoryRequest struct {
StorageMethod database.ProjectStorageMethod `json:"storage_method" validate:"oneof=inline-archive,required"`
@ -160,14 +181,81 @@ func (api *api) postProjectHistoryByOrganization(rw http.ResponseWriter, r *http
render.JSON(rw, r, convertProjectHistory(projectHistory, provisionerJob))
}
func (api *api) projectHistoryParametersByOrganizationAndName(rw http.ResponseWriter, r *http.Request) {
projectHistory := httpmw.ProjectHistoryParam(r)
job, err := api.Database.GetProvisionerJobByID(r.Context(), projectHistory.ImportJobID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get provisioner job: %s", err),
})
return
}
apiJob := convertProvisionerJob(job)
if !apiJob.Status.Completed() {
httpapi.Write(rw, http.StatusPreconditionRequired, httpapi.Response{
Message: fmt.Sprintf("import job hasn't completed: %s", apiJob.Status),
})
return
}
if apiJob.Status != ProvisionerJobStatusSucceeded {
httpapi.Write(rw, http.StatusPreconditionFailed, httpapi.Response{
Message: "import job wasn't successful. no parameters were parsed",
})
return
}
parameters, err := api.Database.GetProjectParametersByHistoryID(r.Context(), projectHistory.ID)
if errors.Is(err, sql.ErrNoRows) {
err = nil
parameters = []database.ProjectParameter{}
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get project parameters: %s", err),
})
return
}
apiParameters := make([]ProjectParameter, 0, len(parameters))
for _, parameter := range parameters {
apiParameters = append(apiParameters, convertProjectParameter(parameter))
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, apiParameters)
}
func convertProjectHistory(history database.ProjectHistory, job database.ProvisionerJob) ProjectHistory {
return ProjectHistory{
ID: history.ID,
ProjectID: history.ProjectID,
CreatedAt: history.CreatedAt,
UpdatedAt: history.UpdatedAt,
Name: history.Name,
Import: convertProvisionerJob(job),
ID: history.ID,
ProjectID: history.ProjectID,
CreatedAt: history.CreatedAt,
UpdatedAt: history.UpdatedAt,
Name: history.Name,
StorageMethod: history.StorageMethod,
Import: convertProvisionerJob(job),
}
}
func convertProjectParameter(parameter database.ProjectParameter) ProjectParameter {
return ProjectParameter{
ID: parameter.ID,
CreatedAt: parameter.CreatedAt,
ProjectHistoryID: parameter.ProjectHistoryID,
Name: parameter.Name,
Description: parameter.Description,
DefaultSourceScheme: parameter.DefaultSourceScheme,
DefaultSourceValue: parameter.DefaultSourceValue.String,
AllowOverrideSource: parameter.AllowOverrideSource,
DefaultDestinationScheme: parameter.DefaultDestinationScheme,
DefaultDestinationValue: parameter.DefaultDestinationValue.String,
AllowOverrideDestination: parameter.AllowOverrideDestination,
DefaultRefresh: parameter.DefaultRefresh,
RedisplayValue: parameter.RedisplayValue,
ValidationError: parameter.ValidationError,
ValidationCondition: parameter.ValidationCondition,
ValidationTypeSystem: parameter.ValidationTypeSystem,
ValidationValueType: parameter.ValidationValueType,
}
}