Merge pull request #5587 from hashicorp/b-cloudstack-instance-name
Make name optional on cloudstack_instance resource
This commit is contained in:
commit
3a44fc7b3f
|
@ -22,8 +22,8 @@ func resourceCloudStackInstance() *schema.Resource {
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"name": &schema.Schema{
|
"name": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
Computed: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"display_name": &schema.Schema{
|
"display_name": &schema.Schema{
|
||||||
|
@ -128,14 +128,16 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
|
||||||
p := cs.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zone.Id)
|
p := cs.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zone.Id)
|
||||||
|
|
||||||
// Set the name
|
// Set the name
|
||||||
name := d.Get("name").(string)
|
name, hasName := d.GetOk("name")
|
||||||
p.SetName(name)
|
if hasName {
|
||||||
|
p.SetName(name.(string))
|
||||||
|
}
|
||||||
|
|
||||||
// Set the display name
|
// Set the display name
|
||||||
if displayname, ok := d.GetOk("display_name"); ok {
|
if displayname, ok := d.GetOk("display_name"); ok {
|
||||||
p.SetDisplayname(displayname.(string))
|
p.SetDisplayname(displayname.(string))
|
||||||
} else {
|
} else if hasName {
|
||||||
p.SetDisplayname(name)
|
p.SetDisplayname(name.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
if zone.Networktype == "Advanced" {
|
if zone.Networktype == "Advanced" {
|
||||||
|
@ -265,7 +267,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attributes that require reboot to update
|
// Attributes that require reboot to update
|
||||||
if d.HasChange("service_offering") || d.HasChange("keypair") {
|
if d.HasChange("name") || d.HasChange("service_offering") || d.HasChange("keypair") {
|
||||||
// Before we can actually make these changes, the virtual machine must be stopped
|
// Before we can actually make these changes, the virtual machine must be stopped
|
||||||
_, err := cs.VirtualMachine.StopVirtualMachine(
|
_, err := cs.VirtualMachine.StopVirtualMachine(
|
||||||
cs.VirtualMachine.NewStopVirtualMachineParams(d.Id()))
|
cs.VirtualMachine.NewStopVirtualMachineParams(d.Id()))
|
||||||
|
@ -274,6 +276,26 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
|
||||||
"Error stopping instance %s before making changes: %s", name, err)
|
"Error stopping instance %s before making changes: %s", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the name has changed and if so, update the name
|
||||||
|
if d.HasChange("name") {
|
||||||
|
log.Printf("[DEBUG] Name for %s changed to %s, starting update", d.Id(), name)
|
||||||
|
|
||||||
|
// Create a new parameter struct
|
||||||
|
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
|
||||||
|
|
||||||
|
// Set the new name
|
||||||
|
p.SetName(name)
|
||||||
|
|
||||||
|
// Update the display name
|
||||||
|
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"Error updating the name for instance %s: %s", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetPartial("name")
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the service offering is changed and if so, update the offering
|
// Check if the service offering is changed and if so, update the offering
|
||||||
if d.HasChange("service_offering") {
|
if d.HasChange("service_offering") {
|
||||||
log.Printf("[DEBUG] Service offering changed for %s, starting update", name)
|
log.Printf("[DEBUG] Service offering changed for %s, starting update", name)
|
||||||
|
|
|
@ -56,6 +56,8 @@ func TestAccCloudStackInstance_update(t *testing.T) {
|
||||||
testAccCheckCloudStackInstanceExists(
|
testAccCheckCloudStackInstanceExists(
|
||||||
"cloudstack_instance.foobar", &instance),
|
"cloudstack_instance.foobar", &instance),
|
||||||
testAccCheckCloudStackInstanceRenamedAndResized(&instance),
|
testAccCheckCloudStackInstanceRenamedAndResized(&instance),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"cloudstack_instance.foobar", "name", "terraform-updated"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"cloudstack_instance.foobar", "display_name", "terraform-updated"),
|
"cloudstack_instance.foobar", "display_name", "terraform-updated"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -166,7 +168,7 @@ func testAccCheckCloudStackInstanceAttributes(
|
||||||
return fmt.Errorf("Bad name: %s", instance.Name)
|
return fmt.Errorf("Bad name: %s", instance.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if instance.Displayname != "terraform" {
|
if instance.Displayname != "terraform-test" {
|
||||||
return fmt.Errorf("Bad display name: %s", instance.Displayname)
|
return fmt.Errorf("Bad display name: %s", instance.Displayname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,6 +192,10 @@ func testAccCheckCloudStackInstanceRenamedAndResized(
|
||||||
instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
|
instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
|
|
||||||
|
if instance.Name != "terraform-updated" {
|
||||||
|
return fmt.Errorf("Bad name: %s", instance.Name)
|
||||||
|
}
|
||||||
|
|
||||||
if instance.Displayname != "terraform-updated" {
|
if instance.Displayname != "terraform-updated" {
|
||||||
return fmt.Errorf("Bad display name: %s", instance.Displayname)
|
return fmt.Errorf("Bad display name: %s", instance.Displayname)
|
||||||
}
|
}
|
||||||
|
@ -226,7 +232,7 @@ func testAccCheckCloudStackInstanceDestroy(s *terraform.State) error {
|
||||||
var testAccCloudStackInstance_basic = fmt.Sprintf(`
|
var testAccCloudStackInstance_basic = fmt.Sprintf(`
|
||||||
resource "cloudstack_instance" "foobar" {
|
resource "cloudstack_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
display_name = "terraform"
|
display_name = "terraform-test"
|
||||||
service_offering= "%s"
|
service_offering= "%s"
|
||||||
network = "%s"
|
network = "%s"
|
||||||
template = "%s"
|
template = "%s"
|
||||||
|
@ -241,7 +247,7 @@ resource "cloudstack_instance" "foobar" {
|
||||||
|
|
||||||
var testAccCloudStackInstance_renameAndResize = fmt.Sprintf(`
|
var testAccCloudStackInstance_renameAndResize = fmt.Sprintf(`
|
||||||
resource "cloudstack_instance" "foobar" {
|
resource "cloudstack_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-updated"
|
||||||
display_name = "terraform-updated"
|
display_name = "terraform-updated"
|
||||||
service_offering= "%s"
|
service_offering= "%s"
|
||||||
network = "%s"
|
network = "%s"
|
||||||
|
@ -258,7 +264,7 @@ resource "cloudstack_instance" "foobar" {
|
||||||
var testAccCloudStackInstance_fixedIP = fmt.Sprintf(`
|
var testAccCloudStackInstance_fixedIP = fmt.Sprintf(`
|
||||||
resource "cloudstack_instance" "foobar" {
|
resource "cloudstack_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
display_name = "terraform"
|
display_name = "terraform-test"
|
||||||
service_offering= "%s"
|
service_offering= "%s"
|
||||||
network = "%s"
|
network = "%s"
|
||||||
ipaddress = "%s"
|
ipaddress = "%s"
|
||||||
|
@ -279,7 +285,7 @@ resource "cloudstack_ssh_keypair" "foo" {
|
||||||
|
|
||||||
resource "cloudstack_instance" "foobar" {
|
resource "cloudstack_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
display_name = "terraform"
|
display_name = "terraform-test"
|
||||||
service_offering= "%s"
|
service_offering= "%s"
|
||||||
network = "%s"
|
network = "%s"
|
||||||
ipaddress = "%s"
|
ipaddress = "%s"
|
||||||
|
@ -297,7 +303,7 @@ resource "cloudstack_instance" "foobar" {
|
||||||
var testAccCloudStackInstance_project = fmt.Sprintf(`
|
var testAccCloudStackInstance_project = fmt.Sprintf(`
|
||||||
resource "cloudstack_instance" "foobar" {
|
resource "cloudstack_instance" "foobar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
display_name = "terraform"
|
display_name = "terraform-test"
|
||||||
service_offering= "%s"
|
service_offering= "%s"
|
||||||
network = "%s"
|
network = "%s"
|
||||||
template = "%s"
|
template = "%s"
|
||||||
|
|
Loading…
Reference in New Issue