Skip to content

Commit 6f4d4e3

Browse files
Merge pull request #19 from ctrlplanedev/deployment-agents-array
chore: support multiple agents for deployment
2 parents 20b53cf + c50d62a commit 6f4d4e3

2 files changed

Lines changed: 83 additions & 56 deletions

File tree

internal/api/client.gen.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/deployment_resource.go

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -198,32 +198,21 @@ func (r *DeploymentResource) ValidateConfig(ctx context.Context, req resource.Va
198198
return
199199
}
200200

201-
if len(data.JobAgent) > 1 {
202-
resp.Diagnostics.AddError(
203-
"Invalid deployment configuration",
204-
"Only one job_agent block can be set.",
205-
)
206-
return
207-
}
208-
209-
if len(data.JobAgent) == 0 {
210-
return
211-
}
212-
213-
ja := data.JobAgent[0]
214-
if ja.Id.IsNull() || (!ja.Id.IsUnknown() && ja.Id.ValueString() == "") {
215-
resp.Diagnostics.AddError(
216-
"Invalid job agent configuration",
217-
"job_agent.id is required when job_agent is set.",
218-
)
219-
return
220-
}
201+
for i, ja := range data.JobAgent {
202+
if ja.Id.IsNull() || (!ja.Id.IsUnknown() && ja.Id.ValueString() == "") {
203+
resp.Diagnostics.AddError(
204+
"Invalid job agent configuration",
205+
fmt.Sprintf("job_agent[%d].id is required.", i),
206+
)
207+
return
208+
}
221209

222-
if countDeploymentJobAgentBlocks(ja) > 1 {
223-
resp.Diagnostics.AddError(
224-
"Invalid job agent configuration",
225-
"Only one of argocd, github, terraform_cloud, or test_runner can be set.",
226-
)
210+
if countDeploymentJobAgentBlocks(ja) > 1 {
211+
resp.Diagnostics.AddError(
212+
"Invalid job agent configuration",
213+
fmt.Sprintf("job_agent[%d]: only one of argocd, github, terraform_cloud, or test_runner can be set.", i),
214+
)
215+
}
227216
}
228217
}
229218

@@ -240,24 +229,12 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
240229
return
241230
}
242231

243-
var jobAgentId *string
244-
var jobAgentConfig *map[string]interface{}
245-
if len(data.JobAgent) > 0 {
246-
ja := data.JobAgent[0]
247-
if !ja.Id.IsNull() && !ja.Id.IsUnknown() {
248-
id := ja.Id.ValueString()
249-
jobAgentId = &id
250-
}
251-
jobAgentConfig = deploymentJobAgentConfigFromModel(ja)
252-
}
253-
254232
requestBody := api.RequestDeploymentCreationJSONRequestBody{
255233
Name: data.Name.ValueString(),
256234
Slug: slug.Make(data.Name.ValueString()),
257235
Metadata: stringMapPointer(data.Metadata),
258236
ResourceSelector: selector,
259-
JobAgentId: jobAgentId,
260-
JobAgentConfig: jobAgentConfig,
237+
JobAgents: deploymentJobAgentsFromModel(data.JobAgent),
261238
}
262239

263240
deployResp, err := r.workspace.Client.RequestDeploymentCreationWithResponse(ctx, r.workspace.ID.String(), requestBody)
@@ -349,19 +326,18 @@ func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest,
349326
data.ResourceSelector = selectorValue
350327
}
351328

