72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package permissions
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"go.infratographer.com/x/gidx"
|
|
)
|
|
|
|
type resourceRelationship struct {
|
|
ResourceID string `json:"resource_id"`
|
|
Relation string `json:"relation"`
|
|
SubjectID string `json:"subject_id"`
|
|
}
|
|
|
|
type ResourceRelationship struct {
|
|
ResourceID gidx.PrefixedID
|
|
Relation string
|
|
SubjectID gidx.PrefixedID
|
|
}
|
|
|
|
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(),
|
|
}
|
|
|
|
var response struct {
|
|
Data []resourceRelationship `json:"data"`
|
|
}
|
|
|
|
if _, err := c.DoRequest(ctx, http.MethodGet, url.String(), nil, &response); err != nil {
|
|
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
|
|
}
|
|
}
|
|
|
|
data[i] = ResourceRelationship{
|
|
ResourceID: resID,
|
|
Relation: entry.Relation,
|
|
SubjectID: subID,
|
|
}
|
|
}
|
|
|
|
return data, nil
|
|
}
|