Skip to content

Commit

Permalink
Merge pull request #599 from tootedom/master
Browse files Browse the repository at this point in the history
Apply Single Zone Settings on Delete
  • Loading branch information
jacobbednarz authored Feb 20, 2020
2 parents 5fce050 + c675ab3 commit e62cd12
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
64 changes: 39 additions & 25 deletions cloudflare/resource_cloudflare_zone_settings_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,8 @@ func resourceCloudflareZoneSettingsOverrideCreate(d *schema.ResourceData, meta i
return errors.Wrap(err, fmt.Sprintf("Error reading initial settings for zone %q", d.Id()))
}

for _, settingName := range fetchAsSingleSetting {
singleSeting, err := client.ZoneSingleSetting(d.Id(), settingName)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error reading initial setting '%q' for zone %q", settingName, d.Id()))
}
zoneSettings.Result = append(zoneSettings.Result, singleSeting)
if err = updateZoneSettingsResponseWithSingleZoneSettings(zoneSettings, d.Id(), client); err != nil {
return err
}

log.Printf("[DEBUG] Read CloudflareZone initial settings: %#v", zoneSettings)
Expand All @@ -529,6 +525,17 @@ func resourceCloudflareZoneSettingsOverrideCreate(d *schema.ResourceData, meta i
return resourceCloudflareZoneSettingsOverrideUpdate(d, meta)
}

func updateZoneSettingsResponseWithSingleZoneSettings(zoneSettings *cloudflare.ZoneSettingResponse, zoneId string, client *cloudflare.API) error {
for _, settingName := range fetchAsSingleSetting {
singleSetting, err := client.ZoneSingleSetting(zoneId, settingName)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error reading setting '%q' for zone %q", settingName, zoneId))
}
zoneSettings.Result = append(zoneSettings.Result, singleSetting)
}
return nil
}

func resourceCloudflareZoneSettingsOverrideRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

Expand All @@ -552,12 +559,8 @@ func resourceCloudflareZoneSettingsOverrideRead(d *schema.ResourceData, meta int
return errors.Wrap(err, fmt.Sprintf("Error reading settings for zone %q", d.Id()))
}

for _, settingName := range fetchAsSingleSetting {
singleSeting, err := client.ZoneSingleSetting(d.Id(), settingName)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error reading setting '%q' for zone %q", settingName, d.Id()))
}
zoneSettings.Result = append(zoneSettings.Result, singleSeting)
if err = updateZoneSettingsResponseWithSingleZoneSettings(zoneSettings, d.Id(), client); err != nil {
return err
}

log.Printf("[DEBUG] Read CloudflareZone Settings: %#v", zoneSettings)
Expand Down Expand Up @@ -637,6 +640,24 @@ func flattenReadOnlyZoneSettings(settings []cloudflare.ZoneSetting) []string {
return ids
}

func updateSingleZoneSettings(zoneSettings []cloudflare.ZoneSetting, client *cloudflare.API, zoneID string) ([]cloudflare.ZoneSetting, error) {
var indexesToCut []int
for i, setting := range zoneSettings {
if contains(fetchAsSingleSetting, setting.ID) {
_, err := client.UpdateZoneSingleSetting(zoneID, setting.ID, setting)
if err != nil {
return zoneSettings, err
}
indexesToCut = append(indexesToCut, i)
}
}

for _, indexToCut := range indexesToCut {
zoneSettings = append(zoneSettings[:indexToCut], zoneSettings[indexToCut+1:]...)
}
return zoneSettings, nil
}

func resourceCloudflareZoneSettingsOverrideUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

Expand All @@ -650,19 +671,8 @@ func resourceCloudflareZoneSettingsOverrideUpdate(d *schema.ResourceData, meta i

log.Printf("[DEBUG] Cloudflare Zone Settings update configuration: %#v", zoneSettings)

var indexesToCut []int
for i, setting := range zoneSettings {
if contains(fetchAsSingleSetting, setting.ID) {
_, err := client.UpdateZoneSingleSetting(d.Id(), setting.ID, setting)
if err != nil {
return err
}
indexesToCut = append(indexesToCut, i)
}
}

for _, indexToCut := range indexesToCut {
zoneSettings = append(zoneSettings[:indexToCut], zoneSettings[indexToCut+1:]...)
if zoneSettings, err = updateSingleZoneSettings(zoneSettings, client, d.Id()); err != nil {
return err
}

if len(zoneSettings) > 0 {
Expand Down Expand Up @@ -770,6 +780,10 @@ func resourceCloudflareZoneSettingsOverrideDelete(d *schema.ResourceData, meta i

log.Printf("[DEBUG] Reverting Cloudflare Zone Settings to initial settings with update configuration: %#v", zoneSettings)

if zoneSettings, err = updateSingleZoneSettings(zoneSettings, client, d.Id()); err != nil {
return err
}

if len(zoneSettings) > 0 {
_, err = client.UpdateZoneSettings(d.Id(), zoneSettings)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cloudflare/resource_cloudflare_zone_settings_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func testAccGetInitialZoneSettings(t *testing.T, zoneID string, settings map[str
t.Fatalf("Zone settings not found")
}

if err = updateZoneSettingsResponseWithSingleZoneSettings(foundZone, zoneID, client); err != nil {
return err
}

for _, zs := range foundZone.Result {
settings[zs.ID] = zs.Value
}
Expand All @@ -197,6 +201,10 @@ func testAccCheckInitialZoneSettings(zoneID string, initialSettings map[string]i
return fmt.Errorf("Zone settings not found")
}

if err = updateZoneSettingsResponseWithSingleZoneSettings(foundZone, zoneID, client); err != nil {
return err
}

for _, zs := range foundZone.Result {
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])
Expand Down

0 comments on commit e62cd12

Please sign in to comment.