From 1d8da51bc1007bd456563f8a9d30b01b45e1b108 Mon Sep 17 00:00:00 2001 From: JonasS Date: Thu, 16 Mar 2023 20:09:20 +0100 Subject: [PATCH] fix: Use soft migration path for public IP disable flags --- README.md | 2 + driver.go | 24 ++++++++++- driver_test.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 14d1be3..c925769 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/driver.go b/driver.go index 35cc6d9..26fe7b1 100644 --- a/driver.go +++ b/driver.go @@ -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] @@ -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, @@ -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) @@ -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) diff --git a/driver_test.go b/driver_test.go index 7ddea3b..b3a77f9 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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, }, @@ -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 { @@ -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 { @@ -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, })) @@ -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)