From c6aa6728a4e9cbcfcf364551796b70b944fd4202 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Tue, 1 Mar 2022 11:11:34 +0100 Subject: [PATCH 01/37] Add new computed values to catalog Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 43 ++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 92f8a7e4d..d3350def3 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -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, @@ -89,7 +89,27 @@ 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.TypeString, + 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.", }, }, } @@ -217,6 +237,9 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return err } + dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) + dSet(d, "owner_name", adminCatalog.AdminCatalog.Owner.User.Name) + d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) return nil From 216743cea9a3410a84f12d400dcd3c517495627b Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Wed, 2 Mar 2022 10:41:36 +0100 Subject: [PATCH 02/37] Add catalog_version, owner_name, number_of_vapp_templates and number_of_media to catalog resource schema Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 31 +++++++++++++++++++-- vcd/resource_vcd_catalog_test.go | 48 ++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index d3350def3..bad8d603d 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -92,7 +92,7 @@ func resourceVcdCatalog() *schema.Resource { Description: "Key and value pairs for catalog metadata.", }, "catalog_version": { - Type: schema.TypeString, + Type: schema.TypeInt, Computed: true, Description: "Catalog version number.", }, @@ -202,6 +202,14 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) } + // catalog user view is retrieved to get the number of vApp templates and medias + catalog, err := adminOrg.GetCatalogById(adminCatalog.AdminCatalog.ID, false) + if err != nil { + log.Printf("[DEBUG] Unable to find catalog. Removing from tfstate") + d.SetId("") + return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) + } + // Check if storage profile is set. Although storage profile structure accepts a list, in UI only one can be picked if adminCatalog.AdminCatalog.CatalogStorageProfiles != nil && len(adminCatalog.AdminCatalog.CatalogStorageProfiles.VdcStorageProfile) > 0 { // By default API does not return Storage Profile Name in response. It has ID and HREF, but not Name so name @@ -238,7 +246,26 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) - dSet(d, "owner_name", adminCatalog.AdminCatalog.Owner.User.Name) + // dSet(d, "owner_name", adminCatalog.AdminCatalog.Owner.User.Name) + + catalogItemTypes, err := catalog.QueryCatalogItemList() + if err != nil { + log.Printf("[DEBUG] Unable to retrieve catalog items: %s", err) + return err + } + + var numberOfVAppTemplates, numberOfMedia int + for _, v := range catalogItemTypes { + switch v.Type { + case "application/vnd.vmware.vcloud.media+xml": + numberOfMedia++ + case "application/vnd.vmware.vcloud.vAppTemplate+xml": + numberOfVAppTemplates++ + } + } + + dSet(d, "number_of_vapp_templates", numberOfVAppTemplates) + dSet(d, "number_of_media", numberOfMedia) d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 50d002a99..51064974c 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -27,11 +27,20 @@ var TestAccVcdCatalogDescription = "TestAccVcdCatalogBasicDescription" func TestAccVcdCatalog(t *testing.T) { preTestChecks(t) var params = StringMap{ - "Org": testConfig.VCD.Org, - "CatalogName": TestAccVcdCatalogName, - "Description": TestAccVcdCatalogDescription, - "StorageProfile": testConfig.VCD.ProviderVdc.StorageProfile, - "Tags": "catalog", + "Org": testConfig.VCD.Org, + "CatalogName": TestAccVcdCatalogName, + "Description": TestAccVcdCatalogDescription, + "StorageProfile": testConfig.VCD.ProviderVdc.StorageProfile, + "CatalogItemName": "TestCatalogItem", + "CatalogItemNameFromUrl": "Test", + "DescriptionFromUrl": "Test", + "OvaPath": testConfig.Ova.OvaPath, + "UploadProgressFromUrl": testConfig.Ova.UploadProgress, + "CatalogMediaName": "TestCatalogMedia", + "MediaPath": testConfig.Media.MediaPath, + "UploadPieceSize": testConfig.Media.UploadPieceSize, + "UploadProgress": testConfig.Media.UploadProgress, + "Tags": "catalog", } configText := templateFill(testAccCheckVcdCatalog, params) @@ -56,7 +65,7 @@ func TestAccVcdCatalog(t *testing.T) { ProviderFactories: testAccProviders, CheckDestroy: testAccCheckCatalogDestroy, Steps: []resource.TestStep{ - // Provision catalog without storage profile + // Provision catalog without storage profile and a vApp template and media resource.TestStep{ Config: configText, Check: resource.ComposeTestCheckFunc( @@ -69,6 +78,10 @@ func TestAccVcdCatalog(t *testing.T) { resourceAddress, "metadata.catalog_metadata", "catalog Metadata"), resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2"), + //resource.TestCheckResourceAttr(), + //resource.TestCheckResourceAttr(), + resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), + resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), ), }, // Set storage profile for existing catalog @@ -372,6 +385,29 @@ resource "vcd_catalog" "test-catalog" { catalog_metadata2 = "catalog Metadata2" } } + +resource "vcd_catalog_item" "{{.CatalogItemName}}" { + org = "{{.Org}}" + catalog = resource.vcd_catalog.test-catalog.name + + name = "{{.CatalogItemName}}" + description = "{{.Description}}" + ova_path = "{{.OvaPath}}" + upload_piece_size = {{.UploadPieceSize}} + show_upload_progress = "{{.UploadProgress}}" +} + +resource "vcd_catalog_media" "{{.CatalogMediaName}}" { + org = "{{.Org}}" + catalog = resource.vcd_catalog.test-catalog.name + + name = "{{.CatalogMediaName}}" + description = "{{.Description}}" + media_path = "{{.MediaPath}}" + upload_piece_size = {{.UploadPieceSize}} + show_upload_progress = "{{.UploadProgress}}" +} + ` const testAccCheckVcdCatalogStep1 = ` From c70114f76aab01884821e3818258c4d05f38a986 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Thu, 3 Mar 2022 15:23:47 +0100 Subject: [PATCH 03/37] Add catalog_version, number_of_vapp_templates and number_of_media to catalog datasource Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 785fac85d..78f891a25 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -62,6 +62,26 @@ 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": { // owner_name is not available because catalog user view doesn't have this value + 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.", + }, "filter": &schema.Schema{ Type: schema.TypeList, MaxItems: 1, @@ -127,6 +147,7 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta dSet(d, "description", catalog.Catalog.Description) dSet(d, "created", catalog.Catalog.DateCreated) dSet(d, "name", catalog.Catalog.Name) + dSet(d, "catalog_version", catalog.Catalog.VersionNumber) d.SetId(catalog.Catalog.ID) if catalog.Catalog.PublishExternalCatalogParams != nil { dSet(d, "publish_enabled", catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally) @@ -139,5 +160,21 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %s", err) } + numberOfVAppTemplates, err := catalog.QueryVappTemplateList() + if err != nil { + log.Printf("[DEBUG] Unable to retrieve vApp templates associated to this catalog: %s", err) + return diag.Errorf("There was an issue when retrieving vApp templates - %s", err) + } + + dSet(d, "number_of_vapp_templates", len(numberOfVAppTemplates)) + + numberOfMedia, err := catalog.QueryMediaList() + if err != nil { + log.Printf("[DEBUG] Unable to retrieve media items associated to this catalog: %s", err) + return diag.Errorf("There was an issue when retrieving media items - %s", err) + } + + dSet(d, "number_of_media", len(numberOfMedia)) + return nil } From 7117261cdc9182b89d29cf211f54e996c31d8235 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Thu, 3 Mar 2022 15:24:44 +0100 Subject: [PATCH 04/37] Add catalog_version, owner_name, number_of_vapp_templates and number_of_media to resource schema Add test for number_of_vapp_templates and number_of_media Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 27 ++++++++++--------------- vcd/resource_vcd_catalog_test.go | 34 ++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index bad8d603d..0115124a0 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -104,7 +104,7 @@ func resourceVcdCatalog() *schema.Resource { "number_of_vapp_templates": { Type: schema.TypeInt, Computed: true, - Description: "Number of vApps this catalog contains.", + Description: "Number of vApps templates this catalog contains.", }, "number_of_media": { Type: schema.TypeInt, @@ -203,7 +203,7 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } // catalog user view is retrieved to get the number of vApp templates and medias - catalog, err := adminOrg.GetCatalogById(adminCatalog.AdminCatalog.ID, false) + catalog, err := adminOrg.GetCatalogById(d.Id(), false) if err != nil { log.Printf("[DEBUG] Unable to find catalog. Removing from tfstate") d.SetId("") @@ -245,27 +245,20 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return err } - dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) + dSet(d, "catalog_version", adminCatalog.AdminCatalog.Catalog.VersionNumber) // dSet(d, "owner_name", adminCatalog.AdminCatalog.Owner.User.Name) - catalogItemTypes, err := catalog.QueryCatalogItemList() + vAppTemplates, err := catalog.QueryVappTemplateList() if err != nil { - log.Printf("[DEBUG] Unable to retrieve catalog items: %s", err) - return err + return fmt.Errorf("error retrieving catalog templates: %s", err) } + dSet(d, "number_of_vapp_templates", len(vAppTemplates)) - var numberOfVAppTemplates, numberOfMedia int - for _, v := range catalogItemTypes { - switch v.Type { - case "application/vnd.vmware.vcloud.media+xml": - numberOfMedia++ - case "application/vnd.vmware.vcloud.vAppTemplate+xml": - numberOfVAppTemplates++ - } + medias, err := catalog.QueryMediaList() + if err != nil { + return fmt.Errorf("error retrieving catalog medias: %s", err) } - - dSet(d, "number_of_vapp_templates", numberOfVAppTemplates) - dSet(d, "number_of_media", numberOfMedia) + dSet(d, "number_of_media", len(medias)) d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 51064974c..331a71210 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -78,10 +78,6 @@ func TestAccVcdCatalog(t *testing.T) { resourceAddress, "metadata.catalog_metadata", "catalog Metadata"), resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2"), - //resource.TestCheckResourceAttr(), - //resource.TestCheckResourceAttr(), - resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), - resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), ), }, // Set storage profile for existing catalog @@ -100,6 +96,10 @@ func TestAccVcdCatalog(t *testing.T) { resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2 v2"), resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata3", "catalog Metadata3"), + //resource.TestCheckResourceAttr(), // catalog owner + //resource.TestCheckResourceAttr(), // catalog version number + resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), + resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), ), }, // Remove storage profile just like it was provisioned in step 0 @@ -391,7 +391,7 @@ resource "vcd_catalog_item" "{{.CatalogItemName}}" { catalog = resource.vcd_catalog.test-catalog.name name = "{{.CatalogItemName}}" - description = "{{.Description}}" + description = "TestDescription" ova_path = "{{.OvaPath}}" upload_piece_size = {{.UploadPieceSize}} show_upload_progress = "{{.UploadProgress}}" @@ -402,7 +402,7 @@ resource "vcd_catalog_media" "{{.CatalogMediaName}}" { catalog = resource.vcd_catalog.test-catalog.name name = "{{.CatalogMediaName}}" - description = "{{.Description}}" + description = "TestDescription" media_path = "{{.MediaPath}}" upload_piece_size = {{.UploadPieceSize}} show_upload_progress = "{{.UploadProgress}}" @@ -431,6 +431,28 @@ resource "vcd_catalog" "test-catalog" { catalog_metadata3 = "catalog Metadata3" } } + +resource "vcd_catalog_item" "{{.CatalogItemName}}" { + org = "{{.Org}}" + catalog = resource.vcd_catalog.test-catalog.name + + name = "{{.CatalogItemName}}" + description = "TestDescription" + ova_path = "{{.OvaPath}}" + upload_piece_size = {{.UploadPieceSize}} + show_upload_progress = "{{.UploadProgress}}" +} + +resource "vcd_catalog_media" "{{.CatalogMediaName}}" { + org = "{{.Org}}" + catalog = resource.vcd_catalog.test-catalog.name + + name = "{{.CatalogMediaName}}" + description = "TestDescription" + media_path = "{{.MediaPath}}" + upload_piece_size = {{.UploadPieceSize}} + show_upload_progress = "{{.UploadProgress}}" +} ` // TestAccVcdCatalogSharedAccess is a test to cover bugfix when Organization Administrator is not able to lookup shared From 3d9d3a619943afd46da3e7f2a9a3237b087d17b2 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Thu, 3 Mar 2022 16:00:36 +0100 Subject: [PATCH 05/37] Add test for catalog_version Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 331a71210..db190df2f 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -97,7 +97,7 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata3", "catalog Metadata3"), //resource.TestCheckResourceAttr(), // catalog owner - //resource.TestCheckResourceAttr(), // catalog version number + resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), ), From 02ce1b47e452aac8b754415f038b6881dedebaac Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 09:07:00 +0100 Subject: [PATCH 06/37] Add date-created to catalog datasource Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 78f891a25..77e9c7321 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -82,6 +82,11 @@ func datasourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, + "date_created": { + Type: schema.TypeString, + Computed: true, + Description: "Date when the catalog was created", + }, "filter": &schema.Schema{ Type: schema.TypeList, MaxItems: 1, @@ -168,6 +173,8 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta dSet(d, "number_of_vapp_templates", len(numberOfVAppTemplates)) + dSet(d, "date_created", catalog.Catalog.DateCreated) + numberOfMedia, err := catalog.QueryMediaList() if err != nil { log.Printf("[DEBUG] Unable to retrieve media items associated to this catalog: %s", err) From e9c709fade60fd2c408cfa346f6558f2ad7bf9f9 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 09:08:08 +0100 Subject: [PATCH 07/37] Add date-created to catalog resource and its tests Remove owner from catalog resource due to issues with API Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 11 +++++++++-- vcd/resource_vcd_catalog_test.go | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 0115124a0..00b40099d 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -96,11 +96,11 @@ func resourceVcdCatalog() *schema.Resource { Computed: true, Description: "Catalog version number.", }, - "owner_name": { + /*"owner_name": { // For some reason some catalogs when retrieved from API doesn't come with this field. Type: schema.TypeString, Computed: true, Description: "Owner name from the catalog.", - }, + },*/ "number_of_vapp_templates": { Type: schema.TypeInt, Computed: true, @@ -111,6 +111,11 @@ func resourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, + "date_created": { + Type: schema.TypeString, + Computed: true, + Description: "Date when the catalog was created", + }, }, } } @@ -260,6 +265,8 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } dSet(d, "number_of_media", len(medias)) + dSet(d, "date_created", adminCatalog.AdminCatalog.Catalog.DateCreated) + d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) return nil diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index db190df2f..41e024c5d 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -78,6 +78,10 @@ func TestAccVcdCatalog(t *testing.T) { resourceAddress, "metadata.catalog_metadata", "catalog Metadata"), resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2"), + resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), + resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "0"), + resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "0"), + resource.TestMatchResourceAttr(resourceAddress, "date_created", regexp.MustCompile(`^\S+`)), ), }, // Set storage profile for existing catalog @@ -100,6 +104,7 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), + resource.TestMatchResourceAttr(resourceAddress, "date_created", regexp.MustCompile(`^\S+`)), ), }, // Remove storage profile just like it was provisioned in step 0 From b0e23ec3a037deee651e2d9c50dd8338dcc59d42 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 09:32:37 +0100 Subject: [PATCH 08/37] Remove date-created as it was present already Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 7 ------- vcd/resource_vcd_catalog.go | 7 ------- vcd/resource_vcd_catalog_test.go | 2 -- 3 files changed, 16 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 77e9c7321..78f891a25 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -82,11 +82,6 @@ func datasourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, - "date_created": { - Type: schema.TypeString, - Computed: true, - Description: "Date when the catalog was created", - }, "filter": &schema.Schema{ Type: schema.TypeList, MaxItems: 1, @@ -173,8 +168,6 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta dSet(d, "number_of_vapp_templates", len(numberOfVAppTemplates)) - dSet(d, "date_created", catalog.Catalog.DateCreated) - numberOfMedia, err := catalog.QueryMediaList() if err != nil { log.Printf("[DEBUG] Unable to retrieve media items associated to this catalog: %s", err) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 00b40099d..694954af4 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -111,11 +111,6 @@ func resourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, - "date_created": { - Type: schema.TypeString, - Computed: true, - Description: "Date when the catalog was created", - }, }, } } @@ -265,8 +260,6 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } dSet(d, "number_of_media", len(medias)) - dSet(d, "date_created", adminCatalog.AdminCatalog.Catalog.DateCreated) - d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) return nil diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 41e024c5d..6e51d95be 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -81,7 +81,6 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "0"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "0"), - resource.TestMatchResourceAttr(resourceAddress, "date_created", regexp.MustCompile(`^\S+`)), ), }, // Set storage profile for existing catalog @@ -104,7 +103,6 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), - resource.TestMatchResourceAttr(resourceAddress, "date_created", regexp.MustCompile(`^\S+`)), ), }, // Remove storage profile just like it was provisioned in step 0 From b8f9ee9ee4eedd525292f1393a81ecdb677f0b46 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 10:30:15 +0100 Subject: [PATCH 09/37] Add is_shared and is_published computed values to catalog datasource Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 78f891a25..15e64c649 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -82,6 +82,16 @@ func datasourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, + "is_shared": { + Type: schema.TypeBool, + Computed: true, + Description: "Indicates if the catalog is shared.", + }, + "is_published": { + Type: schema.TypeBool, + Computed: true, + Description: "Indicates if the catalog is published.", + }, "filter": &schema.Schema{ Type: schema.TypeList, MaxItems: 1, @@ -176,5 +186,17 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta dSet(d, "number_of_media", len(numberOfMedia)) + dSet(d, "is_published", catalog.Catalog.IsPublished) + + controlAccessParams, err := catalog.GetAccessControl(false) + if err != nil { + return diag.Errorf("error retrieving catalog access control: %s", err) + } + if controlAccessParams.IsSharedToEveryone || controlAccessParams.AccessSettings != nil { + dSet(d, "is_shared", true) + } else { + dSet(d, "is_shared", false) + } + return nil } From 807a6d6529e7861e09a56b8ab0200ccd9daadad1 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 10:31:01 +0100 Subject: [PATCH 10/37] Add is_shared and is_published computed fields to catalog resource and tests Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 22 ++++++++++++++++++++++ vcd/resource_vcd_catalog_test.go | 2 ++ 2 files changed, 24 insertions(+) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 694954af4..e678aa807 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -111,6 +111,16 @@ func resourceVcdCatalog() *schema.Resource { Computed: true, Description: "Number of Medias this catalog contains.", }, + "is_shared": { + Type: schema.TypeBool, + Computed: true, + Description: "Indicates if the catalog is shared.", + }, + "is_published": { + Type: schema.TypeBool, + Computed: true, + Description: "Indicates if the catalog is published.", + }, }, } } @@ -260,6 +270,18 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } dSet(d, "number_of_media", len(medias)) + dSet(d, "is_published", adminCatalog.AdminCatalog.IsPublished) + + controlAccessParams, err := adminCatalog.GetAccessControl(false) + if err != nil { + return fmt.Errorf("error retrieving catalog access control: %s", err) + } + if controlAccessParams.IsSharedToEveryone || controlAccessParams.AccessSettings != nil { + dSet(d, "is_shared", true) + } else { + dSet(d, "is_shared", false) + } + d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) return nil diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 6e51d95be..1b5c26829 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -81,6 +81,8 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "0"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "0"), + resource.TestCheckResourceAttr(resourceAddress, "is_shared", "false"), + resource.TestCheckResourceAttr(resourceAddress, "is_published", "false"), ), }, // Set storage profile for existing catalog From 24a36633f48e51dd5d40565bf5eb092496c4efb0 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 11:01:38 +0100 Subject: [PATCH 11/37] Add changelog Signed-off-by: Miguel Sama --- .changes/v3.6.0/800-features.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .changes/v3.6.0/800-features.md diff --git a/.changes/v3.6.0/800-features.md b/.changes/v3.6.0/800-features.md new file mode 100644 index 000000000..eff8717d3 --- /dev/null +++ b/.changes/v3.6.0/800-features.md @@ -0,0 +1 @@ +* Add `catalog_version`, `number_of_vapp_templates`, `number_of_media`, `is_shared`, `is_published` computed fields to catalog resource and datasource [GH-800] \ No newline at end of file From 7be5c6707329d55463cd3cc05869f1a47c749a4e Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 11:01:55 +0100 Subject: [PATCH 12/37] Add datasource docs Signed-off-by: Miguel Sama --- website/docs/d/catalog.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/docs/d/catalog.html.markdown b/website/docs/d/catalog.html.markdown index d64d0fb88..5bd524313 100644 --- a/website/docs/d/catalog.html.markdown +++ b/website/docs/d/catalog.html.markdown @@ -47,6 +47,11 @@ The following arguments are supported: * `cache_enabled` - (*v3.6+*) Enable early catalog export to optimize synchronization. Default is `false`. * `preserve_identity_information` - (*v3.6+*) Enable include BIOS UUIDs and MAC addresses in the downloaded OVF package. Preserving the identity information limits the portability of the package and you should use it only when necessary. Default is `false`. * `metadata` - (*v3.6+*) Key value map of metadata. +* `catalog_version` - (*v3.6+*) Version number from this catalog. +* `number_of_vapp_templates` - (*v3.6+*) Number of vApp templates available in this catalog. +* `number_of_media` - (*v3.6+*) Number of media items available in this catalog. +* `is_shared` - (*v3.6+*) Indicates if the catalog is shared. +* `is_published` - (*v3.6+*) Indicates if the catalog is published. ## Filter arguments From dd3889a754908297492583a41d429c90c402f74d Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 4 Mar 2022 11:15:43 +0100 Subject: [PATCH 13/37] Add docs for catalog resource Signed-off-by: Miguel Sama --- website/docs/r/catalog.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/r/catalog.html.markdown b/website/docs/r/catalog.html.markdown index 65a0dca12..d4e955212 100644 --- a/website/docs/r/catalog.html.markdown +++ b/website/docs/r/catalog.html.markdown @@ -64,6 +64,14 @@ source [vcd_storage_profile](/providers/vmware/vcd/latest/docs/data-sources/stor * `password` - (Optional, *v3.6+*) An optional password to access the catalog. Only ASCII characters are allowed in a valid password. * `metadata` - (Optional; *v3.6+*) Key value map of metadata to assign. +## Attribute Reference + +* `catalog_version` - (*v3.6+*) Version number from this catalog. +* `number_of_vapp_templates` - (*v3.6+*) Number of vApp templates available in this catalog. +* `number_of_media` - (*v3.6+*) Number of media items available in this catalog. +* `is_shared` - (*v3.6+*) Indicates if the catalog is shared. +* `is_published` - (*v3.6+*) Indicates if the catalog is published. + ## Importing ~> **Note:** The current implementation of Terraform import can only import resources into the state. It does not generate From 6a821ce4732c3630767cf70685959c4de96dffdb Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Thu, 17 Mar 2022 17:39:14 +0100 Subject: [PATCH 14/37] Make simple attribute retrival Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 45 ++++++++++--------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index e678aa807..da142102e 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -96,11 +96,11 @@ func resourceVcdCatalog() *schema.Resource { Computed: true, Description: "Catalog version number.", }, - /*"owner_name": { // For some reason some catalogs when retrieved from API doesn't come with this field. + "owner_name": { Type: schema.TypeString, Computed: true, Description: "Owner name from the catalog.", - },*/ + }, "number_of_vapp_templates": { Type: schema.TypeInt, Computed: true, @@ -212,12 +212,11 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) } - // catalog user view is retrieved to get the number of vApp templates and medias - catalog, err := adminOrg.GetCatalogById(d.Id(), false) + // Catalog record is retrieved to get the number of vApp templates, medias and owner name + adminCatalogRecord, err := adminOrg.FindAdminCatalogRecords(adminCatalog.AdminCatalog.Name) // Is this returning one even though there are catalogs with similar names? if err != nil { - log.Printf("[DEBUG] Unable to find catalog. Removing from tfstate") - d.SetId("") - return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) + log.Printf("[DEBUG] Unable to find catalog record: %s", err) + return err } // Check if storage profile is set. Although storage profile structure accepts a list, in UI only one can be picked @@ -255,32 +254,12 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return err } - dSet(d, "catalog_version", adminCatalog.AdminCatalog.Catalog.VersionNumber) - // dSet(d, "owner_name", adminCatalog.AdminCatalog.Owner.User.Name) - - vAppTemplates, err := catalog.QueryVappTemplateList() - if err != nil { - return fmt.Errorf("error retrieving catalog templates: %s", err) - } - dSet(d, "number_of_vapp_templates", len(vAppTemplates)) - - medias, err := catalog.QueryMediaList() - if err != nil { - return fmt.Errorf("error retrieving catalog medias: %s", err) - } - dSet(d, "number_of_media", len(medias)) - - dSet(d, "is_published", adminCatalog.AdminCatalog.IsPublished) - - controlAccessParams, err := adminCatalog.GetAccessControl(false) - if err != nil { - return fmt.Errorf("error retrieving catalog access control: %s", err) - } - if controlAccessParams.IsSharedToEveryone || controlAccessParams.AccessSettings != nil { - dSet(d, "is_shared", true) - } else { - dSet(d, "is_shared", false) - } + dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) + dSet(d, "owner_name", adminCatalogRecord[0].OwnerName) + dSet(d, "number_of_vapp_templates", adminCatalogRecord[0].NumberOfVAppTemplates) + dSet(d, "number_of_media", adminCatalogRecord[0].NumberOfMedia) + dSet(d, "is_published", adminCatalogRecord[0].IsPublished) + dSet(d, "is_shared", adminCatalogRecord[0].IsShared) d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) From 9eadd2ed51739d1dddceb384a26073fd5f6147d4 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Thu, 17 Mar 2022 17:39:37 +0100 Subject: [PATCH 15/37] Improve catalog resource test Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index 1b5c26829..ace5770a1 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -79,6 +79,7 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2"), resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), + resource.TestMatchResourceAttr(resourceAddress, "owner_name", regexp.MustCompile(`^\S+$`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "0"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "0"), resource.TestCheckResourceAttr(resourceAddress, "is_shared", "false"), @@ -101,8 +102,8 @@ func TestAccVcdCatalog(t *testing.T) { resourceAddress, "metadata.catalog_metadata2", "catalog Metadata2 v2"), resource.TestCheckResourceAttr( resourceAddress, "metadata.catalog_metadata3", "catalog Metadata3"), - //resource.TestCheckResourceAttr(), // catalog owner resource.TestMatchResourceAttr(resourceAddress, "catalog_version", regexp.MustCompile(`^\d+`)), + resource.TestMatchResourceAttr(resourceAddress, "owner_name", regexp.MustCompile(`^\S+$`)), resource.TestCheckResourceAttr(resourceAddress, "number_of_vapp_templates", "1"), resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "1"), ), From 9d765806315b1bc5d0b393cd5ba9533de895a734 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 18 Mar 2022 10:10:17 +0100 Subject: [PATCH 16/37] Improve catalog datasource Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 46 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 15e64c649..09dcfdff0 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -2,6 +2,7 @@ package vcd import ( "context" + "github.com/vmware/go-vcloud-director/v2/types/v56" "log" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -67,11 +68,11 @@ func datasourceVcdCatalog() *schema.Resource { Computed: true, Description: "Catalog version number.", }, - /*"owner_name": { // owner_name is not available because catalog user view doesn't have this value + "owner_name": { Type: schema.TypeString, Computed: true, Description: "Owner name from the catalog.", - },*/ + }, "number_of_vapp_templates": { Type: schema.TypeInt, Computed: true, @@ -157,7 +158,7 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta dSet(d, "description", catalog.Catalog.Description) dSet(d, "created", catalog.Catalog.DateCreated) dSet(d, "name", catalog.Catalog.Name) - dSet(d, "catalog_version", catalog.Catalog.VersionNumber) + d.SetId(catalog.Catalog.ID) if catalog.Catalog.PublishExternalCatalogParams != nil { dSet(d, "publish_enabled", catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally) @@ -170,33 +171,30 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %s", err) } - numberOfVAppTemplates, err := catalog.QueryVappTemplateList() - if err != nil { - log.Printf("[DEBUG] Unable to retrieve vApp templates associated to this catalog: %s", err) - return diag.Errorf("There was an issue when retrieving vApp templates - %s", err) - } - - dSet(d, "number_of_vapp_templates", len(numberOfVAppTemplates)) + // Get catalog records - numberOfMedia, err := catalog.QueryMediaList() + catalogRecords, err := adminOrg.QueryCatalogList() if err != nil { - log.Printf("[DEBUG] Unable to retrieve media items associated to this catalog: %s", err) - return diag.Errorf("There was an issue when retrieving media items - %s", err) + return diag.Errorf("There was an issue when retrieving catalog records - %s", err) } - dSet(d, "number_of_media", len(numberOfMedia)) - - dSet(d, "is_published", catalog.Catalog.IsPublished) - - controlAccessParams, err := catalog.GetAccessControl(false) - if err != nil { - return diag.Errorf("error retrieving catalog access control: %s", err) + var catalogRecord *types.CatalogRecord + for _, v := range catalogRecords { + if v.Name == catalog.Catalog.Name { + catalogRecord = v + } } - if controlAccessParams.IsSharedToEveryone || controlAccessParams.AccessSettings != nil { - dSet(d, "is_shared", true) - } else { - dSet(d, "is_shared", false) + + if catalogRecord == nil { + return diag.Errorf("There is no catalog record associated with this catalog") } + dSet(d, "catalog_version", catalog.Catalog.VersionNumber) + dSet(d, "owner_name", catalogRecord.OwnerName) + dSet(d, "number_of_vapp_templates", catalogRecord.NumberOfVAppTemplates) + dSet(d, "number_of_media", catalogRecord.NumberOfMedia) + dSet(d, "is_published", catalogRecord.IsPublished) + dSet(d, "is_shared", catalogRecord.IsShared) + return nil } From 8c277e75ec28efcc3c643c2775e583000630e417 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 18 Mar 2022 16:47:42 +0100 Subject: [PATCH 17/37] Change `Catalog` by `AdminCatalog` in `getCatalogByFilter` Signed-off-by: Miguel Sama --- vcd/filter_get.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vcd/filter_get.go b/vcd/filter_get.go index ab44b8417..66f8f2460 100644 --- a/vcd/filter_get.go +++ b/vcd/filter_get.go @@ -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 @@ -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) } From 388082d72bdbe5014750bb4e4c1e00604247baa3 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 18 Mar 2022 16:48:37 +0100 Subject: [PATCH 18/37] Retrieve `catalogRecords` instead of `adminCatalogRecord` Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index da142102e..aaa1cca3a 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -212,8 +212,8 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) } - // Catalog record is retrieved to get the number of vApp templates, medias and owner name - adminCatalogRecord, err := adminOrg.FindAdminCatalogRecords(adminCatalog.AdminCatalog.Name) // Is this returning one even though there are catalogs with similar names? + // 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(adminCatalog.AdminCatalog.Name) if err != nil { log.Printf("[DEBUG] Unable to find catalog record: %s", err) return err @@ -255,11 +255,11 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) - dSet(d, "owner_name", adminCatalogRecord[0].OwnerName) - dSet(d, "number_of_vapp_templates", adminCatalogRecord[0].NumberOfVAppTemplates) - dSet(d, "number_of_media", adminCatalogRecord[0].NumberOfMedia) - dSet(d, "is_published", adminCatalogRecord[0].IsPublished) - dSet(d, "is_shared", adminCatalogRecord[0].IsShared) + 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) d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) From 632241a6b192e82495d16f70cafb027dad9992de Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 18 Mar 2022 16:49:53 +0100 Subject: [PATCH 19/37] Change `Catalog` by `AdminCatalog` to be consistent with resource. Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 52 ++++++++++++++--------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 09dcfdff0..465e09ada 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -2,7 +2,6 @@ package vcd import ( "context" - "github.com/vmware/go-vcloud-director/v2/types/v56" "log" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -118,7 +117,7 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta vcdClient = meta.(*VCDClient) err error adminOrg *govcd.AdminOrg - catalog *govcd.Catalog + catalog *govcd.AdminCatalog ) if !nameOrFilterIsSet(d) { @@ -141,7 +140,7 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta 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) @@ -155,15 +154,15 @@ 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) + dSet(d, "description", catalog.AdminCatalog.Description) + dSet(d, "created", catalog.AdminCatalog.DateCreated) + dSet(d, "name", catalog.AdminCatalog.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) + 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)) @@ -171,30 +170,19 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %s", err) } - // Get catalog records - - catalogRecords, err := adminOrg.QueryCatalogList() + // 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(catalog.AdminCatalog.Name) if err != nil { - return diag.Errorf("There was an issue when retrieving catalog records - %s", err) - } - - var catalogRecord *types.CatalogRecord - for _, v := range catalogRecords { - if v.Name == catalog.Catalog.Name { - catalogRecord = v - } - } - - if catalogRecord == nil { - return diag.Errorf("There is no catalog record associated with this catalog") + log.Printf("[DEBUG] Unable to find catalog record: %s", err) + return diag.Errorf("There was an issue when retrieving the catalog records - %s", err) } - dSet(d, "catalog_version", catalog.Catalog.VersionNumber) - dSet(d, "owner_name", catalogRecord.OwnerName) - dSet(d, "number_of_vapp_templates", catalogRecord.NumberOfVAppTemplates) - dSet(d, "number_of_media", catalogRecord.NumberOfMedia) - dSet(d, "is_published", catalogRecord.IsPublished) - dSet(d, "is_shared", catalogRecord.IsShared) + dSet(d, "catalog_version", catalog.AdminCatalog.VersionNumber) + 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) return nil } From 0fc3008282c406756d3cc551cab81aa0fbd5cc75 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 14:42:03 +0100 Subject: [PATCH 20/37] Add `publish_subscription_type` attribute Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 465e09ada..89a9d3895 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -85,12 +85,17 @@ func datasourceVcdCatalog() *schema.Resource { "is_shared": { Type: schema.TypeBool, Computed: true, - Description: "Indicates if the catalog is shared.", + Description: "True if this catalog is shared.", }, "is_published": { Type: schema.TypeBool, Computed: true, - Description: "Indicates if the catalog is published.", + 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, @@ -177,12 +182,13 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when retrieving the catalog records - %s", err) } - dSet(d, "catalog_version", catalog.AdminCatalog.VersionNumber) + 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 } From 6f177497ed7a5e0240fcd9564f1a7795070750a7 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 14:42:50 +0100 Subject: [PATCH 21/37] Add `publish_subscription_type` attribute Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index aaa1cca3a..c50b7ca7e 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -114,12 +114,17 @@ func resourceVcdCatalog() *schema.Resource { "is_shared": { Type: schema.TypeBool, Computed: true, - Description: "Indicates if the catalog is shared.", + Description: "True if this catalog is shared.", }, "is_published": { Type: schema.TypeBool, Computed: true, - Description: "Indicates if the catalog is published.", + 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.", }, }, } @@ -254,12 +259,13 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return err } - dSet(d, "catalog_version", adminCatalog.AdminCatalog.VersionNumber) + 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) d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) From 8e8d0312ff21260d79e64b03d35a0125eafdddc2 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 14:43:30 +0100 Subject: [PATCH 22/37] Add `publish_subscription_type` to test Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index ace5770a1..e5654a2d1 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -84,6 +84,7 @@ func TestAccVcdCatalog(t *testing.T) { resource.TestCheckResourceAttr(resourceAddress, "number_of_media", "0"), resource.TestCheckResourceAttr(resourceAddress, "is_shared", "false"), resource.TestCheckResourceAttr(resourceAddress, "is_published", "false"), + resource.TestMatchResourceAttr(resourceAddress, "publish_subscription_type", regexp.MustCompile(`^(PUBLISHED|SUBSCRIBED|UNPUBLISHED)$`)), ), }, // Set storage profile for existing catalog From 84a3814b286d57f284420e33b315c7072e8e7b0f Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 15:13:37 +0100 Subject: [PATCH 23/37] Update changelog Signed-off-by: Miguel Sama --- .changes/v3.6.0/800-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/v3.6.0/800-features.md b/.changes/v3.6.0/800-features.md index eff8717d3..567bcf748 100644 --- a/.changes/v3.6.0/800-features.md +++ b/.changes/v3.6.0/800-features.md @@ -1 +1 @@ -* Add `catalog_version`, `number_of_vapp_templates`, `number_of_media`, `is_shared`, `is_published` computed fields to catalog resource and datasource [GH-800] \ No newline at end of file +* 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] \ No newline at end of file From 3c8d855d0789a0db69cb23c3ccb6e76e06aff8a5 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 16:26:53 +0100 Subject: [PATCH 24/37] Update datasource to handle better not found entities Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 89a9d3895..b787ee42b 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -134,9 +134,8 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta adminOrg, err = vcdClient.VCDClient.GetAdminOrgByName(orgName) if err != nil { - log.Printf("[DEBUG] Org %s not found. Setting ID to nothing", orgName) - d.SetId("") - return nil + log.Printf("[DEBUG] Org %s not found.", orgName) + return diag.Errorf("Org %s not found.", orgName) } log.Printf("[TRACE] Org %s found", orgName) @@ -149,10 +148,16 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta } 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) } + // 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(catalog.AdminCatalog.Name) + if err != nil { + log.Printf("[DEBUG] Unable to retrieve catalog record: %s", err) + return diag.Errorf("There was an issue when retrieving the catalog records - %s", err) + } + metadata, err := catalog.GetMetadata() if err != nil { log.Printf("[DEBUG] Unable to find catalog metadata: %s", err) @@ -175,13 +180,6 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %s", err) } - // 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(catalog.AdminCatalog.Name) - if err != nil { - log.Printf("[DEBUG] Unable to find catalog record: %s", err) - return diag.Errorf("There was an issue when retrieving the catalog records - %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) From f3fb7723ab25770dabd940d9851180f824965f1d Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 16:27:29 +0100 Subject: [PATCH 25/37] Improve resource to handle better not found entities Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index c50b7ca7e..4ca0041bc 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -212,15 +212,19 @@ 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) } // 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(adminCatalog.AdminCatalog.Name) if err != nil { - log.Printf("[DEBUG] Unable to find catalog record: %s", err) + log.Printf("[DEBUG] Unable to retrieve catalog record: %s", err) return err } From f3d8a04f9fd21d95c369df9e5da81aa09f7084a3 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 16:46:37 +0100 Subject: [PATCH 26/37] Update docs Signed-off-by: Miguel Sama --- website/docs/d/catalog.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/website/docs/d/catalog.html.markdown b/website/docs/d/catalog.html.markdown index 5bd524313..4d32f11f9 100644 --- a/website/docs/d/catalog.html.markdown +++ b/website/docs/d/catalog.html.markdown @@ -48,10 +48,12 @@ The following arguments are supported: * `preserve_identity_information` - (*v3.6+*) Enable include BIOS UUIDs and MAC addresses in the downloaded OVF package. Preserving the identity information limits the portability of the package and you should use it only when necessary. Default is `false`. * `metadata` - (*v3.6+*) Key value map of metadata. * `catalog_version` - (*v3.6+*) Version number from this catalog. +* `owner_name` - (*v3.6+*) Owner of the catalog. * `number_of_vapp_templates` - (*v3.6+*) Number of vApp templates available in this catalog. * `number_of_media` - (*v3.6+*) Number of media items available in this catalog. * `is_shared` - (*v3.6+*) Indicates if the catalog is shared. -* `is_published` - (*v3.6+*) Indicates if the catalog is published. +* `is_published` - (*v3.6+*) Indicates if this catalog is shared to all organizations. +* `publish_subscription_type` - (*v3.6+*) Shows if the catalog is published, if it is a subscription from another one or none of those. ## Filter arguments From 080860323d5b9550a1d1d271a7910147b2dfa57b Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 16:46:56 +0100 Subject: [PATCH 27/37] Update docs Signed-off-by: Miguel Sama --- website/docs/r/catalog.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/website/docs/r/catalog.html.markdown b/website/docs/r/catalog.html.markdown index d4e955212..469bc59e8 100644 --- a/website/docs/r/catalog.html.markdown +++ b/website/docs/r/catalog.html.markdown @@ -67,10 +67,12 @@ source [vcd_storage_profile](/providers/vmware/vcd/latest/docs/data-sources/stor ## Attribute Reference * `catalog_version` - (*v3.6+*) Version number from this catalog. +* `owner_name` - (*v3.6+*) Owner of the catalog. * `number_of_vapp_templates` - (*v3.6+*) Number of vApp templates available in this catalog. * `number_of_media` - (*v3.6+*) Number of media items available in this catalog. * `is_shared` - (*v3.6+*) Indicates if the catalog is shared. -* `is_published` - (*v3.6+*) Indicates if the catalog is published. +* `is_published` - (*v3.6+*) Indicates if this catalog is shared to all organizations. +* `publish_subscription_type` - (*v3.6+*) Shows if the catalog is published, if it is a subscription from another one or none of those. ## Importing From ab74f2576dd9ee5d48993140e9c04322ab3f50ed Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 22:30:53 +0100 Subject: [PATCH 28/37] Update catalog datasource to allow use either provider block org or datasource one Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index b787ee42b..bbfcfe10b 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -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", }, @@ -120,24 +120,20 @@ 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.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.", orgName) - return diag.Errorf("Org %s not found.", orgName) + 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") From 1a87ae5fd68c5d2d922c9d4c8393b1444fa527fc Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 22:31:35 +0100 Subject: [PATCH 29/37] Fix small bug metadata catalog resource Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 4ca0041bc..894016472 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -258,9 +258,11 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return err } - err = d.Set("metadata", getMetadataStruct(metadata.MetadataEntry)) - if err != nil { - return err + if len(metadata.MetadataEntry) > 0 { + err = d.Set("metadata", getMetadataStruct(metadata.MetadataEntry)) + if err != nil { + return err + } } dSet(d, "catalog_version", catalogRecords[0].Version) From d58804ad0fe5ebb497ea78973229b8208245d053 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Mon, 21 Mar 2022 22:41:10 +0100 Subject: [PATCH 30/37] Improve changelog Signed-off-by: Miguel Sama --- .changes/v3.6.0/800-features.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changes/v3.6.0/800-features.md b/.changes/v3.6.0/800-features.md index 567bcf748..feea185bc 100644 --- a/.changes/v3.6.0/800-features.md +++ b/.changes/v3.6.0/800-features.md @@ -1 +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] \ No newline at end of file +* 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] \ No newline at end of file From e6741a843a04c884787a33c2e48d2c6e6fcd5526 Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 10:53:07 +0100 Subject: [PATCH 31/37] Update go.mod Signed-off-by: Miguel Sama --- go.mod | 2 ++ go.sum | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e08207879..e1c12818c 100644 --- a/go.mod +++ b/go.mod @@ -9,3 +9,5 @@ require ( github.com/kr/pretty v0.2.1 github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.8 ) + +replace github.com/vmware/go-vcloud-director/v2 => github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a diff --git a/go.sum b/go.sum index ee50a1eea..1a584e31c 100644 --- a/go.sum +++ b/go.sum @@ -262,6 +262,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a h1:SVBtvCi7Lw5je2yNNW3e9BSJ0PRySXvSE6oV2y6vDxc= +github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68= github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= @@ -319,8 +321,6 @@ 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/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= From 316d8aaffbee77e0a1c6f3675deb6c59f34a54cd Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 11:40:40 +0100 Subject: [PATCH 32/37] Add `setCatalogRecordValuesToSchema` function to avoid having the same code twice in catalog resource and datasource Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index 894016472..f1f8b9fb6 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -221,13 +221,6 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("error retrieving catalog %s : %s", d.Id(), err) } - // 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(adminCatalog.AdminCatalog.Name) - if err != nil { - log.Printf("[DEBUG] Unable to retrieve catalog record: %s", err) - return err - } - // Check if storage profile is set. Although storage profile structure accepts a list, in UI only one can be picked if adminCatalog.AdminCatalog.CatalogStorageProfiles != nil && len(adminCatalog.AdminCatalog.CatalogStorageProfiles.VdcStorageProfile) > 0 { // By default API does not return Storage Profile Name in response. It has ID and HREF, but not Name so name @@ -265,13 +258,10 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) 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) + err = setCatalogRecordValuesToSchema(d, adminOrg, adminCatalog.AdminCatalog.Name) + if err != nil { + return err + } d.SetId(adminCatalog.AdminCatalog.ID) log.Printf("[TRACE] Catalog read completed: %#v", adminCatalog.AdminCatalog) @@ -456,3 +446,22 @@ func createOrUpdateAdminCatalogMetadata(d *schema.ResourceData, meta interface{} } return nil } + +func setCatalogRecordValuesToSchema(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 +} From 3f6411e23a04857cb3e278b1a8ca1ccd95f91e3d Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 11:42:06 +0100 Subject: [PATCH 33/37] Add `setCatalogRecordValuesToSchema` function to avoid having the same code twice in catalog resource and datasource Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index bbfcfe10b..308fd8b0a 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -147,13 +147,6 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("error retrieving catalog %s: %s", identifier, err) } - // 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(catalog.AdminCatalog.Name) - if err != nil { - log.Printf("[DEBUG] Unable to retrieve catalog record: %s", err) - return diag.Errorf("There was an issue when retrieving the catalog records - %s", err) - } - metadata, err := catalog.GetMetadata() if err != nil { log.Printf("[DEBUG] Unable to find catalog metadata: %s", err) @@ -176,13 +169,10 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %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) + err = setCatalogRecordValuesToSchema(d, adminOrg, catalog.AdminCatalog.Name) + if err != nil { + return diag.FromErr(err) + } return nil } From a16fe532352cb469f423fc029256165fee7cb4cd Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 12:23:03 +0100 Subject: [PATCH 34/37] Fix issue with test Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/vcd/resource_vcd_catalog_test.go b/vcd/resource_vcd_catalog_test.go index e5654a2d1..75a36ba5a 100644 --- a/vcd/resource_vcd_catalog_test.go +++ b/vcd/resource_vcd_catalog_test.go @@ -147,7 +147,7 @@ func TestAccVcdCatalogWithStorageProfile(t *testing.T) { "Tags": "catalog", } - configText := templateFill(testAccCheckVcdCatalogStep1, params) + configText := templateFill(testAccCheckVcdCatalogWithStorageProfile, params) debugPrintf("#[DEBUG] CONFIGURATION: %s", configText) if vcdShortTest { @@ -462,6 +462,29 @@ resource "vcd_catalog_media" "{{.CatalogMediaName}}" { } ` +const testAccCheckVcdCatalogWithStorageProfile = ` +data "vcd_storage_profile" "sp" { + name = "{{.StorageProfile}}" +} + +resource "vcd_catalog" "test-catalog" { + org = "{{.Org}}" + + name = "{{.CatalogName}}" + description = "{{.Description}}" + storage_profile_id = data.vcd_storage_profile.sp.id + + delete_force = "true" + delete_recursive = "true" + + metadata = { + catalog_metadata = "catalog Metadata v2" + catalog_metadata2 = "catalog Metadata2 v2" + catalog_metadata3 = "catalog Metadata3" + } +} +` + // TestAccVcdCatalogSharedAccess is a test to cover bugfix when Organization Administrator is not able to lookup shared // catalog from another Org // Because of limited Terraform acceptance test functionality it uses go-vcloud-director SDK to pre-configure From 7136c43572d0dd90f6464fd84d059b89562973cc Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 12:30:02 +0100 Subject: [PATCH 35/37] =?UTF-8?q?Change=20=C2=B4setCatalogRecordValuesToSc?= =?UTF-8?q?hema`=20for=20`setCatalogData`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miguel Sama --- vcd/resource_vcd_catalog.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vcd/resource_vcd_catalog.go b/vcd/resource_vcd_catalog.go index f1f8b9fb6..e0d8fa1cb 100644 --- a/vcd/resource_vcd_catalog.go +++ b/vcd/resource_vcd_catalog.go @@ -258,7 +258,7 @@ func genericResourceVcdCatalogRead(d *schema.ResourceData, meta interface{}) err } } - err = setCatalogRecordValuesToSchema(d, adminOrg, adminCatalog.AdminCatalog.Name) + err = setCatalogData(d, adminOrg, adminCatalog.AdminCatalog.Name) if err != nil { return err } @@ -447,7 +447,7 @@ func createOrUpdateAdminCatalogMetadata(d *schema.ResourceData, meta interface{} return nil } -func setCatalogRecordValuesToSchema(d *schema.ResourceData, adminOrg *govcd.AdminOrg, catalogName string) error { +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 { From 84e16d777e95e3f91599df56b8b6d28c89e5d98a Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 12:30:56 +0100 Subject: [PATCH 36/37] =?UTF-8?q?Change=20=C2=B4setCatalogRecordValuesToSc?= =?UTF-8?q?hema`=20for=20`setCatalogData`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miguel Sama --- vcd/datasource_vcd_catalog.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcd/datasource_vcd_catalog.go b/vcd/datasource_vcd_catalog.go index 308fd8b0a..bf979c551 100644 --- a/vcd/datasource_vcd_catalog.go +++ b/vcd/datasource_vcd_catalog.go @@ -169,7 +169,7 @@ func datasourceVcdCatalogRead(ctx context.Context, d *schema.ResourceData, meta return diag.Errorf("There was an issue when setting metadata into the schema - %s", err) } - err = setCatalogRecordValuesToSchema(d, adminOrg, catalog.AdminCatalog.Name) + err = setCatalogData(d, adminOrg, catalog.AdminCatalog.Name) if err != nil { return diag.FromErr(err) } From 81c5cd6960b5d0817953096a1a1d57109dd6b5fe Mon Sep 17 00:00:00 2001 From: Miguel Sama Date: Fri, 25 Mar 2022 13:25:35 +0100 Subject: [PATCH 37/37] Update go.mod Signed-off-by: Miguel Sama --- go.mod | 4 +--- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index e1c12818c..4bd67d459 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +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 ) - -replace github.com/vmware/go-vcloud-director/v2 => github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a diff --git a/go.sum b/go.sum index 1a584e31c..5ca023921 100644 --- a/go.sum +++ b/go.sum @@ -262,8 +262,6 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a h1:SVBtvCi7Lw5je2yNNW3e9BSJ0PRySXvSE6oV2y6vDxc= -github.com/mikeletux/go-vcloud-director/v2 v2.15.0-alpha.1.0.20220325091848-7d836411215a/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68= github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= @@ -321,6 +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.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=