Skip to content

Commit

Permalink
default metro only when facility is not provided
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <mjohansson@equinix.com>
  • Loading branch information
displague committed Apr 23, 2021
1 parent 033e2b0 commit dfbe73d
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions pkg/drivers/metal/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metal
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
Expand All @@ -22,6 +23,8 @@ import (
const (
dockerConfigDir = "/etc/docker"
consumerToken = "24e70949af5ecd17fe8e867b335fc88e7de8bd4ad617c0403d8769a376ddea72"
defaultOS = "ubuntu_20_04"
defaultMetro = "DC"
)

var (
Expand Down Expand Up @@ -80,7 +83,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringFlag{
Name: "metal-os",
Usage: "Equinix Metal OS",
Value: "ubuntu_20_04",
Value: defaultOS,
EnvVar: "METAL_OS",
},
mcnflag.StringFlag{
Expand All @@ -90,8 +93,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
},
mcnflag.StringFlag{
Name: "metal-metro-code",
Usage: "Equinix Metal metro code",
Usage: fmt.Sprintf("Equinix Metal metro code (%q is used if empty and facility is not set)", defaultMetro),
EnvVar: "METAL_METRO_CODE",
// We don't set Value because Facility was previously required and
// defaulted. Existing configurations with "Facility" should not
// break. Setting a default metro value would break those
// configurations.
},
mcnflag.StringFlag{
Name: "metal-plan",
Expand Down Expand Up @@ -220,45 +227,21 @@ func (d *Driver) PreCreateCheck() error {
return fmt.Errorf("specified --metal-os not one of %v", strings.Join(flavors, ", "))
}

if d.Metro != "" && d.Facility != "" {
return fmt.Errorf("facility and metro can not be used together")
}

if d.Metro == "" && d.Facility == "" {
return fmt.Errorf("either facility or metro must be specified")
d.Metro = defaultMetro
}

if d.Facility == "any" {
return nil
if d.Metro != "" && d.Facility != "" {
return fmt.Errorf("facility and metro can not be used together")
}

client := d.getClient()

if d.Metro != "" {
metros, _, err := client.Metros.List(nil)
if err != nil {
return err
}
for _, metro := range metros {
if metro.Code == d.Metro {
return nil
}
}

return fmt.Errorf("metal requires a valid metro")
return validateMetro(client, d.Metro)
}

facilities, _, err := client.Facilities.List(nil)
if err != nil {
return err
}
for _, facility := range facilities {
if facility.Code == d.Facility {
return nil
}
}

return fmt.Errorf("metal requires a valid facility")
return validateFacility(client, d.Facility)
}

func (d *Driver) Create() error {
Expand Down Expand Up @@ -311,7 +294,7 @@ func (d *Driver) Create() error {
if err != nil {
//cleanup ssh keys if device faild
if _, err := client.SSHKeys.Delete(d.SSHKeyID); err != nil {
if er, ok := err.(*packngo.ErrorResponse); !ok || er.Response.StatusCode != 404 {
if er, ok := err.(*packngo.ErrorResponse); !ok || er.Response.StatusCode != http.StatusNotFound {
return err
}
}
Expand Down Expand Up @@ -508,6 +491,38 @@ func (d *Driver) getOsFlavors() ([]string, error) {
return flavors, nil
}

func validateFacility(client *packngo.Client, facility string) error {
if facility == "any" {
return nil
}

facilities, _, err := client.Facilities.List(nil)
if err != nil {
return err
}
for _, f := range facilities {
if f.Code == facility {
return nil
}
}

return fmt.Errorf("metal requires a valid facility")
}

func validateMetro(client *packngo.Client, metro string) error {
metros, _, err := client.Metros.List(nil)
if err != nil {
return err
}
for _, m := range metros {
if m.Code == metro {
return nil
}
}

return fmt.Errorf("metal requires a valid metro")
}

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
Expand Down

0 comments on commit dfbe73d

Please sign in to comment.