Skip to content

Commit

Permalink
updated the test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwal-ibm committed Sep 4, 2024
1 parent 78a5f20 commit 1eaf7c8
Showing 1 changed file with 14 additions and 151 deletions.
165 changes: 14 additions & 151 deletions ibm/service/vpc/resource_ibm_is_subnet_reserved_ip_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccIBMISSubnetReservedIPUpdateResource_basic(t *testing.T) {
func TestAccIBMISSubnetReservedIPPatchResource_basic(t *testing.T) {
var reservedIPID string
vpcName := fmt.Sprintf("tfresip-vpc-%d", acctest.RandIntRange(10, 100))
subnetName := fmt.Sprintf("tfresip-subnet-%d", acctest.RandIntRange(10, 100))
reservedIPName := fmt.Sprintf("tfresip-reservedip-%d", acctest.RandIntRange(10, 100))
reservedIPName2 := fmt.Sprintf("tfresip-reservedip-%d", acctest.RandIntRange(10, 100))
terraformTag := "ibm_is_subnet_reserved_ip.resIP1"
terraformTag := "ibm_is_subnet_reserved_ip_patch.resIP1"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckisSubnetReservedIPDestroy,
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
// Tests create
Config: testAccCheckISSubnetReservedIPConfigBasic(vpcName, subnetName, reservedIPName),
Config: testAccCheckISSubnetReservedIPPatchConfigBasic(vpcName, subnetName, reservedIPName),
Check: resource.ComposeTestCheckFunc(
testAccCheckISSubnetReservedIPExists(terraformTag, &reservedIPID),
testAccCheckISSubnetReservedIPPatchExists(terraformTag, &reservedIPID),
resource.TestCheckResourceAttrSet(terraformTag, "name"),
resource.TestCheckResourceAttr(terraformTag, "name", reservedIPName),
),
},
{
// Tests Update
Config: testAccCheckISSubnetReservedIPConfigBasic(vpcName, subnetName, reservedIPName2),
Config: testAccCheckISSubnetReservedIPPatchConfigBasic(vpcName, subnetName, reservedIPName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckISSubnetReservedIPExists(terraformTag, &reservedIPID),
resource.TestCheckResourceAttr(terraformTag, "name", reservedIPName2),
Expand All @@ -47,106 +47,13 @@ func TestAccIBMISSubnetReservedIPUpdateResource_basic(t *testing.T) {
},
})
}
func TestAccIBMISSubnetReservedIPUpdateResource_targetCrn(t *testing.T) {
var reservedIPID string
vpcName := fmt.Sprintf("tfresip-vpc-%d", acctest.RandIntRange(10, 100))
subnetName := fmt.Sprintf("tfresip-subnet-%d", acctest.RandIntRange(10, 100))
reservedIPName := fmt.Sprintf("tfresip-reservedip-%d", acctest.RandIntRange(10, 100))
reservedIPName2 := fmt.Sprintf("tfresip-reservedip-%d", acctest.RandIntRange(10, 100))
egateway := fmt.Sprintf("tfresip-egateway-%d", acctest.RandIntRange(10, 100))

terraformTag := "ibm_is_subnet_reserved_ip.resIP1"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckisSubnetReservedIPDestroy,
Steps: []resource.TestStep{
{
// Tests create
Config: testAccCheckISSubnetReservedIPConfigTargetCrn(vpcName, subnetName, reservedIPName, egateway),
Check: resource.ComposeTestCheckFunc(
testAccCheckISSubnetReservedIPExists(terraformTag, &reservedIPID),
resource.TestCheckResourceAttr(terraformTag, "name", reservedIPName),
resource.TestCheckResourceAttrSet(
terraformTag, "target_crn"),
resource.TestCheckResourceAttrSet(
terraformTag, "target"),
resource.TestCheckResourceAttrSet(
"ibm_is_virtual_endpoint_gateway.example", "target.#"),
),
},
{
// Tests Update
Config: testAccCheckISSubnetReservedIPConfigTargetCrn(vpcName, subnetName, reservedIPName2, egateway),
Check: resource.ComposeTestCheckFunc(
testAccCheckISSubnetReservedIPExists(terraformTag, &reservedIPID),
resource.TestCheckResourceAttr(terraformTag, "name", reservedIPName2),
resource.TestCheckResourceAttrSet(
terraformTag, "target_crn"),
resource.TestCheckResourceAttrSet(
terraformTag, "target"),
resource.TestCheckResourceAttrSet(
"ibm_is_virtual_endpoint_gateway.example", "target.#"),
),
},
},
})
}
func TestAccIBMISSubnetReservedIPUpdateResource_address(t *testing.T) {
var reservedIPID string
vpcName := fmt.Sprintf("tfresip-vpc-%d", acctest.RandIntRange(10, 100))
subnetName := fmt.Sprintf("tfresip-subnet-%d", acctest.RandIntRange(10, 100))
reservedIPName := fmt.Sprintf("tfresip-reservedip-%d", acctest.RandIntRange(10, 100))
terraformTag := "ibm_is_subnet_reserved_ip.resIP1"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckisSubnetReservedIPDestroy,
Steps: []resource.TestStep{
{
// Tests create
Config: testAccCheckISSubnetReservedIPConfigAddress(vpcName, subnetName, reservedIPName),
Check: resource.ComposeTestCheckFunc(
testAccCheckISSubnetReservedIPExists(terraformTag, &reservedIPID),
resource.TestCheckResourceAttr(terraformTag, "name", reservedIPName),
resource.TestCheckResourceAttr(terraformTag, "address", "10.240.0.14"),
),
},
},
})
}

