Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
142 changes: 142 additions & 0 deletions libs/deploy-engine-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

Expand Down Expand Up @@ -993,6 +994,143 @@ func (c *Client) CleanupReconciliationResults(
return resp.Data, nil
}

// RunStateHousekeeping triggers the upkeep the state backend needs on its own storage,
// such as reclaiming storage that interrupted writes left behind.
// This is an asynchronous operation that returns immediately after triggering it.
// What housekeeping involves is up to the backend. A backend that needs none still
// starts an operation, which completes having deleted nothing.
//
// Poll the operation with GetCleanupOperation or WaitForCleanupCompletion using
// manage.CleanupTypeStateHousekeeping.
//
// This is the `POST {baseURL}/v1/state/housekeeping` API endpoint.
func (c *Client) RunStateHousekeeping(
ctx context.Context,
) (*manage.CleanupOperation, error) {
url := fmt.Sprintf("%s/v1/state/housekeeping", c.endpoint)
resp := &types.CleanupOperationResponse{}
err := c.startCleanupOperation(ctx, url, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}

// CleanupAuditRecords triggers cleanup of old audit records.
// This is an asynchronous operation that returns immediately after triggering the cleanup.
// Audit records older than the configured retention period will be removed.
//
// Poll the operation with GetCleanupOperation or WaitForCleanupCompletion using
// manage.CleanupTypeAuditRecords.
//
// This is the `POST {baseURL}/v1/audit/records/cleanup` API endpoint.
func (c *Client) CleanupAuditRecords(
ctx context.Context,
) (*manage.CleanupOperation, error) {
url := fmt.Sprintf("%s/v1/audit/records/cleanup", c.endpoint)
resp := &types.CleanupOperationResponse{}
err := c.startCleanupOperation(ctx, url, resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}

// QueryAuditRecords reads a page of the deploy engine's audit trail, oldest first,
// narrowed by the query's filters. A nil query reads the first page of the whole trail
// with the engine's default limit.
//
// A page is the complete answer only when its HasMore is false. To read on, pass the
// page's NextCursor as the After field of the next query, keeping the other filters.
//
// A filter the engine rejects, such as an unknown outcome, a negative bound or a limit
// above 1000, returns a ClientError that errors.IsValidationError reports. Negative
// values are sent rather than dropped, so a bad bound is never read as no bound.
//
// This is the `GET {baseURL}/v1/audit/records` API endpoint.
func (c *Client) QueryAuditRecords(
ctx context.Context,
query *types.AuditRecordQuery,
) (*types.AuditRecordsPage, error) {
recordsURL := fmt.Sprintf("%s/v1/audit/records", c.endpoint)
page := &types.AuditRecordsPage{}
err := c.getResourceWithQueryParams(
ctx,
recordsURL,
buildAuditRecordQueryParams(query),
page,
)
if err != nil {
return nil, err
}
if page.HasMore && page.NextCursor == nil {
// Without a cursor the rest of the trail cannot be read, and returning the page
// would leave a caller nothing to tell it from a complete answer.
return nil, createDeserialiseError(
"audit records page reports more records but has no cursor to read them",
)
}
return page, nil
}

func buildAuditRecordQueryParams(query *types.AuditRecordQuery) map[string]string {
queryParams := map[string]string{}
if query == nil {
return queryParams
}
if query.From != 0 {
queryParams["from"] = strconv.FormatInt(query.From, 10)
}
if query.To != 0 {
queryParams["to"] = strconv.FormatInt(query.To, 10)
}
if query.After != nil {
queryParams["afterTimestamp"] = strconv.FormatInt(query.After.AfterTimestamp, 10)
queryParams["afterId"] = query.After.AfterID
}
if query.Action != "" {
queryParams["action"] = query.Action
}
if query.PrincipalSubject != "" {
queryParams["principalSubject"] = query.PrincipalSubject
}
if query.Outcome != "" {
queryParams["outcome"] = query.Outcome
}
if query.Limit != 0 {
queryParams["limit"] = strconv.Itoa(query.Limit)
}
return queryParams
}

// GetAuditRecord retrieves a single audit record by ID.
// An unknown ID returns a ClientError that errors.IsNotFoundError reports.
//
// This is the `GET {baseURL}/v1/audit/records/{id}` API endpoint.
func (c *Client) GetAuditRecord(
ctx context.Context,
recordID string,
) (*manage.AuditRecord, error) {
recordURL := fmt.Sprintf(
"%s/v1/audit/records/%s",
c.endpoint,
url.PathEscape(recordID),
)
record := &manage.AuditRecord{}
err := c.getResource(ctx, recordURL, record)
if err != nil {
return nil, err
}
if record.ID != recordID {
// An ID such as "." is cleaned out of the path by the engine's router, which
// redirects the request to another endpoint that also answers 200.
return nil, createDeserialiseError(
fmt.Sprintf("response does not hold the requested audit record %q", recordID),
)
}
return record, nil
}

// GetCleanupOperation retrieves the status of a cleanup operation by ID.
// This is used to poll the status of an asynchronous cleanup operation.
func (c *Client) GetCleanupOperation(
Expand Down Expand Up @@ -1023,6 +1161,10 @@ func (c *Client) cleanupStatusURL(cleanupType manage.CleanupType, operationID st
return fmt.Sprintf("%s/v1/deployments/changes/cleanup/%s", c.endpoint, operationID)
case manage.CleanupTypeReconciliationResults:
return fmt.Sprintf("%s/v1/deployments/reconciliation-results/cleanup/%s", c.endpoint, operationID)
case manage.CleanupTypeAuditRecords:
return fmt.Sprintf("%s/v1/audit/records/cleanup/%s", c.endpoint, operationID)
case manage.CleanupTypeStateHousekeeping:
return fmt.Sprintf("%s/v1/state/housekeeping/%s", c.endpoint, operationID)
default:
return ""
}
Expand Down
Loading
Loading