|
| 1 | +package rancher2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + norman "github.com/rancher/norman/types" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceRancher2MachineConfigV2() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceRancher2MachineConfigV2Create, |
| 16 | + Read: resourceRancher2MachineConfigV2Read, |
| 17 | + Update: resourceRancher2MachineConfigV2Update, |
| 18 | + Delete: resourceRancher2MachineConfigV2Delete, |
| 19 | + Schema: machineConfigV2Fields(), |
| 20 | + Timeouts: &schema.ResourceTimeout{ |
| 21 | + Create: schema.DefaultTimeout(10 * time.Minute), |
| 22 | + Update: schema.DefaultTimeout(10 * time.Minute), |
| 23 | + Delete: schema.DefaultTimeout(10 * time.Minute), |
| 24 | + }, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func resourceRancher2MachineConfigV2Create(d *schema.ResourceData, meta interface{}) error { |
| 29 | + name := d.Get("name").(string) |
| 30 | + obj := expandMachineConfigV2(d) |
| 31 | + |
| 32 | + log.Printf("[INFO] Creating Machine Config V2 %s kind %s", name, obj.TypeMeta.Kind) |
| 33 | + |
| 34 | + newObj, err := createMachineConfigV2(meta.(*Config), obj) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + d.SetId(newObj.ID) |
| 40 | + d.Set("kind", newObj.TypeMeta.Kind) |
| 41 | + stateConf := &resource.StateChangeConf{ |
| 42 | + Pending: []string{}, |
| 43 | + Target: []string{"active"}, |
| 44 | + Refresh: machineConfigV2StateRefreshFunc(meta, newObj.ID, newObj.TypeMeta.Kind), |
| 45 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 46 | + Delay: 1 * time.Second, |
| 47 | + MinTimeout: 3 * time.Second, |
| 48 | + } |
| 49 | + _, waitErr := stateConf.WaitForState() |
| 50 | + if waitErr != nil { |
| 51 | + return fmt.Errorf("[ERROR] waiting for machine config (%s) to be active: %s", newObj.ID, waitErr) |
| 52 | + } |
| 53 | + |
| 54 | + return resourceRancher2MachineConfigV2Read(d, meta) |
| 55 | +} |
| 56 | + |
| 57 | +func resourceRancher2MachineConfigV2Read(d *schema.ResourceData, meta interface{}) error { |
| 58 | + log.Printf("[INFO] Refreshing Machine Config V2 %s", d.Id()) |
| 59 | + |
| 60 | + kind := d.Get("kind").(string) |
| 61 | + obj, err := getMachineConfigV2ByID(meta.(*Config), d.Id(), kind) |
| 62 | + if err != nil { |
| 63 | + if IsNotFound(err) || IsForbidden(err) { |
| 64 | + log.Printf("[INFO] Machine Config V2 %s not found", d.Id()) |
| 65 | + d.SetId("") |
| 66 | + return nil |
| 67 | + } |
| 68 | + return err |
| 69 | + } |
| 70 | + return flattenMachineConfigV2(d, obj) |
| 71 | +} |
| 72 | + |
| 73 | +func resourceRancher2MachineConfigV2Update(d *schema.ResourceData, meta interface{}) error { |
| 74 | + obj := expandMachineConfigV2(d) |
| 75 | + log.Printf("[INFO] Updating Machine Config V2 %s", d.Id()) |
| 76 | + |
| 77 | + newObj, err := updateMachineConfigV2(meta.(*Config), obj) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + d.SetId(newObj.ID) |
| 82 | + stateConf := &resource.StateChangeConf{ |
| 83 | + Pending: []string{}, |
| 84 | + Target: []string{"active"}, |
| 85 | + Refresh: machineConfigV2StateRefreshFunc(meta, newObj.ID, newObj.TypeMeta.Kind), |
| 86 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 87 | + Delay: 1 * time.Second, |
| 88 | + MinTimeout: 3 * time.Second, |
| 89 | + } |
| 90 | + _, waitErr := stateConf.WaitForState() |
| 91 | + if waitErr != nil { |
| 92 | + return fmt.Errorf("[ERROR] waiting for machine config (%s) to be active: %s", newObj.ID, waitErr) |
| 93 | + } |
| 94 | + return resourceRancher2MachineConfigV2Read(d, meta) |
| 95 | +} |
| 96 | + |
| 97 | +func resourceRancher2MachineConfigV2Delete(d *schema.ResourceData, meta interface{}) error { |
| 98 | + name := d.Get("name").(string) |
| 99 | + kind := d.Get("kind").(string) |
| 100 | + log.Printf("[INFO] Deleting Machine Config V2 %s", name) |
| 101 | + |
| 102 | + obj, err := getMachineConfigV2ByID(meta.(*Config), d.Id(), kind) |
| 103 | + if err != nil { |
| 104 | + if IsNotFound(err) || IsForbidden(err) { |
| 105 | + d.SetId("") |
| 106 | + return nil |
| 107 | + } |
| 108 | + } |
| 109 | + err = deleteMachineConfigV2(meta.(*Config), obj) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + stateConf := &resource.StateChangeConf{ |
| 114 | + Pending: []string{}, |
| 115 | + Target: []string{"removed"}, |
| 116 | + Refresh: machineConfigV2StateRefreshFunc(meta, obj.ID, obj.TypeMeta.Kind), |
| 117 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 118 | + Delay: 1 * time.Second, |
| 119 | + MinTimeout: 3 * time.Second, |
| 120 | + } |
| 121 | + _, waitErr := stateConf.WaitForState() |
| 122 | + if waitErr != nil { |
| 123 | + return fmt.Errorf("[ERROR] waiting for machine config v2 (%s) to be removed: %s", obj.ID, waitErr) |
| 124 | + } |
| 125 | + d.SetId("") |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// machineConfigV2StateRefreshFunc returns a resource.StateRefreshFunc, used to watch a Rancher Machine Config v2. |
| 130 | +func machineConfigV2StateRefreshFunc(meta interface{}, objID, kind string) resource.StateRefreshFunc { |
| 131 | + return func() (interface{}, string, error) { |
| 132 | + obj, err := getMachineConfigV2ByID(meta.(*Config), objID, kind) |
| 133 | + if err != nil { |
| 134 | + if IsNotFound(err) || IsForbidden(err) { |
| 135 | + return obj, "removed", nil |
| 136 | + } |
| 137 | + return nil, "", err |
| 138 | + } |
| 139 | + return obj, "active", nil |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Rancher2 Machine Config V2 API CRUD functions |
| 144 | +func createMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, error) { |
| 145 | + if c == nil { |
| 146 | + return nil, fmt.Errorf("Creating Machine Config V2: Provider config is nil") |
| 147 | + } |
| 148 | + if obj == nil { |
| 149 | + return nil, fmt.Errorf("Creating Machine Config V2: Machine Config V2 is nil") |
| 150 | + } |
| 151 | + var err error |
| 152 | + out := &MachineConfigV2{} |
| 153 | + kind := obj.TypeMeta.Kind |
| 154 | + switch kind { |
| 155 | + case machineConfigV2Amazonec2Kind: |
| 156 | + resp := &MachineConfigV2Amazonec2{} |
| 157 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2Amazonec2APIType, obj.Amazonec2Config, resp) |
| 158 | + out.Amazonec2Config = resp |
| 159 | + out.ID = resp.ID |
| 160 | + out.TypeMeta = resp.TypeMeta |
| 161 | + out.ObjectMeta = resp.ObjectMeta |
| 162 | + case machineConfigV2AzureKind: |
| 163 | + resp := &MachineConfigV2Azure{} |
| 164 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2AzureAPIType, obj.AzureConfig, resp) |
| 165 | + out.AzureConfig = resp |
| 166 | + out.ID = resp.ID |
| 167 | + out.TypeMeta = resp.TypeMeta |
| 168 | + out.ObjectMeta = resp.ObjectMeta |
| 169 | + case machineConfigV2DigitaloceanKind: |
| 170 | + resp := &MachineConfigV2Digitalocean{} |
| 171 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2DigitaloceanAPIType, obj.DigitaloceanConfig, resp) |
| 172 | + out.DigitaloceanConfig = resp |
| 173 | + out.ID = resp.ID |
| 174 | + out.TypeMeta = resp.TypeMeta |
| 175 | + out.ObjectMeta = resp.ObjectMeta |
| 176 | + case machineConfigV2LinodeKind: |
| 177 | + resp := &MachineConfigV2Linode{} |
| 178 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2LinodeAPIType, obj.LinodeConfig, resp) |
| 179 | + out.LinodeConfig = resp |
| 180 | + out.ID = resp.ID |
| 181 | + out.TypeMeta = resp.TypeMeta |
| 182 | + out.ObjectMeta = resp.ObjectMeta |
| 183 | + case machineConfigV2OpenstackKind: |
| 184 | + resp := &MachineConfigV2Openstack{} |
| 185 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2OpenstackAPIType, obj.OpenstackConfig, resp) |
| 186 | + out.OpenstackConfig = resp |
| 187 | + out.ID = resp.ID |
| 188 | + out.TypeMeta = resp.TypeMeta |
| 189 | + out.ObjectMeta = resp.ObjectMeta |
| 190 | + case machineConfigV2VmwarevsphereKind: |
| 191 | + resp := &MachineConfigV2Vmwarevsphere{} |
| 192 | + err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2VmwarevsphereAPIType, obj.VmwarevsphereConfig, resp) |
| 193 | + out.VmwarevsphereConfig = resp |
| 194 | + out.ID = resp.ID |
| 195 | + out.TypeMeta = resp.TypeMeta |
| 196 | + out.ObjectMeta = resp.ObjectMeta |
| 197 | + default: |
| 198 | + return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind) |
| 199 | + } |
| 200 | + if err != nil { |
| 201 | + return nil, fmt.Errorf("Creating Machine Config V2: %s", err) |
| 202 | + } |
| 203 | + return out, nil |
| 204 | +} |
| 205 | + |
| 206 | +func deleteMachineConfigV2(c *Config, obj *MachineConfigV2) error { |
| 207 | + if c == nil { |
| 208 | + return fmt.Errorf("Deleting Machine Config V2: Provider config is nil") |
| 209 | + } |
| 210 | + if obj == nil { |
| 211 | + return fmt.Errorf("Deleting Machine Config V2: Machine Config V2 is nil") |
| 212 | + } |
| 213 | + resource := &norman.Resource{ |
| 214 | + ID: obj.ID, |
| 215 | + Links: obj.Links, |
| 216 | + Type: obj.Type, |
| 217 | + Actions: obj.Actions, |
| 218 | + } |
| 219 | + return c.deleteObjectV2(rancher2DefaultLocalClusterID, resource) |
| 220 | +} |
| 221 | + |
| 222 | +func getMachineConfigV2ByID(c *Config, id, kind string) (*MachineConfigV2, error) { |
| 223 | + if c == nil { |
| 224 | + return nil, fmt.Errorf("Getting Machine Config V2: Provider config is nil") |
| 225 | + } |
| 226 | + if len(id) == 0 { |
| 227 | + return nil, fmt.Errorf("Getting Machine Config V2: Machine Config V2 ID is empty") |
| 228 | + } |
| 229 | + var err error |
| 230 | + out := &MachineConfigV2{} |
| 231 | + switch kind { |
| 232 | + case machineConfigV2Amazonec2Kind: |
| 233 | + resp := &MachineConfigV2Amazonec2{} |
| 234 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2Amazonec2APIType, resp) |
| 235 | + out.Amazonec2Config = resp |
| 236 | + out.ID = resp.ID |
| 237 | + out.Links = resp.Links |
| 238 | + out.Actions = resp.Actions |
| 239 | + out.Type = resp.Type |
| 240 | + out.TypeMeta = resp.TypeMeta |
| 241 | + out.ObjectMeta = resp.ObjectMeta |
| 242 | + case machineConfigV2AzureKind: |
| 243 | + resp := &MachineConfigV2Azure{} |
| 244 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2AzureAPIType, resp) |
| 245 | + out.AzureConfig = resp |
| 246 | + out.ID = resp.ID |
| 247 | + out.Links = resp.Links |
| 248 | + out.Actions = resp.Actions |
| 249 | + out.Type = resp.Type |
| 250 | + out.TypeMeta = resp.TypeMeta |
| 251 | + out.ObjectMeta = resp.ObjectMeta |
| 252 | + case machineConfigV2DigitaloceanKind: |
| 253 | + resp := &MachineConfigV2Digitalocean{} |
| 254 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2DigitaloceanAPIType, resp) |
| 255 | + out.DigitaloceanConfig = resp |
| 256 | + out.ID = resp.ID |
| 257 | + out.Links = resp.Links |
| 258 | + out.Actions = resp.Actions |
| 259 | + out.Type = resp.Type |
| 260 | + out.TypeMeta = resp.TypeMeta |
| 261 | + out.ObjectMeta = resp.ObjectMeta |
| 262 | + case machineConfigV2LinodeKind: |
| 263 | + resp := &MachineConfigV2Linode{} |
| 264 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2LinodeAPIType, resp) |
| 265 | + out.LinodeConfig = resp |
| 266 | + out.ID = resp.ID |
| 267 | + out.Links = resp.Links |
| 268 | + out.Actions = resp.Actions |
| 269 | + out.Type = resp.Type |
| 270 | + out.TypeMeta = resp.TypeMeta |
| 271 | + out.ObjectMeta = resp.ObjectMeta |
| 272 | + case machineConfigV2OpenstackKind: |
| 273 | + resp := &MachineConfigV2Openstack{} |
| 274 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2OpenstackAPIType, resp) |
| 275 | + out.OpenstackConfig = resp |
| 276 | + out.ID = resp.ID |
| 277 | + out.Links = resp.Links |
| 278 | + out.Actions = resp.Actions |
| 279 | + out.Type = resp.Type |
| 280 | + out.TypeMeta = resp.TypeMeta |
| 281 | + out.ObjectMeta = resp.ObjectMeta |
| 282 | + case machineConfigV2VmwarevsphereKind: |
| 283 | + resp := &MachineConfigV2Vmwarevsphere{} |
| 284 | + err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2VmwarevsphereAPIType, resp) |
| 285 | + out.VmwarevsphereConfig = resp |
| 286 | + out.ID = resp.ID |
| 287 | + out.Links = resp.Links |
| 288 | + out.Actions = resp.Actions |
| 289 | + out.Type = resp.Type |
| 290 | + out.TypeMeta = resp.TypeMeta |
| 291 | + out.ObjectMeta = resp.ObjectMeta |
| 292 | + default: |
| 293 | + return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind) |
| 294 | + } |
| 295 | + if err != nil { |
| 296 | + if !IsServerError(err) && !IsNotFound(err) && !IsForbidden(err) { |
| 297 | + return nil, fmt.Errorf("Getting Machine Config V2: %s", err) |
| 298 | + } |
| 299 | + return nil, err |
| 300 | + } |
| 301 | + return out, nil |
| 302 | +} |
| 303 | + |
| 304 | +func updateMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, error) { |
| 305 | + if c == nil { |
| 306 | + return nil, fmt.Errorf("Updating Machine Config V2: Provider config is nil") |
| 307 | + } |
| 308 | + if obj == nil { |
| 309 | + return nil, fmt.Errorf("Updating Machine Config V2: Machine Config V2 is nil") |
| 310 | + } |
| 311 | + var err error |
| 312 | + out := &MachineConfigV2{} |
| 313 | + kind := obj.TypeMeta.Kind |
| 314 | + switch kind { |
| 315 | + case machineConfigV2Amazonec2Kind: |
| 316 | + resp := &MachineConfigV2Amazonec2{} |
| 317 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2Amazonec2APIType, obj.Amazonec2Config, resp) |
| 318 | + out.Amazonec2Config = resp |
| 319 | + out.ID = resp.ID |
| 320 | + out.TypeMeta = resp.TypeMeta |
| 321 | + out.ObjectMeta = resp.ObjectMeta |
| 322 | + case machineConfigV2AzureKind: |
| 323 | + resp := &MachineConfigV2Azure{} |
| 324 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2AzureAPIType, obj.AzureConfig, resp) |
| 325 | + out.AzureConfig = resp |
| 326 | + out.ID = resp.ID |
| 327 | + out.TypeMeta = resp.TypeMeta |
| 328 | + out.ObjectMeta = resp.ObjectMeta |
| 329 | + case machineConfigV2DigitaloceanKind: |
| 330 | + resp := &MachineConfigV2Digitalocean{} |
| 331 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2DigitaloceanAPIType, obj.DigitaloceanConfig, resp) |
| 332 | + out.DigitaloceanConfig = resp |
| 333 | + out.ID = resp.ID |
| 334 | + out.TypeMeta = resp.TypeMeta |
| 335 | + out.ObjectMeta = resp.ObjectMeta |
| 336 | + case machineConfigV2LinodeKind: |
| 337 | + resp := &MachineConfigV2Linode{} |
| 338 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2LinodeAPIType, obj.LinodeConfig, resp) |
| 339 | + out.LinodeConfig = resp |
| 340 | + out.ID = resp.ID |
| 341 | + out.TypeMeta = resp.TypeMeta |
| 342 | + out.ObjectMeta = resp.ObjectMeta |
| 343 | + case machineConfigV2OpenstackKind: |
| 344 | + resp := &MachineConfigV2Openstack{} |
| 345 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2OpenstackAPIType, obj.OpenstackConfig, resp) |
| 346 | + out.OpenstackConfig = resp |
| 347 | + out.ID = resp.ID |
| 348 | + out.TypeMeta = resp.TypeMeta |
| 349 | + out.ObjectMeta = resp.ObjectMeta |
| 350 | + case machineConfigV2VmwarevsphereKind: |
| 351 | + resp := &MachineConfigV2Vmwarevsphere{} |
| 352 | + err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2VmwarevsphereAPIType, obj.VmwarevsphereConfig, resp) |
| 353 | + out.VmwarevsphereConfig = resp |
| 354 | + out.ID = resp.ID |
| 355 | + out.TypeMeta = resp.TypeMeta |
| 356 | + out.ObjectMeta = resp.ObjectMeta |
| 357 | + default: |
| 358 | + return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind) |
| 359 | + } |
| 360 | + if err != nil { |
| 361 | + return nil, fmt.Errorf("Creating Machine Config V2: %s", err) |
| 362 | + } |
| 363 | + return out, err |
| 364 | +} |
0 commit comments