func testAccCheckisSubnetReservedIPUpdateDestroy(s *terraform.State) error {
sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).VpcV1API()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "ibm_is_subnet_reserved_ip" {
continue
}

parts, err := flex.IdParts(rs.Primary.ID)
if err != nil {
return err
}

opt := sess.NewGetSubnetReservedIPOptions(parts[0], parts[1])
_, response, err := sess.GetSubnetReservedIP(opt)
if err == nil {
return fmt.Errorf("Reserved IP still exists: %v", response)
}
}
return nil
}

func testAccCheckISSubnetReservedIPUpdateExists(resIPName string, reservedIPID *string) resource.TestCheckFunc {
func testAccCheckISSubnetReservedIPPatchExists(resIPName string, reservedIPID *string) resource.TestCheckFunc {
return func(s *terraform.State) error {

rs, ok := s.RootModule().Resources[resIPName]
if !ok {
return fmt.Errorf("Not Found (reserved IP): %s", resIPName)
return fmt.Errorf("Not Found (reserved IP patch): %s", resIPName)
}
if rs.Primary.ID == "" {
return fmt.Errorf("[ERROR] No reserved IP ID is set")
Expand All @@ -170,7 +77,7 @@ func testAccCheckISSubnetReservedIPUpdateExists(resIPName string, reservedIPID *
}
}

func testAccCheckISSubnetReservedIPUpdateConfigBasic(vpcName, subnetName, resIPName string) string {
func testAccCheckISSubnetReservedIPPatchConfigBasic(vpcName, subnetName, resIPName string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "vpc1" {
name = "%s"
Expand All @@ -194,57 +101,13 @@ func testAccCheckISSubnetReservedIPUpdateConfigBasic(vpcName, subnetName, resIPN
resource "ibm_is_subnet_reserved_ip" "resIP1" {
subnet = ibm_is_subnet.subnet1.id
name = "%s"
target = ibm_is_virtual_endpoint_gateway.endpoint_gateway.id
}
`, vpcName, subnetName, resIPName)
}
func testAccCheckISSubnetReservedIPUpdateConfigTargetCrn(vpcName, subnetName, resIPName, egateway string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "vpc1" {
name = "%s"
}
resource "ibm_is_subnet" "subnet1" {
name = "%s"
vpc = ibm_is_vpc.vpc1.id
zone = "us-south-1"
total_ipv4_address_count = 256
}
resource "ibm_is_virtual_endpoint_gateway" "example" {
name = "%s"
target {
name = "ibm-ntp-server"
resource_type = "provider_infrastructure_service"
}
vpc = ibm_is_vpc.vpc1.id
}
resource "ibm_is_subnet_reserved_ip" "resIP1" {
resource "ibm_is_subnet_reserved_ip_patch" "resIP1" {
subnet = ibm_is_subnet.subnet1.id
name = "%s"
target = ibm_is_virtual_endpoint_gateway.example.id
reserved_ip = ibm_is_subnet_reserved_ip.resIP1.reserved_ip
}
`, vpcName, subnetName, egateway, resIPName)
}
func testAccCheckISSubnetReservedIPUpdateConfigAddress(vpcName, subnetName, resIPName string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "vpc1" {
name = "%s"
}
resource "ibm_is_subnet" "subnet1" {
name = "%s"
vpc = ibm_is_vpc.vpc1.id
zone = "%s"
ipv4_cidr_block = "%s"
}
resource "ibm_is_subnet_reserved_ip" "resIP1" {
subnet = ibm_is_subnet.subnet1.id
name = "%s"
address = "${replace(ibm_is_subnet.subnet1.ipv4_cidr_block, "0/24", "14")}"
}
`, vpcName, subnetName, acc.ISZoneName, acc.ISCIDR, resIPName)
`, vpcName, subnetName, resIPName)
}

0 comments on commit 1eaf7c8

Please sign in to comment.