Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade metal-go to 0.22.2 and fix enum type errors #350

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/equinix/metal-cli
go 1.19

require (
github.com/equinix-labs/metal-go v0.21.0
github.com/equinix-labs/metal-go v0.22.2
github.com/manifoldco/promptui v0.9.0
github.com/olekukonko/tablewriter v0.0.5
github.com/packethost/packngo v0.30.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinix-labs/metal-go v0.21.0 h1:AWmciCaO+tjNKCKB0r4pMHpoGenzAqua3TbXA5VjZE8=
github.com/equinix-labs/metal-go v0.21.0/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/equinix-labs/metal-go v0.22.2 h1:3uVx1tMUb+P9MXcQzmoh5sRM40+Efdtc7yWwlKOh/ms=
github.com/equinix-labs/metal-go v0.22.2/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
Expand Down
17 changes: 14 additions & 3 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ func (c *Client) Create() *cobra.Command {
var facilityArgs []string

var request metal.ApiCreateDeviceRequest

var validBillingCycle *metal.DeviceCreateInputBillingCycle
var err error

if billingCycle != "" {
validBillingCycle, err = metal.NewDeviceCreateInputBillingCycleFromValue(billingCycle)
if err != nil {
return err
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is returning the original error because that message is nicely descriptive: https://github.com/equinix-labs/metal-go/blob/main/metal/v1/model_device_create_input_billing_cycle.go#L52

The error message does make mention of the type DeviceCreateInputBillingCycle; it would be preferable to reference either the CLI flag name or the attribute name there, but IMO tweaking that is not worth the effort at the moment.

}
}

if facility != "" {
facilityArgs = append(facilityArgs, facility)

Expand All @@ -112,7 +123,7 @@ func (c *Client) Create() *cobra.Command {
}

if billingCycle != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(billingCycle)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(*validBillingCycle)
}
if alwaysPXE {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE)
Expand Down Expand Up @@ -151,7 +162,7 @@ func (c *Client) Create() *cobra.Command {
},
}
if billingCycle != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(billingCycle)
metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(*validBillingCycle)
}

if alwaysPXE {
Expand Down Expand Up @@ -182,7 +193,7 @@ func (c *Client) Create() *cobra.Command {
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}
data := make([][]string, 1)
data[0] = []string{device.GetId(), device.GetHostname(), *device.GetOperatingSystem().Name, device.GetState(), device.GetCreatedAt().String()}
data[0] = []string{device.GetId(), device.GetHostname(), *device.GetOperatingSystem().Name, fmt.Sprintf("%v", device.GetState()), device.GetCreatedAt().String()}

return c.Out.Output(device, header, &data)
},
Expand Down
4 changes: 2 additions & 2 deletions internal/devices/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *Client) Retrieve() *cobra.Command {
header := []string{"ID", "Hostname", "OS", "State", "Created"}

data := make([][]string, 1)
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), device.GetState(), device.GetCreatedAt().String()}
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), fmt.Sprintf("%v", device.GetState()), device.GetCreatedAt().String()}

return c.Out.Output(device, header, &data)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *Client) Retrieve() *cobra.Command {
data := make([][]string, len(devices))

for i, dc := range devices {
data[i] = []string{dc.GetId(), dc.GetHostname(), dc.OperatingSystem.GetName(), dc.GetState(), dc.GetCreatedAt().String()}
data[i] = []string{dc.GetId(), dc.GetHostname(), dc.OperatingSystem.GetName(), fmt.Sprintf("%v", dc.GetState()), dc.GetCreatedAt().String()}
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}

Expand Down
2 changes: 1 addition & 1 deletion internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) Update() *cobra.Command {

header := []string{"ID", "Hostname", "OS", "State"}
data := make([][]string, 1)
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), device.GetState()}
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), fmt.Sprintf("%v", device.GetState())}

return c.Out.Output(device, header, &data)
},
Expand Down
7 changes: 3 additions & 4 deletions internal/organizations/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
include := []string{"Inner_example"}
exclude := []string{"Inner_example"}
withoutProjects := "withoutProjects_example"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The withoutProjects option did not exist prior to metal-go conversion, and is not wired up to any user-facing flags, so I've opted to remove it rather than fix the type error for now.

