Skip to content

Commit

Permalink
continue to support --packet-api-key
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <mjohansson@equinix.com>
  • Loading branch information
displague committed Oct 5, 2021
1 parent 3d2e26f commit 7c5cdc6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ docker-machine create --driver metal

You can find the supported arguments by running `docker-machine create -d metal --help` (Equinix Metal specific arguments are shown below):

| Argument | Default | Description | Environment | Config |
| --------------------------- | -------------- | ---------------------------------------------------------------------------- | ----------- | ---------- |
| `--metal-api-key` | | Equinix Metal API Key | `METAL_AUTH_TOKEN` | `token` or `auth-token`
| Argument | Default | Description | Environment | Config |
| --------------------------- | -------------- | ---------------------------------------------------------------------------- | ------------------------ | ----------------------- |
| `--metal-api-key` | | Deprecated API Key flag (use auth token) | `METAL_API_KEY` |
| `--metal-auth-token` | | Equinix Metal Authentication Token | `METAL_AUTH_TOKEN` | `token` or `auth-token` |
| `--metal-billing-cycle` | `hourly` | Equinix Metal billing cycle, hourly or monthly | `METAL_BILLING_CYCLE` |
| `--metal-facility-code` | | Equinix Metal facility code | `METAL_FACILITY_CODE` |`facility`
| `--metal-facility-code` | | Equinix Metal facility code | `METAL_FACILITY_CODE` | `facility` |
| `--metal-hw-reservation-id` | | Equinix Metal Reserved hardware ID | `METAL_HW_ID` |
| `--metal-metro-code` | | Equinix Metal metro code ("dc" is used if empty and facility is not set) | `METAL_METRO_CODE` |`metro`
| `--metal-os` | `ubuntu_20_04` | Equinix Metal OS | `METAL_OS` |`operating-system`
| `--metal-plan` | `c3.small.x86` | Equinix Metal Server Plan | `METAL_PLAN` |`plan`
| `--metal-project-id` | | Equinix Metal Project Id | `METAL_PROJECT_ID` |`project`
| `--metal-metro-code` | | Equinix Metal metro code ("dc" is used if empty and facility is not set) | `METAL_METRO_CODE` | `metro` |
| `--metal-os` | `ubuntu_20_04` | Equinix Metal OS | `METAL_OS` | `operating-system` |
| `--metal-plan` | `c3.small.x86` | Equinix Metal Server Plan | `METAL_PLAN` | `plan` |
| `--metal-project-id` | | Equinix Metal Project Id | `METAL_PROJECT_ID` | `project` |
| `--metal-spot-instance` | | Request a Equinix Metal Spot Instance | `METAL_SPOT_INSTANCE` |
| `--metal-spot-price-max` | | The maximum Equinix Metal Spot Price | `METAL_SPOT_PRICE_MAX` |
| `--metal-termination-time` | | The Equinix Metal Instance Termination Time | `METAL_TERMINATION_TIME` |
Expand All @@ -40,6 +41,8 @@ You can find the supported arguments by running `docker-machine create -d metal

Where denoted, values may be loaded from the environment or from the `~/.config/equinix/metal.yaml` file which can be created with the [Equinix Metal CLI](https://github.com/equinix/metal-cli#metal-cli).

In order to support existing installations, a Packet branded binary is also available with each release. When the `packet` binary is used, all `METAL` environment variables and `metal` arguments should be substituted for `PACKET` and `packet`, respectively.

### Example usage

This creates the following:
Expand Down Expand Up @@ -108,7 +111,7 @@ To monitor the Docker debugging details and the Equinix Metal API calls:
go build
PACKNGO_DEBUG=1 PATH=`pwd`:$PATH docker-machine \
--debug create -d metal \
--metal-api-key=$METAL_AUTH_TOKEN \
--metal-auth-token=$METAL_AUTH_TOKEN \
--metal-project-id=$METAL_PROJECT \
foo
```
Expand Down
26 changes: 21 additions & 5 deletions pkg/drivers/metal/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
driverName = "metal"

envAuthToken envSuffix = "_AUTH_TOKEN"
envApiKey envSuffix = "_API_KEY"
envProjectID envSuffix = "_PROJECT_ID"
envOS envSuffix = "_OS"
envFacilityCode envSuffix = "_FACILITY_CODE"
Expand All @@ -53,7 +54,8 @@ var (
envTerminationTime envSuffix = "_TERMINATION_TIME"
envUAPrefix envSuffix = "_UA_PREFIX"

argAuthToken argSuffix = "-api-key"
argAuthToken argSuffix = "-auth-token"
argApiKey argSuffix = "-api-key"
argProjectID argSuffix = "-project-id"
argOS argSuffix = "-os"
argFacilityCode argSuffix = "-facility-code"
Expand Down Expand Up @@ -116,9 +118,14 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: argPrefix(argAuthToken),
Usage: "Equinix Metal API Key",
Usage: "Equinix Metal Authentication Token",
EnvVar: envPrefix(envAuthToken),
},
mcnflag.StringFlag{
Name: argPrefix(argApiKey),
Usage: "Authentication Key (deprecated name, use Auth Token)",
EnvVar: envPrefix(envApiKey),
},
mcnflag.StringFlag{
Name: argPrefix(argProjectID),
Usage: "Equinix Metal Project Id",
Expand Down Expand Up @@ -236,6 +243,18 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
}
}

oldApiKey := flags.String(argPrefix(argApiKey))

if d.ApiKey == "" {
d.ApiKey = oldApiKey

if d.ApiKey == "" {
return fmt.Errorf("%s driver requires the --%s option", driverName, argPrefix(argAuthToken))
}
} else if oldApiKey != "" {
log.Warnf("ignoring API Key setting (%s, %s)", argPrefix(argApiKey), envPrefix(envApiKey))
}

if strings.Contains(d.OperatingSystem, "coreos") {
d.SSHUser = "core"
}
Expand Down Expand Up @@ -276,9 +295,6 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
}
}

if d.ApiKey == "" {
return fmt.Errorf("%s driver requires the --%s option", driverName, argPrefix(argAuthToken))
}
if d.ProjectID == "" {
return fmt.Errorf("%s driver requires the --%s option", driverName, argPrefix(argProjectID))
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/drivers/metal/metal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metal

import (
"os"
"testing"

"github.com/docker/machine/libmachine/drivers"
Expand All @@ -9,7 +10,8 @@ import (

func TestSetConfigFromFlags(t *testing.T) {
driver := NewDriver("", "")

configPath := os.Getenv("METAL_CONFIG")
os.Setenv("METAL_CONFIG", "/does-not-exist")
checkFlags := &drivers.CheckDriverOptions{
FlagsValues: map[string]interface{}{
"metal-api-key": "APIKEY",
Expand All @@ -19,7 +21,7 @@ func TestSetConfigFromFlags(t *testing.T) {
}

err := driver.SetConfigFromFlags(checkFlags)

os.Setenv("METAL_CONFIG", configPath)
assert.NoError(t, err)
assert.Empty(t, checkFlags.InvalidFlags)
}

0 comments on commit 7c5cdc6

Please sign in to comment.