Skip to content

Commit

Permalink
Deprecated match_resource_types and Intoduced match_resource_type (#4863
Browse files Browse the repository at this point in the history
)

* Deprecated match_resource_types and Intoduced match_resource_type

* Implemented acceptance test
  • Loading branch information
SunithaGudisagarIBM1 authored Oct 16, 2023
1 parent a3ae7e4 commit d94fb51
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
38 changes: 30 additions & 8 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,22 @@ 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",
ConflictsWith: []string{"match_resource_types"},
ValidateFunc: validate.InvokeValidator("ibm_is_backup_policy", "match_resource_types"),
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 Down Expand Up @@ -118,7 +128,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 @@ -139,9 +149,14 @@ func resourceIBMIsBackupPolicyCreate(context context.Context, d *schema.Resource

createBackupPolicyOptions := &vpcv1.CreateBackupPolicyOptions{}

if _, ok := d.GetOk("match_resource_types"); ok {
if matchResourceType, ok := d.GetOk("match_resource_type"); ok {
matchResourceTypes := matchResourceType.(string)
matchResourceTypesList := []string{matchResourceTypes}
createBackupPolicyOptions.SetMatchResourceTypes(matchResourceTypesList)
} else if _, ok := d.GetOk("match_resource_types"); ok {
createBackupPolicyOptions.SetMatchResourceTypes(flex.ExpandStringList((d.Get("match_resource_types").(*schema.Set)).List()))
}

if _, ok := d.GetOk("match_user_tags"); ok {
createBackupPolicyOptions.SetMatchUserTags((flex.ExpandStringList((d.Get("match_user_tags").(*schema.Set)).List())))
}
Expand Down Expand Up @@ -192,6 +207,13 @@ func resourceIBMIsBackupPolicyRead(context context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf("[ERROR] Error setting match_resource_types: %s", err))
}
}
if backupPolicy.MatchResourceTypes != nil {
for _, matchResourceTypes := range backupPolicy.MatchResourceTypes {
if err = d.Set("match_resource_type", matchResourceTypes); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting match_resource_type: %s", err))
}
}
}
if backupPolicy.MatchUserTags != nil {
if err = d.Set("match_user_tags", backupPolicy.MatchUserTags); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting match_user_tags: %s", err))
Expand Down
101 changes: 101 additions & 0 deletions ibm/service/vpc/resource_ibm_is_backup_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,59 @@ func TestAccIBMIsBackupPolicyBasic(t *testing.T) {
})
}

func TestAccIBMIsBackupPolicyMatchResourceType(t *testing.T) {
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tf-subnet-%d", acctest.RandIntRange(10, 100))
sshname := fmt.Sprintf("tf-ssh-%d", acctest.RandIntRange(10, 100))
volname := fmt.Sprintf("tf-vol-%d", acctest.RandIntRange(10, 100))
backupPolicyName := fmt.Sprintf("tfbakuppolicyname%d", acctest.RandIntRange(10, 100))
backupPolicyNameUpdate := fmt.Sprintf("tfbakuppolicyname%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMIsBackupPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMIsBackupPolicyConfigMatchResourceType(backupPolicyName, vpcname, subnetname, sshname, volname, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ibm_is_backup_policy.is_backup_policy", "name", backupPolicyName),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "match_resource_types.#"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "match_user_tags.#"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "resource_group"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "created_at"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "crn"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "href"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "lifecycle_state"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "resource_type"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "version"),
),
},
resource.TestStep{
Config: testAccCheckIBMIsBackupPolicyConfigMatchResourceType(backupPolicyNameUpdate, vpcname, subnetname, sshname, volname, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ibm_is_backup_policy.is_backup_policy", "name", backupPolicyNameUpdate),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "match_resource_types.#"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "match_user_tags.#"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "resource_group"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "created_at"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "crn"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "href"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "lifecycle_state"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "resource_type"),
resource.TestCheckResourceAttrSet("ibm_is_backup_policy.is_backup_policy", "version"),
),
},
{
ResourceName: "ibm_is_backup_policy.is_backup_policy",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckIBMIsBackupPolicyConfigBasic(backupPolicyName string, vpcname, subnetname, sshname, volName, name string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
Expand Down Expand Up @@ -116,6 +169,54 @@ func testAccCheckIBMIsBackupPolicyConfigBasic(backupPolicyName string, vpcname,
}`, vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, sshname, volName, acc.ISZoneName, name, acc.IsImage, acc.InstanceProfileName, acc.ISZoneName, backupPolicyName)
}

func testAccCheckIBMIsBackupPolicyConfigMatchResourceType(backupPolicyName string, vpcname, subnetname, sshname, volName, name string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_subnet" "testacc_subnet" {
name = "%s"
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
ipv4_cidr_block = "%s"
}
resource "ibm_is_ssh_key" "testacc_sshkey" {
name = "%s"
// public_key = file("../../test-fixtures/.ssh/id_rsa")
public_key = file("~/.ssh/id_rsa.pub")
}
resource "ibm_is_volume" "storage" {
name = "%s"
profile = "10iops-tier"
zone = "%s"
# capacity= 200
tags = ["tag-0"]
}
resource "ibm_is_instance" "testacc_instance" {
name = "%s"
image = "%s"
profile = "%s"
primary_network_interface {
subnet = ibm_is_subnet.testacc_subnet.id
}
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
keys = [ibm_is_ssh_key.testacc_sshkey.id]
volumes = [ibm_is_volume.storage.id]
}
resource "ibm_is_backup_policy" "is_backup_policy" {
depends_on = [ibm_is_instance.testacc_instance]
match_user_tags = ["tag-0"]
match_resource_type = "volume"
name = "%s"
}`, vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, sshname, volName, acc.ISZoneName, name, acc.IsImage, acc.InstanceProfileName, acc.ISZoneName, backupPolicyName)
}

func testAccCheckIBMIsBackupPolicyDestroy(s *terraform.State) error {
vpcClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).VpcV1API()

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/is_backup_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ resource "ibm_is_backup_policy" "example" {
Review the argument reference that you can specify for your resource.

- `match_resource_types` - (Optional, List) 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. The default value is `["volume"]`.

~> **Note**
`match_resource_types` is deprecated. Please use `match_resource_type` instead.
- `match_resource_type` - (Optional, String) The resource type this backup policy will apply to. Resources that have both a matching type and a matching user tag will be subject to the backup policy. The default value is `["volume"]`.
- `match_user_tags` - (Required, List) The user tags this backup policy applies to. Resources that have both a matching user tag and a matching type will be subject to the backup policy.
- `name` - (Required, String) The user-defined name for this backup policy. Names must be unique within the region this backup policy resides in.
- `resource_group` - (Optional, List) The resource group id, to use. If unspecified, the account's [default resource group](https://cloud.ibm.com/apidocs/resource-manager#introduction) is used.
Expand Down

0 comments on commit d94fb51

Please sign in to comment.