Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tagging logic in volume attachments in is_instance #5029

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 68 additions & 40 deletions ibm/service/vpc/resource_ibm_is_instance_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,24 @@ func instanceVolAttachmentCreate(d *schema.ResourceData, meta interface{}, insta
volnamestr := volname.(string)
volProtoVol.Name = &volnamestr
}
var userTags *schema.Set
if v, ok := d.GetOk(isInstanceVolAttTags); ok {
userTags = v.(*schema.Set)
if userTags != nil && userTags.Len() != 0 {
userTagsArray := make([]string, userTags.Len())
for i, userTag := range userTags.List() {
userTagStr := userTag.(string)
userTagsArray[i] = userTagStr
}
schematicTags := os.Getenv("IC_ENV_TAGS")
var envTags []string
if schematicTags != "" {
envTags = strings.Split(schematicTags, ",")
userTagsArray = append(userTagsArray, envTags...)
}
volProtoVol.UserTags = userTagsArray
}
}
// var userTags *schema.Set
// if v, ok := d.GetOk(isInstanceVolAttTags); ok {
// userTags = v.(*schema.Set)
// if userTags != nil && userTags.Len() != 0 {
// userTagsArray := make([]string, userTags.Len())
// for i, userTag := range userTags.List() {
// userTagStr := userTag.(string)
// userTagsArray[i] = userTagStr
// }
// schematicTags := os.Getenv("IC_ENV_TAGS")
// var envTags []string
// if schematicTags != "" {
// envTags = strings.Split(schematicTags, ",")
// userTagsArray = append(userTagsArray, envTags...)
// }
// volProtoVol.UserTags = userTagsArray
// }
// }
volSnapshotStr := ""
if volSnapshot, ok := d.GetOk(isInstanceVolumeSnapshot); ok {
volSnapshotStr = volSnapshot.(string)
Expand Down Expand Up @@ -388,10 +388,21 @@ func instanceVolAttachmentCreate(d *schema.ResourceData, meta interface{}, insta
return fmt.Errorf("[ERROR] Error while attaching volume for instance %s: %q", instanceId, err)
}
d.SetId(makeTerraformVolAttID(instanceId, *instanceVolAtt.ID))
_, err = isWaitForInstanceVolumeAttached(sess, d, instanceId, *instanceVolAtt.ID)
volAtt, err := isWaitForInstanceVolumeAttached(sess, d, instanceId, *instanceVolAtt.ID)
if err != nil {
return err
}

v := os.Getenv("IC_ENV_TAGS")
if _, ok := d.GetOk(isInstanceVolAttTags); ok || v != "" {
volAttRef := volAtt.(*vpcv1.VolumeAttachment)
oldList, newList := d.GetChange(isInstanceVolAttTags)
err = flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, *volAttRef.Volume.CRN, "", isInstanceUserTagType)
if err != nil {
log.Printf(
"Error on create of resource instance volume attachment (%s) tags: %s", d.Id(), err)
}
}
log.Printf("[INFO] Instance (%s) volume attachment : %s", instanceId, *instanceVolAtt.ID)
return nil
}
Expand Down Expand Up @@ -476,6 +487,12 @@ func instanceVolumeAttachmentGet(d *schema.ResourceData, meta interface{}, insta
if volumeDetail.SourceSnapshot != nil {
d.Set(isInstanceVolumeSnapshot, *volumeDetail.SourceSnapshot.ID)
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *volumeDetail.CRN, "", isInstanceUserTagType)
if err != nil {
log.Printf(
"Error on get of resource Instance volume attachment (%s) tags: %s", d.Id(), err)
}
d.Set(isInstanceTags, tags)
return nil
}

Expand All @@ -488,6 +505,17 @@ func instanceVolAttUpdate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
if volumecrnok, ok := d.GetOk("volume_crn"); ok {
volumecrn := volumecrnok.(string)
if d.HasChange(isInstanceTags) {
oldList, newList := d.GetChange(isInstanceTags)
err = flex.UpdateTagsUsingCRN(oldList, newList, meta, volumecrn)
if err != nil {
log.Printf(
"Error on update of resource Instance volume attachment (%s) tags: %s", d.Id(), err)
}
}
}
updateInstanceVolAttOptions := &vpcv1.UpdateInstanceVolumeAttachmentOptions{
InstanceID: &instanceId,
ID: &id,
Expand Down Expand Up @@ -548,7 +576,7 @@ func instanceVolAttUpdate(d *schema.ResourceData, meta interface{}) error {
volId = volIdOk.(string)
}

if volId != "" && (d.HasChange(isInstanceVolIops) || d.HasChange(isInstanceVolProfile) || d.HasChange(isInstanceVolAttTags)) {
if volId != "" && (d.HasChange(isInstanceVolIops) || d.HasChange(isInstanceVolProfile)) { // || d.HasChange(isInstanceVolAttTags)
insId := d.Get(isInstanceId).(string)
getinsOptions := &vpcv1.GetInstanceOptions{
ID: &insId,
Expand Down Expand Up @@ -590,26 +618,26 @@ func instanceVolAttUpdate(d *schema.ResourceData, meta interface{}) error {
iops := int64(d.Get(isVolumeIops).(int))
volumeProfilePatchModel.Iops = &iops
}
if d.HasChange(isInstanceVolAttTags) && !d.IsNewResource() {
if v, ok := d.GetOk(isInstanceVolAttTags); ok {
userTags := v.(*schema.Set)
if userTags != nil && userTags.Len() != 0 {
userTagsArray := make([]string, userTags.Len())
for i, userTag := range userTags.List() {
userTagStr := userTag.(string)
userTagsArray[i] = userTagStr
}
schematicTags := os.Getenv("IC_ENV_TAGS")
var envTags []string
if schematicTags != "" {
envTags = strings.Split(schematicTags, ",")
userTagsArray = append(userTagsArray, envTags...)
}
volumeProfilePatchModel.UserTags = userTagsArray
}
}

}
// if d.HasChange(isInstanceVolAttTags) && !d.IsNewResource() {
// if v, ok := d.GetOk(isInstanceVolAttTags); ok {
// userTags := v.(*schema.Set)
// if userTags != nil && userTags.Len() != 0 {
// userTagsArray := make([]string, userTags.Len())
// for i, userTag := range userTags.List() {
// userTagStr := userTag.(string)
// userTagsArray[i] = userTagStr
// }
// schematicTags := os.Getenv("IC_ENV_TAGS")
// var envTags []string
// if schematicTags != "" {
// envTags = strings.Split(schematicTags, ",")
// userTagsArray = append(userTagsArray, envTags...)
// }
// volumeProfilePatchModel.UserTags = userTagsArray
// }
// }

// }

volumeProfilePatch, err := volumeProfilePatchModel.AsPatch()
if err != nil {
Expand Down
Loading