From 23b85ee048bcb359bb99df38187102fb6a337469 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 17 Sep 2015 01:46:10 +0100 Subject: [PATCH] Gofmt of the aws glacier vault resource --- builtin/providers/aws/config.go | 3 + builtin/providers/aws/provider.go | 2 +- .../aws/resource_aws_glacier_vault.go | 37 ++-- .../aws/resource_aws_glacier_vault_test.go | 175 ++++++++++++++++++ .../aws/r/glacier_vault.html.markdown | 19 +- 5 files changed, 213 insertions(+), 23 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_glacier_vault_test.go diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 3b95fe7e985c..05e42f2fe9dc 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -161,9 +161,12 @@ func (c *Config) Client() (interface{}, error) { log.Println("[INFO] Initializing CloudWatch SDK connection") client.cloudwatchconn = cloudwatch.New(awsConfig) +<<<<<<< HEAD log.Println("[INFO] Initializing CloudWatch Logs connection") client.cloudwatchlogsconn = cloudwatchlogs.New(awsConfig) +======= +>>>>>>> Gofmt of the aws glacier vault resource log.Println("[INFO] Initializing Glacier connection") client.glacierconn = glacier.New(awsConfig) } diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 09a0ec79175a..cc151dca4232 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -182,7 +182,7 @@ func Provider() terraform.ResourceProvider { "aws_elasticache_subnet_group": resourceAwsElasticacheSubnetGroup(), "aws_elb": resourceAwsElb(), "aws_flow_log": resourceAwsFlowLog(), - "aws_glacier_vault": resourceAwsGlacierVault(), + "aws_glacier_vault": resourceAwsGlacierVault(), "aws_iam_access_key": resourceAwsIamAccessKey(), "aws_iam_group_policy": resourceAwsIamGroupPolicy(), "aws_iam_group": resourceAwsIamGroup(), diff --git a/builtin/providers/aws/resource_aws_glacier_vault.go b/builtin/providers/aws/resource_aws_glacier_vault.go index adeb2c231235..21ac4d7cc1d3 100644 --- a/builtin/providers/aws/resource_aws_glacier_vault.go +++ b/builtin/providers/aws/resource_aws_glacier_vault.go @@ -73,7 +73,6 @@ func resourceAwsGlacierVault() *schema.Resource { }, }, - "tags": tagsSchema(), }, } @@ -144,19 +143,22 @@ func resourceAwsGlacierVaultRead(d *schema.ResourceData, meta interface{}) error VaultName: aws.String(d.Id()), }) - if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ResourceNotFoundException" { + if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "ResourceNotFoundException" { d.Set("access_policy", "") - } else if pol != nil { + } else if pol != nil { d.Set("access_policy", normalizeJson(*pol.Policy.Policy)) } else { return err } notifications, err := getGlacierVaultNotification(glacierconn, d.Id()) - if err != nil { + if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "ResourceNotFoundException" { + d.Set("notification", "") + } else if pol != nil { + d.Set("notification", notifications) + } else { return err } - d.Set("notification", notifications) return nil } @@ -180,24 +182,19 @@ func resourceAwsGlacierVaultNotificationUpdate(glacierconn *glacier.Glacier, d * settings := v.([]interface{}) if len(settings) > 1 { - return fmt.Errorf("Only a single Notification setup is allowed for Glacier Vault") + return fmt.Errorf("Only a single Notification Block is allowed for Glacier Vault") } else if len(settings) == 1 { s := settings[0].(map[string]interface{}) var events []*string for _, id := range s["events"].(*schema.Set).List() { - event := id.(string) - if event != "ArchiveRetrievalCompleted" && event != "InventoryRetrievalCompleted" { - return fmt.Errorf("Glacier Vault Notification Events can only be 'ArchiveRetrievalCompleted' or 'InventoryRetrievalCompleted'") - } else { - events = append(events, aws.String(event)) - } + events = append(events, aws.String(id.(string))) } _, err := glacierconn.SetVaultNotifications(&glacier.SetVaultNotificationsInput{ VaultName: aws.String(d.Id()), VaultNotificationConfig: &glacier.VaultNotificationConfig{ SNSTopic: aws.String(s["sns_topic"].(string)), - Events: events, + Events: events, }, }) @@ -205,6 +202,15 @@ func resourceAwsGlacierVaultNotificationUpdate(glacierconn *glacier.Glacier, d * return fmt.Errorf("Error Updating Glacier Vault Notifications: %s", err.Error()) } } + } else { + _, err := glacierconn.DeleteVaultNotifications(&glacier.DeleteVaultNotificationsInput{ + VaultName: aws.String(d.Id()), + }) + + if err != nil { + return fmt.Errorf("Error Removing Glacier Vault Notifications: %s", err.Error()) + } + } return nil @@ -316,7 +322,7 @@ func getGlacierVaultTags(glacierconn *glacier.Glacier, vaultName string) (map[st log.Printf("[DEBUG] Getting the tags: for %s", vaultName) response, err := glacierconn.ListTagsForVault(request) - if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "NoSuchTagSet" { + if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "NoSuchTagSet" { return map[string]string{}, nil } else if err != nil { return nil, err @@ -360,7 +366,7 @@ func glacierPointersToStringList(pointers []*string) []interface{} { return list } -func getGlacierVaultNotification(glacierconn *glacier.Glacier, vaultName string) ([]map[string]interface{},error) { +func getGlacierVaultNotification(glacierconn *glacier.Glacier, vaultName string) ([]map[string]interface{}, error) { request := &glacier.GetVaultNotificationsInput{ VaultName: aws.String(vaultName), } @@ -379,4 +385,3 @@ func getGlacierVaultNotification(glacierconn *glacier.Glacier, vaultName string) return []map[string]interface{}{notifications}, nil } - diff --git a/builtin/providers/aws/resource_aws_glacier_vault_test.go b/builtin/providers/aws/resource_aws_glacier_vault_test.go new file mode 100644 index 000000000000..fc5e150d949c --- /dev/null +++ b/builtin/providers/aws/resource_aws_glacier_vault_test.go @@ -0,0 +1,175 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/glacier" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSGlacierVault_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGlacierVaultDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGlacierVault_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckGlacierVaultExists("aws_glacier_vault.test"), + ), + }, + }, + }) +} + +func TestAccAWSGlacierVault_full(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGlacierVaultDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGlacierVault_full, + Check: resource.ComposeTestCheckFunc( + testAccCheckGlacierVaultExists("aws_glacier_vault.full"), + ), + }, + }, + }) +} + +func TestAccAWSGlacierVault_RemoveNotifications(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGlacierVaultDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccGlacierVault_full, + Check: resource.ComposeTestCheckFunc( + testAccCheckGlacierVaultExists("aws_glacier_vault.full"), + ), + }, + resource.TestStep{ + Config: testAccGlacierVault_withoutNotification, + Check: resource.ComposeTestCheckFunc( + testAccCheckGlacierVaultExists("aws_glacier_vault.full"), + testAccCheckVaultNotificationsMissing("aws_glacier_vault.full"), + ), + }, + }, + }) +} + +func testAccCheckGlacierVaultExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + glacierconn := testAccProvider.Meta().(*AWSClient).glacierconn + out, err := glacierconn.DescribeVault(&glacier.DescribeVaultInput{ + VaultName: aws.String(rs.Primary.ID), + }) + + if err != nil { + return err + } + + if out.VaultARN == nil { + return fmt.Errorf("No Glacier Vault Found") + } + + if *out.VaultName != rs.Primary.ID { + return fmt.Errorf("Glacier Vault Mismatch - existing: %q, state: %q", + *out.VaultName, rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckVaultNotificationsMissing(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + glacierconn := testAccProvider.Meta().(*AWSClient).glacierconn + out, err := glacierconn.GetVaultNotifications(&glacier.GetVaultNotificationsInput{ + VaultName: aws.String(rs.Primary.ID), + }) + + if awserr, ok := err.(awserr.Error); ok && awserr.Code() != "ResourceNotFoundException" { + return fmt.Errorf("Expected ResourceNotFoundException for Vault %s Notification Block but got %s", rs.Primary.ID, awserr.Code()) + } + + if out.VaultNotificationConfig != nil { + return fmt.Errorf("Vault Notification Block has been found for %s", rs.Primary.ID) + } + + return nil + } + +} + +func testAccCheckGlacierVaultDestroy(s *terraform.State) error { + if len(s.RootModule().Resources) > 0 { + return fmt.Errorf("Expected all resources to be gone, but found: %#v", + s.RootModule().Resources) + } + + return nil +} + +const testAccGlacierVault_basic = ` +resource "aws_glacier_vault" "test" { + name = "my_test_vault" +} +` + +const testAccGlacierVault_full = ` +resource "aws_sns_topic" "aws_sns_topic" { + name = "glacier-sns-topic" +} + +resource "aws_glacier_vault" "full" { + name = "my_test_vault" + notification { + sns_topic = "${aws_sns_topic.aws_sns_topic.arn}" + events = ["ArchiveRetrievalCompleted","InventoryRetrievalCompleted"] + } + tags { + Test="Test1" + } +} +` + +const testAccGlacierVault_withoutNotification = ` +resource "aws_sns_topic" "aws_sns_topic" { + name = "glacier-sns-topic" +} + +resource "aws_glacier_vault" "full" { + name = "my_test_vault" + tags { + Test="Test1" + } +} +` diff --git a/website/source/docs/providers/aws/r/glacier_vault.html.markdown b/website/source/docs/providers/aws/r/glacier_vault.html.markdown index ad7e2a6d1457..920bee4f54b3 100644 --- a/website/source/docs/providers/aws/r/glacier_vault.html.markdown +++ b/website/source/docs/providers/aws/r/glacier_vault.html.markdown @@ -10,14 +10,21 @@ description: |- Provides a Glacier Vault Resource. You can refer to the [Glacier Developer Guide](http://docs.aws.amazon.com/amazonglacier/latest/dev/working-with-vaults.html) for a full explanation of the Glacier Vault functionality +~> **NOTE:** When trying to remove a Glacier Vault, the Vault must be empty. + ## Example Usage ``` + +resource "aws_sns_topic" "aws_sns_topic" { + name = "glacier-sns-topic" +} + resource "aws_glacier_vault" "my_archive" { name = "MyArchive" notification { - sns_topic = "arn:aws:sns:us-west-2:432981146916:MyArchiveTopic" + sns_topic = "${aws_sns_topic.aws_sns_topic.arn}" events = ["ArchiveRetrievalCompleted","InventoryRetrievalCompleted"] } @@ -51,15 +58,15 @@ EOF The following arguments are supported: -* `name` - (Required) The name of the Vault. Names can be between 1 and 255 characters long and the valid characters are a-z, A-Z, 0-9, '_' (underscore), '-' (hyphen), and '.' (period). -* `access_policy` - (Required) The policy document. This is a JSON formatted string. - The heredoc syntax or `file` function is helpful here. -* `notification` - (Required) The notifications for the Vault. Fields documented below. +* `name` - (Required) The name of the Vault. Names can be between 1 and 255 characters long and the valid characters are a-z, A-Z, 0-9, '\_' (underscore), '-' (hyphen), and '.' (period). +* `access_policy` - (Optional) The policy document. This is a JSON formatted string. + The heredoc syntax or `file` function is helpful here. Use the [Glacier Developer Guide](https://docs.aws.amazon.com/amazonglacier/latest/dev/vault-access-policy.html) for more information on Glacier Vault Policy +* `notification` - (Optional) The notifications for the Vault. Fields documented below. * `tags` - (Optional) A mapping of tags to assign to the resource. **notification** supports the following: -* `events` - (Required) You can configure a vault to public a notification for `ArchiveRetrievalCompleted` and `InventoryRetrievalCompleted` events. +* `events` - (Required) You can configure a vault to publish a notification for `ArchiveRetrievalCompleted` and `InventoryRetrievalCompleted` events. * `sns_topic` - (Required) The SNS Topic ARN. The following attributes are exported: