diff --git a/aws/provider.go b/aws/provider.go index 386f2b1abfe..ad0540834e0 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_email_channel": resourceAwsPinpointEmailChannel(), "aws_pinpoint_event_stream": resourceAwsPinpointEventStream(), "aws_pinpoint_gcm_channel": resourceAwsPinpointGCMChannel(), diff --git a/aws/resource_aws_pinpoint_baidu_channel.go b/aws/resource_aws_pinpoint_baidu_channel.go new file mode 100644 index 00000000000..595696b7a49 --- /dev/null +++ b/aws/resource_aws_pinpoint_baidu_channel.go @@ -0,0 +1,114 @@ +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{} + + 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), + 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 +} 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..8292de37b5e --- /dev/null +++ b/aws/resource_aws_pinpoint_baidu_channel_test.go @@ -0,0 +1,149 @@ +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" + apikeyUpdated := "234" + 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", "false"), + resource.TestCheckResourceAttr(resourceName, "api_key", apiKey), + resource.TestCheckResourceAttr(resourceName, "secret_key", secretKey), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + 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), + ), + }, + }, + }) +} + +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}" + + 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" +} +`, 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 f75890ce3fa..78f2d856558 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -1689,6 +1689,9 @@