Skip to content

Commit

Permalink
User Tags in Instance, Inst Temp, Inst Vol Attmnt
Browse files Browse the repository at this point in the history
  • Loading branch information
SunithaGudisagarIBM1 committed Sep 8, 2022
1 parent 2edcd44 commit d2a00b5
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 8 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/IBM/scc-go-sdk/v3 v3.1.6
github.com/IBM/schematics-go-sdk v0.2.1
github.com/IBM/secrets-manager-go-sdk v0.1.19
github.com/IBM/vpc-go-sdk v0.22.0
github.com/IBM/vpc-go-sdk v0.23.0
github.com/PromonLogicalis/asn1 v0.0.0-20190312173541-d60463189a56 // indirect
github.com/ScaleFT/sshkeys v0.0.0-20200327173127-6142f742bca5
github.com/Shopify/sarama v1.29.1
Expand Down Expand Up @@ -57,3 +57,4 @@ require (
replace github.com/softlayer/softlayer-go v1.0.3 => github.com/IBM-Cloud/softlayer-go v1.0.5-tf

replace github.com/dgrijalva/jwt-go v3.2.0+incompatible => github.com/golang-jwt/jwt v3.2.1+incompatible

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/IBM/secrets-manager-go-sdk v0.1.19 h1:0GPs5EoTaWNsjo4QPj64GNxlWfN8VHJ
github.com/IBM/secrets-manager-go-sdk v0.1.19/go.mod h1:eO3dBhzPrHkkt+yPex/jB2xD6qHZxBko+Aw+0tfqHeA=
github.com/IBM/vpc-go-sdk v0.22.0 h1:jo2WMfiFXhAyJkdJeCVHwvT6kgTg2tg8sDeP9zrMFVg=
github.com/IBM/vpc-go-sdk v0.22.0/go.mod h1:YPyIfI+/qhPqlYp+I7dyx2U1GLcXgp/jzVvsZfUH4y8=
github.com/IBM/vpc-go-sdk v0.23.0 h1:C26g02pPtTEWyLHNKK0mlFdpHeb9+noku9Q6qJLmdkc=
github.com/IBM/vpc-go-sdk v0.23.0/go.mod h1:YPyIfI+/qhPqlYp+I7dyx2U1GLcXgp/jzVvsZfUH4y8=
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56 h1:vuquMR410psHNax14XKNWa0Ae/kYgWJcXi0IFuX60N0=
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56/go.mod h1:Zb3OT4l0mf7P/GOs2w2Ilj5sdm5Whoq3pa24dAEBHFc=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
Expand Down
26 changes: 26 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ func DataSourceIBMISInstanceTemplate() *schema.Resource {
Computed: true,
Description: "The CRN of the [Key Protect Root Key](https://cloud.ibm.com/docs/key-protect?topic=key-protect-getting-started-tutorial) or [Hyper Protect Crypto Service Root Key](https://cloud.ibm.com/docs/hs-crypto?topic=hs-crypto-get-started) for this resource.",
},
isInstanceTemplateVolAttTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "The user tags associated with this volume.",
},
},
},
},
Expand Down Expand Up @@ -307,6 +314,13 @@ func DataSourceIBMISInstanceTemplate() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
isInstanceTemplateBootVolumeTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "The user tags associated with this volume.",
},
},
},
},
Expand Down Expand Up @@ -574,6 +588,9 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
encryptionKey := volumeInst.EncryptionKey.(*vpcv1.EncryptionKeyIdentity)
newVolume[isInstanceTemplateVolAttVolEncryptionKey] = *encryptionKey.CRN
}
if volumeInst.UserTags != nil {
newVolume[isInstanceTemplateVolAttTags] = instance.BootVolumeAttachment.Volume.UserTags
}
newVolumeArr = append(newVolumeArr, newVolume)
volumeAttach[isInstanceTemplateVolAttVolPrototype] = newVolumeArr

