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

Commit

Permalink
Merged in TER-240_add-update-and-delete-ghost-app (pull request #7)
Browse files Browse the repository at this point in the history
TER-240 add update and delete ghost app

Approved-by: Jordan Caussat <[email protected]>
Approved-by: Patrick Decat <[email protected]>
Approved-by: Burak Karamahmut <[email protected]>
  • Loading branch information
Jordan Caussat authored and pdecat committed Apr 6, 2018
2 parents 80fc4a7 + 4e832a9 commit 53820ae
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 121 deletions.
5 changes: 3 additions & 2 deletions ghost/import_ghost_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func TestAccGhostAppImportBasic(t *testing.T) {
envName := fmt.Sprintf("import_ghost_app_acc_env_basic_%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: testAccCheckGhostAppDestroy,
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccGhostAppConfig(envName),
Expand Down
78 changes: 67 additions & 11 deletions ghost/resource_ghost_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ func resourceGhostApp() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: MatchesRegexp(`^[a-zA-Z0-9_.+-]*$`),
},
"env": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: MatchesRegexp(`^[a-z0-9\-\_]*$`),
},
"role": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: MatchesRegexp(`^[a-z0-9\-\_]*$`),
},
"region": {
Expand Down Expand Up @@ -135,7 +138,7 @@ func resourceGhostApp() *schema.Resource {
},
"ami_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Expand All @@ -159,7 +162,7 @@ func resourceGhostApp() *schema.Resource {
"key_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: MatchesRegexp(`^[\p{Latin}\p{P}]{1,255}$`),
ValidateFunc: MatchesRegexp(`^[a-zA-Z0-9\.\-\_]{1,255}$`),
},
"public_ip_address": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -340,9 +343,10 @@ func resourceGhostApp() *schema.Resource {
ValidateFunc: MatchesRegexp(`^[a-zA-Z0-9]*$`),
},
"parameters": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateJsonString,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: SuppressDiffFeatureParameters(),
},
},
},
Expand Down Expand Up @@ -426,7 +430,7 @@ func resourceGhostApp() *schema.Resource {
},
"last_deployment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
Expand Down Expand Up @@ -466,6 +470,10 @@ func resourceGhostApp() *schema.Resource {
},
},
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -481,6 +489,7 @@ func resourceGhostAppCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("[ERROR] error creating Ghost app: %v", err)
}

d.Set("etag", *eveMetadata.Etag)
d.SetId(eveMetadata.ID)

return resourceGhostAppRead(d, meta)
Expand All @@ -505,15 +514,44 @@ func resourceGhostAppRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceGhostAppUpdate(d *schema.ResourceData, meta interface{}) error {
//client := meta.(*ghost.Client)
client := meta.(*ghost.Client)

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

app_updated := expandGhostApp(d)

eveMetadata, err := client.UpdateApp(&app_updated, d.Id(), d.Get("etag").(string))
if err != nil {
ec := err.Error()[len(err.Error())-3:]
if ec == "412" {
return fmt.Errorf(`[ERROR] error updating Ghost app: app has been updated since
last plan, you should run plan again: %v`, err)
}
return fmt.Errorf("[ERROR] error updating Ghost app: %v", err)
}

d.Set("etag", *eveMetadata.Etag)

return resourceGhostAppRead(d, meta)
}

func resourceGhostAppDelete(d *schema.ResourceData, meta interface{}) error {
//client := meta.(*ghost.Client)
client := meta.(*ghost.Client)

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

err := client.DeleteApp(d.Id(), d.Get("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 since
last destroy plan, you should run destroy plan again: %v`, err)
}
return fmt.Errorf("[ERROR] error deleting Ghost app: %v", err)
}

d.SetId("")

return nil
}

Expand Down Expand Up @@ -549,7 +587,7 @@ 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("etag", app.Etag)

d.Set("modules", flattenGhostAppModules(app.Modules))
d.Set("build_infos", flattenGhostAppBuildInfos(app.BuildInfos))
Expand Down Expand Up @@ -579,7 +617,6 @@ func expandGhostAppModules(d []interface{}) *[]ghost.Module {
PreDeploy: StrToB64(data["pre_deploy"].(string)),
PostDeploy: StrToB64(data["post_deploy"].(string)),
AfterAllDeploy: StrToB64(data["after_all_deploy"].(string)),
LastDeployment: data["last_deployment"].(string),
GID: data["gid"].(int),
UID: data["uid"].(int),
}
Expand Down Expand Up @@ -760,20 +797,39 @@ func flattenGhostAppFeatures(features *[]ghost.Feature) []interface{} {
"parameters": feature.Parameters,
}

if feature.Parameters != nil {
values["parameters"] = fmt.Sprintf("%v", feature.Parameters)
}

featureList = append(featureList, values)
}

return featureList
}

func SuppressDiffFeatureParameters() schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
var jsonDoc interface{}

if err := json.Unmarshal([]byte(new), &jsonDoc); err != nil {
log.Printf("Error loading feature paramaters json: %v", err)
}

newJsonParameter := fmt.Sprintf("%v", jsonDoc)

// If the new parameters structure is equivalent to the old one or is empty,
// ignores the diff during plan
return newJsonParameter == old || old == "map[]"
}
}

// Get build_infos from TF configuration
func expandGhostAppBuildInfos(d []interface{}) *ghost.BuildInfos {
data := d[0].(map[string]interface{})

buildInfos := &ghost.BuildInfos{
SshUsername: data["ssh_username"].(string),
SourceAmi: data["source_ami"].(string),
AmiName: data["ami_name"].(string),
SubnetID: data["subnet_id"].(string),
}

Expand Down
Loading

0 comments on commit 53820ae

Please sign in to comment.