Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vcd_catalog_vapp_template resource and data source for managing vApp Templates and deprecate vcd_catalog_item #899

Merged
merged 68 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
72d44b1
Init
Aug 9, 2022
534e7cb
Add deprecated message
Aug 9, 2022
1e217eb
Finish resource and start datasource
Aug 9, 2022
b26021f
Added filtering
Aug 10, 2022
e29e0ae
Merge branch 'main' of https://github.com/adambarreiro/terraform-prov…
Aug 10, 2022
7e40141
Add changelog
Aug 10, 2022
8b5a663
Documentation
Aug 10, 2022
c3ea20c
Modified tests
Aug 10, 2022
4fa722f
#
Aug 10, 2022
ba7127e
#
Aug 10, 2022
706cba7
#
Aug 10, 2022
157ea1f
Add resource tests
Aug 10, 2022
388f42d
Fix test
Aug 10, 2022
30705e7
Revert refactoring
Aug 10, 2022
d95c31e
go mod
Aug 10, 2022
254002c
Remove skip
Aug 10, 2022
a4c3389
fmt
Aug 10, 2022
f85273e
Fix tests
Sep 13, 2022
c9714d1
#
Sep 15, 2022
ad76dc5
Small fixes
Sep 15, 2022
b5f55a8
Fix test
Sep 15, 2022
c6dc14f
Allow to find vApp Templates with VDC and remove Catalog Item dependency
Sep 21, 2022
e79c14d
Refactor findVappTemplate
Sep 22, 2022
8d6d210
Switch to Catalog ID
Sep 22, 2022
3da1ecb
#
Sep 22, 2022
c022de7
Fix bug
Sep 22, 2022
96bf43b
Data source working
Sep 22, 2022
3a43d18
Make tests for resource stable to begin refactoring
Sep 22, 2022
771e422
Cleanup
Sep 22, 2022
c2b599c
Simplify code
Sep 22, 2022
46a73c3
Improve code
Sep 22, 2022
44b0fe4
fmt
Sep 22, 2022
391c58a
Add comment
Sep 22, 2022
1e3502b
Fix docs
Sep 22, 2022
11d390b
Add data source filter tests
Sep 22, 2022
6bb9608
Add data source filter tests
Sep 22, 2022
1b31118
Add filter test
Sep 22, 2022
300f311
Improve tests
Sep 23, 2022
eb29ae9
Improve tests
Sep 23, 2022
336bece
Merge from main
Sep 23, 2022
b08de38
fmt
Sep 23, 2022
f96ab1f
fmt
Sep 23, 2022
0f172ab
fmt
Sep 23, 2022
84e1d22
Bump SDK to v2.17.0-alpha.3
Sep 23, 2022
747269a
Merge from main
Sep 23, 2022
dd97d72
Amend tests
Sep 26, 2022
202a46d
#
Sep 26, 2022
39c4bb2
Remove deprecation messages
Sep 26, 2022
278f9e4
Bump SDK
Sep 26, 2022
c7efbf4
Remove more references to deprecation of Catalog Item
Sep 26, 2022
427ba08
#
Sep 26, 2022
4fe723e
fmt
Sep 26, 2022
1344610
Merge branch 'main' of https://github.com/adambarreiro/terraform-prov…
Sep 27, 2022
5683343
Add vdc_id computed field to vApp Template resource
Sep 27, 2022
5711301
Add vm_names attribute
Sep 27, 2022
f5668d7
Add vm_names attribute
Sep 27, 2022
c5df761
Add vm_names attribute
Sep 27, 2022
16304ed
Add VDC support for Import
Sep 27, 2022
28555c0
Amend typo in documentation
Sep 30, 2022
44ac14e
Revert last commit
Sep 30, 2022
6e542df
Amend data source test
Oct 3, 2022
a469562
SDK bump
Oct 3, 2022
82f0e53
Merge from main
Oct 5, 2022
0560583
Change func name
Oct 17, 2022
ea7123b
Fix typo in docs
Oct 17, 2022
daff599
Apply suggestion
Oct 17, 2022
5ed099d
Merge from main
Oct 19, 2022
51912a9
Bump Go SDK before merge
Oct 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changes/v3.8.0/899-features.md
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]
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
* **New Data Source:** `vcd_catalog_vapp_template` to fetch existing vApp Templates [GH-899]
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-version v1.5.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0
github.com/kr/pretty v0.2.1
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.2
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.3
)

