Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Single Zone Settings on Delete #599

Merged
merged 4 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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