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