diff --git a/vcd/common_test.go b/vcd/common_test.go index fe82640cf..4d448bff6 100644 --- a/vcd/common_test.go +++ b/vcd/common_test.go @@ -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 diff --git a/vcd/resource_vcd_multisite_org_association_test.go b/vcd/resource_vcd_multisite_org_association_test.go index e160c9207..b78d10ddd 100644 --- a/vcd/resource_vcd_multisite_org_association_test.go +++ b/vcd/resource_vcd_multisite_org_association_test.go @@ -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" @@ -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} { @@ -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}} diff --git a/vcd/resource_vcd_multisite_site_association_test.go b/vcd/resource_vcd_multisite_site_association_test.go index 68c59bac6..2401ed7a7 100644 --- a/vcd/resource_vcd_multisite_site_association_test.go +++ b/vcd/resource_vcd_multisite_site_association_test.go @@ -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" @@ -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}}