diff --git a/builtin/providers/heroku/resource_heroku_app.go b/builtin/providers/heroku/resource_heroku_app.go index 275bcaeec..3fe5a3ea4 100644 --- a/builtin/providers/heroku/resource_heroku_app.go +++ b/builtin/providers/heroku/resource_heroku_app.go @@ -42,7 +42,7 @@ func (a *application) Update() error { func resourceHerokuApp() *schema.Resource { return &schema.Resource{ - Create: resourceHerokuAppCreate, + Create: switchHerokuAppCreate, Read: resourceHerokuAppRead, Update: resourceHerokuAppUpdate, Delete: resourceHerokuAppDelete, @@ -93,10 +93,25 @@ func resourceHerokuApp() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "organization": &schema.Schema{ + Type: schema.TypeString, + Description: "Name of Organization to create application in. Leave blank for personal apps.", + Optional: true, + ForceNew: true, + }, }, } } +func switchHerokuAppCreate(d *schema.ResourceData, meta interface{}) error { + if _, ok := d.GetOk("organization"); ok { + return resourceHerokuOrgAppCreate(d, meta) + } else { + return resourceHerokuAppCreate(d, meta) + } +} + func resourceHerokuAppCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*heroku.Service) @@ -138,6 +153,50 @@ func resourceHerokuAppCreate(d *schema.ResourceData, meta interface{}) error { return resourceHerokuAppRead(d, meta) } +func resourceHerokuOrgAppCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*heroku.Service) + // Build up our creation options + opts := heroku.OrganizationAppCreateOpts{} + if v := d.Get("organization"); v != nil { + vs := v.(string) + log.Printf("[DEBUG] App name: %s", vs) + opts.Organization = &vs + } + if v := d.Get("name"); v != nil { + vs := v.(string) + log.Printf("[DEBUG] App name: %s", vs) + opts.Name = &vs + } + if v := d.Get("region"); v != nil { + vs := v.(string) + log.Printf("[DEBUG] App region: %s", vs) + opts.Region = &vs + } + if v := d.Get("stack"); v != nil { + vs := v.(string) + log.Printf("[DEBUG] App stack: %s", vs) + opts.Stack = &vs + } + + log.Printf("[DEBUG] Creating Heroku app...") + a, err := client.OrganizationAppCreate(opts) + if err != nil { + return err + } + + d.SetId(a.Name) + log.Printf("[INFO] App ID: %s", d.Id()) + + if v := d.Get("config_vars"); v != nil { + err = update_config_vars(d.Id(), client, nil, v.([]interface{})) + if err != nil { + return err + } + } + + return resourceHerokuAppRead(d, meta) +} + func resourceHerokuAppRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*heroku.Service) app, err := resource_heroku_app_retrieve(d.Id(), client)