Expand All @@ -597,6 +614,9 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
volProfInst := volProfIntf.(*vpcv1.VolumeProfileIdentity)
bootVol[isInstanceTemplateBootProfile] = volProfInst.Name
}
if instance.BootVolumeAttachment.Volume.UserTags != nil {
bootVol[isInstanceTemplateBootVolumeTags] = instance.BootVolumeAttachment.Volume.UserTags
}
}
bootVolList = append(bootVolList, bootVol)
d.Set(isInstanceTemplateBootVolumeAttachment, bootVolList)
Expand Down Expand Up @@ -820,6 +840,9 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
encryptionKey := volumeInst.EncryptionKey.(*vpcv1.EncryptionKeyIdentity)
newVolume[isInstanceTemplateVolAttVolEncryptionKey] = *encryptionKey.CRN
}
if volumeInst.UserTags != nil {
newVolume[isInstanceTemplateVolAttTags] = volumeInst.UserTags
}
newVolumeArr = append(newVolumeArr, newVolume)
volumeAttach[isInstanceTemplateVolAttVolPrototype] = newVolumeArr

Expand All @@ -843,6 +866,9 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
volProfInst := volProfIntf.(*vpcv1.VolumeProfileIdentity)
bootVol[isInstanceTemplateBootProfile] = volProfInst.Name
}
if instance.BootVolumeAttachment.Volume.UserTags != nil {
bootVol[isInstanceTemplateBootVolumeTags] = instance.BootVolumeAttachment.Volume.UserTags
}
}
bootVolList = append(bootVolList, bootVol)
d.Set(isInstanceTemplateBootVolumeAttachment, bootVolList)
Expand Down
20 changes: 20 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_instance_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ func DataSourceIBMISInstanceTemplates() *schema.Resource {
Computed: true,
Description: "The CRN of the [Key Protect Root Key](https://cloud.ibm.com/docs/key-protect?topic=key-protect-getting-started-tutorial) or [Hyper Protect Crypto Service Root Key](https://cloud.ibm.com/docs/hs-crypto?topic=hs-crypto-get-started) for this resource.",
},
isInstanceTemplateVolAttTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "The user tags associated with this volume.",
},
},
},
},
Expand Down Expand Up @@ -359,6 +366,13 @@ func DataSourceIBMISInstanceTemplates() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
isInstanceTemplateBootVolumeTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "The user tags associated with this volume.",
},
},
},
},
Expand Down Expand Up @@ -598,6 +612,9 @@ func dataSourceIBMISInstanceTemplatesRead(d *schema.ResourceData, meta interface
encryptionKey := volumeInst.EncryptionKey.(*vpcv1.EncryptionKeyIdentity)
newVolume[isInstanceTemplateVolAttVolEncryptionKey] = *encryptionKey.CRN
}
if volumeInst.UserTags != nil {
newVolume[isInstanceTemplateVolAttTags] = instance.BootVolumeAttachment.Volume.UserTags
}
newVolumeArr = append(newVolumeArr, newVolume)
volumeAttach[isInstanceTemplateVolAttVolPrototype] = newVolumeArr

Expand All @@ -621,6 +638,9 @@ func dataSourceIBMISInstanceTemplatesRead(d *schema.ResourceData, meta interface
volProfInst := volProfIntf.(*vpcv1.VolumeProfileIdentity)
bootVol[isInstanceTemplateBootProfile] = volProfInst.Name
}
if instance.BootVolumeAttachment.Volume.UserTags != nil {
bootVol[isInstanceTemplateBootVolumeTags] = instance.BootVolumeAttachment.Volume.UserTags
}
}
bootVolList = append(bootVolList, bootVol)
template[isInstanceTemplatesBootVolumeAttachment] = bootVolList
Expand Down
91 changes: 91 additions & 0 deletions ibm/service/vpc/resource_ibm_is_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
IsInstanceCRN = "crn"
isInstanceKeys = "keys"
isInstanceTags = "tags"
isInstanceBootVolumeTags = "tags"
isInstanceNetworkInterfaces = "network_interfaces"
isInstancePrimaryNetworkInterface = "primary_network_interface"
isInstanceNicName = "name"
Expand Down Expand Up @@ -603,6 +604,14 @@ func ResourceIBMISInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
isInstanceBootVolumeTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_instance", "tags")},
Set: flex.ResourceIBMVPCHash,
Description: "UserTags for the volume instance",
},
},
},
},
Expand Down Expand Up @@ -1013,6 +1022,18 @@ func instanceCreateByImage(d *schema.ResourceData, meta interface{}, profile, na
volTemplate.Profile = &vpcv1.VolumeProfileIdentity{
Name: &volprof,
}
var userTags *schema.Set
if v, ok := bootvol[isInstanceBootVolumeTags]; 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
}
volTemplate.UserTags = userTagsArray
}
}
deletebool := true
instanceproto.BootVolumeAttachment = &vpcv1.VolumeAttachmentPrototypeInstanceByImageContext{
DeleteVolumeOnInstanceDelete: &deletebool,
Expand Down Expand Up @@ -1367,6 +1388,18 @@ func instanceCreateByTemplate(d *schema.ResourceData, meta interface{}, profile,
volTemplate.Profile = &vpcv1.VolumeProfileIdentity{
Name: &volprof,
}
var userTags *schema.Set
if v, ok := bootvol[isInstanceBootVolumeTags]; 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
}
volTemplate.UserTags = userTagsArray
}
}
deletebool := true