include := c.Servicer.Includes(nil)
exclude := c.Servicer.Excludes(nil)

if organizationID == "" {
orgs, err := pager.GetAllOrganizations(c.Service, include, exclude, withoutProjects)
orgs, err := pager.GetAllOrganizations(c.Service, include, exclude)
if err != nil {
return fmt.Errorf("Could not list Organizations: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ func GetAllEvents(s metal.ApiFindEventsRequest) ([]metal.Event, error) {
}
}

func GetAllOrganizations(s metal.OrganizationsApiService, include, exclude []string, withOutProjects string) ([]metal.Organization, error) {
func GetAllOrganizations(s metal.OrganizationsApiService, include, exclude []string) ([]metal.Organization, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the withoutProjects setting from the organization get subcommand means we can also remove it here.

var orgs []metal.Organization
page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(56) // int32 | Items returned per page (optional) (default to 10)

for {
orgPage, _, err := s.FindOrganizations(context.Background()).Include(include).Exclude(exclude).WithoutProjects(withOutProjects).Page(page).PerPage(perPage).Execute()
orgPage, _, err := s.FindOrganizations(context.Background()).Include(include).Exclude(exclude).Page(page).PerPage(perPage).Execute()
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/plans/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"

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

Expand All @@ -43,7 +44,11 @@ func (c *Client) Retrieve() *cobra.Command {
filters := c.Servicer.Filters()

if filters["type"] != "" {
request = request.Type_(filters["type"])
validType, err := metal.NewFindPlansTypeParameterFromValue(filters["type"])
if err != nil {
return err
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to just return the error here rather than wrapping it, since the error that's thrown is already helpful: https://github.com/equinix-labs/metal-go/blob/main/metal/v1/model_plan_type.go#L60

}
request = request.Type_(*validType)
}

if filters["slug"] != "" {
Expand Down
24 changes: 22 additions & 2 deletions internal/users/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func (c *Client) Add() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
invitationInput := metal.NewInvitationInput(email)
invitationInput.SetRoles(roles)
validRoles, err := validateRoles(roles)
if err != nil {
return err
}
invitationInput.SetRoles(validRoles)
invitationInput.SetProjectsIds(projectsIDs)

invitation, _, err := c.InvitationService.CreateOrganizationInvitation(context.Background(), organizationID).InvitationInput(*invitationInput).Include(c.Servicer.Includes(nil)).Execute()
Expand All @@ -58,7 +62,11 @@ func (c *Client) Add() *cobra.Command {

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

data[0] = []string{invitation.GetId(), invitation.GetNonce(), invitation.GetInvitee(), invitation.Organization.GetHref(), strconv.Itoa(len(invitation.GetProjects())), strings.Join(invitation.GetRoles(), ", ")}
var roles []string
for _, role := range invitation.GetRoles() {
roles = append(roles, fmt.Sprintf("%v", role))
}
data[0] = []string{invitation.GetId(), invitation.GetNonce(), invitation.GetInvitee(), invitation.Organization.GetHref(), strconv.Itoa(len(invitation.GetProjects())), strings.Join(roles, ", ")}
header := []string{"ID", "Nonce", "Email", "Organization", "Projects", "Roles"}

return c.Out.Output(invitation, header, &data)
Expand All @@ -74,3 +82,15 @@ func (c *Client) Add() *cobra.Command {

return addUserCmd
}

func validateRoles(roles []string) ([]metal.InvitationRolesInner, error) {
validRoles := make([]metal.InvitationRolesInner, len(roles))
for _, role := range roles {
validRole, err := metal.NewInvitationRolesInnerFromValue(role)
if err != nil {
return nil, err
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again opted to return the original error without wrapping it, since the message looks good to me: https://github.com/equinix-labs/metal-go/blob/main/metal/v1/model_invitation_roles_inner.go#L62

}
validRoles = append(validRoles, *validRole)
}
return validRoles, nil
}
Loading