Skip to content

Commit

Permalink
Merge pull request #287 from vasubabu/events
Browse files Browse the repository at this point in the history
Updated metal-go client for sub-command events
  • Loading branch information
cprivitere authored Jun 30, 2023
2 parents 9aef362 + 76f83ed commit 510c609
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 22 deletions.
21 changes: 10 additions & 11 deletions internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
},
}

Expand All @@ -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
}

Expand Down
24 changes: 13 additions & 11 deletions internal/events/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -53,47 +55,47 @@ 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() {
case outputs.FormatJSON, outputs.FormatYAML:
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)
}
Expand All @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 510c609

Please sign in to comment.