Skip to content

Commit

Permalink
Add import test for multisite resources
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Maxia <[email protected]>
  • Loading branch information
Giuseppe Maxia committed Jun 24, 2024
1 parent d4c3255 commit a54246d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion vcd/common_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build network || nsxt || gateway || org || catalog || access_control || networkPool || providerVdc || vm || vapp || standaloneVm || user || ALL || functional
//go:build network || nsxt || gateway || org || catalog || access_control || networkPool || providerVdc || vm || vapp || standaloneVm || user || multisite || ALL || functional

package vcd

Expand Down
34 changes: 33 additions & 1 deletion vcd/resource_vcd_multisite_org_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package vcd
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
"os"
"regexp"
Expand Down Expand Up @@ -49,7 +51,6 @@ func TestVcdMultisiteOrgAssociation(t *testing.T) {
t.Skip(acceptanceTestsSkipped)
return
}

defer func() {
// Remove XML files, if they were left behind
for _, fName := range []string{org1Xml, org2Xml} {
Expand Down Expand Up @@ -93,12 +94,43 @@ func TestVcdMultisiteOrgAssociation(t *testing.T) {
resource.TestCheckResourceAttr("vcd_multisite_org_association.org2-org1", "status", string(types.StatusActive)),
),
},
{
ResourceName: "vcd_multisite_org_association.org1-org2",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importOrgStateIdFromXmlFile(org2Xml, org1Name),
ImportStateVerifyIgnore: []string{"association_data_file", "connection_timeout_mins"},
},
{
ResourceName: "vcd_multisite_org_association.org2-org1",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importOrgStateIdFromXmlFile(org1Xml, org2Name),
ImportStateVerifyIgnore: []string{"association_data_file", "connection_timeout_mins"},
},
},
})

postTestChecks(t)
}

func importOrgStateIdFromXmlFile(fileName, orgName string) resource.ImportStateIdFunc {
return func(*terraform.State) (string, error) {
result, err := govcd.ReadXmlDataFromFile[types.OrgAssociationMember](fileName)
if err != nil {
return "", fmt.Errorf("error getting %T from file %s: %s", types.OrgAssociationMember{}, fileName, err)
}
conn := testAccProvider.Meta().(*VCDClient)

org, err := conn.GetOrgByName(orgName)
if err != nil {
return "", fmt.Errorf("error retrieving org %s: %s", orgName, err)
}

return org.Org.ID + ImportSeparator + result.OrgID, nil
}
}

const testAccMultisiteOrgCommon = `
data "vcd_resource_list" "orgs" {
provider = {{.ProviderVcdSystem}}
Expand Down
26 changes: 26 additions & 0 deletions vcd/resource_vcd_multisite_site_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package vcd
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
"os"
"regexp"
Expand Down Expand Up @@ -141,12 +143,36 @@ func TestVcdMultisiteSiteAssociation(t *testing.T) {
resource.TestCheckResourceAttr("vcd_multisite_site_association.site2-site1", "status", string(types.StatusActive)),
),
},
{
ResourceName: "vcd_multisite_site_association.site1-site2",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importSiteStateIdFromXmlFile(site2XmlName),
ImportStateVerifyIgnore: []string{"association_data_file", "connection_timeout_mins"},
},
{
ResourceName: "vcd_multisite_site_association.site2-site1",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importSiteStateIdFromXmlFile(site1XmlName),
ImportStateVerifyIgnore: []string{"association_data_file", "connection_timeout_mins"},
},
},
})

postTestChecks(t)
}

func importSiteStateIdFromXmlFile(fileName string) resource.ImportStateIdFunc {
return func(*terraform.State) (string, error) {
result, err := govcd.ReadXmlDataFromFile[types.SiteAssociationMember](fileName)
if err != nil {
return "", fmt.Errorf("error getting %T from file %s: %s", types.SiteAssociationMember{}, fileName, err)
}
return result.SiteID, nil
}
}

const testAccMultisiteSiteData = `
{{.SkipNotice}}
Expand Down

0 comments on commit a54246d

Please sign in to comment.