instanceproto.BootVolumeAttachment = &vpcv1.VolumeAttachmentPrototypeInstanceByImageContext{
Expand Down Expand Up @@ -1705,6 +1738,18 @@ func instanceCreateByVolume(d *schema.ResourceData, meta interface{}, profile, n
volTemplate.Profile = &vpcv1.VolumeProfileIdentity{
Name: &volprof,
}
var userTags *schema.Set
if v, ok := bootvol[isInstanceBootVolumeTags]; 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
}
volTemplate.UserTags = userTagsArray
}
}
snapshotId, ok := bootvol[isInstanceVolumeSnapshot]
snapshotIdStr := snapshotId.(string)
if snapshotIdStr != "" && ok {
Expand Down Expand Up @@ -2391,6 +2436,9 @@ func instanceGet(d *schema.ResourceData, meta interface{}, id string) error {
if vol.SourceSnapshot != nil {
bootVol[isInstanceVolumeSnapshot] = vol.SourceSnapshot.ID
}
if vol.UserTags != nil {
bootVol[isInstanceBootVolumeTags] = vol.UserTags
}
}
}
bootVolList = append(bootVolList, bootVol)
Expand Down Expand Up @@ -2481,6 +2529,49 @@ func instanceUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}
}
bootVolTags := "boot_volume.0.tags"
if d.HasChange(bootVolTags) && !d.IsNewResource() {
var userTags *schema.Set
if v, ok := d.GetOk("boot_volume.0.tags"); ok {
volId := d.Get("boot_volume.0.volume_id").(string)
updateVolumeOptions := &vpcv1.UpdateVolumeOptions{
ID: &volId,
}
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
}
volumePatchModel := &vpcv1.VolumePatch{}
volumePatchModel.UserTags = userTagsArray
volumePatch, err := volumePatchModel.AsPatch()
if err != nil {
return fmt.Errorf("[ERROR] Error encountered while apply as patch for boot volume of instance %s", err)
}
optionsget := &vpcv1.GetVolumeOptions{
ID: &volId,
}
_, response, err := instanceC.GetVolume(optionsget)
if err != nil {
return fmt.Errorf("[ERROR] Error getting Boot Volume (%s): %s\n%s", id, err, response)
}
eTag := response.Headers.Get("ETag")
updateVolumeOptions.IfMatch = &eTag
updateVolumeOptions.VolumePatch = volumePatch
vol, res, err := instanceC.UpdateVolume(updateVolumeOptions)
if vol == nil || err != nil {
return (fmt.Errorf("[ERROR] Error encountered while applying tags for boot volume of instance %s/n%s", err, res))
}
_, err = isWaitForVolumeAvailable(instanceC, volId, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
}
}
}

