github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_ipaddress.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func resourceCloudStackIPAddress() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceCloudStackIPAddressCreate, 15 Read: resourceCloudStackIPAddressRead, 16 Delete: resourceCloudStackIPAddressDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "network_id": &schema.Schema{ 20 Type: schema.TypeString, 21 Optional: true, 22 ForceNew: true, 23 }, 24 25 "vpc_id": &schema.Schema{ 26 Type: schema.TypeString, 27 Optional: true, 28 ForceNew: true, 29 }, 30 31 "project": &schema.Schema{ 32 Type: schema.TypeString, 33 Optional: true, 34 Computed: true, 35 ForceNew: true, 36 }, 37 38 "ip_address": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 }, 43 } 44 } 45 46 func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) error { 47 cs := meta.(*cloudstack.CloudStackClient) 48 49 if err := verifyIPAddressParams(d); err != nil { 50 return err 51 } 52 53 // Create a new parameter struct 54 p := cs.Address.NewAssociateIpAddressParams() 55 56 if networkid, ok := d.GetOk("network_id"); ok { 57 // Set the networkid 58 p.SetNetworkid(networkid.(string)) 59 } 60 61 if vpcid, ok := d.GetOk("vpc_id"); ok { 62 // Set the vpcid 63 p.SetVpcid(vpcid.(string)) 64 } 65 66 // If there is a project supplied, we retrieve and set the project id 67 if err := setProjectid(p, cs, d); err != nil { 68 return err 69 } 70 71 // Associate a new IP address 72 r, err := cs.Address.AssociateIpAddress(p) 73 if err != nil { 74 return fmt.Errorf("Error associating a new IP address: %s", err) 75 } 76 77 d.SetId(r.Id) 78 79 return resourceCloudStackIPAddressRead(d, meta) 80 } 81 82 func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error { 83 cs := meta.(*cloudstack.CloudStackClient) 84 85 // Get the IP address details 86 ip, count, err := cs.Address.GetPublicIpAddressByID( 87 d.Id(), 88 cloudstack.WithProject(d.Get("project").(string)), 89 ) 90 if err != nil { 91 if count == 0 { 92 log.Printf( 93 "[DEBUG] IP address with ID %s is no longer associated", d.Id()) 94 d.SetId("") 95 return nil 96 } 97 98 return err 99 } 100 101 // Updated the IP address 102 d.Set("ip_address", ip.Ipaddress) 103 104 if _, ok := d.GetOk("network_id"); ok { 105 d.Set("network_id", ip.Associatednetworkid) 106 } 107 108 if _, ok := d.GetOk("vpc_id"); ok { 109 d.Set("vpc_id", ip.Vpcid) 110 } 111 112 setValueOrID(d, "project", ip.Project, ip.Projectid) 113 114 return nil 115 } 116 117 func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{}) error { 118 cs := meta.(*cloudstack.CloudStackClient) 119 120 // Create a new parameter struct 121 p := cs.Address.NewDisassociateIpAddressParams(d.Id()) 122 123 // Disassociate the IP address 124 if _, err := cs.Address.DisassociateIpAddress(p); err != nil { 125 // This is a very poor way to be told the ID does no longer exist :( 126 if strings.Contains(err.Error(), fmt.Sprintf( 127 "Invalid parameter id value=%s due to incorrect long value format, "+ 128 "or entity does not exist", d.Id())) { 129 return nil 130 } 131 132 return fmt.Errorf("Error disassociating IP address %s: %s", d.Get("name").(string), err) 133 } 134 135 return nil 136 } 137 138 func verifyIPAddressParams(d *schema.ResourceData) error { 139 _, network := d.GetOk("network_id") 140 _, vpc := d.GetOk("vpc_id") 141 142 if (network && vpc) || (!network && !vpc) { 143 return fmt.Errorf( 144 "You must supply a value for either (so not both) the 'network_id' or 'vpc_id' parameter") 145 } 146 147 return nil 148 }