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 catalog details represeting properties as computed fields #800

Merged
merged 37 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c6aa672
Add new computed values to catalog
Mar 1, 2022
216743c
Add catalog_version, owner_name, number_of_vapp_templates and number_…
Mar 2, 2022
c70114f
Add catalog_version, number_of_vapp_templates and number_of_media to …
Mar 3, 2022
7117261
Add catalog_version, owner_name, number_of_vapp_templates and number_…
Mar 3, 2022
3d9d3a6
Add test for catalog_version
Mar 3, 2022
02ce1b4
Add date-created to catalog datasource
Mar 4, 2022
e9c709f
Add date-created to catalog resource and its tests
Mar 4, 2022
b0e23ec
Remove date-created as it was present already
Mar 4, 2022
b8f9ee9
Add is_shared and is_published computed values to catalog datasource
Mar 4, 2022
807a6d6
Add is_shared and is_published computed fields to catalog resource an…
Mar 4, 2022
24a3663
Add changelog
Mar 4, 2022
7be5c67
Add datasource docs
Mar 4, 2022
dd3889a
Add docs for catalog resource
Mar 4, 2022
6a821ce
Make simple attribute retrival
Mar 17, 2022
9eadd2e
Improve catalog resource test
Mar 17, 2022
9d76580
Improve catalog datasource
Mar 18, 2022
8c277e7
Change `Catalog` by `AdminCatalog` in `getCatalogByFilter`
Mar 18, 2022
388082d
Retrieve `catalogRecords` instead of `adminCatalogRecord`
Mar 18, 2022
632241a
Change `Catalog` by `AdminCatalog` to be consistent with resource.
Mar 18, 2022
0fc3008
Add `publish_subscription_type` attribute
Mar 21, 2022
6f17749
Add `publish_subscription_type` attribute
Mar 21, 2022
8e8d031
Add `publish_subscription_type` to test
Mar 21, 2022
84a3814
Update changelog
Mar 21, 2022
3c8d855
Update datasource to handle better not found entities
Mar 21, 2022
f3fb772
Improve resource to handle better not found entities
Mar 21, 2022
f3d8a04
Update docs
Mar 21, 2022
0808603
Update docs
Mar 21, 2022
ab74f25
Update catalog datasource to allow use either provider block org or d…
Mar 21, 2022
1a87ae5
Fix small bug metadata catalog resource
Mar 21, 2022
d58804a
Improve changelog
Mar 21, 2022
e6741a8
Update go.mod
Mar 25, 2022
316d8aa
Add `setCatalogRecordValuesToSchema` function to avoid having the sam…
Mar 25, 2022
3f6411e
Add `setCatalogRecordValuesToSchema` function to avoid having the sam…
Mar 25, 2022
a16fe53
Fix issue with test
Mar 25, 2022
7136c43
Change ´setCatalogRecordValuesToSchema` for `setCatalogData`
Mar 25, 2022
84e16d7
Change ´setCatalogRecordValuesToSchema` for `setCatalogData`
Mar 25, 2022
81c5cd6
Update go.mod
Mar 25, 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.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)
Didainius marked this conversation as resolved.
Show resolved Hide resolved
mikeletux marked this conversation as resolved.
Show resolved Hide resolved
}

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