Skip to content

Commit

Permalink
match_resource_type development
Browse files Browse the repository at this point in the history
  • Loading branch information
SunithaGudisagarIBM1 committed Oct 12, 2023
1 parent 66b1397 commit b8576f9
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 126 deletions.
19 changes: 12 additions & 7 deletions common/github.com/IBM/vpc-go-sdk/vpcv1/vpc_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewVpcV1(options *VpcV1Options) (service *VpcV1, err error) {
}

if options.Version == nil {
options.Version = core.StringPtr("2023-08-08")
options.Version = core.StringPtr("2023-09-01")
}

service = &VpcV1{
Expand Down Expand Up @@ -9856,8 +9856,10 @@ func (vpc *VpcV1) ListBackupPoliciesWithContext(ctx context.Context, listBackupP
}
builder.AddHeader("Accept", "application/json")

builder.AddQuery("version", fmt.Sprint(*vpc.Version))
builder.AddQuery("version", fmt.Sprint("2024-01-01"))
builder.AddQuery("generation", fmt.Sprint(*vpc.generation))
builder.AddQuery("future_version", fmt.Sprint("true"))
// builder.AddQuery("generation", fmt.Sprint(*vpc.generation))
if listBackupPoliciesOptions.Start != nil {
builder.AddQuery("start", fmt.Sprint(*listBackupPoliciesOptions.Start))
}
Expand Down Expand Up @@ -9932,8 +9934,9 @@ func (vpc *VpcV1) CreateBackupPolicyWithContext(ctx context.Context, createBacku
builder.AddHeader("Accept", "application/json")
builder.AddHeader("Content-Type", "application/json")

builder.AddQuery("version", fmt.Sprint(*vpc.Version))
builder.AddQuery("version", fmt.Sprint("2024-01-01"))
builder.AddQuery("generation", fmt.Sprint(*vpc.generation))
builder.AddQuery("future_version", fmt.Sprint("true"))

_, err = builder.SetBodyContentJSON(createBackupPolicyOptions.BackupPolicyPrototype)
if err != nil {
Expand Down Expand Up @@ -10535,8 +10538,9 @@ func (vpc *VpcV1) DeleteBackupPolicyWithContext(ctx context.Context, deleteBacku
builder.AddHeader("If-Match", fmt.Sprint(*deleteBackupPolicyOptions.IfMatch))
}

builder.AddQuery("version", fmt.Sprint(*vpc.Version))
builder.AddQuery("version", fmt.Sprint("2024-01-01"))
builder.AddQuery("generation", fmt.Sprint(*vpc.generation))
builder.AddQuery("future_version", fmt.Sprint("true"))

request, err := builder.Build()
if err != nil {
Expand Down Expand Up @@ -10598,9 +10602,9 @@ func (vpc *VpcV1) GetBackupPolicyWithContext(ctx context.Context, getBackupPolic
}
builder.AddHeader("Accept", "application/json")

builder.AddQuery("version", fmt.Sprint(*vpc.Version))
builder.AddQuery("version", fmt.Sprint("2024-01-01"))
builder.AddQuery("generation", fmt.Sprint(*vpc.generation))

builder.AddQuery("future_version", fmt.Sprint("true"))
request, err := builder.Build()
if err != nil {
return
Expand Down Expand Up @@ -10666,8 +10670,9 @@ func (vpc *VpcV1) UpdateBackupPolicyWithContext(ctx context.Context, updateBacku
builder.AddHeader("If-Match", fmt.Sprint(*updateBackupPolicyOptions.IfMatch))
}

builder.AddQuery("version", fmt.Sprint(*vpc.Version))
builder.AddQuery("version", fmt.Sprint("2024-01-01"))
builder.AddQuery("generation", fmt.Sprint(*vpc.generation))
builder.AddQuery("future_version", fmt.Sprint("true"))

_, err = builder.SetBodyContentJSON(updateBackupPolicyOptions.BackupPolicyPatch)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions examples/ibm-is-ng/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1385,3 +1385,30 @@ resource "ibm_is_image_deprecate" "example" {
resource "ibm_is_image_obsolete" "example" {
image = ibm_is_image.image1.id
}


//snapshot consistency group

resource "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group_instance" {
delete_snapshots_on_delete = true
snapshots {
name = "exmaple-snapshot"
source_volume = ibm_is_instance.instance.volume_attachments[0].volume_id
}
snapshots {
name = "example-snapshot-1"
source_volume = ibm_is_instance.instance.volume_attachments[1].volume_id
}
name = "example-snapshot-consistency-group"
}

data "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group_instance" {
identifier = ibm_is_snapshot_consistency_group.is_snapshot_consistency_group_instance.id
}
data "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group_instance" {
name = "example-snapshot-consistency-group"
}
data "ibm_is_snapshot_consistency_groups" "is_snapshot_consistency_group_instance" {
depends_on = [ibm_is_snapshot_consistency_group.is_snapshot_consistency_group_instance]
name = "example-snapshot-consistency-group"
}
4 changes: 2 additions & 2 deletions ibm/service/vpc/data_source_ibm_is_backup_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func dataSourceBackupPolicyCollectionBackupPoliciesToMap(backupPoliciesItem vpcv
if backupPoliciesItem.LastJobCompletedAt != nil {
backupPoliciesMap["last_job_completed_at"] = flex.DateTimeToString(backupPoliciesItem.LastJobCompletedAt)
}
if backupPoliciesItem.MatchResourceTypes != nil {
backupPoliciesMap["match_resource_types"] = backupPoliciesItem.MatchResourceTypes
if backupPoliciesItem.MatchResourceType != nil {
backupPoliciesMap["match_resource_types"] = []string{*backupPoliciesItem.MatchResourceType}
}
if backupPoliciesItem.MatchUserTags != nil {
backupPoliciesMap["match_user_tags"] = backupPoliciesItem.MatchUserTags
Expand Down
8 changes: 2 additions & 6 deletions ibm/service/vpc/data_source_ibm_is_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,9 @@ func dataSourceIBMIsBackupPolicyRead(context context.Context, d *schema.Resource
}
}

matchResourceType := make([]string, 0)
if backupPolicy.MatchResourceTypes != nil {
for _, matchResourceTyp := range backupPolicy.MatchResourceTypes {
matchResourceType = append(matchResourceType, matchResourceTyp)
}
if backupPolicy.MatchResourceType != nil {
d.Set("match_resource_types", []string{*backupPolicy.MatchResourceType})
}
d.Set("match_resource_types", matchResourceType)

if backupPolicy.IncludedContent != nil {
if err = d.Set("included_content", backupPolicy.IncludedContent); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ package vpc_test

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMIsSnapshotConsistencyGroupDataSourceBasic(t *testing.T) {
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("tf-instance-%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))
deleteSnapshotsOnDelete := "true"
scgname := fmt.Sprintf("tf-snap-cons-grp-name-%d", acctest.RandIntRange(10, 100))
snapname := fmt.Sprintf("tf-snap-name-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMIsSnapshotConsistencyGroupDataSourceConfigBasic(),
Config: testAccCheckIBMIsSnapshotConsistencyGroupDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, name, scgname, snapname, deleteSnapshotsOnDelete),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "id"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "created_at"),
Expand All @@ -30,16 +42,20 @@ func TestAccIBMIsSnapshotConsistencyGroupDataSourceBasic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "resource_group.#"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "resource_type"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshots.#"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshots.0.id"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshots.0.name"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshots.0.crn"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshots.0.href"),
),
},
},
})
}

func testAccCheckIBMIsSnapshotConsistencyGroupDataSourceConfigBasic() string {
return fmt.Sprintf(`
data "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group" {
identifier = "r134-628982dd-bb79-4f7c-ac64-d1b70b1064e8"
}
`)
func testAccCheckIBMIsSnapshotConsistencyGroupDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, name, snapname, scgname, deleteSnapshotsOnDelete string) string {
return testAccCheckIBMIsSnapshotConsistencyGroupConfig(vpcname, subnetname, sshname, publicKey, name, scgname, snapname, deleteSnapshotsOnDelete) + fmt.Sprintf(`
data "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group" {
identifier = ibm_is_snapshot_consistency_group.is_snapshot_consistency_group.id
}
`)
}
46 changes: 34 additions & 12 deletions ibm/service/vpc/resource_ibm_is_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ func ResourceIBMIsBackupPolicy() *schema.Resource {

Schema: map[string]*schema.Schema{
"match_resource_types": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Set: schema.HashString,
Description: "A resource type this backup policy applies to. Resources that have both a matching type and a matching user tag will be subject to the backup policy.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
Optional: true,
Computed: true,
Set: schema.HashString,
Deprecated: "match_resource_types is being deprecated. Use match_resource_type instead",
Description: "A resource type this backup policy applies to. Resources that have both a matching type and a matching user tag will be subject to the backup policy.",
ConflictsWith: []string{"match_resource_type"},
Elem: &schema.Schema{Type: schema.TypeString},
},
"match_resource_type": {
Type: schema.TypeString,
Optional: true,
Default: "volume",
ForceNew: true,
ConflictsWith: []string{"match_resource_types"},
ValidateFunc: validate.InvokeValidator("ibm_is_backup_policy", "match_resource_type"),
Description: "A resource type this backup policy applies to. Resources that have both a matching type and a matching user tag will be subject to the backup policy.",
},
"match_user_tags": &schema.Schema{
Type: schema.TypeSet,
Expand All @@ -44,6 +55,7 @@ func ResourceIBMIsBackupPolicy() *schema.Resource {
"included_content": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Set: schema.HashString,
Description: "The included content for backups created using this policy",
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_backup_policy", "included_content")},
Expand Down Expand Up @@ -134,7 +146,7 @@ func ResourceIBMIsBackupPolicyValidator() *validate.ResourceValidator {
)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "match_resource_types",
Identifier: "match_resource_type",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Expand All @@ -156,8 +168,11 @@ func resourceIBMIsBackupPolicyCreate(context context.Context, d *schema.Resource
createBackupPolicyOptions := &vpcv1.CreateBackupPolicyOptions{}
backupPolicyPrototype := &vpcv1.BackupPolicyPrototype{}

if _, ok := d.GetOk("match_resource_types"); ok {
backupPolicyPrototype.MatchResourceTypes = flex.ExpandStringList((d.Get("match_resource_types").(*schema.Set)).List())
if matchResourceType, ok := d.GetOk("match_resource_type"); ok {
backupPolicyPrototype.MatchResourceType = core.StringPtr(matchResourceType.(string))
} else if matchResourceTypes, ok := d.GetOk("match_resource_types"); ok {
matchResourceTypeList := flex.ExpandStringList((matchResourceTypes.(*schema.Set)).List())
backupPolicyPrototype.MatchResourceType = core.StringPtr(matchResourceTypeList[0])
}
if _, ok := d.GetOk("included_content"); ok {
backupPolicyPrototype.IncludedContent = flex.ExpandStringList((d.Get("included_content").(*schema.Set)).List())
Expand All @@ -175,7 +190,7 @@ func resourceIBMIsBackupPolicyCreate(context context.Context, d *schema.Resource
}
backupPolicyPrototype.ResourceGroup = &resourceGroup
}

createBackupPolicyOptions.SetBackupPolicyPrototype(backupPolicyPrototype)
backupPolicyIntf, response, err := vpcClient.CreateBackupPolicyWithContext(context, createBackupPolicyOptions)
if err != nil {
log.Printf("[DEBUG] CreateBackupPolicyWithContext failed %s\n%s", err, response)
Expand Down Expand Up @@ -209,11 +224,18 @@ func resourceIBMIsBackupPolicyRead(context context.Context, d *schema.ResourceDa
}
backupPolicy := backupPolicyIntf.(*vpcv1.BackupPolicy)

if backupPolicy.MatchResourceTypes != nil {
if err = d.Set("match_resource_types", backupPolicy.MatchResourceTypes); err != nil {
if backupPolicy.MatchResourceType != nil {
matchResourceTypes := *backupPolicy.MatchResourceType
matchResourceTypesList := []string{matchResourceTypes}
if err = d.Set("match_resource_types", matchResourceTypesList); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting match_resource_types: %s", err))
}
}
if backupPolicy.MatchResourceType != nil {
if err = d.Set("match_resource_type", backupPolicy.MatchResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting match_resource_type: %s", err))
}
}
if backupPolicy.IncludedContent != nil {
if err = d.Set("included_content", backupPolicy.IncludedContent); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting included_content: %s", err))
Expand Down
24 changes: 14 additions & 10 deletions ibm/service/vpc/resource_ibm_is_snapshot_consistency_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
scgname := fmt.Sprintf("tf-snap-cons-grp-name-%d", acctest.RandIntRange(10, 100))
deleteSnapshotsOnDeleteUpdate := "false"
snapname := fmt.Sprintf("tf-snap-name-%d", acctest.RandIntRange(10, 100))
scgnameUpdate := fmt.Sprintf("tf-snap-cons-grp-name-%d", acctest.RandIntRange(10, 100))
scgnameUpdate := fmt.Sprintf("tf-snap-cons-grp-name-update-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Expand All @@ -42,7 +42,16 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIsSnapshotConsistencyGroupExists("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", conf),
resource.TestCheckResourceAttr("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "delete_snapshots_on_delete", deleteSnapshotsOnDelete),
resource.TestCheckResourceAttr("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "name", name),
resource.TestCheckResourceAttr("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "name", scgname),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "id"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "name"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "created_at"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "crn"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "href"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "lifecycle_state"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshot_reference.0.id"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshot_reference.0.crn"),
resource.TestCheckResourceAttrSet("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "snapshot_reference.0.name"),
),
},
resource.TestStep{
Expand All @@ -52,16 +61,11 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
resource.TestCheckResourceAttr("ibm_is_snapshot_consistency_group.is_snapshot_consistency_group", "name", scgnameUpdate),
),
},
resource.TestStep{
ResourceName: "ibm_is_snapshot_consistency_group.is_snapshot_consistency_group",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckIBMIsSnapshotConsistencyGroupConfig(vpcname, subnetname, sshname, publicKey, name, scgname, snapname, deleteSnapshotsOnDelete string) string {
func testAccCheckIBMIsSnapshotConsistencyGroupConfig(vpcname, subnetname, sshname, publicKey, name, snapname, scgname, deleteSnapshotsOnDelete string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
Expand Down Expand Up @@ -96,15 +100,15 @@ func testAccCheckIBMIsSnapshotConsistencyGroupConfig(vpcname, subnetname, sshnam
}
}
resource "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group_instance" {
resource "ibm_is_snapshot_consistency_group" "is_snapshot_consistency_group" {
delete_snapshots_on_delete = "%s"
snapshots {
name = "%s"
source_volume = ibm_is_instance.testacc_instance.volume_attachments[0].volume_id
}
name = "%s"
}
`, vpcname, subnetname, acc.ISZoneName, sshname, publicKey, name, "r134-f47cc24c-e020-4db5-ad96-1e5be8b5853b", acc.InstanceProfileName, acc.ISZoneName, deleteSnapshotsOnDelete, snapname, scgname)
`, vpcname, subnetname, acc.ISZoneName, sshname, publicKey, name, "r134-f47cc24c-e020-4db5-ad96-1e5be8b5853b", acc.InstanceProfileName, acc.ISZoneName, deleteSnapshotsOnDelete, scgname, snapname)
}

func testAccCheckIBMIsSnapshotConsistencyGroupExists(n string, obj vpcv1.SnapshotConsistencyGroup) resource.TestCheckFunc {
Expand Down
Loading

0 comments on commit b8576f9

Please sign in to comment.