|
| 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