|
| 1 | +package datafactory |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 11 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" |
| 12 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" |
| 13 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" |
| 14 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" |
| 15 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse" |
| 16 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" |
| 17 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" |
| 18 | +) |
| 19 | + |
| 20 | +func resourceArmDataFactoryLinkedServiceSynapse() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Create: resourceArmDataFactoryLinkedServiceSynapseCreateUpdate, |
| 23 | + Read: resourceArmDataFactoryLinkedServiceSynapseRead, |
| 24 | + Update: resourceArmDataFactoryLinkedServiceSynapseCreateUpdate, |
| 25 | + Delete: resourceArmDataFactoryLinkedServiceSynapseDelete, |
| 26 | + |
| 27 | + Importer: &schema.ResourceImporter{ |
| 28 | + State: schema.ImportStatePassthrough, |
| 29 | + }, |
| 30 | + |
| 31 | + Timeouts: &schema.ResourceTimeout{ |
| 32 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 33 | + Read: schema.DefaultTimeout(5 * time.Minute), |
| 34 | + Update: schema.DefaultTimeout(30 * time.Minute), |
| 35 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 36 | + }, |
| 37 | + |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + ValidateFunc: validateAzureRMDataFactoryLinkedServiceDatasetName, |
| 44 | + }, |
| 45 | + |
| 46 | + "data_factory_name": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + ValidateFunc: validate.DataFactoryName(), |
| 51 | + }, |
| 52 | + |
| 53 | + // There's a bug in the Azure API where this is returned in lower-case |
| 54 | + // BUG: https://github.com/Azure/azure-rest-api-specs/issues/5788 |
| 55 | + "resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(), |
| 56 | + |
| 57 | + "connection_string": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + DiffSuppressFunc: azureRmDataFactoryLinkedServiceConnectionStringDiff, |
| 61 | + ValidateFunc: validation.StringIsNotEmpty, |
| 62 | + }, |
| 63 | + |
| 64 | + "key_vault_password": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Optional: true, |
| 67 | + MaxItems: 1, |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "linked_service_name": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Required: true, |
| 73 | + ValidateFunc: validation.StringIsNotEmpty, |
| 74 | + }, |
| 75 | + |
| 76 | + "secret_name": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Required: true, |
| 79 | + ValidateFunc: validation.StringIsNotEmpty, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + |
| 85 | + "description": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Optional: true, |
| 88 | + ValidateFunc: validation.StringIsNotEmpty, |
| 89 | + }, |
| 90 | + |
| 91 | + "integration_runtime_name": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Optional: true, |
| 94 | + ValidateFunc: validation.StringIsNotEmpty, |
| 95 | + }, |
| 96 | + |
| 97 | + "parameters": { |
| 98 | + Type: schema.TypeMap, |
| 99 | + Optional: true, |
| 100 | + Elem: &schema.Schema{ |
| 101 | + Type: schema.TypeString, |
| 102 | + ValidateFunc: validation.StringIsNotEmpty, |
| 103 | + }, |
| 104 | + }, |
| 105 | + |
| 106 | + "annotations": { |
| 107 | + Type: schema.TypeList, |
| 108 | + Optional: true, |
| 109 | + Elem: &schema.Schema{ |
| 110 | + Type: schema.TypeString, |
| 111 | + ValidateFunc: validation.StringIsNotEmpty, |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + "additional_properties": { |
| 116 | + Type: schema.TypeMap, |
| 117 | + Optional: true, |
| 118 | + Elem: &schema.Schema{ |
| 119 | + Type: schema.TypeString, |
| 120 | + ValidateFunc: validation.StringIsNotEmpty, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func resourceArmDataFactoryLinkedServiceSynapseCreateUpdate(d *schema.ResourceData, meta interface{}) error { |
| 128 | + client := meta.(*clients.Client).DataFactory.LinkedServiceClient |
| 129 | + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) |
| 130 | + defer cancel() |
| 131 | + |
| 132 | + name := d.Get("name").(string) |
| 133 | + dataFactoryName := d.Get("data_factory_name").(string) |
| 134 | + resourceGroup := d.Get("resource_group_name").(string) |
| 135 | + |
| 136 | + if d.IsNewResource() { |
| 137 | + existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") |
| 138 | + if err != nil { |
| 139 | + if !utils.ResponseWasNotFound(existing.Response) { |
| 140 | + return fmt.Errorf("Error checking for presence of existing Data Factory Linked Service Synapse%q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if existing.ID != nil && *existing.ID != "" { |
| 145 | + return tf.ImportAsExistsError("azurerm_data_factory_linked_service_synapse", *existing.ID) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + password := d.Get("key_vault_password").([]interface{}) |
| 150 | + |
| 151 | + sqlDWLinkedService := &datafactory.AzureSQLDWLinkedService{ |
| 152 | + Description: utils.String(d.Get("description").(string)), |
| 153 | + AzureSQLDWLinkedServiceTypeProperties: &datafactory.AzureSQLDWLinkedServiceTypeProperties{ |
| 154 | + ConnectionString: d.Get("connection_string").(string), |
| 155 | + Password: expandAzureKeyVaultPassword(password), |
| 156 | + }, |
| 157 | + Type: datafactory.TypeAzureSQLDW, |
| 158 | + } |
| 159 | + |
| 160 | + if v, ok := d.GetOk("parameters"); ok { |
| 161 | + sqlDWLinkedService.Parameters = expandDataFactoryParameters(v.(map[string]interface{})) |
| 162 | + } |
| 163 | + |
| 164 | + if v, ok := d.GetOk("integration_runtime_name"); ok { |
| 165 | + sqlDWLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string)) |
| 166 | + } |
| 167 | + |
| 168 | + if v, ok := d.GetOk("additional_properties"); ok { |
| 169 | + sqlDWLinkedService.AdditionalProperties = v.(map[string]interface{}) |
| 170 | + } |
| 171 | + |
| 172 | + if v, ok := d.GetOk("annotations"); ok { |
| 173 | + annotations := v.([]interface{}) |
| 174 | + sqlDWLinkedService.Annotations = &annotations |
| 175 | + } |
| 176 | + |
| 177 | + linkedService := datafactory.LinkedServiceResource{ |
| 178 | + Properties: sqlDWLinkedService, |
| 179 | + } |
| 180 | + |
| 181 | + if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { |
| 182 | + return fmt.Errorf("Error creating/updating Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) |
| 183 | + } |
| 184 | + |
| 185 | + resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") |
| 186 | + if err != nil { |
| 187 | + return fmt.Errorf("Error retrieving Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) |
| 188 | + } |
| 189 | + |
| 190 | + if resp.ID == nil { |
| 191 | + return fmt.Errorf("Cannot read Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) |
| 192 | + } |
| 193 | + |
| 194 | + d.SetId(*resp.ID) |
| 195 | + |
| 196 | + return resourceArmDataFactoryLinkedServiceSynapseRead(d, meta) |
| 197 | +} |
| 198 | + |
| 199 | +func resourceArmDataFactoryLinkedServiceSynapseRead(d *schema.ResourceData, meta interface{}) error { |
| 200 | + client := meta.(*clients.Client).DataFactory.LinkedServiceClient |
| 201 | + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) |
| 202 | + defer cancel() |
| 203 | + |
| 204 | + id, err := parse.LinkedServiceID(d.Id()) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") |
| 210 | + if err != nil { |
| 211 | + if utils.ResponseWasNotFound(resp.Response) { |
| 212 | + d.SetId("") |
| 213 | + return nil |
| 214 | + } |
| 215 | + |
| 216 | + return fmt.Errorf("Error retrieving Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) |
| 217 | + } |
| 218 | + |
| 219 | + d.Set("name", id.Name) |
| 220 | + d.Set("resource_group_name", id.ResourceGroup) |
| 221 | + d.Set("data_factory_name", id.FactoryName) |
| 222 | + |
| 223 | + sqlDW, ok := resp.Properties.AsAzureSQLDWLinkedService() |
| 224 | + if !ok { |
| 225 | + return fmt.Errorf("Error classifying Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeAzureSQLDW, *resp.Type) |
| 226 | + } |
| 227 | + |
| 228 | + d.Set("additional_properties", sqlDW.AdditionalProperties) |
| 229 | + d.Set("description", sqlDW.Description) |
| 230 | + |
| 231 | + annotations := flattenDataFactoryAnnotations(sqlDW.Annotations) |
| 232 | + if err := d.Set("annotations", annotations); err != nil { |
| 233 | + return fmt.Errorf("Error setting `annotations`: %+v", err) |
| 234 | + } |
| 235 | + |
| 236 | + parameters := flattenDataFactoryParameters(sqlDW.Parameters) |
| 237 | + if err := d.Set("parameters", parameters); err != nil { |
| 238 | + return fmt.Errorf("Error setting `parameters`: %+v", err) |
| 239 | + } |
| 240 | + |
| 241 | + if connectVia := sqlDW.ConnectVia; connectVia != nil { |
| 242 | + if connectVia.ReferenceName != nil { |
| 243 | + d.Set("integration_runtime_name", connectVia.ReferenceName) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + if properties := sqlDW.AzureSQLDWLinkedServiceTypeProperties; properties != nil { |
| 248 | + if properties.ConnectionString != nil { |
| 249 | + if val, ok := properties.ConnectionString.(string); ok { |
| 250 | + d.Set("connection_string", val) |
| 251 | + } else { |
| 252 | + d.Set("connection_string", "") |
| 253 | + log.Printf("[DEBUG] Skipping connection string %q since it's not a string", val) |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + if err := d.Set("key_vault_password", flattenAzureKeyVaultPassword(properties.Password)); err != nil { |
| 258 | + return fmt.Errorf("setting `key_vault_password`: %+v", err) |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + return nil |
| 263 | +} |
| 264 | + |
| 265 | +func resourceArmDataFactoryLinkedServiceSynapseDelete(d *schema.ResourceData, meta interface{}) error { |
| 266 | + client := meta.(*clients.Client).DataFactory.LinkedServiceClient |
| 267 | + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) |
| 268 | + defer cancel() |
| 269 | + |
| 270 | + id, err := parse.LinkedServiceID(d.Id()) |
| 271 | + if err != nil { |
| 272 | + return err |
| 273 | + } |
| 274 | + |
| 275 | + response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) |
| 276 | + if err != nil { |
| 277 | + if !utils.ResponseWasNotFound(response) { |
| 278 | + return fmt.Errorf("Error deleting Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + return nil |
| 283 | +} |
0 commit comments