Skip to content

Commit

Permalink
fix: fixed name update issue on is_instance boot_volume
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwal-ibm authored and hkantare committed Jan 30, 2024
1 parent 38db859 commit ef4e365
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ibm/service/vpc/resource_ibm_is_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3545,6 +3545,30 @@ func instanceUpdate(d *schema.ResourceData, meta interface{}) error {
}
}
}
bootVolName := "boot_volume.0.name"
if d.HasChange(bootVolName) && !d.IsNewResource() {
volId := d.Get("boot_volume.0.volume_id").(string)
volName := d.Get(bootVolName).(string)
updateVolumeOptions := &vpcv1.UpdateVolumeOptions{
ID: &volId,
}
volPatchModel := &vpcv1.VolumePatch{
Name: &volName,
}
volPatchModelAsPatch, err := volPatchModel.AsPatch()

if err != nil {
return (fmt.Errorf("[ERROR] Error encountered while apply as patch for boot volume name update of instance %s", err))
}

updateVolumeOptions.VolumePatch = volPatchModelAsPatch

vol, res, err := instanceC.UpdateVolume(updateVolumeOptions)

if vol == nil || err != nil {
return (fmt.Errorf("[ERROR] Error encountered while updating name of boot volume of instance %s/n%s", err, res))
}
}
bootVolAutoDel := "boot_volume.0.auto_delete_volume"
if d.HasChange(bootVolAutoDel) && !d.IsNewResource() {
listvolattoptions := &vpcv1.ListInstanceVolumeAttachmentsOptions{
Expand Down
83 changes: 83 additions & 0 deletions ibm/service/vpc/resource_ibm_is_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,54 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
},
})
}
func TestAccIBMISInstance_RenameBoot(t *testing.T) {
var instance string
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tf-subnet-%d", acctest.RandIntRange(10, 100))
publicKey := strings.TrimSpace(`
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
`)
sshname := fmt.Sprintf("tf-ssh-%d", acctest.RandIntRange(10, 100))
userData1 := "a"
rename1 := fmt.Sprintf("tf-bootvol-%d", acctest.RandIntRange(10, 100))
rename2 := fmt.Sprintf("tf-bootvol-update-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISInstanceRenameConfig(vpcname, subnetname, sshname, publicKey, name, userData1, rename1),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISInstanceExists("ibm_is_instance.testacc_instance", instance),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "name", name),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "user_data", userData1),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "boot_volume.0.name", rename1),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "zone", acc.ISZoneName),
),
},
{
Config: testAccCheckIBMISInstanceRenameConfig(vpcname, subnetname, sshname, publicKey, name, userData1, rename2),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISInstanceExists("ibm_is_instance.testacc_instance", instance),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "name", name),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "user_data", userData1),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "boot_volume.0.name", rename2),
resource.TestCheckResourceAttr(
"ibm_is_instance.testacc_instance", "zone", acc.ISZoneName),
),
},
},
})
}

func TestAccIBMISInstance_bootVolumeUserTags(t *testing.T) {
var instance string
Expand Down Expand Up @@ -1219,6 +1267,41 @@ func testAccCheckIBMISInstanceResizeConfig(vpcname, subnetname, sshname, publicK
keys = [ibm_is_ssh_key.testacc_sshkey.id]
}`, vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, sshname, publicKey, name, acc.IsImage, acc.InstanceProfileName, resize, userData, acc.ISZoneName)
}
func testAccCheckIBMISInstanceRenameConfig(vpcname, subnetname, sshname, publicKey, name, userData, rename string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_subnet" "testacc_subnet" {
name = "%s"
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
ipv4_cidr_block = "%s"
}
resource "ibm_is_ssh_key" "testacc_sshkey" {
name = "%s"
public_key = "%s"
}
resource "ibm_is_instance" "testacc_instance" {
name = "%s"
image = "%s"
profile = "%s"
boot_volume {
name = "%s"
}
primary_network_interface {
subnet = ibm_is_subnet.testacc_subnet.id
}
user_data = "%s"
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
keys = [ibm_is_ssh_key.testacc_sshkey.id]
}
`, vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, sshname, publicKey, name, acc.IsImage, acc.InstanceProfileName, rename, userData, acc.ISZoneName)
}

func testAccCheckIBMISInstanceBandwidthConfig(vpcname, subnetname, sshname, publicKey, name string, bandwidth int) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit ef4e365

Please sign in to comment.