if d.HasChange(isPlacementTargetDedicatedHost) || d.HasChange(isPlacementTargetDedicatedHostGroup) && !d.IsNewResource() {
dedicatedHost := d.Get(isPlacementTargetDedicatedHost).(string)
dedicatedHostGroup := d.Get(isPlacementTargetDedicatedHostGroup).(string)
Expand Down
59 changes: 58 additions & 1 deletion ibm/service/vpc/resource_ibm_is_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

const (
isInstanceTemplateBootVolume = "boot_volume"
isInstanceTemplateBootVolumeTags = "tags"
isInstanceTemplateVolAttTags = "tags"
isInstanceTemplateCRN = "crn"
isInstanceTemplateVolAttVolAutoDelete = "auto_delete"
isInstanceTemplateVolAttVol = "volume"
Expand Down Expand Up @@ -239,6 +241,14 @@ func ResourceIBMISInstanceTemplate() *schema.Resource {
ForceNew: true,
Description: "The CRN of the [Key Protect Root Key](https://cloud.ibm.com/docs/key-protect?topic=key-protect-getting-started-tutorial) or [Hyper Protect Crypto Service Root Key](https://cloud.ibm.com/docs/hs-crypto?topic=hs-crypto-get-started) for this resource.",
},
isInstanceTemplateVolAttTags: {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_instance_template", "tags")},
Set: flex.ResourceIBMVPCHash,
Description: "UserTags for the volume instance",
},
},
},
},
Expand Down Expand Up @@ -437,6 +447,14 @@ func ResourceIBMISInstanceTemplate() *schema.Resource {
Optional: true,
Computed: true,
},
isInstanceTemplateBootVolumeTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_instance_template", "tags")},
Set: flex.ResourceIBMVPCHash,
Description: "UserTags for the volume instance",
},
isInstanceTemplateBootEncryption: {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -521,7 +539,15 @@ func ResourceIBMISInstanceTemplateValidator() *validate.ResourceValidator {
Type: validate.TypeString,
Optional: true,
AllowedValues: host_failure})

validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "tags",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[A-Za-z0-9:_ .-]+$`,
MinValueLength: 1,
MaxValueLength: 128})
ibmISInstanceTemplateValidator := validate.ResourceValidator{ResourceName: "ibm_is_instance_template", Schema: validateSchema}
return &ibmISInstanceTemplateValidator
}
Expand Down Expand Up @@ -671,6 +697,19 @@ func instanceTemplateCreate(d *schema.ResourceData, meta interface{}, profile, n
volTemplate.Name = &namestr
}

var userTags *schema.Set
if v, ok := bootvol[isInstanceTemplateBootVolumeTags]; 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
}
volTemplate.UserTags = userTagsArray
}
}

volcap := 100
volcapint64 := int64(volcap)
volprof := "general-purpose"
Expand Down Expand Up @@ -740,6 +779,18 @@ func instanceTemplateCreate(d *schema.ResourceData, meta interface{}, profile, n
CRN: &encryptionKey,
}
}
var userTags *schema.Set
if v, ok := newvol[isInstanceTemplateVolAttTags]; 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
}
volPrototype.UserTags = userTagsArray
}
}
volInterface.Volume = volPrototype
}

Expand Down Expand Up @@ -1184,6 +1235,9 @@ func instanceTemplateGet(d *schema.ResourceData, meta interface{}, ID string) er
encryptionKey := volumeInst.EncryptionKey.(*vpcv1.EncryptionKeyIdentity)
newVolume[isInstanceTemplateVolAttVolEncryptionKey] = *encryptionKey.CRN
}
if volumeInst.UserTags != nil {
newVolume[isInstanceTemplateVolAttTags] = volumeInst.UserTags
}
if len(newVolume) > 0 {
newVolumeArr = append(newVolumeArr, newVolume)
}
Expand All @@ -1210,6 +1264,9 @@ func instanceTemplateGet(d *schema.ResourceData, meta interface{}, ID string) er
volEncryptionIntf := volEncryption.(*vpcv1.EncryptionKeyIdentity)
bootVol[isInstanceTemplateBootEncryption] = volEncryptionIntf.CRN
}
if volumeIntf.UserTags != nil {
bootVol[isVolumeTags] = volumeIntf.UserTags
}
}

bootVolList = append(bootVolList, bootVol)
Expand Down
Loading

0 comments on commit d2a00b5

Please sign in to comment.