Skip to content

Commit

Permalink
Merge branch 'dealloc-pieces' into 'master'
Browse files Browse the repository at this point in the history
feat(provider): Add deallocate parameter and profile options

Closes product-backlog#396

See merge request rackn/terraform-provider-drp!104
  • Loading branch information
galthaus committed May 25, 2023
2 parents cbec2ad + 7c53490 commit 865d50a
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions drpv4/resource_drp_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ type MachineResource struct {
type MachineResourceModel struct {
Id types.String `tfsdk:"id"`

Pool types.String `tfsdk:"pool"`
AllocateWorkflow types.String `tfsdk:"allocate_workflow"`
DeallocateWorkflow types.String `tfsdk:"deallocate_workflow"`
Timeout types.String `tfsdk:"timeout"`
AddProfiles types.List `tfsdk:"add_profiles"`
AddParameters types.List `tfsdk:"add_parameters"`
Filters types.List `tfsdk:"filters"`
AuthorizedKeys types.List `tfsdk:"authorized_keys"`
Pool types.String `tfsdk:"pool"`
AllocateWorkflow types.String `tfsdk:"allocate_workflow"`
DeallocateWorkflow types.String `tfsdk:"deallocate_workflow"`
Timeout types.String `tfsdk:"timeout"`
AddProfiles types.List `tfsdk:"add_profiles"`
AddParameters types.List `tfsdk:"add_parameters"`
Filters types.List `tfsdk:"filters"`
AuthorizedKeys types.List `tfsdk:"authorized_keys"`
DeallocateProfiles types.List `tfsdk:"deallocate_profiles"`
DeallocateParameters types.List `tfsdk:"deallocate_parameters"`

Address types.String `tfsdk:"address"`
Name types.String `tfsdk:"name"`
Expand Down Expand Up @@ -116,6 +118,22 @@ func (r *MachineResource) Schema(ctx context.Context, req resource.SchemaRequest
listplanmodifier.RequiresReplace(),
},
},
"deallocate_profiles": schema.ListAttribute{
ElementType: types.StringType,
MarkdownDescription: "List of profiles to add to the machine when deallocating.",
Optional: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
},
"deallocate_parameters": schema.ListAttribute{
ElementType: types.StringType,
MarkdownDescription: "List of parameters to add to the machine when deallocating.",
Optional: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
},
"filters": schema.ListAttribute{
ElementType: types.StringType,
MarkdownDescription: "List of filters to restrict the search for a machie (usee Digital Rebar format e.g. FilterVar=Fn(value))",
Expand Down Expand Up @@ -434,6 +452,37 @@ func (r *MachineResource) Delete(ctx context.Context, req resource.DeleteRequest
parms["pool/remove-parameters"] = parameters
}

profiles = []string{}
diags = plan.DeallocateProfiles.ElementsAs(ctx, &profiles, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if len(profiles) > 0 {
parms["pool/add-profiles"] = profiles
}

params := map[string]interface{}{}
aparams = []string{}
diags = plan.DeallocateParameters.ElementsAs(ctx, &aparams, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for _, p := range aparams {
param := strings.Split(p, ":")
if len(param) < 2 {
resp.Diagnostics.AddError("deallocate_parameter format not correct", p)
return
}
key := param[0]
value := strings.TrimLeft(param[1], " ")
params[key] = value
}
if len(params) > 0 {
parms["pool/add-parameters"] = params
}

pr := []*models.PoolResult{}
creq := r.session.Req().Post(parms).UrlFor("pools", pool, "releaseMachines")
if err := creq.Do(&pr); err != nil {
Expand Down

0 comments on commit 865d50a

Please sign in to comment.