Skip to content

Commit

Permalink
Updated metal-go client for capacity sub-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jul 17, 2023
1 parent ba51298 commit 0d63d40
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
9 changes: 4 additions & 5 deletions internal/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
package capacity

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
Service packngo.CapacityService
Service metal.CapacityApiService
Out outputs.Outputer
}

Expand All @@ -44,7 +44,7 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.Service = c.Servicer.API(cmd).CapacityService
c.Service = *c.Servicer.MetalAPI(cmd).CapacityApi
},
}

Expand All @@ -56,8 +56,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
89 changes: 50 additions & 39 deletions internal/capacity/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
package capacity

import (
"context"
"errors"
"fmt"
"strconv"

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

Expand All @@ -47,16 +48,17 @@ func (c *Client) Check() *cobra.Command {
metal capacity check -m sv,da -P c3.medium.x86,m3.large.x86 -q 4`,

RunE: func(cmd *cobra.Command, args []string) error {
var checker func(*packngo.CapacityInput) (*packngo.CapacityInput, *packngo.Response, error)
var locationField string
var locationer func(si packngo.ServerInfo) string
req := &packngo.CapacityInput{
Servers: []packngo.ServerInfo{},
var returnOut error
quantityStr := strconv.Itoa(quantity)
req := &metal.CapacityInput{
Servers: []metal.ServerInfo{},
}

if metro != "" {
metros = append(metros, metro)
}

if facility != "" {
facilities = append(facilities, facility)
}
Expand All @@ -71,56 +73,65 @@ func (c *Client) Check() *cobra.Command {
}

if len(facilities) > 0 {
checker = c.Service.Check
locationField = "Facility"
locationer = func(si packngo.ServerInfo) string {
return si.Facility
}
for _, f := range facilities {
for _, p := range plans {
req.Servers = append(req.Servers, packngo.ServerInfo{
Facility: f,
Plan: p,
Quantity: quantity,
req.Servers = append(req.Servers, metal.ServerInfo{
Facility: &f,
Plan: &p,
Quantity: &quantityStr,
},
)
}
}
} else if len(metros) > 0 {
checker = c.Service.CheckMetros
locationField = "Metro"
locationer = func(si packngo.ServerInfo) string {
return si.Metro
facilityList, _, err := c.Service.CheckCapacityForFacility(context.Background()).CapacityInput(*req).Execute()
if err != nil {
return fmt.Errorf("could not check facility: %w", err)
}
locationField = "Facility"
ServerList := facilityList.GetServers()

data := make([][]string, len(ServerList))
for i, s := range ServerList {
data[i] = []string{
s.GetFacility(),
s.GetPlan(),
s.GetQuantity(),
strconv.FormatBool(s.GetAvailable()),
}
}
header := []string{locationField, "Plan", "Quantity", "Availability"}
returnOut = c.Out.Output(ServerList, header, &data)
} else if len(metros) > 0 {
for _, m := range metros {
for _, p := range plans {
req.Servers = append(req.Servers, packngo.ServerInfo{
Metro: m,
Plan: p,
Quantity: quantity,
req.Servers = append(req.Servers, metal.ServerInfo{
Metro: &m,
Plan: &p,
Quantity: &quantityStr,
},
)
}
}
}

availability, _, err := checker(req)
if err != nil {
return fmt.Errorf("Could not check capacity: %w", err)
}
metroList, _, err := c.Service.CheckCapacityForMetro(context.Background()).CapacityInput(*req).Execute()
if err != nil {
return fmt.Errorf("could not check capacity: %w", err)
}

data := make([][]string, len(availability.Servers))
for i, s := range availability.Servers {
data[i] = []string{
locationer(s),
s.Plan,
strconv.Itoa(s.Quantity),
strconv.FormatBool(s.Available),
locationField = "Metro"
ServerList := metroList.GetServers()
data := make([][]string, len(ServerList))
for i, s := range ServerList {
data[i] = []string{
s.GetMetro(),
s.GetPlan(),
s.GetQuantity(),
strconv.FormatBool(s.GetAvailable()),
}
}
header := []string{locationField, "Plan", "Quantity", "Availability"}
returnOut = c.Out.Output(ServerList, header, &data)
}

header := []string{locationField, "Plan", "Quantity", "Availability"}
return c.Out.Output(availability, header, &data)
return returnOut
},
}

Expand Down
36 changes: 21 additions & 15 deletions internal/capacity/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
package capacity

import (
"context"
"fmt"

"errors"

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

Expand All @@ -51,60 +53,64 @@ func (c *Client) Retrieve() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var locationField string
lister := c.Service.List // Default to facilities
var capacitiesList *metal.CapacityList
capacitiesList, _, err = c.Service.FindCapacityForFacility(context.Background()).Execute() // Default to facilities
if err != nil {
return fmt.Errorf("could not get Capacity: %w", err)
}

fs := cmd.Flags()
if fs.Changed("metros") && fs.Changed("facilities") {
return errors.New("Either facilities or metros, but not both, can be set")
return errors.New("either facilities or metros, but not both, can be set")
}
if fs.Changed("facility") && fs.Changed("metro") {
return errors.New("Either --facility (-f) or --metro (-m), but not both, can be set")
return errors.New("either --facility (-f) or --metro (-m), but not both, can be set")
}
if fs.Changed("facility") && fs.Changed("metros") || fs.Changed("facilities") && fs.Changed("metro") {
return errors.New("Cannot specify both facility and metro filtering")
return errors.New("cannot specify both facility and metro filtering")
}
if fs.Changed("metro") && fs.Changed("metros") || fs.Changed("facility") && fs.Changed("facilities") {
return errors.New("Cannot use both --metro (-m) and --metros or --facility (-f) and --facilities")
return errors.New("cannot use both --metro (-m) and --metros or --facility (-f) and --facilities")
}

cmd.SilenceUsage = true

if len(facilities) > 0 {
locationField = "Facility"
locs = append(locs, facilities...)

} else if len(metros) > 0 || checkMetro {
lister = c.Service.ListMetros
// lister = metal.ApiFindCapacityForFacilityRequest(c.Service.FindCapacityForMetro(context.Background()))
capacitiesList, _, err = c.Service.FindCapacityForMetro(context.Background()).Execute()
if err != nil {
return fmt.Errorf("could not get Capacity: %w", err)
}
locationField = "Metro"
locs = append(locs, metros...)
}

capacities, _, err := lister()
if err != nil {
return fmt.Errorf("Could not get Capacity: %w", err)
}

header := []string{locationField, "Plan", "Level"}
data := [][]string{}
capacities := capacitiesList.GetCapacity()

filtered := map[string]map[string]map[string]string{}

for locCode, capacity := range *capacities {
for locCode, capacity := range capacities {
if len(locs) > 0 && !contains(locs, locCode) {
continue
}
for plan, bm := range capacity {
if len(plans) > 0 && !contains(plans, plan) {
continue
}
loc := []string{locCode, plan, bm.Level}
loc := []string{locCode, plan, bm.GetLevel()}
data = append(data, loc)
if len(filtered[locCode]) == 0 {
filtered[locCode] = map[string]map[string]string{}
}
filtered[locCode][plan] = map[string]string{"levels": bm.Level}
filtered[locCode][plan] = map[string]string{"levels": bm.GetLevel()}
}
}

return c.Out.Output(filtered, header, &data)
},
}
Expand Down

0 comments on commit 0d63d40

Please sign in to comment.