github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 return fmt.Errorf("Error occured while fetching a nic ID %s %s", d.Id(), nic.Response) 113 } 114 log.Printf("[INFO] LAN ON NIC: %d", nic.Properties.Lan) 115 d.Set("dhcp", nic.Properties.Dhcp) 116 d.Set("lan", nic.Properties.Lan) 117 d.Set("name", nic.Properties.Name) 118 d.Set("ips", nic.Properties.Ips) 119 120 return nil 121 } 122 123 func resourceProfitBricksNicUpdate(d *schema.ResourceData, meta interface{}) error { 124 properties := profitbricks.NicProperties{} 125 126 if d.HasChange("name") { 127 _, n := d.GetChange("name") 128 129 properties.Name = n.(string) 130 } 131 if d.HasChange("lan") { 132 _, n := d.GetChange("lan") 133 properties.Lan = n.(int) 134 } 135 if d.HasChange("dhcp") { 136 _, n := d.GetChange("dhcp") 137 properties.Dhcp = n.(bool) 138 } 139 if d.HasChange("ip") { 140 _, raw := d.GetChange("ip") 141 ips := strings.Split(raw.(string), ",") 142 properties.Ips = ips 143 } 144 if d.HasChange("nat") { 145 _, raw := d.GetChange("nat") 146 nat := raw.(bool) 147 properties.Nat = nat 148 } 149 150 nic := profitbricks.PatchNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id(), properties) 151 152 if nic.StatusCode > 299 { 153 return fmt.Errorf("Error occured while updating a nic: %s", nic.Response) 154 } 155 err := waitTillProvisioned(meta, nic.Headers.Get("Location")) 156 if err != nil { 157 return err 158 } 159 return resourceProfitBricksNicRead(d, meta) 160 } 161 162 func resourceProfitBricksNicDelete(d *schema.ResourceData, meta interface{}) error { 163 resp := profitbricks.DeleteNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id()) 164 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 165 if err != nil { 166 return err 167 } 168 d.SetId("") 169 return nil 170 }