-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
resource_arm_data_factory_linked_service_postgresql.go
240 lines (193 loc) · 7.73 KB
/
resource_arm_data_factory_linked_service_postgresql.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
package azurerm
import (
"fmt"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmDataFactoryLinkedServicePostgreSQL() *schema.Resource {
return &schema.Resource{
Create: resourceArmDataFactoryLinkedServicePostgreSQLCreateOrUpdate,
Read: resourceArmDataFactoryLinkedServicePostgreSQLRead,
Update: resourceArmDataFactoryLinkedServicePostgreSQLCreateOrUpdate,
Delete: resourceArmDataFactoryLinkedServicePostgreSQLDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMDataFactoryLinkedServiceDatasetName,
},
"data_factory_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$`),
`Invalid name for Data Factory, see https://docs.microsoft.com/en-us/azure/data-factory/naming-rules`,
),
},
"resource_group_name": resourceGroupNameSchema(),
"connection_string": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: azureRmDataFactoryLinkedServiceConnectionStringDiff,
ValidateFunc: validate.NoEmptyStrings,
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
},
"integration_runtime_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
},
"parameters": {
Type: schema.TypeMap,
Optional: true,
},
"annotations": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"additional_properties": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceArmDataFactoryLinkedServicePostgreSQLCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataFactoryLinkedServiceClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
dataFactoryName := d.Get("data_factory_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_data_factory_linked_service_postgresql", *existing.ID)
}
}
connectionString := d.Get("connection_string").(string)
secureString := datafactory.SecureString{
Value: &connectionString,
Type: datafactory.TypeSecureString,
}
postgresqlProperties := &datafactory.PostgreSQLLinkedServiceTypeProperties{
ConnectionString: &secureString,
}
description := d.Get("description").(string)
postgresqlLinkedService := &datafactory.PostgreSQLLinkedService{
Description: &description,
PostgreSQLLinkedServiceTypeProperties: postgresqlProperties,
Type: datafactory.TypePostgreSQL,
}
if v, ok := d.GetOk("parameters"); ok {
postgresqlLinkedService.Parameters = expandDataFactoryParameters(v.(map[string]interface{}))
}
if v, ok := d.GetOk("integration_runtime_name"); ok {
postgresqlLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string))
}
if v, ok := d.GetOk("additional_properties"); ok {
postgresqlLinkedService.AdditionalProperties = v.(map[string]interface{})
}
if v, ok := d.GetOk("annotations"); ok {
annotations := v.([]interface{})
postgresqlLinkedService.Annotations = &annotations
}
linkedService := datafactory.LinkedServiceResource{
Properties: postgresqlLinkedService,
}
if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil {
return fmt.Errorf("Error creating/updating Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
if err != nil {
return fmt.Errorf("Error retrieving Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
d.SetId(*resp.ID)
return resourceArmDataFactoryLinkedServicePostgreSQLRead(d, meta)
}
func resourceArmDataFactoryLinkedServicePostgreSQLRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataFactoryLinkedServiceClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]
resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("data_factory_name", dataFactoryName)
postgresql, ok := resp.Properties.AsPostgreSQLLinkedService()
if !ok {
return fmt.Errorf("Error classifiying Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", name, dataFactoryName, resourceGroup, datafactory.TypeMySQL, *resp.Type)
}
d.Set("additional_properties", postgresql.AdditionalProperties)
if postgresql.Description != nil {
d.Set("description", *postgresql.Description)
}
annotations := flattenDataFactoryAnnotations(postgresql.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("Error setting `annotations`: %+v", err)
}
parameters := flattenDataFactoryParameters(postgresql.Parameters)
if err := d.Set("parameters", parameters); err != nil {
return fmt.Errorf("Error setting `parameters`: %+v", err)
}
if connectVia := postgresql.ConnectVia; connectVia != nil {
if connectVia.ReferenceName != nil {
d.Set("integration_runtime_name", connectVia.ReferenceName)
}
}
return nil
}
func resourceArmDataFactoryLinkedServicePostgreSQLDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataFactoryLinkedServiceClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]
response, err := client.Delete(ctx, resourceGroup, dataFactoryName, name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("Error deleting Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}
}
return nil
}