Skip to content

Commit

Permalink
chore: add drp_machine_set_pool resource
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirkrumshtein-f3 committed Jan 10, 2025
1 parent ce53c6a commit 08e750c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 26 deletions.
23 changes: 12 additions & 11 deletions drpv4/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ func Provider() *schema.Provider {
return &schema.Provider{

ResourcesMap: map[string]*schema.Resource{
"drp_machine": resourceMachine(),
"drp_param": resourceParam(),
"drp_template": resourceTemplate(),
"drp_task": resourceTask(),
"drp_stage": resourceStage(),
"drp_workflow": resourceWorkflow(),
"drp_subnet": resourceSubnet(),
"drp_reservation": resourceReservation(),
"drp_pool": resourcePool(),
"drp_profile": resourceProfile(),
"drp_profile_param": resourceProfileParam(),
"drp_machine": resourceMachine(),
"drp_machine_set_pool": resourceMachinePool(),
"drp_param": resourceParam(),
"drp_template": resourceTemplate(),
"drp_task": resourceTask(),
"drp_stage": resourceStage(),
"drp_workflow": resourceWorkflow(),
"drp_subnet": resourceSubnet(),
"drp_reservation": resourceReservation(),
"drp_pool": resourcePool(),
"drp_profile": resourceProfile(),
"drp_profile_param": resourceProfileParam(),
},

// note yet, but potentially pools, params and profiles
Expand Down
16 changes: 1 addition & 15 deletions drpv4/resource_drp_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/VictorLowther/jsonpatch2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gitlab.com/rackn/provision/v4/models"
)
Expand Down Expand Up @@ -155,24 +154,11 @@ func resourceMachineAllocate(d *schema.ResourceData, m interface{}) error {
parms["pool/filter"] = filters.([]interface{})
}

requuid := cc.session.Req().Get().UrlFor("machines", "Address=", d.Get("address").(string))
mruuid := []*models.Machine{}
if err := requuid.Do(&mruuid); err != nil {
log.Printf("[DEBUG] Get error %+v | %+v", err, requuid)
return fmt.Errorf("error getting machine UUID for address %s: %s", d.Get("address").(string), err)
}
patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: pool}}
reqm := cc.session.Req().Patch(patch).UrlFor("machines", string(mruuid[0].Uuid))
mr := []*models.Machine{}
if err := reqm.Do(&mr); err != nil {
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
return fmt.Errorf("error set pool %s: %s", pool, err)
}
pr := []*models.PoolResult{}
req := cc.session.Req().Post(parms).UrlFor("pools", pool, "allocateMachines")
if err := req.Do(&pr); err != nil {
log.Printf("[DEBUG] POST error %+v | %+v", err, req)
return fmt.Errorf("error allocated from pool %s: %s", pool, err)
return fmt.Errorf("Error allocated from pool %s: %s", pool, err)
}
mc := pr[0]
log.Printf("[DEBUG] Allocated %s machine %s (%s)", mc.Status, mc.Name, mc.Uuid)
Expand Down
137 changes: 137 additions & 0 deletions drpv4/resource_drp_machine_set_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package drpv4

/*
* Copyright RackN 2020
*/

import (
"fmt"
"log"
"time"

"github.com/VictorLowther/jsonpatch2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gitlab.com/rackn/provision/v4/models"
)

func resourceMachinePool() *schema.Resource {
r := &schema.Resource{
Create: resourceMachineSetPool,
Read: resourceMachineGetPool,
Update: resourceMachineSetPoolUpdate,
Delete: resourceMachineSetPoolDelete,

Schema: map[string]*schema.Schema{

"pool": &schema.Schema{
Type: schema.TypeString,
Default: "default",
Description: "Which pool to add machine to",
Optional: true,
},
"machineName": &schema.Schema{
Type: schema.TypeString,
Description: "Machine Name",
ForceNew: true,
},

// Machine.Address
"address": &schema.Schema{
Type: schema.TypeString,
Description: "Returns Digital Rebar Machine.Address",
Computed: true,
},
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(25 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}

return r
}

func resourceMachineSetPool(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] [resourceMachineAllocate] Allocating new drp_machine")
cc := m.(*Config)

pool := d.Get("pool").(string)
if pool == "" {
pool = "default"
}
d.Set("pool", pool)
name := d.Get("machineName").(string)

requuid := cc.session.Req().Get().UrlFor("machines", "Name=", name)
mruuid := []*models.Machine{}
if err := requuid.Do(&mruuid); err != nil {
log.Printf("[DEBUG] Get error %+v | %+v", err, requuid)
return fmt.Errorf("error getting machine UUID for address %s: %s", name, err)
}
patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: pool}}
reqm := cc.session.Req().Patch(patch).UrlFor("machines", string(mruuid[0].Uuid))
mr := []*models.Machine{}
if err := reqm.Do(&mr); err != nil {
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
return fmt.Errorf("error set pool %s: %s", pool, err)
}

d.Set("address", mr[0].Address)
d.SetId(string(mr[0].Uuid))
return resourceMachineGetPool(d, m)
}

func resourceMachineGetPool(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] [resourceMachineGetPool] Reading drp_machine")
cc := m.(*Config)
uuid := d.Id()
log.Printf("[DEBUG] Reading machine %s", uuid)
mo, err := cc.session.GetModel("machines", uuid)
if err != nil {
log.Printf("[ERROR] [resourceMachineRead] Unable to get machine: %s", uuid)
return fmt.Errorf("Unable to get machine %s", uuid)
}
machineObject := mo.(*models.Machine)
d.Set("address", machineObject.Address.String())

return nil
}

func resourceMachineSetPoolUpdate(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] [resourceMachineUpdate] Updating drp_machine")
cc := m.(*Config)

// at this time there are no updates
log.Printf("[DEBUG] Config %v", cc)

return resourceMachineGetPool(d, m)
}

func resourceMachineSetPoolDelete(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] [resourceMachineRelease] Releasing machine_set_pool")
cc := m.(*Config)

uuid := d.Id()
if uuid == "" {
return fmt.Errorf("Requires Uuid from id")
}
log.Printf("[DEBUG] Releasing %s ", uuid)

patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: "default"}}
reqm := cc.session.Req().Patch(patch).UrlFor("machines", uuid)
mr := []*models.Machine{}
if err := reqm.Do(&mr); err != nil {
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
return fmt.Errorf("error set pool %s: %s", "default", err)
}

mc := mr[0]
if mc.Pool == "default" {
d.SetId("")
return nil
} else {
return fmt.Errorf("Could not set default pool for %s", uuid)
}
}

0 comments on commit 08e750c

Please sign in to comment.