68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package emgql
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"go.infratographer.com/x/gidx"
|
|
|
|
"go.equinixmetal.net/infra9-metal-bridge/internal/metal/models"
|
|
)
|
|
|
|
// GetProjectDetails fetchs the provided project id with membership information.
|
|
func (c *Client) GetProjectDetails(ctx context.Context, id gidx.PrefixedID) (*models.ProjectDetails, error) {
|
|
q := `
|
|
query ($projectId: ProjectId!){
|
|
project(id: $projectId) {
|
|
id
|
|
name
|
|
users {
|
|
id
|
|
roles
|
|
}
|
|
organization {
|
|
id
|
|
}
|
|
}
|
|
}
|
|
`
|
|
variables := map[string]string{
|
|
"projectId": id.String(),
|
|
}
|
|
|
|
res, err := c.graphquery(ctx, q, variables)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error loading project details: %w", err)
|
|
}
|
|
|
|
prj, err := projectFromResponse(res)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
}
|
|
|
|
out := prj.ToProjectDetails()
|
|
return &out, nil
|
|
}
|
|
|
|
func projectFromResponse(r io.ReadCloser) (project, error) {
|
|
out := struct {
|
|
Data struct {
|
|
Project project `json:"project"`
|
|
} `json:"data"`
|
|
}{}
|
|
|
|
body, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return out.Data.Project, fmt.Errorf("emgql: %w", err)
|
|
}
|
|
|
|
err = json.Unmarshal(body, &out)
|
|
if err != nil {
|
|
return out.Data.Project, fmt.Errorf("emgql: %w", err)
|
|
}
|
|
|
|
return out.Data.Project, nil
|
|
}
|