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

Fix issue tf-provider-vcd-944 - allow org users to read shared catalogs #531

Merged
merged 7 commits into from
Dec 5, 2022
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
1 change: 1 addition & 0 deletions .changes/v2.18.0/531-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Added `client` methods `QueryCatalogRecords` and `GetCatalogByHref` [GH-531]
6 changes: 6 additions & 0 deletions govcd/access_control_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ func (vcd *TestVCD) testCatalogAccessControl(adminOrg *AdminOrg, catalog accessC
}
err = testAccessControl(catalogName+" catalog two org", catalog, twoOrgsSettings, twoOrgsSettings, true, catalogTenantContext, check)
check.Assert(err, IsNil)
catalogs, err := vcd.client.Client.QueryCatalogRecords(catalogName, TenantContext{newOrg.AdminOrg.ID, newOrg.AdminOrg.Name})
check.Assert(err, IsNil)
check.Assert(len(catalogs), Equals, 1)
foundCatalog, err := vcd.client.Client.GetCatalogByHref(catalogs[0].HREF)
check.Assert(err, IsNil)
check.Assert(foundCatalog.AdminCatalog.ID, Equals, catalog.GetId())
}

// Set empty settings explicitly
Expand Down
53 changes: 53 additions & 0 deletions govcd/admincatalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,56 @@ func (catalog *AdminCatalog) QueryTaskList(filter map[string]string) ([]*types.Q
filter["object"] = catalogHref
return catalog.client.QueryTaskList(filter)
}

// GetCatalogByHref allows retrieving a catalog from HREF, without its parent
func (client *Client) GetCatalogByHref(catalogHref string) (*AdminCatalog, error) {
catalogHref = strings.Replace(catalogHref, "/api/catalog", "/api/admin/catalog", 1)

cat := NewAdminCatalog(client)

_, err := client.ExecuteRequest(catalogHref, http.MethodGet,
"", "error retrieving catalog: %s", nil, cat.AdminCatalog)

if err != nil {
return nil, err
}

return cat, nil
}

// QueryCatalogRecords given a catalog name, retrieves the catalogRecords that match its name
// Returns a list of catalog records for such name, empty list if none was found
func (client *Client) QueryCatalogRecords(name string, ctx TenantContext) ([]*types.CatalogRecord, error) {
util.Logger.Printf("[DEBUG] QueryCatalogRecords")

var filter string
if name != "" {
filter = fmt.Sprintf("name==%s", url.QueryEscape(name))
}

var tenantHeaders map[string]string

if client.IsSysAdmin && ctx.OrgId != "" && ctx.OrgName != "" {
// Set tenant context headers just for the query
tenantHeaders = map[string]string{
types.HeaderAuthContext: ctx.OrgName,
types.HeaderTenantContext: ctx.OrgId,
}
}

queryType := types.QtCatalog

results, err := client.cumulativeQueryWithHeaders(queryType, nil, map[string]string{
"type": queryType,
"filter": filter,
"filterEncoded": "true",
}, tenantHeaders)
if err != nil {
return nil, err
}

catalogs := results.Results.CatalogRecord

util.Logger.Printf("[DEBUG] QueryCatalogRecords returned with : %#v (%d) and error: %v", catalogs, len(catalogs), err)
return catalogs, nil
}
1 change: 1 addition & 0 deletions types/v56/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3142,6 +3142,7 @@ type CatalogRecord struct {
Description string `xml:"description,attr,omitempty"`
IsPublished bool `xml:"isPublished,attr,omitempty"`
IsShared bool `xml:"isShared,attr,omitempty"`
IsLocal bool `xml:"isLocal,attr,omitempty"`
CreationDate string `xml:"creationDate,attr,omitempty"`
OrgName string `xml:"orgName,attr,omitempty"`
OwnerName string `xml:"ownerName,attr,omitempty"`
Expand Down