Skip to content

Commit

Permalink
Add catalog details represeting properties as computed fields (#800)
Browse files Browse the repository at this point in the history
Add support for computed attributes `catalog_version`, `owner_name`,  `number_of_vapp_templates`, `number_of_media` , `is_shared`, `is_published` and `publish_subscription_type` for vcd_catalog resource and datasource.

Signed-off-by: Miguel Sama <[email protected]>
  • Loading branch information
Miguel Sama authored Mar 25, 2022
1 parent 79cb534 commit 1ef8224
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .changes/v3.6.0/800-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Add `catalog_version`, `number_of_vapp_templates`, `number_of_media`, `is_shared`, `is_published`, `publish_subscription_type` computed fields to catalog resource and datasource [GH-800]
* Update `vcd_catalog` datasource so now it can take org from provider level or datasource level like modern resources/datasources [GH-800]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ require (
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.0
github.com/kr/pretty v0.2.1
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.8
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.9
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaU
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.8 h1:0+DsW+KbPU+aKXzKSjSvJA7ZvpjB2V6q2TvhWXXnryA=
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.8/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.9 h1:4bmhpu6116aDka6yqHC4Qw2LJXf4SC4hm9jgrARZ4Ws=
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.9/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
79 changes: 57 additions & 22 deletions vcd/datasource_vcd_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func datasourceVcdCatalog() *schema.Resource {
Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Required: true,
Optional: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
Expand Down Expand Up @@ -62,6 +62,41 @@ func datasourceVcdCatalog() *schema.Resource {
Computed: true,
Description: "Key and value pairs for catalog metadata",
},
"catalog_version": {
Type: schema.TypeInt,
Computed: true,
Description: "Catalog version number.",
},
"owner_name": {
Type: schema.TypeString,
Computed: true,
Description: "Owner name from the catalog.",
},
"number_of_vapp_templates": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of vApps this catalog contains.",
},
"number_of_media": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of Medias this catalog contains.",
},
"is_shared": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this catalog is shared.",
},
"is_published": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this catalog is shared to all organizations.",
},
"publish_subscription_type": {
Type: schema.TypeString,
Computed: true,
Description: "PUBLISHED if published externally, SUBSCRIBED if subscribed to an external catalog, UNPUBLISHED otherwise.",
},
"filter": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Expand All @@ -85,36 +120,30 @@ func datasourceVcdCatalog() *schema.Resource {
func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
vcdClient = meta.(*VCDClient)
err error
adminOrg *govcd.AdminOrg
catalog *govcd.Catalog
catalog *govcd.AdminCatalog
)

if !nameOrFilterIsSet(d) {
return diag.Errorf(noNameOrFilterError, "vcd_catalog")
}
orgName := d.Get("org").(string)
identifier := d.Get("name").(string)
log.Printf("[TRACE] Reading Org %s", orgName)
adminOrg, err = vcdClient.VCDClient.GetAdminOrgByName(orgName)

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
if err != nil {
log.Printf("[DEBUG] Org %s not found. Setting ID to nothing", orgName)
d.SetId("")
return nil
return diag.Errorf(errorRetrievingOrg, err)
}
log.Printf("[TRACE] Org %s found", orgName)
log.Printf("[TRACE] Org %s found", adminOrg.AdminOrg.Name)

identifier := d.Get("name").(string)

filter, hasFilter := d.GetOk("filter")

if hasFilter {
catalog, err = getCatalogByFilter(adminOrg, filter, vcdClient.Client.IsSysAdmin)
} else {
catalog, err = adminOrg.GetCatalogByNameOrId(identifier, false)
catalog, err = adminOrg.GetAdminCatalogByNameOrId(identifier, false)
}
if err != nil {
log.Printf("[DEBUG] Catalog %s not found. Setting ID to nothing", identifier)
d.SetId("")
return diag.Errorf("error retrieving catalog %s: %s", identifier, err)
}

Expand All @@ -124,20 +153,26 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("There was an issue when retrieving metadata - %s", err)
}

dSet(d, "description", catalog.Catalog.Description)
dSet(d, "created", catalog.Catalog.DateCreated)
dSet(d, "name", catalog.Catalog.Name)
d.SetId(catalog.Catalog.ID)
if catalog.Catalog.PublishExternalCatalogParams != nil {
dSet(d, "publish_enabled", catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally)
dSet(d, "cache_enabled", catalog.Catalog.PublishExternalCatalogParams.IsCachedEnabled)
dSet(d, "preserve_identity_information", catalog.Catalog.PublishExternalCatalogParams.PreserveIdentityInfoFlag)
dSet(d, "description", catalog.AdminCatalog.Description)
dSet(d, "created", catalog.AdminCatalog.DateCreated)
dSet(d, "name", catalog.AdminCatalog.Name)

d.SetId(catalog.AdminCatalog.ID)
if catalog.AdminCatalog.PublishExternalCatalogParams != nil {
dSet(d, "publish_enabled", catalog.AdminCatalog.PublishExternalCatalogParams.IsPublishedExternally)
dSet(d, "cache_enabled", catalog.AdminCatalog.PublishExternalCatalogParams.IsCachedEnabled)
dSet(d, "preserve_identity_information", catalog.AdminCatalog.PublishExternalCatalogParams.PreserveIdentityInfoFlag)
}

err = d.Set("metadata", getMetadataStruct(metadata.MetadataEntry))
if err != nil {
return diag.Errorf("There was an issue when setting metadata into the schema - %s", err)
}

err = setCatalogData(d, adminOrg, catalog.AdminCatalog.Name)
if err != nil {
return diag.FromErr(err)
}

return nil
}
4 changes: 2 additions & 2 deletions vcd/filter_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getEntityByFilter(search searchByFilterFunc, queryType, label string, filte
}

