Skip to content

Commit

Permalink
fix: Use soft migration path for public IP disable flags
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasProgrammer committed Mar 16, 2023
1 parent 77c2002 commit 1d8da51
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ $ docker-machine create --driver hetzner
### 4.0.0

* `--hetzner-user-data-from-file` will be fully deprecated and its flag description will only read 'DEPRECATED, legacy'; current fallback behaviour will be retained. `--hetzner-flag-user-data-file` should be used instead.
* `--hetzner-disable-public-4`/`--hetzner-disable-public-6` will be fully deprecated and its flag description will only read 'DEPRECATED, legacy'; current fallback behaviour will be retained. `--hetzner-disable-public-ipv4`/`--hetzner-disable-public-ipv6` should be used instead.

### 5.0.0

* `--hetzner-user-data-from-file` will be removed entirely, including its fallback behavior
* `--hetzner-disable-public-4`/`--hetzner-disable-public-6` ill be removed entirely, including their fallback behavior
24 changes: 22 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ const (
defaultWaitOnError = 0

legacyFlagUserDataFromFile = "hetzner-user-data-from-file"
legacyFlagDisablePublic4 = "hetzner-disable-public-4"
legacyFlagDisablePublic6 = "hetzner-disable-public-6"
)

// NewDriver initializes a new driver instance; see [drivers.Driver.NewDriver]
Expand Down Expand Up @@ -205,11 +207,21 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Name: flagDisablePublic4,
Usage: "Disable public ipv4",
},
mcnflag.BoolFlag{
EnvVar: "HETZNER_DISABLE_PUBLIC_4",
Name: legacyFlagDisablePublic4,
Usage: "DEPRECATED, use --hetzner-disable-public-ipv4; disable public ipv4",
},
mcnflag.BoolFlag{
EnvVar: "HETZNER_DISABLE_PUBLIC_IPV6",
Name: flagDisablePublic6,
Usage: "Disable public ipv6",
},
mcnflag.BoolFlag{
EnvVar: "HETZNER_DISABLE_PUBLIC_6",
Name: legacyFlagDisablePublic6,
Usage: "DEPRECATED, use --hetzner-disable-public-ipv6; disable public ipv6",
},
mcnflag.BoolFlag{
EnvVar: "HETZNER_DISABLE_PUBLIC",
Name: flagDisablePublic,
Expand Down Expand Up @@ -306,8 +318,8 @@ func (d *Driver) setConfigFromFlagsImpl(opts drivers.DriverOptions) error {
d.Networks = opts.StringSlice(flagNetworks)
disablePublic := opts.Bool(flagDisablePublic)
d.UsePrivateNetwork = opts.Bool(flagUsePrivateNetwork) || disablePublic
d.DisablePublic4 = opts.Bool(flagDisablePublic4) || disablePublic
d.DisablePublic6 = opts.Bool(flagDisablePublic6) || disablePublic
d.DisablePublic4 = d.deprecatedBooleanFlag(opts, flagDisablePublic4, legacyFlagDisablePublic4) || disablePublic
d.DisablePublic6 = d.deprecatedBooleanFlag(opts, flagDisablePublic6, legacyFlagDisablePublic6) || disablePublic
d.PrimaryIPv4 = opts.String(flagPrimary4)
d.PrimaryIPv6 = opts.String(flagPrimary6)
d.Firewalls = opts.StringSlice(flagFirewalls)
Expand Down Expand Up @@ -361,6 +373,14 @@ func (d *Driver) setConfigFromFlagsImpl(opts drivers.DriverOptions) error {
return nil
}

func (d *Driver) deprecatedBooleanFlag(opts drivers.DriverOptions, flag, deprecatedFlag string) bool {
if opts.Bool(deprecatedFlag) {
log.Warnf("--%v is deprecated, use --%v instead", deprecatedFlag, flag)
return true
}
return opts.Bool(flag)
}

func (d *Driver) setUserDataFlags(opts drivers.DriverOptions) error {
userData := opts.String(flagUserData)
userDataFile := opts.String(flagUserDataFile)
Expand Down
106 changes: 102 additions & 4 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func TestUserData(t *testing.T) {
d = NewDriver()
err = d.setConfigFromFlagsImpl(&commandstest.FakeFlagger{
Data: map[string]interface{}{
flagAPIToken: "foo",
legacyFlagUserDataFromFile: true,
flagUserDataFile: file,
},
Expand All @@ -56,7 +55,6 @@ func TestUserData(t *testing.T) {
// inline user data
d = NewDriver()
err = d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagAPIToken: "foo",
flagUserData: inlineContents,
}))
if err != nil {
Expand All @@ -74,7 +72,6 @@ func TestUserData(t *testing.T) {
// file user data
d = NewDriver()
err = d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagAPIToken: "foo",
flagUserDataFile: file,
}))
if err != nil {
Expand All @@ -92,7 +89,6 @@ func TestUserData(t *testing.T) {
// legacy file user data
d = NewDriver()
err = d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagAPIToken: "foo",
flagUserData: file,
legacyFlagUserDataFromFile: true,
}))
Expand All @@ -109,6 +105,108 @@ func TestUserData(t *testing.T) {
}
}

func TestDisablePublic(t *testing.T) {
d := NewDriver()
err := d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagDisablePublic: true,
}))
if err != nil {
t.Fatalf("unexpected error, %v", err)
}

