Files
bridge/internal/metal/providers/emapi/users.go
2023-07-11 21:56:39 +00:00

95 lines
1.8 KiB
Go

package emapi
import (
"context"
"fmt"
"net/http"
"go.infratographer.com/x/gidx"
"go.equinixmetal.net/infra9-metal-bridge/internal/metal/models"
)
const (
usersPath = "/users"
)
type Users []*User
func (u Users) ToDetails() []*models.UserDetails {
users := make([]*models.UserDetails, len(u))
var nextIndex int
for _, user := range u {
users[nextIndex] = user.ToDetails()
if users[nextIndex] != nil {
nextIndex++
}
}
if nextIndex < len(u) {
return users[:nextIndex]
}
return users
}
type User struct {
client *Client
HREF string `json:"href"`
ID string `json:"id"`
FullName string `json:"full_name"`
Email string `json:"email"`
Projects Projects `json:"projects"`
}
func (u *User) ToDetails() *models.UserDetails {
var id string
if u != nil {
id = idOrLinkID(u.ID, u.HREF)
}
if id == "" {
return nil
}
return &models.UserDetails{
ID: id,
FullName: u.FullName,
Organizations: nil,
Projects: u.Projects.ToDetails(),
}
}
func (c *Client) getUser(ctx context.Context, id string) (*User, error) {
var user User
_, err := c.DoRequest(ctx, http.MethodGet, usersPath+"/"+id, nil, &user)
if err != nil {
return nil, fmt.Errorf("error loading organization: %w", err)
}
return &user, nil
}
func (c *Client) GetUserDetails(ctx context.Context, id gidx.PrefixedID) (*models.UserDetails, error) {
user, err := c.getUser(ctx, id.String()[gidx.PrefixPartLength+1:])
if err != nil {
return nil, err
}
return user.ToDetails(), nil
}
func (c *Client) GetUserOrganizationRole(ctx context.Context, userID, orgID gidx.PrefixedID) (string, error) {
return "collaborator", nil
}
func (c *Client) GetUserProjectRole(ctx context.Context, userID, projectID gidx.PrefixedID) (string, error) {
return "collaborator", nil
}