-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vcd_catalog_vapp_template resource and data source for managing v…
…App Templates (#899) Signed-off-by: abarreiro <abarreiro@vmware.com>
- Loading branch information
1 parent
867c7eb
commit d9aa0ed
Showing
21 changed files
with
1,325 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* **New Resource:** `vcd_catalog_vapp_template` to manage the upload and usage of vApp Templates [GH-899] | ||
* **New Data Source:** `vcd_catalog_vapp_template` to fetch existing vApp Templates [GH-899] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package vcd | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func datasourceVcdCatalogVappTemplate() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: datasourceVcdCatalogVappTemplateRead, | ||
Schema: map[string]*schema.Schema{ | ||
"org": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The name of organization to use, optional if defined at provider " + | ||
"level. Useful when connected as sysadmin working across different organizations", | ||
}, | ||
"catalog_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "ID of the catalog containing the vApp Template. Can't be used if a specific VDC identifier is set", | ||
ExactlyOneOf: []string{"catalog_id", "vdc_id"}, | ||
}, | ||
"vdc_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "ID of the VDC to which the vApp Template belongs. Can't be used if a specific Catalog identifier is set", | ||
ExactlyOneOf: []string{"catalog_id", "vdc_id"}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Name of the vApp Template. It is optional when a filter is provided", | ||
ExactlyOneOf: []string{"name", "filter"}, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Timestamp of when the vApp Template was created", | ||
}, | ||
"metadata": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Description: "Key and value pairs from the metadata of the vApp template", | ||
}, | ||
"vm_names": { | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
Description: "Set of VM names within the vApp template", | ||
}, | ||
"filter": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
MinItems: 1, | ||
Optional: true, | ||
Description: "Criteria for retrieving a vApp Template by various attributes", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name_regex": elementNameRegex, | ||
"date": elementDate, | ||
"earliest": elementEarliest, | ||
"latest": elementLatest, | ||
"metadata": elementMetadata, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func datasourceVcdCatalogVappTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return genericVcdCatalogVappTemplateRead(ctx, d, meta, "datasource") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//go:build catalog || ALL || functional | ||
// +build catalog ALL functional | ||
|
||
package vcd | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// Test catalog and vApp Template data sources. | ||
// Using a catalog data source we reference a vApp Template data source. | ||
// Using a vApp Template data source we create another vApp Template. | ||
// where the description is the first data source ID. | ||
func TestAccVcdCatalogAndVappTemplateDatasource(t *testing.T) { | ||
preTestChecks(t) | ||
createdVAppTemplateName := t.Name() | ||
|
||
var params = StringMap{ | ||
"Org": testConfig.VCD.Org, | ||
"Vdc": testConfig.Nsxt.Vdc, | ||
"Catalog": testConfig.VCD.Catalog.NsxtBackedCatalogName, | ||
"VAppTemplate": testConfig.VCD.Catalog.NsxtCatalogItem, | ||
} | ||
testParamsNotEmpty(t, params) | ||
|
||
configText := templateFill(testAccCheckVcdCatalogVAppTemplateDS, params) | ||
if vcdShortTest { | ||
t.Skip(acceptanceTestsSkipped) | ||
return | ||
} | ||
debugPrintf("#[DEBUG] CONFIGURATION: %s", configText) | ||
|
||
datasourceCatalog := "data.vcd_catalog." + params["Catalog"].(string) | ||
datasourceVdc := "data.vcd_org_vdc." + params["Vdc"].(string) | ||
datasourceCatalogVappTemplate1 := "data.vcd_catalog_vapp_template." + params["VAppTemplate"].(string) + "_1" | ||
datasourceCatalogVappTemplate2 := "data.vcd_catalog_vapp_template." + params["VAppTemplate"].(string) + "_2" | ||
datasourceCatalogVappTemplate3 := "data.vcd_catalog_vapp_template." + params["VAppTemplate"].(string) + "_3" | ||
datasourceCatalogVappTemplate4 := "data.vcd_catalog_vapp_template." + params["VAppTemplate"].(string) + "_4" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { preRunChecks(t) }, | ||
ProviderFactories: testAccProviders, | ||
CheckDestroy: catalogVAppTemplateDestroyed(testSuiteCatalogName, createdVAppTemplateName), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configText, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr(datasourceCatalogVappTemplate1, "id", regexp.MustCompile(`urn:vcloud:vapptemplate:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$`)), | ||
// Check that the attributes from the retrieved vApp Template match the related elements | ||
resource.TestCheckResourceAttrPair(datasourceCatalog, "id", datasourceCatalogVappTemplate1, "catalog_id"), | ||
resource.TestCheckResourceAttrPair(datasourceVdc, "id", datasourceCatalogVappTemplate1, "vdc_id"), | ||
resource.TestCheckResourceAttrSet(datasourceCatalogVappTemplate1, "vm_names.0"), | ||
|
||
// Check both data sources fetched by VDC and Catalog ID are equal | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate1, "id", datasourceCatalogVappTemplate2, "id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate1, "catalog_id", datasourceCatalogVappTemplate2, "catalog_id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate1, "vdc_id", datasourceCatalogVappTemplate2, "vdc_id"), | ||
resource.TestCheckResourceAttrSet(datasourceCatalogVappTemplate2, "vm_names.0"), | ||
|
||
// Check data sources with filter. Not using resourceFieldsEqual here as we'd need to exclude all filtering options by hardcoding the combinations. | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate3, "id", datasourceCatalogVappTemplate1, "id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate3, "catalog_id", datasourceCatalogVappTemplate1, "catalog_id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate3, "vdc_id", datasourceCatalogVappTemplate1, "vdc_id"), | ||
resource.TestCheckResourceAttrSet(datasourceCatalogVappTemplate3, "vm_names.0"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate4, "id", datasourceCatalogVappTemplate1, "id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate4, "catalog_id", datasourceCatalogVappTemplate1, "catalog_id"), | ||
resource.TestCheckResourceAttrPair(datasourceCatalogVappTemplate4, "vdc_id", datasourceCatalogVappTemplate1, "vdc_id"), | ||
resource.TestCheckResourceAttrSet(datasourceCatalogVappTemplate4, "vm_names.0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
postTestChecks(t) | ||
} | ||
|
||
func catalogVAppTemplateDestroyed(catalog, itemName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*VCDClient) | ||
org, err := conn.GetOrgByName(testConfig.VCD.Org) | ||
if err != nil { | ||
return err | ||
} | ||
cat, err := org.GetCatalogByName(catalog, false) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = cat.GetVAppTemplateByName(itemName) | ||
if err == nil { | ||
return fmt.Errorf("vApp Template %s not deleted", itemName) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
const testAccCheckVcdCatalogVAppTemplateDS = ` | ||
data "vcd_catalog" "{{.Catalog}}" { | ||
org = "{{.Org}}" | ||
name = "{{.Catalog}}" | ||
} | ||
data "vcd_org_vdc" "{{.Vdc}}" { | ||
org = "{{.Org}}" | ||
name = "{{.Vdc}}" | ||
} | ||
data "vcd_catalog_vapp_template" "{{.VAppTemplate}}_1" { | ||
org = "{{.Org}}" | ||
catalog_id = data.vcd_catalog.{{.Catalog}}.id | ||
name = "{{.VAppTemplate}}" | ||
} | ||
data "vcd_catalog_vapp_template" "{{.VAppTemplate}}_2" { | ||
org = "{{.Org}}" | ||
vdc_id = data.vcd_org_vdc.{{.Vdc}}.id | ||
name = "{{.VAppTemplate}}" | ||
} | ||
data "vcd_catalog_vapp_template" "{{.VAppTemplate}}_3" { | ||
org = "{{.Org}}" | ||
catalog_id = data.vcd_catalog.{{.Catalog}}.id | ||
filter { | ||
name_regex = "{{.VAppTemplate}}" | ||
} | ||
} | ||
data "vcd_catalog_vapp_template" "{{.VAppTemplate}}_4" { | ||
org = "{{.Org}}" | ||
vdc_id = data.vcd_org_vdc.{{.Vdc}}.id | ||
filter { | ||
name_regex = "{{.VAppTemplate}}" | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.