github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/profitbricks/resource_profitbricks_lan.go (about) 1 package profitbricks 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/terraform/helper/schema" 6 "github.com/profitbricks/profitbricks-sdk-go" 7 "log" 8 "time" 9 ) 10 11 func resourceProfitBricksLan() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceProfitBricksLanCreate, 14 Read: resourceProfitBricksLanRead, 15 Update: resourceProfitBricksLanUpdate, 16 Delete: resourceProfitBricksLanDelete, 17 Schema: map[string]*schema.Schema{ 18 19 "public": { 20 Type: schema.TypeBool, 21 Required: true, 22 }, 23 "name": { 24 Type: schema.TypeString, 25 Optional: true, 26 }, 27 "datacenter_id": { 28 Type: schema.TypeString, 29 Required: true, 30 }, 31 }, 32 } 33 } 34 35 func resourceProfitBricksLanCreate(d *schema.ResourceData, meta interface{}) error { 36 request := profitbricks.Lan{ 37 Properties: profitbricks.LanProperties{ 38 Public: d.Get("public").(bool), 39 }, 40 } 41 42 log.Printf("[DEBUG] NAME %s", d.Get("name")) 43 if d.Get("name") != nil { 44 request.Properties.Name = d.Get("name").(string) 45 } 46 47 lan := profitbricks.CreateLan(d.Get("datacenter_id").(string), request) 48 49 log.Printf("[DEBUG] LAN ID: %s", lan.Id) 50 log.Printf("[DEBUG] LAN RESPONSE: %s", lan.Response) 51 52 if lan.StatusCode > 299 { 53 return fmt.Errorf("An error occured while creating a lan: %s", lan.Response) 54 } 55 56 err := waitTillProvisioned(meta, lan.Headers.Get("Location")) 57 if err != nil { 58 return err 59 } 60 d.SetId(lan.Id) 61 return resourceProfitBricksLanRead(d, meta) 62 } 63 64 func resourceProfitBricksLanRead(d *schema.ResourceData, meta interface{}) error { 65 lan := profitbricks.GetLan(d.Get("datacenter_id").(string), d.Id()) 66 67 if lan.StatusCode > 299 { 68 if lan.StatusCode == 404 { 69 d.SetId("") 70 return nil 71 } 72 return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lan.Response) 73 } 74 75 d.Set("public", lan.Properties.Public) 76 d.Set("name", lan.Properties.Name) 77 d.Set("datacenter_id", d.Get("datacenter_id").(string)) 78 return nil 79 } 80 81 func resourceProfitBricksLanUpdate(d *schema.ResourceData, meta interface{}) error { 82 properties := &profitbricks.LanProperties{} 83 if d.HasChange("public") { 84 _, newValue := d.GetChange("public") 85 properties.Public = newValue.(bool) 86 } 87 if d.HasChange("name") { 88 _, newValue := d.GetChange("name") 89 properties.Name = newValue.(string) 90 } 91 log.Printf("[DEBUG] LAN UPDATE: %s : %s", properties, d.Get("name")) 92 if properties != nil { 93 lan := profitbricks.PatchLan(d.Get("datacenter_id").(string), d.Id(), *properties) 94 if lan.StatusCode > 299 { 95 return fmt.Errorf("An error occured while patching a lan ID %s %s", d.Id(), lan.Response) 96 } 97 err := waitTillProvisioned(meta, lan.Headers.Get("Location")) 98 if err != nil { 99 return err 100 } 101 } 102 return resourceProfitBricksLanRead(d, meta) 103 } 104 105 func resourceProfitBricksLanDelete(d *schema.ResourceData, meta interface{}) error { 106 resp := profitbricks.DeleteLan(d.Get("datacenter_id").(string), d.Id()) 107 if resp.StatusCode > 299 { 108 //try again in 20 seconds 109 time.Sleep(60 * time.Second) 110 resp = profitbricks.DeleteLan(d.Get("datacenter_id").(string), d.Id()) 111 if resp.StatusCode > 299 && resp.StatusCode != 404 { 112 return fmt.Errorf("An error occured while deleting a lan dcId %s ID %s %s", d.Get("datacenter_id").(string), d.Id(), string(resp.Body)) 113 } 114 } 115 116 if resp.Headers.Get("Location") != "" { 117 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 118 if err != nil { 119 return err 120 } 121 } 122 d.SetId("") 123 return nil 124 }