Skip to content

Commit 08e750c

Browse files
chore: add drp_machine_set_pool resource
1 parent ce53c6a commit 08e750c

File tree

3 files changed

+150
-26
lines changed

3 files changed

+150
-26
lines changed

drpv4/provider.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ func Provider() *schema.Provider {
2222
return &schema.Provider{
2323

2424
ResourcesMap: map[string]*schema.Resource{
25-
"drp_machine": resourceMachine(),
26-
"drp_param": resourceParam(),
27-
"drp_template": resourceTemplate(),
28-
"drp_task": resourceTask(),
29-
"drp_stage": resourceStage(),
30-
"drp_workflow": resourceWorkflow(),
31-
"drp_subnet": resourceSubnet(),
32-
"drp_reservation": resourceReservation(),
33-
"drp_pool": resourcePool(),
34-
"drp_profile": resourceProfile(),
35-
"drp_profile_param": resourceProfileParam(),
25+
"drp_machine": resourceMachine(),
26+
"drp_machine_set_pool": resourceMachinePool(),
27+
"drp_param": resourceParam(),
28+
"drp_template": resourceTemplate(),
29+
"drp_task": resourceTask(),
30+
"drp_stage": resourceStage(),
31+
"drp_workflow": resourceWorkflow(),
32+
"drp_subnet": resourceSubnet(),
33+
"drp_reservation": resourceReservation(),
34+
"drp_pool": resourcePool(),
35+
"drp_profile": resourceProfile(),
36+
"drp_profile_param": resourceProfileParam(),
3637
},
3738

3839
// note yet, but potentially pools, params and profiles

drpv4/resource_drp_machine.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

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

158-
requuid := cc.session.Req().Get().UrlFor("machines", "Address=", d.Get("address").(string))
159-
mruuid := []*models.Machine{}
160-
if err := requuid.Do(&mruuid); err != nil {
161-
log.Printf("[DEBUG] Get error %+v | %+v", err, requuid)
162-
return fmt.Errorf("error getting machine UUID for address %s: %s", d.Get("address").(string), err)
163-
}
164-
patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: pool}}
165-
reqm := cc.session.Req().Patch(patch).UrlFor("machines", string(mruuid[0].Uuid))
166-
mr := []*models.Machine{}
167-
if err := reqm.Do(&mr); err != nil {
168-
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
169-
return fmt.Errorf("error set pool %s: %s", pool, err)
170-
}
171157
pr := []*models.PoolResult{}
172158
req := cc.session.Req().Post(parms).UrlFor("pools", pool, "allocateMachines")
173159
if err := req.Do(&pr); err != nil {
174160
log.Printf("[DEBUG] POST error %+v | %+v", err, req)
175-
return fmt.Errorf("error allocated from pool %s: %s", pool, err)
161+
return fmt.Errorf("Error allocated from pool %s: %s", pool, err)
176162
}
177163
mc := pr[0]
178164
log.Printf("[DEBUG] Allocated %s machine %s (%s)", mc.Status, mc.Name, mc.Uuid)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package drpv4
2+
3+
/*
4+
* Copyright RackN 2020
5+
*/
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"time"
11+
12+
"github.com/VictorLowther/jsonpatch2"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+
"gitlab.com/rackn/provision/v4/models"
15+
)
16+
17+
func resourceMachinePool() *schema.Resource {
18+
r := &schema.Resource{
19+
Create: resourceMachineSetPool,
20+
Read: resourceMachineGetPool,
21+
Update: resourceMachineSetPoolUpdate,
22+
Delete: resourceMachineSetPoolDelete,
23+
24+
Schema: map[string]*schema.Schema{
25+
26+
"pool": &schema.Schema{
27+
Type: schema.TypeString,
28+
Default: "default",
29+
Description: "Which pool to add machine to",
30+
Optional: true,
31+
},
32+
"machineName": &schema.Schema{
33+
Type: schema.TypeString,
34+
Description: "Machine Name",
35+
ForceNew: true,
36+
},
37+
38+
// Machine.Address
39+
"address": &schema.Schema{
40+
Type: schema.TypeString,
41+
Description: "Returns Digital Rebar Machine.Address",
42+
Computed: true,
43+
},
44+
},
45+
46+
Timeouts: &schema.ResourceTimeout{
47+
Create: schema.DefaultTimeout(25 * time.Minute),
48+
Update: schema.DefaultTimeout(10 * time.Minute),
49+
Delete: schema.DefaultTimeout(10 * time.Minute),
50+
},
51+
}
52+
53+
return r
54+
}
55+
56+
func resourceMachineSetPool(d *schema.ResourceData, m interface{}) error {
57+
log.Println("[DEBUG] [resourceMachineAllocate] Allocating new drp_machine")
58+
cc := m.(*Config)
59+
60+
pool := d.Get("pool").(string)
61+
if pool == "" {
62+
pool = "default"
63+
}
64+
d.Set("pool", pool)
65+
name := d.Get("machineName").(string)
66+
67+
requuid := cc.session.Req().Get().UrlFor("machines", "Name=", name)
68+
mruuid := []*models.Machine{}
69+
if err := requuid.Do(&mruuid); err != nil {
70+
log.Printf("[DEBUG] Get error %+v | %+v", err, requuid)
71+
return fmt.Errorf("error getting machine UUID for address %s: %s", name, err)
72+
}
73+
patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: pool}}
74+
reqm := cc.session.Req().Patch(patch).UrlFor("machines", string(mruuid[0].Uuid))
75+
mr := []*models.Machine{}
76+
if err := reqm.Do(&mr); err != nil {
77+
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
78+
return fmt.Errorf("error set pool %s: %s", pool, err)
79+
}
80+
81+
d.Set("address", mr[0].Address)
82+
d.SetId(string(mr[0].Uuid))
83+
return resourceMachineGetPool(d, m)
84+
}
85+
86+
func resourceMachineGetPool(d *schema.ResourceData, m interface{}) error {
87+
log.Println("[DEBUG] [resourceMachineGetPool] Reading drp_machine")
88+
cc := m.(*Config)
89+
uuid := d.Id()
90+
log.Printf("[DEBUG] Reading machine %s", uuid)
91+
mo, err := cc.session.GetModel("machines", uuid)
92+
if err != nil {
93+
log.Printf("[ERROR] [resourceMachineRead] Unable to get machine: %s", uuid)
94+
return fmt.Errorf("Unable to get machine %s", uuid)
95+
}
96+
machineObject := mo.(*models.Machine)
97+
d.Set("address", machineObject.Address.String())
98+
99+
return nil
100+
}
101+
102+
func resourceMachineSetPoolUpdate(d *schema.ResourceData, m interface{}) error {
103+
log.Println("[DEBUG] [resourceMachineUpdate] Updating drp_machine")
104+
cc := m.(*Config)
105+
106+
// at this time there are no updates
107+
log.Printf("[DEBUG] Config %v", cc)
108+
109+
return resourceMachineGetPool(d, m)
110+
}
111+
112+
func resourceMachineSetPoolDelete(d *schema.ResourceData, m interface{}) error {
113+
log.Println("[DEBUG] [resourceMachineRelease] Releasing machine_set_pool")
114+
cc := m.(*Config)
115+
116+
uuid := d.Id()
117+
if uuid == "" {
118+
return fmt.Errorf("Requires Uuid from id")
119+
}
120+
log.Printf("[DEBUG] Releasing %s ", uuid)
121+
122+
patch := jsonpatch2.Patch{{Op: "replace", Path: "/Pool", Value: "default"}}
123+
reqm := cc.session.Req().Patch(patch).UrlFor("machines", uuid)
124+
mr := []*models.Machine{}
125+
if err := reqm.Do(&mr); err != nil {
126+
log.Printf("[DEBUG] POST error %+v | %+v", err, reqm)
127+
return fmt.Errorf("error set pool %s: %s", "default", err)
128+
}
129+
130+
mc := mr[0]
131+
if mc.Pool == "default" {
132+
d.SetId("")
133+
return nil
134+
} else {
135+
return fmt.Errorf("Could not set default pool for %s", uuid)
136+
}
137+
}

0 commit comments

Comments
 (0)