in cloudstack/resource_cloudstack_ipaddress.go [85:143]
func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
if err := verifyIPAddressParams(d); err != nil {
return err
}
// Create a new parameter struct
p := cs.Address.NewAssociateIpAddressParams()
if d.Get("is_portable").(bool) {
p.SetIsportable(true)
}
if networkid, ok := d.GetOk("network_id"); ok {
// Set the networkid
p.SetNetworkid(networkid.(string))
if vpcid, ok := d.GetOk("vpc_id"); ok && vpcid.(string) != "" {
return fmt.Errorf("set only network_id or vpc_id")
}
}
if vpcid, ok := d.GetOk("vpc_id"); ok {
// Set the vpcid
p.SetVpcid(vpcid.(string))
}
if zone, ok := d.GetOk("zone"); ok {
// Retrieve the zone ID
zoneid, e := retrieveID(cs, "zone", zone.(string))
if e != nil {
return e.Error()
}
// Set the zoneid
p.SetZoneid(zoneid)
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Associate a new IP address
r, err := cs.Address.AssociateIpAddress(p)
if err != nil {
return fmt.Errorf("Error associating a new IP address: %s", err)
}
d.SetId(r.Id)
// Set tags if necessary
err = setTags(cs, d, "PublicIpAddress")
if err != nil {
return fmt.Errorf("Error setting tags on the IP address: %s", err)
}
return resourceCloudStackIPAddressRead(d, meta)
}