From af82885311fdc435c396064a251b0bbe24545939 Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Mon, 8 Oct 2018 14:48:31 +0200 Subject: [PATCH 1/2] resource/aws_pinpoint_gcm_channel --- aws/provider.go | 1 + aws/resource_aws_pinpoint_gcm_channel.go | 113 +++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 aws/resource_aws_pinpoint_gcm_channel.go diff --git a/aws/provider.go b/aws/provider.go index d01014e8f843..709e2cfa2a75 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -674,6 +674,7 @@ func Provider() terraform.ResourceProvider { "aws_batch_job_queue": resourceAwsBatchJobQueue(), "aws_pinpoint_app": resourceAwsPinpointApp(), "aws_pinpoint_event_stream": resourceAwsPinpointEventStream(), + "aws_pinpoint_gcm_channel": resourceAwsPinpointGCMChannel(), "aws_pinpoint_sms_channel": resourceAwsPinpointSMSChannel(), // ALBs are actually LBs because they can be type `network` or `application` diff --git a/aws/resource_aws_pinpoint_gcm_channel.go b/aws/resource_aws_pinpoint_gcm_channel.go new file mode 100644 index 000000000000..328bbd24f07a --- /dev/null +++ b/aws/resource_aws_pinpoint_gcm_channel.go @@ -0,0 +1,113 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/pinpoint" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsPinpointGCMChannel() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsPinpointGCMChannelUpsert, + Read: resourceAwsPinpointGCMChannelRead, + Update: resourceAwsPinpointGCMChannelUpsert, + Delete: resourceAwsPinpointGCMChannelDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "application_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "api_key": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + }, + "enabled": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + }, + } +} + +func resourceAwsPinpointGCMChannelUpsert(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + applicationId := d.Get("application_id").(string) + + params := &pinpoint.GCMChannelRequest{} + + if d.HasChange("api_key") { + params.ApiKey = aws.String(d.Get("api_key").(string)) + } + + if d.HasChange("enabled") { + params.Enabled = aws.Bool(d.Get("enabled").(bool)) + } + + req := pinpoint.UpdateGcmChannelInput{ + ApplicationId: aws.String(applicationId), + GCMChannelRequest: params, + } + + _, err := conn.UpdateGcmChannel(&req) + if err != nil { + return fmt.Errorf("error putting Pinpoint GCM Channel for application %s: %s", applicationId, err) + } + + d.SetId(applicationId) + + return resourceAwsPinpointGCMChannelRead(d, meta) +} + +func resourceAwsPinpointGCMChannelRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + log.Printf("[INFO] Reading Pinpoint GCM Channel for application %s", d.Id()) + + output, err := conn.GetGcmChannel(&pinpoint.GetGcmChannelInput{ + ApplicationId: aws.String(d.Id()), + }) + if err != nil { + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + log.Printf("[WARN] Pinpoint GCM Channel for application %s not found, error code (404)", d.Id()) + d.SetId("") + return nil + } + + return fmt.Errorf("error getting Pinpoint GCM Channel for application %s: %s", d.Id(), err) + } + + d.Set("application_id", output.GCMChannelResponse.ApplicationId) + d.Set("enabled", output.GCMChannelResponse.Enabled) + // api_key is never returned + + return nil +} + +func resourceAwsPinpointGCMChannelDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + log.Printf("[DEBUG] Deleting Pinpoint GCM Channel for application %s", d.Id()) + _, err := conn.DeleteGcmChannel(&pinpoint.DeleteGcmChannelInput{ + ApplicationId: aws.String(d.Id()), + }) + + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + return nil + } + + if err != nil { + return fmt.Errorf("error deleting Pinpoint GCM Channel for application %s: %s", d.Id(), err) + } + return nil +} From ebae8163a9dbc825f95fd47e8eb0f35a975d18fe Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Mon, 8 Oct 2018 15:35:40 +0200 Subject: [PATCH 2/2] resource/aws_pinpoint_gcm_channel: docs + tests --- aws/resource_aws_pinpoint_gcm_channel_test.go | 124 ++++++++++++++++++ website/aws.erb | 3 + website/docs/r/pinpoint_gcm_channel.markdown | 42 ++++++ 3 files changed, 169 insertions(+) create mode 100644 aws/resource_aws_pinpoint_gcm_channel_test.go create mode 100644 website/docs/r/pinpoint_gcm_channel.markdown diff --git a/aws/resource_aws_pinpoint_gcm_channel_test.go b/aws/resource_aws_pinpoint_gcm_channel_test.go new file mode 100644 index 000000000000..f7a8fbc3d585 --- /dev/null +++ b/aws/resource_aws_pinpoint_gcm_channel_test.go @@ -0,0 +1,124 @@ +package aws + +import ( + "fmt" + "os" + "testing" + + "github.com/aws/aws-sdk-go/service/pinpoint" + + "github.com/aws/aws-sdk-go/aws" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +/** + Before running this test, the following ENV variable must be set: + + GCM_API_KEY - Google Cloud Messaging Api Key +**/ + +func TestAccAWSPinpointGCMChannel_basic(t *testing.T) { + oldDefaultRegion := os.Getenv("AWS_DEFAULT_REGION") + os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + defer os.Setenv("AWS_DEFAULT_REGION", oldDefaultRegion) + + var channel pinpoint.GCMChannelResponse + resourceName := "aws_pinpoint_gcm_channel.test_gcm_channel" + + if os.Getenv("GCM_API_KEY") == "" { + t.Skipf("GCM_API_KEY env missing, skip test") + } + + apiKey := os.Getenv("GCM_API_KEY") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: resourceName, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSPinpointGCMChannelDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSPinpointGCMChannelConfig_basic(apiKey), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSPinpointGCMChannelExists(resourceName, &channel), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"api_key"}, + }, + }, + }) +} + +func testAccCheckAWSPinpointGCMChannelExists(n string, channel *pinpoint.GCMChannelResponse) 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("No Pinpoint GCM Channel with that application ID exists") + } + + conn := testAccProvider.Meta().(*AWSClient).pinpointconn + + // Check if the app exists + params := &pinpoint.GetGcmChannelInput{ + ApplicationId: aws.String(rs.Primary.ID), + } + output, err := conn.GetGcmChannel(params) + + if err != nil { + return err + } + + *channel = *output.GCMChannelResponse + + return nil + } +} + +func testAccAWSPinpointGCMChannelConfig_basic(apiKey string) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} + +resource "aws_pinpoint_app" "test_app" {} + +resource "aws_pinpoint_gcm_channel" "test_gcm_channel" { + application_id = "${aws_pinpoint_app.test_app.application_id}" + enabled = "true" + api_key = "%s" +}`, apiKey) +} + +func testAccCheckAWSPinpointGCMChannelDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).pinpointconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_gcm_channel" { + continue + } + + // Check if the event stream exists + params := &pinpoint.GetGcmChannelInput{ + ApplicationId: aws.String(rs.Primary.ID), + } + _, err := conn.GetGcmChannel(params) + if err != nil { + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + continue + } + return err + } + return fmt.Errorf("GCM Channel exists when it should be destroyed!") + } + + return nil +} diff --git a/website/aws.erb b/website/aws.erb index 4b92eb760092..7aa8603804bd 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -1682,6 +1682,9 @@ > aws_pinpoint_event_stream + > + aws_pinpoint_gcm_channel + > aws_pinpoint_sms_channel diff --git a/website/docs/r/pinpoint_gcm_channel.markdown b/website/docs/r/pinpoint_gcm_channel.markdown new file mode 100644 index 000000000000..2aa760f4da85 --- /dev/null +++ b/website/docs/r/pinpoint_gcm_channel.markdown @@ -0,0 +1,42 @@ +--- +layout: "aws" +page_title: "AWS: aws_pinpoint_gcm_channel" +sidebar_current: "docs-aws-resource-pinpoint-gcm-channel" +description: |- + Provides a Pinpoint GCM Channel resource. +--- + +# aws_pinpoint_gcm_channel + +Provides a Pinpoint GCM Channel resource. + +~> **Note:** Api Key argument will be stored in the raw state as plain-text. +[Read more about sensitive data in state](/docs/state/sensitive-data.html). + +## Example Usage + +```hcl +resource "aws_pinpoint_gcm_channel" "gcm" { + application_id = "${aws_pinpoint_app.app.application_id}" + api_key = "api_key" +} + +resource "aws_pinpoint_app" "app" {} +``` + + +## Argument Reference + +The following arguments are supported: + +* `application_id` - (Required) The application ID. +* `api_key` - (Required) Platform credential API key from Google. +* `enabled` - (Optional) Whether the channel is enabled or disabled. Defaults to `true`. + +## Import + +Pinpoint GCM Channel can be imported using the `application-id`, e.g. + +``` +$ terraform import aws_pinpoint_gcm_channel.gcm application-id +```