Skip to content

Commit

Permalink
Create deployment in a single api request (now that our api supports …
Browse files Browse the repository at this point in the history
…it) (#89)
  • Loading branch information
danafallon authored Aug 29, 2024
1 parent 39e11d3 commit d793aaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
15 changes: 5 additions & 10 deletions internal/artieclient/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ type DestinationConfig struct {
SchemaNamePrefix string `json:"schemaNamePrefix"`
}

func (bd BaseDeployment) ToFullDeployment(_uuid uuid.UUID, status string) Deployment {
return Deployment{
UUID: _uuid,
Status: status,
BaseDeployment: bd,
}
}

type DeploymentClient struct {
client Client
}
Expand Down Expand Up @@ -115,8 +107,11 @@ func (dc DeploymentClient) Get(ctx context.Context, deploymentUUID string) (Depl
return response.Deployment, nil
}

func (dc DeploymentClient) Create(ctx context.Context, sourceType SourceType) (Deployment, error) {
body := map[string]any{"source": sourceType}
func (dc DeploymentClient) Create(ctx context.Context, deployment BaseDeployment) (Deployment, error) {
body := map[string]any{
"deployment": deployment,
"startDeployment": true,
}
return makeRequest[Deployment](ctx, dc.client, http.MethodPost, dc.basePath(), body)
}

Expand Down
16 changes: 2 additions & 14 deletions internal/provider/deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,13 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
return
}

// Our API's create endpoint only accepts the source type, so we need to send two requests:
// one to create the bare-bones deployment, then one to update it with the rest of the data
createdDeployment, err := r.client.Deployments().Create(ctx, deployment.Source.Type)
createdDeployment, err := r.client.Deployments().Create(ctx, deployment)
if err != nil {
resp.Diagnostics.AddError("Unable to Create Deployment", err.Error())
return
}

// Fill in computed fields from the API response of the newly created deployment
fullDeployment := deployment.ToFullDeployment(createdDeployment.UUID, createdDeployment.Status)

// Second API request: update the newly created deployment
updatedDeployment, err := r.client.Deployments().Update(ctx, fullDeployment)
if err != nil {
resp.Diagnostics.AddError("Unable to Update Deployment", err.Error())
return
}

r.SetStateData(ctx, &resp.State, resp.Diagnostics, updatedDeployment)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, createdDeployment)
}

func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down

0 comments on commit d793aaa

Please sign in to comment.