// getCatalogByFilter finds a catalog using a filter block
func getCatalogByFilter(org *govcd.AdminOrg, filter interface{}, isSysAdmin bool) (*govcd.Catalog, error) {
func getCatalogByFilter(org *govcd.AdminOrg, filter interface{}, isSysAdmin bool) (*govcd.AdminCatalog, error) {
queryType := types.QtCatalog
if isSysAdmin {
queryType = types.QtAdminCatalog
Expand All @@ -52,7 +52,7 @@ func getCatalogByFilter(org *govcd.AdminOrg, filter interface{}, isSysAdmin bool
return nil, err
}

catalog, err := org.GetCatalogByHref(queryItem.GetHref())
catalog, err := org.GetAdminCatalogByHref(queryItem.GetHref())
if err != nil {
return nil, fmt.Errorf("[getCatalogByFilter] error retrieving catalog %s: %s", queryItem.GetName(), err)
}
Expand Down
91 changes: 78 additions & 13 deletions vcd/resource_vcd_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,50 @@ func resourceVcdCatalog() *schema.Resource {
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
},
"storage_profile_id": &schema.Schema{
"storage_profile_id": {
Type: schema.TypeString,
Optional: true,
Description: "Optional storage profile ID",
},
"created": &schema.Schema{
"created": {
Type: schema.TypeString,
Computed: true,
Description: "Time stamp of when the catalog was created",
},
"delete_force": &schema.Schema{
"delete_force": {
Type: schema.TypeBool,
Required: true,
ForceNew: false,
Description: "When destroying use delete_force=True with delete_recursive=True to remove a catalog and any objects it contains, regardless of their state.",
},
"delete_recursive": &schema.Schema{
"delete_recursive": {
Type: schema.TypeBool,
Required: true,
ForceNew: false,
Description: "When destroying use delete_recursive=True to remove the catalog and any objects it contains that are in a state that normally allows removal.",
},
"publish_enabled": &schema.Schema{
"publish_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "True allows to publish a catalog externally to make its vApp templates and media files available for subscription by organizations outside the Cloud Director installation.",
},
"cache_enabled": &schema.Schema{
"cache_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "True enables early catalog export to optimize synchronization",
},
"preserve_identity_information": &schema.Schema{
"preserve_identity_information": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Expand All @@ -89,7 +89,42 @@ func resourceVcdCatalog() *schema.Resource {
"metadata": {
Type: schema.TypeMap,
Optional: true,
Description: "Key and value pairs for catalog metadata",
Description: "Key and value pairs for catalog metadata.",
},
"catalog_version": {
Type: schema.TypeInt,
Computed: true,
Description: "Catalog version number.",
},
"owner_name": {
Type: schema.TypeString,
Computed: true,
Description: "Owner name from the catalog.",
},
"number_of_vapp_templates": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of vApps templates this catalog contains.",
},
"number_of_media": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of Medias this catalog contains.",
},
"is_shared": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this catalog is shared.",
},
"is_published": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this catalog is shared to all organizations.",
},
"publish_subscription_type": {
Type: schema.TypeString,
Computed: true,
Description: "PUBLISHED if published externally, SUBSCRIBED if subscribed to an external catalog, UNPUBLISHED otherwise.",
},
},
}
Expand Down Expand Up @@ -177,8 +212,12 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err

adminCatalog, err := adminOrg.GetAdminCatalogByNameOrId(d.Id(), false)
if err != nil {
log.Printf("[DEBUG] Unable to find catalog. Removing from tfstate")
d.SetId("")
if govcd.ContainsNotFound(err) {
log.Printf("[DEBUG] Unable to find catalog. Removing from tfstate")
d.SetId("")
return nil
}

return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err)
}

Expand Down Expand Up @@ -212,7 +251,14 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err
return err
}

err = d.Set("metadata", getMetadataStruct(metadata.MetadataEntry))
if len(metadata.MetadataEntry) > 0 {
err = d.Set("metadata", getMetadataStruct(metadata.MetadataEntry))
if err != nil {
return err
}
}

err = setCatalogData(d, adminOrg, adminCatalog.AdminCatalog.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -400,3 +446,22 @@ func createOrUpdateAdminCatalogMetadata(d *schema.ResourceData, meta interface{}
}
return nil
}

func setCatalogData(d *schema.ResourceData, adminOrg *govcd.AdminOrg, catalogName string) error {
// Catalog record is retrieved to get the owner name, number of vApp templates and medias, and if the catalog is shared and published
catalogRecords, err := adminOrg.FindCatalogRecords(catalogName)
if err != nil {
log.Printf("[DEBUG] Unable to retrieve catalog record: %s", err)
return fmt.Errorf("unable to retrieve catalog record - %s", err)
}

dSet(d, "catalog_version", catalogRecords[0].Version)
dSet(d, "owner_name", catalogRecords[0].OwnerName)
dSet(d, "number_of_vapp_templates", catalogRecords[0].NumberOfVAppTemplates)
dSet(d, "number_of_media", catalogRecords[0].NumberOfMedia)
dSet(d, "is_published", catalogRecords[0].IsPublished)
dSet(d, "is_shared", catalogRecords[0].IsShared)
dSet(d, "publish_subscription_type", catalogRecords[0].PublishSubscriptionType)

return nil
}
Loading

0 comments on commit 1ef8224

Please sign in to comment.