Skip to content

Commit

Permalink
add metro support and update default OS
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 f896ddb commit 033e2b0
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions pkg/drivers/metal/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Driver struct {
Plan string
HardwareReserverationID string
Facility string
Metro string
OperatingSystem string
BillingCycle string
DeviceID string
Expand Down Expand Up @@ -68,7 +69,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "metal-api-key",
Usage: "Equinix Metal api key",
Usage: "Equinix Metal API Key",
EnvVar: "METAL_AUTH_TOKEN",
},
mcnflag.StringFlag{
Expand All @@ -79,15 +80,19 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringFlag{
Name: "metal-os",
Usage: "Equinix Metal OS",
Value: "ubuntu_16_04",
Value: "ubuntu_20_04",
EnvVar: "METAL_OS",
},
mcnflag.StringFlag{
Name: "metal-facility-code",
Usage: "Equinix Metal facility code",
Value: "ewr1",
EnvVar: "METAL_FACILITY_CODE",
},
mcnflag.StringFlag{
Name: "metal-metro-code",
Usage: "Equinix Metal metro code",
EnvVar: "METAL_METRO_CODE",
},
mcnflag.StringFlag{
Name: "metal-plan",
Usage: "Equinix Metal Server Plan",
Expand Down Expand Up @@ -149,6 +154,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.ProjectID = flags.String("metal-project-id")
d.OperatingSystem = flags.String("metal-os")
d.Facility = flags.String("metal-facility-code")
d.Metro = flags.String("metal-metro-code")
d.BillingCycle = flags.String("metal-billing-cycle")
d.UserAgentPrefix = flags.String("metal-ua-prefix")
d.UserDataFile = flags.String("metal-userdata")
Expand Down Expand Up @@ -214,11 +220,34 @@ 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")
}

if d.Facility == "any" {
return nil
}

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")
}

facilities, _, err := client.Facilities.List(nil)
if err != nil {
return err
Expand Down Expand Up @@ -262,7 +291,7 @@ func (d *Driver) Create() error {
Hostname: d.MachineName,
Plan: d.Plan,
HardwareReservationID: hardwareReservationId,
Facility: []string{d.Facility},
Metro: d.Metro,
OS: d.OperatingSystem,
BillingCycle: d.BillingCycle,
ProjectID: d.ProjectID,
Expand All @@ -273,6 +302,10 @@ func (d *Driver) Create() error {
TerminationTime: d.TerminationTime,
}

if d.Facility != "" {
createRequest.Facility = []string{d.Facility}
}

log.Info("Provisioning Equinix Metal server...")
newDevice, _, err := client.Devices.Create(createRequest)
if err != nil {
Expand Down

0 comments on commit 033e2b0

Please sign in to comment.