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

added tagging and access tags support for is_snapshot #4134

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions ibm/flex/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,42 @@ func ResourceTagsCustomizeDiff(diff *schema.ResourceDiff) error {
return nil
}

func ResourceValidateAccessTags(diff *schema.ResourceDiff, meta interface{}) error {

if value, ok := diff.GetOkExists("access_tags"); ok {
tagSet := value.(*schema.Set)
newTagList := tagSet.List()
tagType := "access"
gtClient, err := meta.(conns.ClientSession).GlobalTaggingAPIv1()
if err != nil {
return fmt.Errorf("Error getting global tagging client settings: %s", err)
}

listTagsOptions := &globaltaggingv1.ListTagsOptions{
TagType: &tagType,
}
taggingResult, _, err := gtClient.ListTags(listTagsOptions)
if err != nil {
return err
}
var taglist []string
for _, item := range taggingResult.Items {
taglist = append(taglist, *item.Name)
}
existingAccessTags := NewStringSet(ResourceIBMVPCHash, taglist)
errStatement := ""
for _, tag := range newTagList {
if !existingAccessTags.Contains(tag) {
errStatement = errStatement + " " + tag.(string)
}
}
if errStatement != "" {
return fmt.Errorf("[ERROR] Error : Access tag(s) %s does not exist", errStatement)
}
}
return nil
}

func ResourceLBListenerPolicyCustomizeDiff(diff *schema.ResourceDiff) error {
policyActionIntf, _ := diff.GetOk(isLBListenerPolicyAction)
policyAction := policyActionIntf.(string)
Expand Down
21 changes: 21 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vpc

import (
"fmt"
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
Expand Down Expand Up @@ -50,6 +51,14 @@ func DataSourceSnapshot() *schema.Resource {
Description: "If present, the image id from which the data on this volume was most directly provisioned.",
},

isSnapshotAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},

isSnapshotOperatingSystem: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -259,6 +268,12 @@ func snapshotGetByNameOrID(d *schema.ResourceData, meta interface{}, name, id st
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
d.Set(isSnapshotBackupPolicyPlan, backupPolicyPlanList)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
d.Set(isSnapshotAccessTags, accesstags)
return nil
}
}
Expand Down Expand Up @@ -319,6 +334,12 @@ func snapshotGetByNameOrID(d *schema.ResourceData, meta interface{}, name, id st
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
d.Set(isSnapshotBackupPolicyPlan, backupPolicyPlanList)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
d.Set(isSnapshotAccessTags, accesstags)
return nil
}
}
15 changes: 15 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vpc

import (
"fmt"
"log"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
Expand Down Expand Up @@ -157,6 +158,14 @@ func DataSourceSnapshots() *schema.Resource {
Description: "User Tags for the snapshot",
},

isSnapshotAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},

isSnapshotBackupPolicyPlan: {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -311,6 +320,12 @@ func getSnapshots(d *schema.ResourceData, meta interface{}) error {
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
l[isSnapshotBackupPolicyPlan] = backupPolicyPlanList
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
l[isSnapshotAccessTags] = accesstags
snapshotsInfo = append(snapshotsInfo, l)
}
d.SetId(dataSourceIBMISSnapshotsID(d))
Expand Down
55 changes: 51 additions & 4 deletions ibm/service/vpc/resource_ibm_is_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
isSnapshotSourceVolume = "source_volume"
isSnapshotSourceImage = "source_image"
isSnapshotUserTags = "tags"
isSnapshotAccessTags = "access_tags"
isSnapshotCRN = "crn"
isSnapshotHref = "href"
isSnapshotEncryption = "encryption"
Expand Down Expand Up @@ -61,10 +62,15 @@ func ResourceIBMSnapshot() *schema.Resource {
Delete: schema.DefaultTimeout(10 * time.Minute),
},

CustomizeDiff: customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceTagsCustomizeDiff(diff)
},
CustomizeDiff: customdiff.All(
customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceTagsCustomizeDiff(diff)
}),
customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceValidateAccessTags(diff, v)
}),
),

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -148,6 +154,15 @@ func ResourceIBMSnapshot() *schema.Resource {
Description: "The resource type of the snapshot",
},

isSnapshotAccessTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_snapshot", "accesstag")},
Set: flex.ResourceIBMVPCHash,
Description: "List of access management tags",
},

isSnapshotSize: {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -231,6 +246,15 @@ func ResourceIBMISSnapshotValidator() *validate.ResourceValidator {
Regexp: `^[A-Za-z0-9:_ .-]+$`,
MinValueLength: 1,
MaxValueLength: 128})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "accesstag",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^([A-Za-z0-9_.-]|[A-Za-z0-9_.-][A-Za-z0-9_ .-]*[A-Za-z0-9_.-]):([A-Za-z0-9_.-]|[A-Za-z0-9_.-][A-Za-z0-9_ .-]*[A-Za-z0-9_.-])$`,
MinValueLength: 1,
MaxValueLength: 128})
ibmISSnapshotResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_snapshot", Schema: validateSchema}
return &ibmISSnapshotResourceValidator
}
Expand Down Expand Up @@ -294,6 +318,14 @@ func resourceIBMISSnapshotCreate(d *schema.ResourceData, meta interface{}) error
return err
}

if _, ok := d.GetOk(isSnapshotAccessTags); ok {
oldList, newList := d.GetChange(isSubnetAccessTags)
err = flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on create of resource snapshot (%s) access tags: %s", d.Id(), err)
}
}
return resourceIBMISSnapshotRead(d, meta)
}

Expand Down Expand Up @@ -402,6 +434,12 @@ func snapshotGet(d *schema.ResourceData, meta interface{}, id string) error {
backupPolicyPlanList = append(backupPolicyPlanList, backupPolicyPlan)
}
d.Set(isSnapshotBackupPolicyPlan, backupPolicyPlanList)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *snapshot.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource snapshot (%s) access tags: %s", d.Id(), err)
}
d.Set(isSnapshotAccessTags, accesstags)
return nil
}

Expand Down Expand Up @@ -504,6 +542,15 @@ func snapshotUpdate(d *schema.ResourceData, meta interface{}, id, name string, h
return err
}
}

if d.HasChange(isSnapshotAccessTags) {
oldList, newList := d.GetChange(isSnapshotAccessTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, d.Get(isSnapshotCRN).(string), "", isAccessTagType)
if err != nil {
log.Printf(
"Error on update of resource snapshot (%s) access tags: %s", d.Id(), err)
}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/is_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Review the argument references that you can specify for your data source.

## Attribute reference
In addition to all argument reference list, you can access the following attribute reference after your data source is created.

- `access_tags` - (Array of Strings) Access management tags associated with the snapshot.
- `backup_policy_plan` - (List) If present, the backup policy plan which created this snapshot.

Nested scheme for `backup_policy_plan`:
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/is_snapshots.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ In addition to all argument reference list, you can access the following attribu
- `snapshots` - (List) List of snapshots in the IBM Cloud Infrastructure.

Nested scheme for `snapshots`:
- `access_tags` - (Array of Strings) Access management tags associated with the snapshot.
- `id` - (String) The unique identifier for this snapshot.
- `backup_policy_plan` - (List) If present, the backup policy plan which created this snapshot.

Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/is_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ The `ibm_is_snapshot` resource provides the following [Timeouts](https://www.ter
## Argument reference
Review the argument references that you can specify for your resource.

- `access_tags` - (Optional, List of Strings) A list of access management tags to attach to the bare metal server.

~> **Note:**
**&#x2022;** You can attach only those access tags that already exists.</br>
**&#x2022;** For more information, about creating access tags, see [working with tags](https://cloud.ibm.com/docs/account?topic=account-tag&interface=ui#create-access-console).</br>
**&#x2022;** You must have the access listed in the [Granting users access to tag resources](https://cloud.ibm.com/docs/account?topic=account-access) for `access_tags`</br>
**&#x2022;** `access_tags` must be in the format `key:value`.
- `name` - (Optional, String) The name of the snapshot.
- `resource_group` - (Optional, Forces new resource, String) The resource group ID where the snapshot is to be created
- `source_volume` - (Required, Forces new resource, String) The unique identifier for the volume for which snapshot is to be created.
Expand Down