initial commit
This commit is contained in:
84
internal/metal/providers/emapi/organizations.go
Normal file
84
internal/metal/providers/emapi/organizations.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package emapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.infratographer.com/x/gidx"
|
||||
|
||||
"go.equinixmetal.net/infra9-metal-bridge/internal/metal/models"
|
||||
)
|
||||
|
||||
const (
|
||||
organizationsPath = "/organizations"
|
||||
)
|
||||
|
||||
type Organizations []*Organization
|
||||
|
||||
func (o Organizations) ToDetails() []*models.OrganizationDetails {
|
||||
orgs := make([]*models.OrganizationDetails, len(o))
|
||||
|
||||
var nextIndex int
|
||||
|
||||
for _, org := range o {
|
||||
orgs[nextIndex] = org.ToDetails()
|
||||
|
||||
if orgs[nextIndex] != nil {
|
||||
nextIndex++
|
||||
}
|
||||
}
|
||||
|
||||
if nextIndex < len(o) {
|
||||
return orgs[:nextIndex]
|
||||
}
|
||||
|
||||
return orgs
|
||||
}
|
||||
|
||||
type Organization struct {
|
||||
client *Client
|
||||
|
||||
HREF string `json:"href"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Memberships Memberships `json:"memberships"`
|
||||
Projects Projects `json:"projects"`
|
||||
}
|
||||
|
||||
func (o *Organization) ToDetails() *models.OrganizationDetails {
|
||||
if o == nil || o.ID == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
details := &models.OrganizationDetails{
|
||||
ID: o.ID,
|
||||
Name: o.Name,
|
||||
Projects: o.Projects.ToDetails(),
|
||||
}
|
||||
|
||||
details.Memberships = o.Memberships.ToDetailsWithOrganizationDetails(details)
|
||||
|
||||
return details
|
||||
}
|
||||
|
||||
func (c *Client) getOrganizationWithMemberships(ctx context.Context, id string) (*Organization, error) {
|
||||
var org Organization
|
||||
|
||||
_, err := c.DoRequest(ctx, http.MethodGet, organizationsPath+"/"+id+"?include=memberships.user,projects.memberships.user", nil, &org)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading organization: %w", err)
|
||||
}
|
||||
|
||||
return &org, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetOrganizationDetails(ctx context.Context, id gidx.PrefixedID) (*models.OrganizationDetails, error) {
|
||||
org, err := c.getOrganizationWithMemberships(ctx, id.String()[gidx.PrefixPartLength+1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return org.ToDetails(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user