Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resource: aws_pinpoint_baidu_channel #6111

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
114 changes: 114 additions & 0 deletions aws/resource_aws_pinpoint_baidu_channel.go
Original file line number Diff line number Diff line change
@@ -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
}
149 changes: 149 additions & 0 deletions aws/resource_aws_pinpoint_baidu_channel_test.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,9 @@
<li<%= sidebar_current("docs-aws-resource-pinpoint-adm-channel") %>>
<a href="/docs/providers/aws/r/pinpoint_adm_channel.html">aws_pinpoint_adm_channel</a>
</li>
<li<%= sidebar_current("docs-aws-resource-pinpoint-baidu-channel") %>>
<a href="/docs/providers/aws/r/pinpoint_baidu_channel.html">aws_pinpoint_baidu_channel</a>
</li>
<li<%= sidebar_current("docs-aws-resource-pinpoint-email-channel") %>>
<a href="/docs/providers/aws/r/pinpoint_email_channel.html">aws_pinpoint_email_channel</a>
</li>
Expand Down
46 changes: 46 additions & 0 deletions website/docs/r/pinpoint_baidu_channel.markdown
Original file line number Diff line number Diff line change
@@ -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
```