From 76f83ed31d233c1ca68d7834ffb1cf590ef7e0b2 Mon Sep 17 00:00:00 2001 From: vasubabu Date: Mon, 19 Jun 2023 21:18:49 +0530 Subject: [PATCH] Updated metal-go client for sub-command events --- internal/events/event.go | 21 +++++---- internal/events/retrieve.go | 24 ++++++----- internal/pagination/pager.go | 84 ++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/internal/events/event.go b/internal/events/event.go index 6ffc03c8..5c6d608e 100644 --- a/internal/events/event.go +++ b/internal/events/event.go @@ -21,17 +21,17 @@ package events 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 - EventService packngo.EventService - DeviceService packngo.DeviceService - ProjectService packngo.ProjectService - OrganizationService packngo.OrganizationService + EventService metal.EventsApiService + DeviceService metal.DevicesApiService + ProjectService metal.ProjectsApiService + OrganizationService metal.OrganizationsApiService Out outputs.Outputer } @@ -48,10 +48,10 @@ func (c *Client) NewCommand() *cobra.Command { root.PersistentPreRun(cmd, args) } } - c.EventService = c.Servicer.API(cmd).Events - c.DeviceService = c.Servicer.API(cmd).Devices - c.ProjectService = c.Servicer.API(cmd).Projects - c.OrganizationService = c.Servicer.API(cmd).Organizations + c.EventService = *c.Servicer.MetalAPI(cmd).EventsApi + c.DeviceService = *c.Servicer.MetalAPI(cmd).DevicesApi + c.ProjectService = *c.Servicer.MetalAPI(cmd).ProjectsApi + c.OrganizationService = *c.Servicer.MetalAPI(cmd).OrganizationsApi }, } @@ -62,8 +62,7 @@ func (c *Client) NewCommand() *cobra.Command { } type Servicer interface { - API(*cobra.Command) *packngo.Client - ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions + MetalAPI(*cobra.Command) *metal.APIClient Format() outputs.Format } diff --git a/internal/events/retrieve.go b/internal/events/retrieve.go index 0c27c50f..9a6fc54c 100644 --- a/internal/events/retrieve.go +++ b/internal/events/retrieve.go @@ -21,10 +21,12 @@ package events import ( + "context" "fmt" + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/equinix/metal-cli/internal/outputs" - "github.com/packethost/packngo" + pager "github.com/equinix/metal-cli/internal/pagination" "github.com/spf13/cobra" ) @@ -53,10 +55,11 @@ func (c *Client) Retrieve() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true - var events []packngo.Event + var events []metal.Event var err error header := []string{"ID", "Body", "Type", "Created"} inc := []string{} + exc := []string{} // only fetch extra details when rendered switch c.Servicer.Format() { @@ -64,36 +67,35 @@ func (c *Client) Retrieve() *cobra.Command { inc = append(inc, "relationship") } - listOpt := c.Servicer.ListOptions(inc, nil) - if deviceID != "" && projectID != "" && organizationID != "" && eventID != "" { return fmt.Errorf("id, project-id, device-id, and organization-id parameters are mutually exclusive") } else if deviceID != "" { - events, _, err = c.DeviceService.ListEvents(deviceID, listOpt) + events, err = pager.GetDeviceEvents(c.EventService, deviceID, inc, exc) if err != nil { return fmt.Errorf("Could not list Device Events: %w", err) } } else if projectID != "" { - events, _, err = c.ProjectService.ListEvents(projectID, listOpt) + events, err = pager.GetProjectEvents(c.EventService, projectID, inc, exc) if err != nil { return fmt.Errorf("Could not list Project Events: %w", err) } } else if organizationID != "" { - events, _, err = c.OrganizationService.ListEvents(organizationID, listOpt) + events, err = pager.GetOrganizationEvents(c.EventService, organizationID, inc, exc) if err != nil { return fmt.Errorf("Could not list Organization Events: %w", err) } } else if eventID != "" { - event, _, err := c.EventService.Get(eventID, listOpt) + event, _, err := c.EventService.FindEventById(context.Background(), eventID).Include(inc).Exclude(exc).Execute() if err != nil { return fmt.Errorf("Could not get Event: %w", err) } + data := make([][]string, 1) - data[0] = []string{event.ID, event.Body, event.Type, event.CreatedAt.String()} + data[0] = []string{event.GetId(), event.GetBody(), event.GetType(), event.GetCreatedAt().String()} return c.Out.Output(event, header, &data) } else { - events, _, err = c.EventService.List(listOpt) + events, err = pager.GetAllEvents(c.EventService, inc, exc) if err != nil { return fmt.Errorf("Could not list Events: %w", err) } @@ -102,7 +104,7 @@ func (c *Client) Retrieve() *cobra.Command { data := make([][]string, len(events)) for i, event := range events { - data[i] = []string{event.ID, event.Body, event.Type, event.CreatedAt.String()} + data[i] = []string{event.GetId(), event.GetBody(), event.GetType(), event.GetCreatedAt().String()} } return c.Out.Output(events, header, &data) diff --git a/internal/pagination/pager.go b/internal/pagination/pager.go index bd7db421..422941d3 100644 --- a/internal/pagination/pager.go +++ b/internal/pagination/pager.go @@ -24,3 +24,87 @@ func GetAllProjects(s metal.ProjectsApiService, include []string, exclude []stri return projects, nil } } + +func GetDeviceEvents(s metal.EventsApiService, deviceId string, include []string, exclude []string) ([]metal.Event, error) { + var events []metal.Event + + page := int32(1) // int32 | Page to return (optional) (default to 1) + perPage := int32(20) // int32 | Items returned per page (optional) (default to 10) + + for { + eventsPage, _, err := s.FindDeviceEvents(context.Background(), deviceId).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute() + if err != nil { + return nil, err + } + + events = append(events, eventsPage.GetEvents()...) + if eventsPage.Meta.GetLastPage() > eventsPage.Meta.GetCurrentPage() { + page = page + 1 + continue + } + return events, nil + } +} + +func GetProjectEvents(s metal.EventsApiService, projectId string, include []string, exclude []string) ([]metal.Event, error) { + var events []metal.Event + + page := int32(1) // int32 | Page to return (optional) (default to 1) + perPage := int32(20) // int32 | Items returned per page (optional) (default to 10) + + for { + eventsPage, _, err := s.FindProjectEvents(context.Background(), projectId).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute() + if err != nil { + return nil, err + } + + events = append(events, eventsPage.GetEvents()...) + if eventsPage.Meta.GetLastPage() > eventsPage.Meta.GetCurrentPage() { + page = page + 1 + continue + } + return events, nil + } +} + +func GetOrganizationEvents(s metal.EventsApiService, orgId string, include []string, exclude []string) ([]metal.Event, error) { + var events []metal.Event + + page := int32(1) // int32 | Page to return (optional) (default to 1) + perPage := int32(20) // int32 | Items returned per page (optional) (default to 10) + + for { + eventsPage, _, err := s.FindOrganizationEvents(context.Background(), orgId).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute() + if err != nil { + return nil, err + } + + events = append(events, eventsPage.GetEvents()...) + if eventsPage.Meta.GetLastPage() > eventsPage.Meta.GetCurrentPage() { + page = page + 1 + continue + } + return events, nil + } +} + +func GetAllEvents(s metal.EventsApiService, include []string, exclude []string) ([]metal.Event, error) { + var events []metal.Event + + page := int32(1) // int32 | Page to return (optional) (default to 1) + perPage := int32(20) // int32 | Items returned per page (optional) (default to 10) + + for { + eventsPage, _, err := s.FindEvents(context.Background()).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute() + if err != nil { + return nil, err + } + + events = append(events, eventsPage.GetEvents()...) + if eventsPage.Meta.GetLastPage() > eventsPage.Meta.GetCurrentPage() { + page = page + 1 + continue + } + return events, nil + } +}