forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_cognitive_account.go
263 lines (219 loc) · 6.97 KB
/
resource_arm_cognitive_account.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package azurerm
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/cognitiveservices/mgmt/2017-04-18/cognitiveservices"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
type cognitiveServicesPropertiesStruct struct{}
func resourceArmCognitiveAccount() *schema.Resource {
return &schema.Resource{
Create: resourceArmCognitiveAccountCreate,
Read: resourceArmCognitiveAccountRead,
Update: resourceArmCognitiveAccountUpdate,
Delete: resourceArmCognitiveAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.CognitiveServicesAccountName(),
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"kind": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Academic",
"Bing.Autosuggest",
"Bing.Autosuggest.v7",
"Bing.CustomSearch",
"Bing.Search",
"Bing.Search.v7",
"Bing.Speech",
"Bing.SpellCheck",
"Bing.SpellCheck.v7",
"ComputerVision",
"ContentModerator",
"CustomSpeech",
"Emotion",
"Face",
"LUIS",
"Recommendations",
"SpeakerRecognition",
"Speech",
"SpeechTranslation",
"TextAnalytics",
"TextTranslation",
"WebLM",
}, false),
},
"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(cognitiveservices.F0),
string(cognitiveservices.S0),
string(cognitiveservices.S1),
string(cognitiveservices.S2),
string(cognitiveservices.S3),
string(cognitiveservices.S4),
string(cognitiveservices.S5),
string(cognitiveservices.S6),
string(cognitiveservices.P0),
string(cognitiveservices.P1),
string(cognitiveservices.P2),
}, false),
},
"tier": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(cognitiveservices.Free),
string(cognitiveservices.Standard),
string(cognitiveservices.Premium),
}, false),
},
},
},
},
"tags": tagsSchema(),
"endpoint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmCognitiveAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).cognitiveAccountsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
kind := d.Get("kind").(string)
tags := d.Get("tags").(map[string]interface{})
sku := expandCognitiveAccountSku(d)
properties := cognitiveservices.AccountCreateParameters{
Kind: cognitiveservices.Kind(kind),
Location: utils.String(location),
Sku: sku,
Properties: &cognitiveServicesPropertiesStruct{},
Tags: expandTags(tags),
}
_, err := client.Create(ctx, resourceGroup, name, properties)
if err != nil {
return fmt.Errorf("Error creating Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
read, err := client.GetProperties(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(*read.ID)
return resourceArmCognitiveAccountRead(d, meta)
}
func resourceArmCognitiveAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).cognitiveAccountsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["accounts"]
tags := d.Get("tags").(map[string]interface{})
sku := expandCognitiveAccountSku(d)
properties := cognitiveservices.AccountUpdateParameters{
Sku: sku,
Tags: expandTags(tags),
}
_, err = client.Update(ctx, resourceGroup, name, properties)
if err != nil {
return fmt.Errorf("Error updating Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
return resourceArmCognitiveAccountRead(d, meta)
}
func resourceArmCognitiveAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).cognitiveAccountsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["accounts"]
resp, err := client.GetProperties(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Cognitive Services Account %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}
return err
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("kind", resp.Kind)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if err := d.Set("sku", flattenCognitiveAccountSku(resp.Sku)); err != nil {
return fmt.Errorf("Error flattening `sku`: %+v", err)
}
if props := resp.AccountProperties; props != nil {
d.Set("endpoint", props.Endpoint)
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func resourceArmCognitiveAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).cognitiveAccountsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["accounts"]
resp, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("Error deleting Cognitive Services Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}
return nil
}
func expandCognitiveAccountSku(d *schema.ResourceData) *cognitiveservices.Sku {
skus := d.Get("sku").([]interface{})
sku := skus[0].(map[string]interface{})
return &cognitiveservices.Sku{
Name: cognitiveservices.SkuName(sku["name"].(string)),
Tier: cognitiveservices.SkuTier(sku["tier"].(string)),
}
}
func flattenCognitiveAccountSku(input *cognitiveservices.Sku) []interface{} {
if input == nil {
return []interface{}{}
}
return []interface{}{
map[string]interface{}{
"name": string(input.Name),
"tier": string(input.Tier),
},
}
}