forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_factory_linked_service_cosmosdb_mongoapi_resource.go
258 lines (210 loc) · 8.91 KB
/
data_factory_linked_service_cosmosdb_mongoapi_resource.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
package datafactory
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/datafactory/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/datafactory/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceDataFactoryLinkedServiceCosmosDbMongoAPI() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate,
Read: resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead,
Update: resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate,
Delete: resourceDataFactoryLinkedServiceCosmosDbMongoAPIDelete,
Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error {
_, err := parse.LinkedServiceID(id)
return err
}, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI)),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LinkedServiceDatasetName,
},
"data_factory_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataFactoryName(),
},
// There's a bug in the Azure API where this is returned in lower-case
// BUG: https://github.com/Azure/azure-rest-api-specs/issues/5788
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),
"connection_string": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
DiffSuppressFunc: azureRmDataFactoryLinkedServiceConnectionStringDiff,
ValidateFunc: validation.StringIsNotEmpty,
},
"database": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"server_version_is_32_or_higher": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"description": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"integration_runtime_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"parameters": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"annotations": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"additional_properties": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
}
}
func resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_data_factory_linked_service_cosmosdb", *existing.ID)
}
}
cosmosdbProperties := &datafactory.CosmosDbMongoDbAPILinkedServiceTypeProperties{}
connectionStringSecureString := datafactory.SecureString{
Value: utils.String(d.Get("connection_string").(string)),
Type: datafactory.TypeSecureString,
}
cosmosdbProperties.ConnectionString = connectionStringSecureString
cosmosdbProperties.Database = d.Get("database").(string)
cosmosdbProperties.IsServerVersionAbove32 = d.Get("server_version_is_32_or_higher").(bool)
cosmosdbLinkedService := &datafactory.CosmosDbMongoDbAPILinkedService{
Description: utils.String(d.Get("description").(string)),
CosmosDbMongoDbAPILinkedServiceTypeProperties: cosmosdbProperties,
Type: datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI,
}
if v, ok := d.GetOk("parameters"); ok {
cosmosdbLinkedService.Parameters = expandDataFactoryParameters(v.(map[string]interface{}))
}
if v, ok := d.GetOk("integration_runtime_name"); ok {
cosmosdbLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string))
}
if v, ok := d.GetOk("additional_properties"); ok {
cosmosdbLinkedService.AdditionalProperties = v.(map[string]interface{})
}
if v, ok := d.GetOk("annotations"); ok {
annotations := v.([]interface{})
cosmosdbLinkedService.Annotations = &annotations
}
linkedService := datafactory.LinkedServiceResource{
Properties: cosmosdbLinkedService,
}
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil {
return fmt.Errorf("creating/updating Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
d.SetId(id.ID())
return resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d, meta)
}
func resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Data Factory Linked Service CosmosDB %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)
cosmosdb, ok := resp.Properties.AsCosmosDbMongoDbAPILinkedService()
if !ok {
return fmt.Errorf("classifying Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI, *resp.Type)
}
d.Set("additional_properties", cosmosdb.AdditionalProperties)
d.Set("description", cosmosdb.Description)
annotations := flattenDataFactoryAnnotations(cosmosdb.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("setting `annotations`: %+v", err)
}
parameters := flattenDataFactoryParameters(cosmosdb.Parameters)
if err := d.Set("parameters", parameters); err != nil {
return fmt.Errorf("setting `parameters`: %+v", err)
}
if connectVia := cosmosdb.ConnectVia; connectVia != nil {
if connectVia.ReferenceName != nil {
d.Set("integration_runtime_name", connectVia.ReferenceName)
}
}
databaseName := cosmosdb.CosmosDbMongoDbAPILinkedServiceTypeProperties.Database
d.Set("database", databaseName)
versionAbove32 := cosmosdb.CosmosDbMongoDbAPILinkedServiceTypeProperties.IsServerVersionAbove32
d.Set("server_version_is_32_or_higher", versionAbove32)
return nil
}
func resourceDataFactoryLinkedServiceCosmosDbMongoAPIDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("deleting Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}
return nil
}