forked from hashicorp/terraform-provider-aws
/
resource_aws_pinpoint_apns_sandbox_channel.go
159 lines (132 loc) · 4.57 KB
/
resource_aws_pinpoint_apns_sandbox_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package aws
import (
"errors"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/pinpoint"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsPinpointAPNSSandboxChannel() *schema.Resource {
return &schema.Resource{
Create: resourceAwsPinpointAPNSSandboxChannelUpsert,
Read: resourceAwsPinpointAPNSSandboxChannelRead,
Update: resourceAwsPinpointAPNSSandboxChannelUpsert,
Delete: resourceAwsPinpointAPNSSandboxChannelDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"application_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bundle_id": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"certificate": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"default_authentication_method": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"private_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"team_id": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"token_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"token_key_id": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
},
}
}
func resourceAwsPinpointAPNSSandboxChannelUpsert(d *schema.ResourceData, meta interface{}) error {
certificate, certificateOk := d.GetOk("certificate")
privateKey, privateKeyOk := d.GetOk("private_key")
bundleId, bundleIdOk := d.GetOk("bundle_id")
teamId, teamIdOk := d.GetOk("team_id")
tokenKey, tokenKeyOk := d.GetOk("token_key")
tokenKeyId, tokenKeyIdOk := d.GetOk("token_key_id")
if !(certificateOk && privateKeyOk) && !(bundleIdOk && teamIdOk && tokenKeyOk && tokenKeyIdOk) {
return errors.New("At least one set of credentials is required; either [certificate, private_key] or [bundle_id, team_id, token_key, token_key_id]")
}
conn := meta.(*AWSClient).pinpointconn
applicationId := d.Get("application_id").(string)
params := &pinpoint.APNSSandboxChannelRequest{}
params.DefaultAuthenticationMethod = aws.String(d.Get("default_authentication_method").(string))
params.Enabled = aws.Bool(d.Get("enabled").(bool))
params.Certificate = aws.String(certificate.(string))
params.PrivateKey = aws.String(privateKey.(string))
params.BundleId = aws.String(bundleId.(string))
params.TeamId = aws.String(teamId.(string))
params.TokenKey = aws.String(tokenKey.(string))
params.TokenKeyId = aws.String(tokenKeyId.(string))
req := pinpoint.UpdateApnsSandboxChannelInput{
ApplicationId: aws.String(applicationId),
APNSSandboxChannelRequest: params,
}
_, err := conn.UpdateApnsSandboxChannel(&req)
if err != nil {
return fmt.Errorf("error updating Pinpoint APNs Sandbox Channel for Application %s: %s", applicationId, err)
}
d.SetId(applicationId)
return resourceAwsPinpointAPNSSandboxChannelRead(d, meta)
}
func resourceAwsPinpointAPNSSandboxChannelRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).pinpointconn
log.Printf("[INFO] Reading Pinpoint APNs Channel for Application %s", d.Id())
output, err := conn.GetApnsSandboxChannel(&pinpoint.GetApnsSandboxChannelInput{
ApplicationId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Pinpoint APNs Sandbox Channel for application %s not found, error code (404)", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error getting Pinpoint APNs Sandbox Channel for application %s: %s", d.Id(), err)
}
d.Set("application_id", output.APNSSandboxChannelResponse.ApplicationId)
d.Set("default_authentication_method", output.APNSSandboxChannelResponse.DefaultAuthenticationMethod)
d.Set("enabled", output.APNSSandboxChannelResponse.Enabled)
// Sensitive params are not returned
return nil
}
func resourceAwsPinpointAPNSSandboxChannelDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).pinpointconn
log.Printf("[DEBUG] Deleting Pinpoint APNs Sandbox Channel: %s", d.Id())
_, err := conn.DeleteApnsSandboxChannel(&pinpoint.DeleteApnsSandboxChannelInput{
ApplicationId: aws.String(d.Id()),
})
if isAWSErr(err, pinpoint.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting Pinpoint APNs Sandbox Channel for Application %s: %s", d.Id(), err)
}
return nil
}