update permissions relationships calls to use from/to
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"go.infratographer.com/x/gidx"
|
||||
)
|
||||
@@ -58,53 +57,54 @@ func (c *client) DeleteResourceRelationship(ctx context.Context, resourceID gidx
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListResourceRelationships returns resources related to the given id.
|
||||
// If relatedResourceType is not provied, relations to subjects are returned.
|
||||
// If relatedResourceType is provided, relations to the given resource are returned which match the given type.
|
||||
func (c *client) ListResourceRelationships(ctx context.Context, resourceID gidx.PrefixedID, relatedResourceType string) ([]ResourceRelationship, error) {
|
||||
query := url.Values{
|
||||
"resourceType": []string{relatedResourceType},
|
||||
}
|
||||
|
||||
url := url.URL{
|
||||
Path: fmt.Sprintf("/api/v1/resources/%s/relationships", resourceID.String()),
|
||||
RawQuery: query.Encode(),
|
||||
}
|
||||
|
||||
// ListResourceRelationshipsFrom returns resources related to the given id.
|
||||
func (c *client) ListResourceRelationshipsFrom(ctx context.Context, resourceID gidx.PrefixedID) ([]ResourceRelationship, error) {
|
||||
var response struct {
|
||||
Data []resourceRelationship `json:"data"`
|
||||
}
|
||||
|
||||
if _, err := c.DoRequest(ctx, http.MethodGet, url.String(), nil, &response); err != nil { // nolint:bodyclose // closed by Do on json decode.
|
||||
if _, err := c.DoRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/relationships/from/%s", resourceID.String()), nil, &response); err != nil { // nolint:bodyclose // closed by Do on json decode.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]ResourceRelationship, len(response.Data))
|
||||
|
||||
for i, entry := range response.Data {
|
||||
var (
|
||||
resID, subID gidx.PrefixedID
|
||||
err error
|
||||
)
|
||||
|
||||
if entry.ResourceID != "" {
|
||||
resID, err = gidx.Parse(entry.ResourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if entry.SubjectID != "" {
|
||||
subID, err = gidx.Parse(entry.SubjectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
subID, err := gidx.Parse(entry.SubjectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data[i] = ResourceRelationship{
|
||||
ResourceID: resID,
|
||||
Relation: entry.Relation,
|
||||
SubjectID: subID,
|
||||
Relation: entry.Relation,
|
||||
SubjectID: subID,
|
||||
}
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// ListResourceRelationshipsTo returns resources related to the given id.
|
||||
func (c *client) ListResourceRelationshipsTo(ctx context.Context, resourceID gidx.PrefixedID) ([]ResourceRelationship, error) {
|
||||
var response struct {
|
||||
Data []resourceRelationship `json:"data"`
|
||||
}
|
||||
|
||||
if _, err := c.DoRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/relationships/to/%s", resourceID.String()), nil, &response); err != nil { // nolint:bodyclose // closed by Do on json decode.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]ResourceRelationship, len(response.Data))
|
||||
|
||||
for i, entry := range response.Data {
|
||||
resID, err := gidx.Parse(entry.ResourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data[i] = ResourceRelationship{
|
||||
ResourceID: resID,
|
||||
Relation: entry.Relation,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user