diff --git a/internal/init/init.go b/internal/init/init.go index 15500b55..b1627b1e 100644 --- a/internal/init/init.go +++ b/internal/init/init.go @@ -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" @@ -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 } diff --git a/internal/pagination/pager.go b/internal/pagination/pager.go new file mode 100644 index 00000000..bd7db421 --- /dev/null +++ b/internal/pagination/pager.go @@ -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 + } +} diff --git a/internal/projects/create.go b/internal/projects/create.go index 1c9294df..670a7d2b 100644 --- a/internal/projects/create.go +++ b/internal/projects/create.go @@ -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" ) @@ -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) }, diff --git a/internal/projects/delete.go b/internal/projects/delete.go index 63c03ad8..f05c90de 100644 --- a/internal/projects/delete.go +++ b/internal/projects/delete.go @@ -21,6 +21,7 @@ package projects import ( + "context" "fmt" "github.com/manifoldco/promptui" @@ -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 } diff --git a/internal/projects/project.go b/internal/projects/project.go index 83f5e595..7fac1c43 100644 --- a/internal/projects/project.go +++ b/internal/projects/project.go @@ -21,6 +21,7 @@ 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" @@ -28,7 +29,7 @@ import ( type Client struct { Servicer Servicer - ProjectService packngo.ProjectService + ProjectService metal.ProjectsApiService BGPConfigService packngo.BGPConfigService Out outputs.Outputer @@ -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 }, } @@ -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 diff --git a/internal/projects/retrieve.go b/internal/projects/retrieve.go index a0055df7..9406fc94 100644 --- a/internal/projects/retrieve.go +++ b/internal/projects/retrieve.go @@ -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" ) @@ -50,8 +51,8 @@ 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() { @@ -59,10 +60,8 @@ func (c *Client) Retrieve() *cobra.Command { 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) } @@ -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 } } @@ -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) } diff --git a/internal/projects/update.go b/internal/projects/update.go index 9c28d287..3e467a31 100644 --- a/internal/projects/update.go +++ b/internal/projects/update.go @@ -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" ) @@ -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) },