package emapi import ( "context" "fmt" "net/http" "go.infratographer.com/x/gidx" "go.equinixmetal.net/infra9-metal-bridge/internal/metal/models" ) const ( usersPath = "/users" ) // Users contains a list of users. type Users []*User // ToDetails converts the objects to generic user details. 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 } // User contains user information. type User struct { HREF string `json:"href"` ID string `json:"id"` FullName string `json:"full_name"` Email string `json:"email"` Projects Projects `json:"projects"` } // ToDetails converts the user to generic user details. 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(), } } // getUser fetches the provided user. 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 } // GetUserDetails fetches the provided user id. 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 } // GetUserOrganizationRole returns collaborator for all organizations. func (c *Client) GetUserOrganizationRole(ctx context.Context, userID, orgID gidx.PrefixedID) (string, error) { return "collaborator", nil } // GetUserProjectRole returns collaborator for all projects. func (c *Client) GetUserProjectRole(ctx context.Context, userID, projectID gidx.PrefixedID) (string, error) { return "collaborator", nil }