forked from hashicorp/terraform-provider-azurerm
/
data_source_notification_hub.go
176 lines (145 loc) · 4.79 KB
/
data_source_notification_hub.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package azurerm
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceNotificationHub() *schema.Resource {
return &schema.Resource{
Read: dataSourceNotificationHubRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"location": locationForDataSourceSchema(),
"apns_credential": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"application_mode": {
Type: schema.TypeString,
Computed: true,
},
"bundle_id": {
Type: schema.TypeString,
Computed: true,
},
"key_id": {
Type: schema.TypeString,
Computed: true,
},
// Team ID (within Apple & the Portal) == "AppID" (within the API)
"team_id": {
Type: schema.TypeString,
Computed: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},
"gcm_credential": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},
// NOTE: skipping tags as there's a bug in the API where the Keys for Tags are returned in lower-case
// Azure Rest API Specs issue: https://github.com/Azure/azure-sdk-for-go/issues/2239
//"tags": tagsForDataSourceSchema(),
},
}
}
func dataSourceNotificationHubRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).notificationHubsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
namespaceName := d.Get("namespace_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
resp, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Notification Hub %q was not found in Namespace %q / Resource Group %q", name, namespaceName, resourceGroup)
}
return fmt.Errorf("Error making Read request on Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}
credentials, err := client.GetPnsCredentials(ctx, resourceGroup, namespaceName, name)
if err != nil {
return fmt.Errorf("Error retrieving Credentials for Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}
d.SetId(*resp.ID)
d.Set("name", resp.Name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if props := credentials.PnsCredentialsProperties; props != nil {
apns := flattenNotificationHubsDataSourceAPNSCredentials(props.ApnsCredential)
if d.Set("apns_credential", apns); err != nil {
return fmt.Errorf("Error setting `apns_credential`: %+v", err)
}
gcm := flattenNotificationHubsDataSourceGCMCredentials(props.GcmCredential)
if d.Set("gcm_credential", gcm); err != nil {
return fmt.Errorf("Error setting `gcm_credential`: %+v", err)
}
}
return nil
}
func flattenNotificationHubsDataSourceAPNSCredentials(input *notificationhubs.ApnsCredential) []interface{} {
if input == nil {
return make([]interface{}, 0)
}
output := make(map[string]interface{})
if bundleId := input.AppName; bundleId != nil {
output["bundle_id"] = *bundleId
}
if endpoint := input.Endpoint; endpoint != nil {
applicationEndpoints := map[string]string{
"https://api.push.apple.com:443/3/device": "Production",
"https://api.development.push.apple.com:443/3/device": "Sandbox",
}
applicationMode := applicationEndpoints[*endpoint]
output["application_mode"] = applicationMode
}
if keyId := input.KeyID; keyId != nil {
output["key_id"] = *keyId
}
if teamId := input.AppID; teamId != nil {
output["team_id"] = *teamId
}
if token := input.Token; token != nil {
output["token"] = *token
}
return []interface{}{output}
}
func flattenNotificationHubsDataSourceGCMCredentials(input *notificationhubs.GcmCredential) []interface{} {
if input == nil {
return []interface{}{}
}
output := make(map[string]interface{})
if props := input.GcmCredentialProperties; props != nil {
if apiKey := props.GoogleAPIKey; apiKey != nil {
output["api_key"] = *apiKey
}
}
return []interface{}{output}
}