in cloudstack/resource_cloudstack_instance.go [223:420]
func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Retrieve the service_offering ID
serviceofferingid, e := retrieveID(cs, "service_offering", d.Get("service_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()
}
// Retrieve the zone object
zone, _, err := cs.Zone.GetZoneByID(zoneid)
if err != nil {
return err
}
// Retrieve the template ID
templateid, e := retrieveTemplateID(cs, zone.Id, d.Get("template").(string))
if e != nil {
return e.Error()
}
// Create a new parameter struct
p := cs.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zone.Id)
p.SetStartvm(d.Get("start_vm").(bool))
vmDetails := make(map[string]string)
if details, ok := d.GetOk("details"); ok {
for k, v := range details.(map[string]interface{}) {
vmDetails[k] = v.(string)
}
p.SetDetails(vmDetails)
}
// Set VM Properties
vmProperties := make(map[string]string)
if properties, ok := d.GetOk("properties"); ok {
for k, v := range properties.(map[string]interface{}) {
vmProperties[k] = v.(string)
}
p.SetProperties(vmProperties)
}
// SetNicNetworkList
if nicnetworklist, ok := d.GetOk("nicnetworklist"); ok {
nicNetworkDetails := []map[string]string{
{
"nic": nicnetworklist.(map[string]interface{})["nic"].(string),
"network": nicnetworklist.(map[string]interface{})["network"].(string),
},
}
p.SetNicnetworklist(nicNetworkDetails)
}
// Set the name
name, hasName := d.GetOk("name")
if hasName {
p.SetName(name.(string))
}
// Set the display name
if displayname, ok := d.GetOk("display_name"); ok {
p.SetDisplayname(displayname.(string))
} else if hasName {
p.SetDisplayname(name.(string))
}
// If there is a root_disk_size supplied, add it to the parameter struct
if rootdisksize, ok := d.GetOk("root_disk_size"); ok {
p.SetRootdisksize(int64(rootdisksize.(int)))
}
if d.Get("uefi").(bool) {
p.SetBoottype("UEFI")
p.SetBootmode("Legacy")
}
if zone.Networktype == "Advanced" {
// Set the default network ID
p.SetNetworkids([]string{d.Get("network_id").(string)})
}
// If there is a ipaddres supplied, add it to the parameter struct
if ipaddress, ok := d.GetOk("ip_address"); ok {
p.SetIpaddress(ipaddress.(string))
}
// If there is a group supplied, add it to the parameter struct
if group, ok := d.GetOk("group"); ok {
p.SetGroup(group.(string))
}
// If there are affinity group IDs supplied, add them to the parameter struct
if agIDs := d.Get("affinity_group_ids").(*schema.Set); agIDs.Len() > 0 {
var groups []string
for _, group := range agIDs.List() {
groups = append(groups, group.(string))
}
p.SetAffinitygroupids(groups)
}
// If there are affinity group names supplied, add them to the parameter struct
if agNames := d.Get("affinity_group_names").(*schema.Set); agNames.Len() > 0 {
var groups []string
for _, group := range agNames.List() {
groups = append(groups, group.(string))
}
p.SetAffinitygroupnames(groups)
}
// If there are security group IDs supplied, add them to the parameter struct
if sgIDs := d.Get("security_group_ids").(*schema.Set); sgIDs.Len() > 0 {
var groups []string
for _, group := range sgIDs.List() {
groups = append(groups, group.(string))
}
p.SetSecuritygroupids(groups)
}
// If there are security group names supplied, add them to the parameter struct
if sgNames := d.Get("security_group_names").(*schema.Set); sgNames.Len() > 0 {
var groups []string
for _, group := range sgNames.List() {
groups = append(groups, group.(string))
}
p.SetSecuritygroupnames(groups)
}
// If there is a project supplied, we retrieve and set the project id
if err := setProjectid(p, cs, d); err != nil {
return err
}
// If a keypair is supplied, add it to the parameter struct
if keypair, ok := d.GetOk("keypair"); ok {
p.SetKeypair(keypair.(string))
}
if keypairs, ok := d.GetOk("keypairs"); ok {
var keypairStrings []string
for _, kp := range keypairs.([]interface{}) {
keypairStrings = append(keypairStrings, fmt.Sprintf("%v", kp))
}
p.SetKeypairs(keypairStrings)
}
// If a host_id is supplied, add it to the parameter struct
if hostid, ok := d.GetOk("host_id"); ok {
p.SetHostid(hostid.(string))
}
// If a pod_id is supplied, add it to the parameter struct
if podid, ok := d.GetOk("pod_id"); ok {
p.SetPodid(podid.(string))
}
// If a cluster_id is supplied, add it to the parameter struct
if clusterid, ok := d.GetOk("cluster_id"); ok {
p.SetClusterid(clusterid.(string))
}
if userData, ok := d.GetOk("user_data"); ok {
ud, err := getUserData(userData.(string))
if err != nil {
return err
}
p.SetUserdata(ud)
}
// Create the new instance
r, err := cs.VirtualMachine.DeployVirtualMachine(p)
if err != nil {
return fmt.Errorf("Error creating the new instance %s: %s", name, err)
}
d.SetId(r.Id)
// Set tags if necessary
if err = setTags(cs, d, "userVm"); err != nil {
return fmt.Errorf("Error setting tags on the new instance %s: %s", name, err)
}
// Set the connection info for any configured provisioners
d.SetConnInfo(map[string]string{
"host": r.Nic[0].Ipaddress,
"password": r.Password,
})
return resourceCloudStackInstanceRead(d, meta)
}