Skip to content

Commit

Permalink
added tagging and access tags support for is_snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
SunithaGudisagarIBM1 authored and hkantare committed Nov 24, 2022
1 parent ab549ce commit 960e82a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
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

0 comments on commit 960e82a

Please sign in to comment.