Skip to content

Commit

Permalink
feat(Catalog Management): can create catalog data source with id or l…
Browse files Browse the repository at this point in the history
…abel
  • Loading branch information
benbuchanan authored and hkantare committed Jun 21, 2024
1 parent 648addf commit e5456e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
24 changes: 22 additions & 2 deletions ibm/service/catalogmanagement/data_source_ibm_cm_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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.",
},
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion website/docs/d/cm_catalog.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e5456e2

Please sign in to comment.