Skip to content

Commit

Permalink
Merge pull request #276 from vasubabu/facility-projects
Browse files Browse the repository at this point in the history
Updated metal-go client for projects sub-commands
  • Loading branch information
ctreatma authored Jun 20, 2023
2 parents 91ee878 + 4189bb6 commit b5f4ee5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
24 changes: 3 additions & 21 deletions internal/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"syscall"

metal "github.com/equinix-labs/metal-go/metal/v1"
pager "github.com/equinix/metal-cli/internal/pagination"
"github.com/spf13/cobra"
"golang.org/x/term"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -135,29 +136,10 @@ func (c *Client) NewCommand() *cobra.Command {
return initCmd
}

func getAllProjects(s metal.ProjectsApiService) ([]metal.Project, error) {
var projects []metal.Project

func getFirstProjectID(s metal.ProjectsApiService, userOrg string) (string, error) {
include := []string{"organization"} // []string | Nested attributes to include. Included objects will return their full attributes. Attribute names can be dotted (up to 3 levels) to included deeply nested objects. (optional)
exclude := []string{"devices", "members", "memberships", "invitations", "ssh_keys", "volumes", "backend_transfer_enabled", "updated_at", "customdata", "event_alert_configuration"}
page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(56) // int32 | Items returned per page (optional) (default to 10)
for {
projectPage, _, err := s.FindProjects(context.Background()).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute()
if err != nil {
return nil, err
}
projects = append(projects, projectPage.GetProjects()...)
if projectPage.Meta.GetLastPage() > projectPage.Meta.GetCurrentPage() {
page = page + 1
continue
}
return projects, nil
}
}

func getFirstProjectID(s metal.ProjectsApiService, userOrg string) (string, error) {
projects, err := getAllProjects(s)
projects, err := pager.GetAllProjects(s, include, exclude)
if err != nil {
return "", err
}
Expand Down
26 changes: 26 additions & 0 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pager

import (
"context"

metal "github.com/equinix-labs/metal-go/metal/v1"
)

func GetAllProjects(s metal.ProjectsApiService, include []string, exclude []string) ([]metal.Project, error) {
var projects []metal.Project

page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(20) // int32 | Items returned per page (optional) (default to 10)
for {
projectPage, _, err := s.FindProjects(context.Background()).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute()
if err != nil {
return nil, err
}
projects = append(projects, projectPage.GetProjects()...)
if projectPage.Meta.GetLastPage() > projectPage.Meta.GetCurrentPage() {
page = page + 1
continue
}
return projects, nil
}
}
15 changes: 7 additions & 8 deletions internal/projects/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package projects

import (
"context"
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -47,26 +48,24 @@ func (c *Client) Create() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := packngo.ProjectCreateRequest{
Name: name,
}
req := metal.NewProjectCreateFromRootInput(name)

if organizationID != "" {
req.OrganizationID = organizationID
req.OrganizationId = &organizationID
}

if paymentMethodID != "" {
req.PaymentMethodID = paymentMethodID
req.PaymentMethodId = &paymentMethodID
}

p, _, err := c.ProjectService.Create(&req)
p, _, err := c.ProjectService.CreateProject(context.Background()).ProjectCreateFromRootInput(*req).Execute()
if err != nil {
return fmt.Errorf("Could not create Project: %w", err)
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
data[0] = []string{p.GetId(), p.GetName(), p.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}
return c.Out.Output(p, header, &data)
},
Expand Down
3 changes: 2 additions & 1 deletion internal/projects/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package projects

import (
"context"
"fmt"

"github.com/manifoldco/promptui"
Expand All @@ -33,7 +34,7 @@ func (c *Client) Delete() *cobra.Command {
projectID string
)
deleteProject := func(id string) error {
_, err := c.ProjectService.Delete(id)
_, err := c.ProjectService.DeleteProject(context.Background(), id).Execute()
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions internal/projects/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
package projects

import (
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/equinix/metal-cli/internal/outputs"
"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

type Client struct {
Servicer Servicer
ProjectService packngo.ProjectService
ProjectService metal.ProjectsApiService
BGPConfigService packngo.BGPConfigService

Out outputs.Outputer
Expand All @@ -47,7 +48,7 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.ProjectService = c.Servicer.API(cmd).Projects
c.ProjectService = *c.Servicer.MetalAPI(cmd).ProjectsApi
c.BGPConfigService = c.Servicer.API(cmd).BGPConfig
},
}
Expand All @@ -65,6 +66,7 @@ func (c *Client) NewCommand() *cobra.Command {
}

type Servicer interface {
MetalAPI(*cobra.Command) *metal.APIClient
API(*cobra.Command) *packngo.Client
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
Format() outputs.Format
Expand Down
20 changes: 9 additions & 11 deletions internal/projects/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
package projects

import (
"context"
"fmt"

"github.com/equinix/metal-cli/internal/outputs"
"github.com/packethost/packngo"
pager "github.com/equinix/metal-cli/internal/pagination"
"github.com/spf13/cobra"
)

Expand All @@ -50,19 +51,17 @@ func (c *Client) Retrieve() *cobra.Command {
if projectID != "" && projectName != "" {
return fmt.Errorf("Must specify only one of project-id and project name")
}

inc := []string{}
exc := []string{}

// only fetch extra details when rendered
switch c.Servicer.Format() {
case outputs.FormatJSON, outputs.FormatYAML:
inc = append(inc, "members")
}

listOpts := c.Servicer.ListOptions(inc, nil)

if projectID == "" {
projects, _, err := c.ProjectService.List(listOpts)
projects, err := pager.GetAllProjects(c.ProjectService, inc, exc)
if err != nil {
return fmt.Errorf("Could not list Projects: %w", err)
}
Expand All @@ -71,13 +70,13 @@ func (c *Client) Retrieve() *cobra.Command {
if projectName == "" {
data = make([][]string, len(projects))
for i, p := range projects {
data[i] = []string{p.ID, p.Name, p.Created}
data[i] = []string{p.GetId(), p.GetName(), p.GetCreatedAt().String()}
}
} else {
data = make([][]string, 0)
for _, p := range projects {
if p.Name == projectName {
data = append(data, []string{p.ID, p.Name, p.Created})
if p.GetName() == projectName {
data = append(data, []string{p.GetId(), p.GetName(), p.GetCreatedAt().String()})
break
}
}
Expand All @@ -89,15 +88,14 @@ func (c *Client) Retrieve() *cobra.Command {
header := []string{"ID", "Name", "Created"}
return c.Out.Output(projects, header, &data)
} else {
getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
p, _, err := c.ProjectService.Get(projectID, getOpts)
p, _, err := c.ProjectService.FindProjectById(context.Background(), projectID).Include(inc).Exclude(exc).Execute()
if err != nil {
return fmt.Errorf("Could not get Project: %w", err)
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
data[0] = []string{p.GetId(), p.GetName(), p.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}
return c.Out.Output(p, header, &data)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/projects/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package projects
import (
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -42,22 +42,22 @@ func (c *Client) Update() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.ProjectUpdateRequest{}
req := metal.NewProjectUpdateInput()
if name != "" {
req.Name = &name
}

if paymentMethodID != "" {
req.PaymentMethodID = &paymentMethodID
req.PaymentMethodId = &paymentMethodID
}
p, _, err := c.ProjectService.Update(projectID, req)
p, _, err := c.ProjectService.UpdateProject(cmd.Context(), projectID).ProjectUpdateInput(*req).Execute()
if err != nil {
return fmt.Errorf("Could not update Project: %w", err)
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
data[0] = []string{p.GetId(), p.GetName(), p.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}
return c.Out.Output(p, header, &data)
},
Expand Down

0 comments on commit b5f4ee5

Please sign in to comment.