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

Updated metal-go client for capacity sub-commands #285

Merged
merged 1 commit into from
Jul 25, 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
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
98 changes: 54 additions & 44 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,16 @@ 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)
var serverListInput []metal.ServerInfo
req := *metal.NewCapacityInput()

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

if facility != "" {
facilities = append(facilities, facility)
}
Expand All @@ -71,56 +72,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,
},
)
var serverInfoInput metal.ServerInfo
serverInfoInput.SetFacility(f)
serverInfoInput.SetPlan(p)
serverInfoInput.SetQuantity(quantityStr)
serverListInput = append(serverListInput, serverInfoInput)
}
}
} else if len(metros) > 0 {
checker = c.Service.CheckMetros
locationField = "Metro"
locationer = func(si packngo.ServerInfo) string {
return si.Metro
req.SetServers(serverListInput)
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,
},
)
var serverInfoInput metal.ServerInfo
serverInfoInput.SetMetro(m)
serverInfoInput.SetPlan(p)
serverInfoInput.SetQuantity(quantityStr)
serverListInput = append(serverListInput, serverInfoInput)
}
}
}

availability, _, err := checker(req)
if err != nil {
return fmt.Errorf("Could not check capacity: %w", err)
}
req.SetServers(serverListInput)
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
35 changes: 20 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,63 @@ 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
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
Loading
Loading