if !d.DisablePublic4 {
t.Error("expected public ipv4 to be disabled")
}
if !d.DisablePublic6 {
t.Error("expected public ipv6 to be disabled")
}
if !d.UsePrivateNetwork {
t.Error("expected private network to be enabled")
}
}

func TestDisablePublic46(t *testing.T) {
d := NewDriver()
err := d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagDisablePublic4: true,
}))
if err != nil {
t.Fatalf("unexpected error, %v", err)
}

if !d.DisablePublic4 {
t.Error("expected public ipv4 to be disabled")
}
if d.DisablePublic6 {
t.Error("public ipv6 disabled unexpectedly")
}
if d.UsePrivateNetwork {
t.Error("network enabled unexpectedly")
}

// 6
d = NewDriver()
err = d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
flagDisablePublic6: true,
}))
if err != nil {
t.Fatalf("unexpected error, %v", err)
}

if d.DisablePublic4 {
t.Error("public ipv4 disabled unexpectedly")
}
if !d.DisablePublic6 {
t.Error("expected public ipv6 to be disabled")
}
if d.UsePrivateNetwork {
t.Error("network enabled unexpectedly")
}
}

func TestDisablePublic46Legacy(t *testing.T) {
d := NewDriver()
err := d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
legacyFlagDisablePublic4: true,
// any truthy flag should take precedence
flagDisablePublic4: false,
}))
if err != nil {
t.Fatalf("unexpected error, %v", err)
}

if !d.DisablePublic4 {
t.Error("expected public ipv4 to be disabled")
}
if d.DisablePublic6 {
t.Error("public ipv6 disabled unexpectedly")
}
if d.UsePrivateNetwork {
t.Error("network enabled unexpectedly")
}

// 6
d = NewDriver()
err = d.setConfigFromFlagsImpl(makeFlags(map[string]interface{}{
legacyFlagDisablePublic6: true,
// any truthy flag should take precedence
flagDisablePublic6: false,
}))
if err != nil {
t.Fatalf("unexpected error, %v", err)
}

if d.DisablePublic4 {
t.Error("public ipv4 disabled unexpectedly")
}
if !d.DisablePublic6 {
t.Error("expected public ipv6 to be disabled")
}
if d.UsePrivateNetwork {
t.Error("network enabled unexpectedly")
}
}

func assertMutualExclusion(t *testing.T, err error, flag1, flag2 string) {
if err == nil {
t.Errorf("expected mutually exclusive flags to fail, but no error was thrown: %v %v", flag1, flag2)
Expand Down

0 comments on commit 1d8da51

Please sign in to comment.