in cloudstack/resource_cloudstack_network.go [161:301]
func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
d.Partial(true)
name := d.Get("name").(string)
// Retrieve the network_offering ID
networkofferingid, e := retrieveID(cs, "network_offering", d.Get("network_offering").(string))
if e != nil {
return e.Error()
}
// Retrieve the zone ID
zoneid, e := retrieveID(cs, "zone", d.Get("zone").(string))
if e != nil {
return e.Error()
}
// Compute/set the display text
displaytext, ok := d.GetOk("display_text")
if !ok {
displaytext = name
}
// Create a new parameter struct
p := cs.Network.NewCreateNetworkParams(displaytext.(string), name, networkofferingid, zoneid)
// Get the network offering to check if it supports specifying IP ranges
no, _, err := cs.NetworkOffering.GetNetworkOfferingByID(networkofferingid)
if err != nil {
return err
}
m, err := parseCIDR(d, no.Specifyipranges)
if err != nil {
return err
}
// Set the needed IP config
p.SetGateway(m["gateway"])
p.SetNetmask(m["netmask"])
// Only set the start IP if we have one
if startip, ok := m["startip"]; ok {
p.SetStartip(startip)
}
// Only set the end IP if we have one
if endip, ok := m["endip"]; ok {
p.SetEndip(endip)
}
// Set the network domain if we have one
if networkDomain, ok := d.GetOk("network_domain"); ok {
p.SetNetworkdomain(networkDomain.(string))
}
if vlan, ok := d.GetOk("vlan"); ok {
p.SetVlan(strconv.Itoa(vlan.(int)))
}
// Check is this network needs to be created in a VPC
if vpcid, ok := d.GetOk("vpc_id"); ok {
// Set the vpc id
p.SetVpcid(vpcid.(string))
// Since we're in a VPC, check if we want to associate an ACL list
if aclid, ok := d.GetOk("acl_id"); ok && aclid.(string) != none {
// Set the acl ID
p.SetAclid(aclid.(string))
}
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// Create the new network
r, err := cs.Network.CreateNetwork(p)
if err != nil {
return fmt.Errorf("Error creating network %s: %s", name, err)
}
d.SetPartial("name")
d.SetPartial("display_text")
d.SetPartial("cidr")
d.SetPartial("gateway")
d.SetPartial("startip")
d.SetPartial("endip")
d.SetPartial("network_domain")
d.SetPartial("network_offering")
d.SetPartial("vlan")
d.SetPartial("vpc_id")
d.SetPartial("acl_id")
d.SetPartial("project")
d.SetPartial("zone")
d.SetId(r.Id)
// Set tags if necessary
if err = setTags(cs, d, "network"); err != nil {
return fmt.Errorf("Error setting tags: %v", err)
}
d.SetPartial("tags")
if d.Get("source_nat_ip").(bool) {
// Create a new parameter struct
p := cs.Address.NewAssociateIpAddressParams()
// Set required options
p.SetNetworkid(r.Id)
p.SetZoneid(zoneid)
if vpcid, ok := d.GetOk("vpc_id"); ok {
// Set the vpcid
p.SetVpcid(vpcid.(string))
}
// 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
ip, err := cs.Address.AssociateIpAddress(p)
if err != nil {
return fmt.Errorf("Error associating a new IP address: %s", err)
}
d.Set("source_nat_ip_address", ip.Ipaddress)
d.Set("source_nat_ip_id", ip.Id)
// Set the additional partial
d.SetPartial("source_nat_ip")
d.SetPartial("source_nat_ip_address")
d.SetPartial("source_nat_ip_id")
}
d.Partial(false)
return resourceCloudStackNetworkRead(d, meta)
}