hashicorp/terraform — pull request #389 by catsby (2014-10-10 17:08:35+00:00)
Title: Starting point for adding Organization app support for Heroku Provider

This is a hack-ish first pass at adding Organization app support ([1][1],[2][2]) to the Heroku Provider.

This PR is meant as a starting point for discussion because frankly it's ugly :)

With this PR, you can create an app under and Organization by adding the org name to the resource:

```
resource "heroku_app" "web" {
name = "catsby-test-app"
region = "us"
organization = "catsby"
}
```

```diff
return &schema.Resource{
-	Create: resourceHerokuAppCreate,
+	Create: switchHerokuAppCreate,
```

The intent here is to conditionally return the right method to create the app, depending on if the `organization` field was found in the schema. The name is not the best :/

For the actual conditional logic:

```diff
+	if len(isOrg.(string)) != 0 {
```

This seems horrible, but a previous check for `nil` was wrong because the `organization` field seems to get the default string value `""` (I think).

```diff
+func resourceHerokuOrgAppCreate(d *schema.ResourceData, meta interface{}) error {
```

I copy-pasted 99% of this method from `resourceHerokuAppCreate`, only adding/changing org specific things, so there's ugly duplication. Still being a Go novice, I wasn't sure how to separate the similar logic and change the methods to be specific to their cases.

I wasn't sure how far to go with the Organization support, as [there are other fields like `locked`, `joined`][3], etc. Maybe the `organization` should be a map in the configuration, like so?

```
resource "heroku_app" "web" {
name = "catsby-test-app"
region = "us"
organization {
name = "catsby"
locked = true
}
}
```

I would really like feedback and guidance on DRY/cleanup suggestions here, as well as guidance on how to write a test for this. I've verified it *works*, but I'm not sure where to start on the testing :/

[1]: https://devcenter.heroku.com/categories/organization-accounts
[2]: https://devcenter.heroku.com/articles/platform-api-reference#organization-app
[3]: https://github.com/cyberdelia/heroku-go/blob/master/v3/heroku.go#L1317