github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/profitbricks/resource_profitbricks_nic.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 "strings" 9 ) 10 11 func resourceProfitBricksNic() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceProfitBricksNicCreate, 14 Read: resourceProfitBricksNicRead, 15 Update: resourceProfitBricksNicUpdate, 16 Delete: resourceProfitBricksNicDelete, 17 Schema: map[string]*schema.Schema{ 18 19 "lan": { 20 Type: schema.TypeInt, 21 Required: true, 22 }, 23 "name": { 24 Type: schema.TypeString, 25 Optional: true, 26 }, 27 "dhcp": { 28 Type: schema.TypeBool, 29 Optional: true, 30 }, 31 "ip": { 32 Type: schema.TypeString, 33 Optional: true, 34 }, 35 "ips": { 36 Type: schema.TypeList, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 Computed: true, 39 }, 40 "firewall_active": { 41 Type: schema.TypeBool, 42 Optional: true, 43 }, 44 "nat": { 45 Type: schema.TypeBool, 46 Optional: true, 47 }, 48 "server_id": { 49 Type: schema.TypeString, 50 Required: true, 51 }, 52 "datacenter_id": { 53 Type: schema.TypeString, 54 Required: true, 55 }, 56 }, 57 } 58 } 59 60 func resourceProfitBricksNicCreate(d *schema.ResourceData, meta interface{}) error { 61 nic := profitbricks.Nic{ 62 Properties: profitbricks.NicProperties{ 63 Lan: d.Get("lan").(int), 64 }, 65 } 66 if _, ok := d.GetOk("name"); ok { 67 nic.Properties.Name = d.Get("name").(string) 68 } 69 if _, ok := d.GetOk("dhcp"); ok { 70 nic.Properties.Dhcp = d.Get("dhcp").(bool) 71 } 72 73 if _, ok := d.GetOk("ip"); ok { 74 raw := d.Get("ip").(string) 75 ips := strings.Split(raw, ",") 76 nic.Properties.Ips = ips 77 } 78 if _, ok := d.GetOk("firewall_active"); ok { 79 raw := d.Get("firewall_active").(bool) 80 nic.Properties.FirewallActive = raw 81 } 82 if _, ok := d.GetOk("nat"); ok { 83 raw := d.Get("nat").(bool) 84 nic.Properties.Nat = raw 85 } 86 87 nic = profitbricks.CreateNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), nic) 88 if nic.StatusCode > 299 { 89 return fmt.Errorf("Error occured while creating a nic: %s", nic.Response) 90 } 91 92 err := waitTillProvisioned(meta, nic.Headers.Get("Location")) 93 if err != nil { 94 return err 95 } 96 resp := profitbricks.RebootServer(d.Get("datacenter_id").(string), d.Get("server_id").(string)) 97 if resp.StatusCode > 299 { 98 return fmt.Errorf("Error occured while creating a nic: %s", string(resp.Body)) 99 100 } 101 err = waitTillProvisioned(meta, resp.Headers.Get("Location")) 102 if err != nil { 103 return err 104 } 105 d.SetId(nic.Id) 106 return resourceProfitBricksNicRead(d, meta) 107 } 108 109 func resourceProfitBricksNicRead(d *schema.ResourceData, meta interface{}) error { 110 nic := profitbricks.GetNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id()) 111 if nic.StatusCode > 299 { 112 if nic.StatusCode == 404 { 113 d.SetId("") 114 return nil 115 } 116 return fmt.Errorf("Error occured while fetching a nic ID %s %s", d.Id(), nic.Response) 117 } 118 log.Printf("[INFO] LAN ON NIC: %d", nic.Properties.Lan) 119 d.Set("dhcp", nic.Properties.Dhcp) 120 d.Set("lan", nic.Properties.Lan) 121 d.Set("name", nic.Properties.Name) 122 d.Set("ips", nic.Properties.Ips) 123 124 return nil 125 } 126 127 func resourceProfitBricksNicUpdate(d *schema.ResourceData, meta interface{}) error { 128 properties := profitbricks.NicProperties{} 129 130 if d.HasChange("name") { 131 _, n := d.GetChange("name") 132 133 properties.Name = n.(string) 134 } 135 if d.HasChange("lan") { 136 _, n := d.GetChange("lan") 137 properties.Lan = n.(int) 138 } 139 if d.HasChange("dhcp") { 140 _, n := d.GetChange("dhcp") 141 properties.Dhcp = n.(bool) 142 } 143 if d.HasChange("ip") { 144 _, raw := d.GetChange("ip") 145 ips := strings.Split(raw.(string), ",") 146 properties.Ips = ips 147 } 148 if d.HasChange("nat") { 149 _, raw := d.GetChange("nat") 150 nat := raw.(bool) 151 properties.Nat = nat 152 } 153 154 nic := profitbricks.PatchNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id(), properties) 155 156 if nic.StatusCode > 299 { 157 return fmt.Errorf("Error occured while updating a nic: %s", nic.Response) 158 } 159 err := waitTillProvisioned(meta, nic.Headers.Get("Location")) 160 if err != nil { 161 return err 162 } 163 return resourceProfitBricksNicRead(d, meta) 164 } 165 166 func resourceProfitBricksNicDelete(d *schema.ResourceData, meta interface{}) error { 167 resp := profitbricks.DeleteNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id()) 168 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 169 if err != nil { 170 return err 171 } 172 d.SetId("") 173 return nil 174 }