initial commit
This commit is contained in:
84
internal/metal/providers/emapi/projects.go
Normal file
84
internal/metal/providers/emapi/projects.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 (
|
||||
projectsPath = "/projects"
|
||||
)
|
||||
|
||||
type Projects []*Project
|
||||
|
||||
func (p Projects) ToDetails() []*models.ProjectDetails {
|
||||
projects := make([]*models.ProjectDetails, len(p))
|
||||
|
||||
var nextIndex int
|
||||
|
||||
for _, project := range p {
|
||||
projects[nextIndex] = project.ToDetails()
|
||||
|
||||
if projects[nextIndex] != nil {
|
||||
nextIndex++
|
||||
}
|
||||
}
|
||||
|
||||
if nextIndex < len(p) {
|
||||
return projects[:nextIndex]
|
||||
}
|
||||
|
||||
return projects
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
client *Client
|
||||
|
||||
HREF string `json:"href"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Memberships Memberships `json:"memberships"`
|
||||
Organization *Organization `json:"organization"`
|
||||
}
|
||||
|
||||
func (p *Project) ToDetails() *models.ProjectDetails {
|
||||
if p == nil || p.ID == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
details := &models.ProjectDetails{
|
||||
ID: p.ID,
|
||||
Name: p.Name,
|
||||
Organization: p.Organization.ToDetails(),
|
||||
}
|
||||
|
||||
details.Memberships = p.Memberships.ToDetailsWithProjectDetails(details)
|
||||
|
||||
return details
|
||||
}
|
||||
|
||||
func (c *Client) getProject(ctx context.Context, id string) (*Project, error) {
|
||||
var project Project
|
||||
|
||||
_, err := c.DoRequest(ctx, http.MethodGet, c.baseURL.JoinPath(projectsPath, id).String(), nil, &project)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading project: %w", err)
|
||||
}
|
||||
|
||||
return &project, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetProjectDetails(ctx context.Context, id gidx.PrefixedID) (*models.ProjectDetails, error) {
|
||||
project, err := c.getProject(ctx, id.String()[gidx.PrefixPartLength+1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return project.ToDetails(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user