69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package emgql
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"go.infratographer.com/x/gidx"
|
|
|
|
"go.equinixmetal.net/infra9-metal-bridge/internal/metal/models"
|
|
)
|
|
|
|
// GetOrganizationDetails fetches the organization id provided with its memberships.
|
|
func (c *Client) GetOrganizationDetails(ctx context.Context, id gidx.PrefixedID) (*models.OrganizationDetails, error) {
|
|
q := `
|
|
query ($orgId: OrgId!){
|
|
organization(id: $orgId) {
|
|
id
|
|
name
|
|
users {
|
|
id
|
|
roles
|
|
}
|
|
projects {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
`
|
|
variables := map[string]string{
|
|
"orgId": id.String(),
|
|
}
|
|
|
|
res, err := c.graphquery(ctx, q, variables)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("emgql: error loading organization details: %w", err)
|
|
}
|
|
|
|
org, err := organizationFromResponse(res)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("emgql: failed to parse response: %w", err)
|
|
}
|
|
|
|
out := org.ToOrganizationDetails()
|
|
return &out, nil
|
|
}
|
|
|
|
func organizationFromResponse(r io.ReadCloser) (organization, error) {
|
|
out := struct {
|
|
Data struct {
|
|
Organization organization `json:"organization"`
|
|
} `json:"data"`
|
|
}{}
|
|
|
|
body, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return out.Data.Organization, fmt.Errorf("emgql: %w", err)
|
|
}
|
|
|
|
err = json.Unmarshal(body, &out)
|
|
if err != nil {
|
|
return out.Data.Organization, fmt.Errorf("emgql: %w", err)
|
|
}
|
|
|
|
return out.Data.Organization, nil
|
|
}
|