in cloudstack/resource_cloudstack_network.go [161:281]
func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
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()
}
// Create a new parameter struct
p := cs.Network.NewCreateNetworkParams(name, networkofferingid, zoneid)
if displaytext, ok := d.GetOk("display_text"); ok {
p.SetDisplaytext(displaytext.(string))
} else {
p.SetDisplaytext(name)
}
// 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.SetId(r.Id)
// Set tags if necessary
if err = setTags(cs, d, "network"); err != nil {
return fmt.Errorf("Error setting tags: %v", err)
}
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
}
return resourceCloudStackNetworkRead(d, meta)
}