352-
if dep.JobAgentId != nil || len(dep.JobAgentConfig) > 0 {
329+
if dep.JobAgents != nil && len(*dep.JobAgents) > 0 {
330+
data.JobAgent = deploymentJobAgentModelsFromAPI(*dep.JobAgents)
331+
} else if dep.JobAgentId != nil {
353332
jobAgent := DeploymentJobAgentModel{
354-
Id: types.StringNull(),
333+
Id: types.StringValue(*dep.JobAgentId),
355334
Priority: types.Int64Null(),
356335
Selector: types.StringNull(),
357336
ArgoCD: nil,
358337
GitHub: nil,
359338
TerraformCloud: nil,
360339
TestRunner: nil,
361340
}
362-
if dep.JobAgentId != nil {
363-
jobAgent.Id = types.StringValue(*dep.JobAgentId)
364-
}
365341
if len(dep.JobAgentConfig) > 0 {
366342
setDeploymentJobAgentBlocksFromConfig(&jobAgent, dep.JobAgentConfig)
367343
}
@@ -386,24 +362,12 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
386362
return
387363
}
388364

389-
var jobAgentId *string
390-
var jobAgentConfig *map[string]interface{}
391-
if len(data.JobAgent) > 0 {
392-
ja := data.JobAgent[0]
393-
if !ja.Id.IsNull() && !ja.Id.IsUnknown() {
394-
id := ja.Id.ValueString()
395-
jobAgentId = &id
396-
}
397-
jobAgentConfig = deploymentJobAgentConfigFromModel(ja)
398-
}
399-
400365
requestBody := api.UpsertDeploymentRequest{
401366
Name: data.Name.ValueString(),
402367
Slug: slug.Make(data.Name.ValueString()),
403368
Metadata: stringMapPointer(data.Metadata),
404369
ResourceSelector: selector,
405-
JobAgentId: jobAgentId,
406-
JobAgentConfig: jobAgentConfig,
370+
JobAgents: deploymentJobAgentsFromModel(data.JobAgent),
407371
}
408372

409373
deployResp, err := r.workspace.Client.RequestDeploymentUpsertWithResponse(ctx, r.workspace.ID.String(), data.ID.ValueString(), requestBody)
@@ -502,6 +466,57 @@ type DeploymentJobAgentTestRunnerModel struct {
502466
Status types.String `tfsdk:"status"`
503467
}
504468

469+
func deploymentJobAgentsFromModel(agents []DeploymentJobAgentModel) *[]api.DeploymentJobAgent {
470+
if len(agents) == 0 {
471+
return nil
472+
}
473+
result := make([]api.DeploymentJobAgent, 0, len(agents))
474+
for _, ja := range agents {
475+
config := api.JobAgentConfig{}
476+
if cfgPtr := deploymentJobAgentConfigFromModel(ja); cfgPtr != nil {
477+
config = api.JobAgentConfig(*cfgPtr)
478+
}
479+
480+
selector := ""
481+
if !ja.Selector.IsNull() && !ja.Selector.IsUnknown() {
482+
selector = ja.Selector.ValueString()
483+
}
484+
485+
result = append(result, api.DeploymentJobAgent{
486+
Ref: ja.Id.ValueString(),
487+
Config: config,
488+
Selector: selector,
489+
})
490+
}
491+
return &result
492+
}
493+
494+
func deploymentJobAgentModelsFromAPI(agents []api.DeploymentJobAgent) []DeploymentJobAgentModel {
495+
if len(agents) == 0 {
496+
return nil
497+
}
498+
result := make([]DeploymentJobAgentModel, 0, len(agents))
499+
for _, agent := range agents {
500+
model := DeploymentJobAgentModel{
501+
Id: types.StringValue(agent.Ref),
502+
Priority: types.Int64Null(),
503+
Selector: types.StringNull(),
504+
ArgoCD: nil,
505+
GitHub: nil,
506+
TerraformCloud: nil,
507+
TestRunner: nil,
508+
}
509+
if agent.Selector != "" {
510+
model.Selector = types.StringValue(agent.Selector)
511+
}
512+
if len(agent.Config) > 0 {
513+
setDeploymentJobAgentBlocksFromConfig(&model, agent.Config)
514+
}
515+
result = append(result, model)
516+
}
517+
return result
518+
}
519+
505520
func countDeploymentJobAgentBlocks(ja DeploymentJobAgentModel) int {
506521
count := 0
507522
if ja.ArgoCD != nil {

0 commit comments

Comments
 (0)