-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #903 from asedge/resource_aws_organization
New Resource: aws_organizations_organization
- Loading branch information
Showing
8 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func testAccAwsOrganizationsOrganization_importBasic(t *testing.T) { | ||
resourceName := "aws_organizations_organization.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsOrganizationsOrganizationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAwsOrganizationsOrganizationConfig, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/organizations" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func resourceAwsOrganizationsOrganization() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsOrganizationsOrganizationCreate, | ||
Read: resourceAwsOrganizationsOrganizationRead, | ||
Delete: resourceAwsOrganizationsOrganizationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"master_account_arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"master_account_email": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"master_account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"feature_set": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: organizations.OrganizationFeatureSetAll, | ||
ValidateFunc: validation.StringInSlice([]string{organizations.OrganizationFeatureSetAll, organizations.OrganizationFeatureSetConsolidatedBilling}, true), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsOrganizationsOrganizationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).organizationsconn | ||
|
||
createOpts := &organizations.CreateOrganizationInput{ | ||
FeatureSet: aws.String(d.Get("feature_set").(string)), | ||
} | ||
log.Printf("[DEBUG] Creating Organization: %#v", createOpts) | ||
|
||
resp, err := conn.CreateOrganization(createOpts) | ||
if err != nil { | ||
return fmt.Errorf("Error creating organization: %s", err) | ||
} | ||
|
||
org := resp.Organization | ||
d.SetId(*org.Id) | ||
|
||
return resourceAwsOrganizationsOrganizationRead(d, meta) | ||
} | ||
|
||
func resourceAwsOrganizationsOrganizationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).organizationsconn | ||
|
||
log.Printf("[INFO] Reading Organization: %s", d.Id()) | ||
org, err := conn.DescribeOrganization(&organizations.DescribeOrganizationInput{}) | ||
if err != nil { | ||
if isAWSErr(err, organizations.ErrCodeAWSOrganizationsNotInUseException, "") { | ||
log.Printf("[WARN] Organization does not exist, removing from state: %s", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("arn", org.Organization.Arn) | ||
d.Set("feature_set", org.Organization.FeatureSet) | ||
d.Set("master_account_arn", org.Organization.MasterAccountArn) | ||
d.Set("master_account_email", org.Organization.MasterAccountEmail) | ||
d.Set("master_account_id", org.Organization.MasterAccountId) | ||
return nil | ||
} | ||
|
||
func resourceAwsOrganizationsOrganizationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).organizationsconn | ||
|
||
log.Printf("[INFO] Deleting Organization: %s", d.Id()) | ||
|
||
_, err := conn.DeleteOrganization(&organizations.DeleteOrganizationInput{}) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting Organization: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/organizations" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func testAccAwsOrganizationsOrganization_basic(t *testing.T) { | ||
var organization organizations.Organization | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsOrganizationsOrganizationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAwsOrganizationsOrganizationConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsOrganizationsOrganizationExists("aws_organizations_organization.test", &organization), | ||
resource.TestCheckResourceAttr("aws_organizations_organization.test", "feature_set", organizations.OrganizationFeatureSetAll), | ||
resource.TestCheckResourceAttrSet("aws_organizations_organization.test", "arn"), | ||
resource.TestCheckResourceAttrSet("aws_organizations_organization.test", "master_account_arn"), | ||
resource.TestCheckResourceAttrSet("aws_organizations_organization.test", "master_account_email"), | ||
resource.TestCheckResourceAttrSet("aws_organizations_organization.test", "feature_set"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAwsOrganizationsOrganization_consolidatedBilling(t *testing.T) { | ||
var organization organizations.Organization | ||
|
||
feature_set := organizations.OrganizationFeatureSetConsolidatedBilling | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsOrganizationsOrganizationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAwsOrganizationsOrganizationConfigConsolidatedBilling(feature_set), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsOrganizationsOrganizationExists("aws_organizations_organization.test", &organization), | ||
resource.TestCheckResourceAttr("aws_organizations_organization.test", "feature_set", feature_set), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsOrganizationsOrganizationDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).organizationsconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_organizations_organization" { | ||
continue | ||
} | ||
|
||
params := &organizations.DescribeOrganizationInput{} | ||
|
||
resp, err := conn.DescribeOrganization(params) | ||
|
||
if err != nil { | ||
if isAWSErr(err, organizations.ErrCodeAWSOrganizationsNotInUseException, "") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if resp != nil && resp.Organization != nil { | ||
return fmt.Errorf("Bad: Organization still exists: %q", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAwsOrganizationsOrganizationExists(n string, a *organizations.Organization) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Organization ID not set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).organizationsconn | ||
params := &organizations.DescribeOrganizationInput{} | ||
|
||
resp, err := conn.DescribeOrganization(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp == nil || resp.Organization == nil { | ||
return fmt.Errorf("Organization %q does not exist", rs.Primary.ID) | ||
} | ||
|
||
a = resp.Organization | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccAwsOrganizationsOrganizationConfig = "resource \"aws_organizations_organization\" \"test\" {}" | ||
|
||
func testAccAwsOrganizationsOrganizationConfigConsolidatedBilling(feature_set string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_organizations_organization" "test" { | ||
feature_set = "%s" | ||
} | ||
`, feature_set) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAccAWSOrganizations(t *testing.T) { | ||
testCases := map[string]map[string]func(t *testing.T){ | ||
"Organization": { | ||
"basic": testAccAwsOrganizationsOrganization_basic, | ||
"importBasic": testAccAwsOrganizationsOrganization_importBasic, | ||
"consolidatedBilling": testAccAwsOrganizationsOrganization_consolidatedBilling, | ||
}, | ||
} | ||
|
||
for group, m := range testCases { | ||
m := m | ||
t.Run(group, func(t *testing.T) { | ||
for name, tc := range m { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
tc(t) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_organizations_organization | ||
sidebar_current: "docs-aws-resource-organizations-organization|" | ||
description: |- | ||
Provides a resource to create an organization. | ||
--- | ||
|
||
# aws_organizations_organization | ||
|
||
Provides a resource to create an organization. | ||
|
||
## Example Usage: | ||
|
||
```hcl | ||
resource "aws_organizations_organization" "org" { | ||
feature_set = "ALL" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `feature_set` - (Optional) Specify "ALL" (default) or "CONSOLIDATED_BILLING". | ||
|
||
## Attributes Reference | ||
|
||
The following additional attributes are exported: | ||
|
||
* `arn` - ARN of the organization | ||
* `id` - Identifier of the organization | ||
* `master_account_arn` - ARN of the master account | ||
* `master_account_email` - Email address of the master account | ||
* `master_account_id` - Identifier of the master account | ||
|
||
## Import | ||
|
||
The AWS organization can be imported by using the `id`, e.g. | ||
|
||
``` | ||
$ terraform import aws_organizations_organization.my_org o-1234567 | ||
``` |