From d83ecc1882ffc3254c1a421b5f104d9f00828a50 Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Wed, 10 Oct 2018 12:32:41 +0200 Subject: [PATCH 1/3] resource/aws_pinpoint_baidu_channel --- aws/provider.go | 1 + aws/resource_aws_pinpoint_baidu_channel.go | 122 +++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 aws/resource_aws_pinpoint_baidu_channel.go diff --git a/aws/provider.go b/aws/provider.go index dbda0f83849..d61079414da 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -677,6 +677,7 @@ func Provider() terraform.ResourceProvider { "aws_batch_job_queue": resourceAwsBatchJobQueue(), "aws_pinpoint_app": resourceAwsPinpointApp(), "aws_pinpoint_adm_channel": resourceAwsPinpointADMChannel(), + "aws_pinpoint_baidu_channel": resourceAwsPinpointBaiduChannel(), "aws_pinpoint_event_stream": resourceAwsPinpointEventStream(), "aws_pinpoint_gcm_channel": resourceAwsPinpointGCMChannel(), "aws_pinpoint_sms_channel": resourceAwsPinpointSMSChannel(), diff --git a/aws/resource_aws_pinpoint_baidu_channel.go b/aws/resource_aws_pinpoint_baidu_channel.go new file mode 100644 index 00000000000..0040485955a --- /dev/null +++ b/aws/resource_aws_pinpoint_baidu_channel.go @@ -0,0 +1,122 @@ +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 resourceAwsPinpointBaiduChannel() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsPinpointBaiduChannelUpsert, + Read: resourceAwsPinpointBaiduChannelRead, + Update: resourceAwsPinpointBaiduChannelUpsert, + Delete: resourceAwsPinpointBaiduChannelDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "application_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "enabled": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + "api_key": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + }, + "secret_key": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + }, + }, + } +} + +func resourceAwsPinpointBaiduChannelUpsert(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + applicationId := d.Get("application_id").(string) + + params := &pinpoint.BaiduChannelRequest{} + + if d.HasChange("enabled") { + params.Enabled = aws.Bool(d.Get("enabled").(bool)) + } + + if d.HasChange("api_key") { + params.ApiKey = aws.String(d.Get("api_key").(string)) + } + + if d.HasChange("secret_key") { + params.SecretKey = aws.String(d.Get("secret_key").(string)) + } + + req := pinpoint.UpdateBaiduChannelInput{ + ApplicationId: aws.String(applicationId), + BaiduChannelRequest: params, + } + + _, err := conn.UpdateBaiduChannel(&req) + if err != nil { + return fmt.Errorf("error updating Pinpoint Baidu Channel for application %s: %s", applicationId, err) + } + + d.SetId(applicationId) + + return resourceAwsPinpointBaiduChannelRead(d, meta) +} + +func resourceAwsPinpointBaiduChannelRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + log.Printf("[INFO] Reading Pinpoint Baidu Channel for application %s", d.Id()) + + output, err := conn.GetBaiduChannel(&pinpoint.GetBaiduChannelInput{ + ApplicationId: aws.String(d.Id()), + }) + if err != nil { + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + log.Printf("[WARN] Pinpoint Baidu Channel for application %s not found, error code (404)", d.Id()) + d.SetId("") + return nil + } + + return fmt.Errorf("error getting Pinpoint Baidu Channel for application %s: %s", d.Id(), err) + } + + d.Set("application_id", output.BaiduChannelResponse.ApplicationId) + d.Set("enabled", output.BaiduChannelResponse.Enabled) + // ApiKey and SecretKey are never returned + + return nil +} + +func resourceAwsPinpointBaiduChannelDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).pinpointconn + + log.Printf("[DEBUG] Deleting Pinpoint Baidu Channel for application %s", d.Id()) + _, err := conn.DeleteBaiduChannel(&pinpoint.DeleteBaiduChannelInput{ + ApplicationId: aws.String(d.Id()), + }) + + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + return nil + } + + if err != nil { + return fmt.Errorf("error deleting Pinpoint Baidu Channel for application %s: %s", d.Id(), err) + } + return nil +} From bd55af8e9e3db5fdbb6e4ba8d3adb1f6de9aebba Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Wed, 10 Oct 2018 12:35:47 +0200 Subject: [PATCH 2/3] resource/aws_pinpoint_baidu_channel: docs + tests --- ...esource_aws_pinpoint_baidu_channel_test.go | 120 ++++++++++++++++++ website/aws.erb | 3 + .../docs/r/pinpoint_baidu_channel.markdown | 46 +++++++ 3 files changed, 169 insertions(+) create mode 100644 aws/resource_aws_pinpoint_baidu_channel_test.go create mode 100644 website/docs/r/pinpoint_baidu_channel.markdown diff --git a/aws/resource_aws_pinpoint_baidu_channel_test.go b/aws/resource_aws_pinpoint_baidu_channel_test.go new file mode 100644 index 00000000000..16cd3b4746f --- /dev/null +++ b/aws/resource_aws_pinpoint_baidu_channel_test.go @@ -0,0 +1,120 @@ +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" +) + +func TestAccAWSPinpointBaiduChannel_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.BaiduChannelResponse + resourceName := "aws_pinpoint_baidu_channel.channel" + + apiKey := "123" + secretKey := "456" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: resourceName, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSPinpointBaiduChannelDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSPinpointBaiduChannelConfig_basic(apiKey, secretKey), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSPinpointBaiduChannelExists(resourceName, &channel), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "api_key", apiKey), + resource.TestCheckResourceAttr(resourceName, "secret_key", secretKey), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"api_key", "secret_key"}, + }, + }, + }) +} + +func testAccCheckAWSPinpointBaiduChannelExists(n string, channel *pinpoint.BaiduChannelResponse) 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 Baidu channel with that Application ID exists") + } + + conn := testAccProvider.Meta().(*AWSClient).pinpointconn + + // Check if the Baidu Channel exists + params := &pinpoint.GetBaiduChannelInput{ + ApplicationId: aws.String(rs.Primary.ID), + } + output, err := conn.GetBaiduChannel(params) + + if err != nil { + return err + } + + *channel = *output.BaiduChannelResponse + + return nil + } +} + +func testAccAWSPinpointBaiduChannelConfig_basic(apiKey, secretKey string) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} + +resource "aws_pinpoint_app" "test_app" {} + +resource "aws_pinpoint_baidu_channel" "channel" { + application_id = "${aws_pinpoint_app.test_app.application_id}" + + api_key = "%s" + secret_key = "%s" +} +`, apiKey, secretKey) +} + +func testAccCheckAWSPinpointBaiduChannelDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).pinpointconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_baidu_channel" { + continue + } + + // Check if the Baidu channel exists by fetching its attributes + params := &pinpoint.GetBaiduChannelInput{ + ApplicationId: aws.String(rs.Primary.ID), + } + _, err := conn.GetBaiduChannel(params) + if err != nil { + if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") { + continue + } + return err + } + return fmt.Errorf("Baidu Channel exists when it should be destroyed!") + } + + return nil +} diff --git a/website/aws.erb b/website/aws.erb index f5d5818888a..42707fac48c 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -1689,6 +1689,9 @@ > aws_pinpoint_adm_channel + > + aws_pinpoint_baidu_channel + > aws_pinpoint_event_stream diff --git a/website/docs/r/pinpoint_baidu_channel.markdown b/website/docs/r/pinpoint_baidu_channel.markdown new file mode 100644 index 00000000000..f7380e70473 --- /dev/null +++ b/website/docs/r/pinpoint_baidu_channel.markdown @@ -0,0 +1,46 @@ +--- +layout: "aws" +page_title: "AWS: aws_pinpoint_baidu_channel" +sidebar_current: "docs-aws-resource-pinpoint-baidu-channel" +description: |- + Provides a Pinpoint Baidu Channel resource. +--- + +# aws_pinpoint_baidu_channel + +Provides a Pinpoint Baidu Channel resource. + +~> **Note:** All arguments including the Api Key and Secret Key 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_app" "app" {} + +resource "aws_pinpoint_baidu_channel" "channel" { + application_id = "${aws_pinpoint_app.app.application_id}" + api_key = "" + secret_key = "" + +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `application_id` - (Required) The application ID. +* `enabled` - (Optional) Specifies whether to enable the channel. Defaults to `true`. +* `api_key` - (Required) Platform credential API key from Baidu. +* `secret_key` - (Required) Platform credential Secret key from Baidu. + +## Import + +Pinpoint Baidu Channel can be imported using the `application-id`, e.g. + +``` +$ terraform import aws_pinpoint_baidu_channel.channel application-id +``` From b172ae7255c598ab37168e0a5f7c319da9b54fc8 Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Wed, 10 Oct 2018 15:16:40 +0200 Subject: [PATCH 3/3] fix: update needs all the params --- aws/resource_aws_pinpoint_baidu_channel.go | 14 ++------- ...esource_aws_pinpoint_baidu_channel_test.go | 31 ++++++++++++++++++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_pinpoint_baidu_channel.go b/aws/resource_aws_pinpoint_baidu_channel.go index 0040485955a..595696b7a49 100644 --- a/aws/resource_aws_pinpoint_baidu_channel.go +++ b/aws/resource_aws_pinpoint_baidu_channel.go @@ -51,17 +51,9 @@ func resourceAwsPinpointBaiduChannelUpsert(d *schema.ResourceData, meta interfac params := &pinpoint.BaiduChannelRequest{} - if d.HasChange("enabled") { - params.Enabled = aws.Bool(d.Get("enabled").(bool)) - } - - if d.HasChange("api_key") { - params.ApiKey = aws.String(d.Get("api_key").(string)) - } - - if d.HasChange("secret_key") { - params.SecretKey = aws.String(d.Get("secret_key").(string)) - } + params.Enabled = aws.Bool(d.Get("enabled").(bool)) + params.ApiKey = aws.String(d.Get("api_key").(string)) + params.SecretKey = aws.String(d.Get("secret_key").(string)) req := pinpoint.UpdateBaiduChannelInput{ ApplicationId: aws.String(applicationId), diff --git a/aws/resource_aws_pinpoint_baidu_channel_test.go b/aws/resource_aws_pinpoint_baidu_channel_test.go index 16cd3b4746f..8292de37b5e 100644 --- a/aws/resource_aws_pinpoint_baidu_channel_test.go +++ b/aws/resource_aws_pinpoint_baidu_channel_test.go @@ -21,6 +21,7 @@ func TestAccAWSPinpointBaiduChannel_basic(t *testing.T) { resourceName := "aws_pinpoint_baidu_channel.channel" apiKey := "123" + apikeyUpdated := "234" secretKey := "456" resource.Test(t, resource.TestCase{ @@ -33,7 +34,7 @@ func TestAccAWSPinpointBaiduChannel_basic(t *testing.T) { Config: testAccAWSPinpointBaiduChannelConfig_basic(apiKey, secretKey), Check: resource.ComposeTestCheckFunc( testAccCheckAWSPinpointBaiduChannelExists(resourceName, &channel), - resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), resource.TestCheckResourceAttr(resourceName, "api_key", apiKey), resource.TestCheckResourceAttr(resourceName, "secret_key", secretKey), ), @@ -44,6 +45,15 @@ func TestAccAWSPinpointBaiduChannel_basic(t *testing.T) { ImportStateVerify: true, ImportStateVerifyIgnore: []string{"api_key", "secret_key"}, }, + { + Config: testAccAWSPinpointBaiduChannelConfig_update(apikeyUpdated, secretKey), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSPinpointBaiduChannelExists(resourceName, &channel), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "api_key", apikeyUpdated), + resource.TestCheckResourceAttr(resourceName, "secret_key", secretKey), + ), + }, }, }) } @@ -87,7 +97,26 @@ resource "aws_pinpoint_app" "test_app" {} resource "aws_pinpoint_baidu_channel" "channel" { application_id = "${aws_pinpoint_app.test_app.application_id}" + + enabled = "false" + api_key = "%s" + secret_key = "%s" +} +`, apiKey, secretKey) +} + +func testAccAWSPinpointBaiduChannelConfig_update(apiKey, secretKey string) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} + +resource "aws_pinpoint_app" "test_app" {} +resource "aws_pinpoint_baidu_channel" "channel" { + application_id = "${aws_pinpoint_app.test_app.application_id}" + + enabled = "false" api_key = "%s" secret_key = "%s" }