initial commit
This commit is contained in:
80
internal/metal/providers/emapi/users.go
Normal file
80
internal/metal/providers/emapi/users.go
Normal file
@@ -0,0 +1,80 @@
|
||||
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 {
|
||||
if u.ID == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &models.UserDetails{
|
||||
ID: u.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, c.baseURL.JoinPath(usersPath, id).String(), nil, &user)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading user: %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
|
||||
}
|
||||
Reference in New Issue
Block a user