From 5d666c723895915110a314ba7cfcd846b32a50d5 Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Wed, 22 Apr 2020 21:34:33 +0200 Subject: [PATCH 1/6] checks if universal_ssl is set in schema before making API calls --- .../resource_cloudflare_zone_settings_override.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cloudflare/resource_cloudflare_zone_settings_override.go b/cloudflare/resource_cloudflare_zone_settings_override.go index 0e694a66dc..cc9b33b7f1 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override.go +++ b/cloudflare/resource_cloudflare_zone_settings_override.go @@ -509,9 +509,11 @@ func resourceCloudflareZoneSettingsOverrideCreate(d *schema.ResourceData, meta i return err } - // pulling USSL status and wrapping it into a cloudflare.ZoneSetting that we can set initial_settings - if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { - return err + if d.Get("universal_ssl") != nil { + // pulling USSL status and wrapping it into a cloudflare.ZoneSetting that we can set initial_settings + if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { + return err + } } log.Printf("[DEBUG] Read CloudflareZone initial settings: %#v", zoneSettings) @@ -587,8 +589,10 @@ func resourceCloudflareZoneSettingsOverrideRead(d *schema.ResourceData, meta int return err } - if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { - return err + if d.Get("universal_ssl") != nil { + if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { + return err + } } log.Printf("[DEBUG] Read CloudflareZone Settings: %#v", zoneSettings) From 5090eb2f666f574e3e14195da23a366c5cfa1c1c Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Wed, 22 Apr 2020 21:41:50 +0200 Subject: [PATCH 2/6] fixing typo --- cloudflare/resource_cloudflare_zone_settings_override.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudflare/resource_cloudflare_zone_settings_override.go b/cloudflare/resource_cloudflare_zone_settings_override.go index cc9b33b7f1..ab69bbcc91 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override.go +++ b/cloudflare/resource_cloudflare_zone_settings_override.go @@ -509,7 +509,7 @@ func resourceCloudflareZoneSettingsOverrideCreate(d *schema.ResourceData, meta i return err } - if d.Get("universal_ssl") != nil { + if d.Get("settings.0.universal_ssl") != nil { // pulling USSL status and wrapping it into a cloudflare.ZoneSetting that we can set initial_settings if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { return err @@ -589,7 +589,7 @@ func resourceCloudflareZoneSettingsOverrideRead(d *schema.ResourceData, meta int return err } - if d.Get("universal_ssl") != nil { + if d.Get("settings.0.universal_ssl") != nil { if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { return err } From 6343fad521c55ba32d993e83964ae4f282456cd9 Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Wed, 22 Apr 2020 21:56:25 +0200 Subject: [PATCH 3/6] using GetOk --- cloudflare/resource_cloudflare_zone_settings_override.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudflare/resource_cloudflare_zone_settings_override.go b/cloudflare/resource_cloudflare_zone_settings_override.go index ab69bbcc91..9af163e1d3 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override.go +++ b/cloudflare/resource_cloudflare_zone_settings_override.go @@ -509,7 +509,7 @@ func resourceCloudflareZoneSettingsOverrideCreate(d *schema.ResourceData, meta i return err } - if d.Get("settings.0.universal_ssl") != nil { + if _, ok := d.GetOk("settings.0.universal_ssl"); ok { // pulling USSL status and wrapping it into a cloudflare.ZoneSetting that we can set initial_settings if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { return err @@ -589,7 +589,7 @@ func resourceCloudflareZoneSettingsOverrideRead(d *schema.ResourceData, meta int return err } - if d.Get("settings.0.universal_ssl") != nil { + if _, ok := d.GetOk("settings.0.universal_ssl"); ok { if err = updateZoneSettingsResponseWithUniversalSSLSettings(zoneSettings, d.Id(), client); err != nil { return err } From 99550ec5818d511ea46765ef41a32be241e28252 Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Wed, 22 Apr 2020 22:44:41 +0200 Subject: [PATCH 4/6] skip updating ussl status when deleting resource and initial_settings were empty --- cloudflare/resource_cloudflare_zone_settings_override.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cloudflare/resource_cloudflare_zone_settings_override.go b/cloudflare/resource_cloudflare_zone_settings_override.go index 9af163e1d3..696042d881 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override.go +++ b/cloudflare/resource_cloudflare_zone_settings_override.go @@ -693,10 +693,13 @@ func updateSingleZoneSettings(zoneSettings []cloudflare.ZoneSetting, client *clo func updateUniversalSSLSetting(zoneSettings []cloudflare.ZoneSetting, client *cloudflare.API, zoneID string) ([]cloudflare.ZoneSetting, error) { indexToCut := -1 for i, setting := range zoneSettings { + // Skipping USSL Update if value is empty, especially when we are reverting to the initial state and we did not had the information if setting.ID == "universal_ssl" { - _, err := client.EditUniversalSSLSetting(zoneID, cloudflare.UniversalSSLSetting{Enabled: boolFromString(setting.Value.(string))}) - if err != nil { - return zoneSettings, err + if setting.Value.(string) != "" { + _, err := client.EditUniversalSSLSetting(zoneID, cloudflare.UniversalSSLSetting{Enabled: boolFromString(setting.Value.(string))}) + if err != nil { + return zoneSettings, err + } } indexToCut = i } From 75b033217fc5d387a8383365171131e95357e7f0 Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Thu, 23 Apr 2020 16:20:53 +0200 Subject: [PATCH 5/6] adds test --- .../resource_cloudflare_zone_settings_override_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cloudflare/resource_cloudflare_zone_settings_override_test.go b/cloudflare/resource_cloudflare_zone_settings_override_test.go index 4bbd583b2c..e748bf73ac 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override_test.go +++ b/cloudflare/resource_cloudflare_zone_settings_override_test.go @@ -112,6 +112,11 @@ func testAccCheckCloudflareZoneSettingsEmpty(n string) resource.TestCheckFunc { return fmt.Errorf("No Zone ID is set") } + // Test if universal_ssl is not read from the API when it's not provided in the configuration + if rs.Primary.Attributes["initial_settings.0.universal_ssl"] != "" { + return fmt.Errorf("initial_settings.0.universal_ssl is not empty when it should be") + } + for k, v := range rs.Primary.Attributes { if strings.Contains(k, "initial_settings") && k != "initial_settings_read_at" && !strings.Contains(k, "#") { currentSettingKey := strings.TrimPrefix(k, "initial_") From 0a5a7cff99cbd1cb65d06231366d4b9364dfa903 Mon Sep 17 00:00:00 2001 From: Christian Ehrig Date: Fri, 24 Apr 2020 02:49:44 +0200 Subject: [PATCH 6/6] skipping ussl attribute --- cloudflare/resource_cloudflare_zone_settings_override_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cloudflare/resource_cloudflare_zone_settings_override_test.go b/cloudflare/resource_cloudflare_zone_settings_override_test.go index e748bf73ac..291fa34439 100644 --- a/cloudflare/resource_cloudflare_zone_settings_override_test.go +++ b/cloudflare/resource_cloudflare_zone_settings_override_test.go @@ -221,6 +221,9 @@ func testAccCheckInitialZoneSettings(zoneID string, initialSettings map[string]i } for _, zs := range foundZone.Result { + if zs.ID == "universal_ssl" { + continue + } if !reflect.DeepEqual(zs.Value, initialSettings[zs.ID]) { return fmt.Errorf("Final setting for %q: %+v not equal to initial setting: %+v", zs.ID, zs.Value, initialSettings[zs.ID]) }