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

feat(Catalog Management): can create catalog data source with id or label #5439

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading