Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
TER-240: Add force-destroy option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Caussat committed Mar 30, 2018
1 parent b999d2d commit 61ef043
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
7 changes: 4 additions & 3 deletions ghost/import_ghost_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func TestAccGhostAppImportBasic(t *testing.T) {
},

resource.TestStep{
ResourceName: "ghost_app.test",
ImportState: true,
ImportStateVerify: true,
ResourceName: "ghost_app.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
Expand Down
39 changes: 36 additions & 3 deletions ghost/resource_ghost_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ func resourceGhostApp() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"force_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Force destroying the app even if it's has been updated from cloud-deploy",
},
},
}
}
Expand Down Expand Up @@ -515,9 +521,16 @@ func resourceGhostAppUpdate(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Updating Ghost app %s", d.Get("name").(string))

app := expandGhostApp(d)
app_updated := expandGhostApp(d)

// Get last etag
app, err := client.GetApp(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] error updating Ghost app: %v", err)
}
d.Set("eve_etag", app.Etag)

eveMetadata, err := client.UpdateApp(&app, d.Id(), d.Get("eve_etag").(string))
eveMetadata, err := client.UpdateApp(&app_updated, d.Id(), d.Get("eve_etag").(string))
if err != nil {
return fmt.Errorf("[ERROR] error updating Ghost app: %v", err)
}
Expand All @@ -532,8 +545,22 @@ func resourceGhostAppDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Deleting Ghost app %s", d.Get("name").(string))

if d.Get("force_destroy").(bool) {
// Get last Etag
app, err := client.GetApp(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] error deleting Ghost app: %v", err)
}
d.Set("eve_etag", app.Etag)
}

log.Printf("[INFO] %s\n\n\n\n", d.Get("eve_etag").(string))
err := client.DeleteApp(d.Id(), d.Get("eve_etag").(string))
if err != nil {
ec := err.Error()[len(err.Error())-3:]
if ec == "412" {
return fmt.Errorf("[ERROR] error deleting Ghost app: app has been updated, use force_destroy = true: %v", err)
}
return fmt.Errorf("[ERROR] error deleting Ghost app: %v", err)
}

Expand Down Expand Up @@ -574,7 +601,6 @@ func flattenGhostApp(d *schema.ResourceData, app ghost.App) error {
d.Set("instance_type", app.InstanceType)
d.Set("vpc_id", app.VpcID)
d.Set("instance_monitoring", app.InstanceMonitoring)
d.Set("eve_etag", app.Etag)

d.Set("modules", flattenGhostAppModules(app.Modules))
d.Set("build_infos", flattenGhostAppBuildInfos(app.BuildInfos))
Expand Down Expand Up @@ -990,3 +1016,10 @@ func flattenGhostAppStringList(strings []string) []interface{} {

return stringList
}

func resourceGhostAppImportState(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
// Force destroy can't be fetched from API calls so it needs a default value
d.Set("force_destroy", false)

return []*schema.ResourceData{d}, nil
}

0 comments on commit 61ef043

Please sign in to comment.