From 3e366a5dd9f37e478374d7d9e349ce49765378b4 Mon Sep 17 00:00:00 2001 From: Ben Buchanan Date: Tue, 18 Jun 2024 15:01:51 -0400 Subject: [PATCH] feat(Catalog Management): can create catalog data source with id or label --- .../data_source_ibm_cm_catalog.go | 24 +++++++++++++++++-- .../data_source_ibm_cm_catalog_test.go | 4 ++-- website/docs/d/cm_catalog.html.markdown | 3 ++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ibm/service/catalogmanagement/data_source_ibm_cm_catalog.go b/ibm/service/catalogmanagement/data_source_ibm_cm_catalog.go index c534c987701..d89a313b576 100644 --- a/ibm/service/catalogmanagement/data_source_ibm_cm_catalog.go +++ b/ibm/service/catalogmanagement/data_source_ibm_cm_catalog.go @@ -23,7 +23,7 @@ func DataSourceIBMCmCatalog() *schema.Resource { Schema: map[string]*schema.Schema{ "catalog_identifier": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, Description: "Catalog identifier.", }, "id": &schema.Schema{ @@ -38,6 +38,7 @@ func DataSourceIBMCmCatalog() *schema.Resource { }, "label": &schema.Schema{ Type: schema.TypeString, + Optional: true, Computed: true, Description: "Display Name in the requested language.", }, @@ -302,9 +303,28 @@ func dataSourceIBMCmCatalogRead(context context.Context, d *schema.ResourceData, return diag.FromErr(err) } + listCatalogsOptions := &catalogmanagementv1.ListCatalogsOptions{} + catalogs, response, err := catalogManagementClient.ListCatalogsWithContext(context, listCatalogsOptions) + if err != nil { + log.Printf("[DEBUG] ListCatalogsWithContext failed %s\n%s", err, response) + return diag.FromErr(fmt.Errorf("ListCatalogsWithContext failed %s\n%s", err, response)) + } + + var catalogId string + for _, catalog := range catalogs.Resources { + if *catalog.ID == d.Get("catalog_identifier").(string) || *catalog.Label == d.Get("label").(string) { + catalogId = *catalog.ID + } + } + + if catalogId == "" { + log.Printf("[DEBUG] Could not find catalog from provided ID or label") + return diag.FromErr(fmt.Errorf("Could not find catalog from provided ID or label")) + } + getCatalogOptions := &catalogmanagementv1.GetCatalogOptions{} - getCatalogOptions.SetCatalogIdentifier(d.Get("catalog_identifier").(string)) + getCatalogOptions.SetCatalogIdentifier(catalogId) catalog, response, err := catalogManagementClient.GetCatalogWithContext(context, getCatalogOptions) if err != nil { diff --git a/ibm/service/catalogmanagement/data_source_ibm_cm_catalog_test.go b/ibm/service/catalogmanagement/data_source_ibm_cm_catalog_test.go index ab7f6549c72..07b4531aad3 100644 --- a/ibm/service/catalogmanagement/data_source_ibm_cm_catalog_test.go +++ b/ibm/service/catalogmanagement/data_source_ibm_cm_catalog_test.go @@ -40,7 +40,7 @@ func TestAccIBMCmCatalogDataSourceSimpleArgs(t *testing.T) { resource.TestStep{ Config: testAccCheckIBMCmCatalogDataSourceConfig(catalogLabel, catalogShortDescription), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("data.ibm_cm_catalog.cm_catalog", "catalog_identifier"), + resource.TestCheckResourceAttrSet("data.ibm_cm_catalog.cm_catalog", "id"), resource.TestCheckResourceAttrSet("data.ibm_cm_catalog.cm_catalog", "label"), resource.TestCheckResourceAttrSet("data.ibm_cm_catalog.cm_catalog", "short_description"), ), @@ -71,7 +71,7 @@ func testAccCheckIBMCmCatalogDataSourceConfig(catalogLabel string, catalogShortD } data "ibm_cm_catalog" "cm_catalog" { - catalog_identifier = ibm_cm_catalog.cm_catalog.id + label = ibm_cm_catalog.cm_catalog.label } `, catalogLabel, catalogShortDescription) } diff --git a/website/docs/d/cm_catalog.html.markdown b/website/docs/d/cm_catalog.html.markdown index 6fb9a9add10..c13d91ba84f 100644 --- a/website/docs/d/cm_catalog.html.markdown +++ b/website/docs/d/cm_catalog.html.markdown @@ -22,7 +22,8 @@ data "ibm_cm_catalog" "cm_catalog" { Review the argument reference that you can specify for your data source. -* `catalog_identifier` - (Required, Forces new resource, String) Catalog identifier. +* `catalog_identifier` - (Optional, String) Catalog identifier. +* `label` - (Optional, String) Catalog label. ## Attribute Reference