require (
Expand Down Expand Up @@ -59,4 +59,4 @@ require (
google.golang.org/protobuf v1.28.0 // indirect
)

replace github.com/vmware/go-vcloud-director/v2 => github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20220928073049-f27fe7e7496a
replace github.com/vmware/go-vcloud-director/v2 => github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20221003074257-de6792512ffa
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C6
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20220928073049-f27fe7e7496a h1:ZPYLW1zn8TDhBmP0AOyev4RNvBwocEha+MbhSvQVUbE=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20220928073049-f27fe7e7496a/go.mod h1:VRA1ZLDf6CtL1atU1ceMj6/3h9HJg+zjBLaMNODF1qQ=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20221003074257-de6792512ffa h1:h1HD7U2ezGMUj55wcnqRTdA+DIgJ4jMiy7CipkWlXwk=
github.com/adambarreiro/go-vcloud-director/v2 v2.17.0-alpha.1.0.20221003074257-de6792512ffa/go.mod h1:VRA1ZLDf6CtL1atU1ceMj6/3h9HJg+zjBLaMNODF1qQ=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand Down
1 change: 0 additions & 1 deletion vcd/catalogitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func deleteCatalogItem(d *schema.ResourceData, vcdClient *VCDClient) diag.Diagno
}

// Finds catalog item which can be vApp template OVA or media ISO file
// TODO: This function should be updated in the context of Issue #502
func findCatalogItem(d *schema.ResourceData, vcdClient *VCDClient, origin string) (*govcd.CatalogItem, error) {
log.Printf("[TRACE] Catalog item read initiated")

Expand Down
1 change: 1 addition & 0 deletions vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type TestConfig struct {
CatalogItemWithMultiVms string `json:"catalogItemWithMultiVms,omitempty"`
VmName1InMultiVmItem string `json:"vmName1InMultiVmItem,omitempty"`
VmName2InMultiVmItem string `json:"VmName2InMultiVmItem,omitempty"`
NsxtCatalogItem string `json:"nsxtCatalogItem,omitempty"`
NsxtBackedCatalogName string `json:"nsxtBackedCatalogName,omitempty"`
} `json:"catalog"`
} `json:"vcd"`
Expand Down
30 changes: 30 additions & 0 deletions vcd/datasource_not_found_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,36 @@ func addMandatoryParams(dataSourceName string, mandatoryFields []string, t *test
case "catalog":
testParamsNotEmpty(t, StringMap{"VCD.Catalog.Name": testConfig.VCD.Catalog.Name})
templateFields = templateFields + `catalog = "` + testConfig.VCD.Catalog.Name + `"` + "\n"
case "catalog_id":
testParamsNotEmpty(t, StringMap{
"VCD.Org": testConfig.VCD.Org,
"VCD.Catalog.Name": testConfig.VCD.Catalog.Name})
org, err := vcdClient.GetOrgByName(testConfig.VCD.Org)
if err != nil {
t.Skip("No suitable Organization found for this test")
return ""
}
catalog, err := org.GetCatalogByName(testConfig.VCD.Catalog.Name, false)
if err != nil {
t.Skip("No suitable Catalog found for this test")
return ""
}
templateFields = templateFields + `catalog_id = "` + catalog.Catalog.ID + `"` + "\n"
case "vdc_id":
testParamsNotEmpty(t, StringMap{
"VCD.Org": testConfig.VCD.Org,
"VCD.Vdc": testConfig.VCD.Vdc})
org, err := vcdClient.GetOrgByName(testConfig.VCD.Org)
if err != nil {
t.Skip("No suitable Organization found for this test")
return ""
}
vdc, err := org.GetVDCByName(testConfig.VCD.Vdc, false)
if err != nil {
t.Skip("No suitable VDC found for this test")
return ""
}
templateFields = templateFields + `vdc_id = "` + vdc.Vdc.ID + `"` + "\n"
case "vapp_name":
testParamsNotEmpty(t, StringMap{"VCD.Org": testConfig.VCD.Org, "testConfig.Nsxt.Vdc": testConfig.Nsxt.Vdc})
vapp, err := getAvailableVapp()
Expand Down
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_catalog_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAccVcdCatalogAndItemDatasource(t *testing.T) {
datasourceCatalogItem := "data.vcd_catalog_item." + testSuiteCatalogOVAItem
resourceCatalogItem := "vcd_catalog_item." + TestCatalogItemDS
resource.Test(t, resource.TestCase{
PreCheck: func() { preRunChecks(t, params) },
PreCheck: func() { preRunChecks(t) },
ProviderFactories: testAccProviders,
CheckDestroy: catalogItemDestroyed(testSuiteCatalogName, TestCatalogItemDS),
Steps: []resource.TestStep{
Expand Down
4 changes: 2 additions & 2 deletions vcd/datasource_vcd_catalog_media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAccVcdCatalogAndMediaDatasource(t *testing.T) {
debugPrintf("#[DEBUG] CONFIGURATION: %s", configText)

resource.Test(t, resource.TestCase{
PreCheck: func() { preRunChecks(t, params) },
PreCheck: func() { preRunChecks(t) },
ProviderFactories: testAccProviders,
CheckDestroy: catalogMediaDestroyed(testConfig.VCD.Catalog.Name, TestCatalogMediaDS),
Steps: []resource.TestStep{
Expand All @@ -54,7 +54,7 @@ func TestAccVcdCatalogAndMediaDatasource(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckVcdCatalogMediaExists("vcd_catalog_media."+TestAccVcdDataSourceMedia),
resource.TestMatchOutput("owner_name", regexp.MustCompile(`^\S+`)),
resource.TestMatchOutput("creation_date", regexp.MustCompile(`^^\d{4}-\d{2}-\d{2}.*`)),
resource.TestMatchOutput("creation_date", regexp.MustCompile(`^\d{4}-\d{2}-\d{2}.*`)),
resource.TestCheckOutput("status", "RESOLVED"),
resource.TestMatchOutput("storage_profile_name", regexp.MustCompile(`^\S+`)),
testCheckMediaNonStringOutputs(),
Expand Down
81 changes: 81 additions & 0 deletions vcd/datasource_vcd_catalog_vapp_template.go
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",
},
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
"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")
}
138 changes: 138 additions & 0 deletions vcd/datasource_vcd_catalog_vapp_template_test.go
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.
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
// 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}}"
}
}
`
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_storage_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccVcdStorageProfileDS(t *testing.T) {
debugPrintf("#[DEBUG] CONFIGURATION: %s", configText)

resource.Test(t, resource.TestCase{
PreCheck: func() { preRunChecks(t, params) },
PreCheck: func() { preRunChecks(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Expand Down
44 changes: 43 additions & 1 deletion vcd/filter_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func getCatalogByFilter(org *govcd.AdminOrg, filter interface{}, isSysAdmin bool
}

// getCatalogItemByFilter finds a catalog item using a filter block
// TODO: This function should be updated in the context of Issue #502
func getCatalogItemByFilter(catalog *govcd.Catalog, filter interface{}, isSysAdmin bool) (*govcd.CatalogItem, error) {
queryType := types.QtVappTemplate
if isSysAdmin {
Expand All @@ -81,6 +80,49 @@ func getCatalogItemByFilter(catalog *govcd.Catalog, filter interface{}, isSysAdm
return catalogItem, nil
}

// getVappTemplateByCatalogAndFilter finds a vApp Template using a filter block.
// This function deprecates getCatalogItemByFilter
func getVappTemplateByCatalogAndFilter(catalog *govcd.Catalog, filter interface{}, isSysAdmin bool) (*govcd.VAppTemplate, error) {
queryType := types.QtVappTemplate
if isSysAdmin {
queryType = types.QtAdminVappTemplate
}
var searchFunc = func(queryType string, criteria *govcd.FilterDef) ([]govcd.QueryItem, string, error) {
return catalog.SearchByFilter(queryType, "catalogName", criteria)
}
queryItem, err := getEntityByFilter(searchFunc, queryType, "vApp Template", filter)
if err != nil {
return nil, err
}

vAppTemplate, err := catalog.GetVAppTemplateByName(queryItem.GetName())
if err != nil {
return nil, fmt.Errorf("[getVappTemplateByCatalogAndFilter] error retrieving vApp Template %s: %s", queryItem.GetName(), err)
}
return vAppTemplate, nil
}

// getVappTemplateByVdcAndFilter finds a vApp Template using a filter block.
func getVappTemplateByVdcAndFilter(vdc *govcd.Vdc, filter interface{}, isSysAdmin bool) (*govcd.VAppTemplate, error) {
queryType := types.QtVappTemplate
if isSysAdmin {
queryType = types.QtAdminVappTemplate
}
var searchFunc = func(queryType string, criteria *govcd.FilterDef) ([]govcd.QueryItem, string, error) {
return vdc.SearchByFilter(queryType, "vdc", criteria)
}
queryItem, err := getEntityByFilter(searchFunc, queryType, "vApp Template", filter)
if err != nil {
return nil, err
}

vAppTemplate, err := vdc.GetVAppTemplateByName(queryItem.GetName())
if err != nil {
return nil, fmt.Errorf("[getVappTemplateByVdcAndFilter] error retrieving vApp Template %s: %s", queryItem.GetName(), err)
}
return vAppTemplate, nil
}

// getMediaByFilter finds a media item using a filter block
func getMediaByFilter(catalog *govcd.Catalog, filter interface{}, isSysAdmin bool) (*govcd.Media, error) {
queryType := types.QtMedia
Expand Down
Loading