Skip to content

Commit

Permalink
New Resource: azurerm_data_factory_linked_service_odata (#11556)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomverte authored Jun 16, 2021
1 parent d31ac48 commit cb587a5
Show file tree
Hide file tree
Showing 4 changed files with 611 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
package datafactory

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmDataFactoryLinkedServiceOData() *schema.Resource {
return &schema.Resource{
Create: resourceArmDataFactoryLinkedServiceODataCreateUpdate,
Read: resourceArmDataFactoryLinkedServiceODataRead,
Update: resourceArmDataFactoryLinkedServiceODataCreateUpdate,
Delete: resourceArmDataFactoryLinkedServiceODataDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LinkedServiceDatasetName,
},

"data_factory_name": {
Type: schema.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(),

"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"basic_authentication": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
// This isn't returned from the API so we'll ignore changes when it's empty
DiffSuppressFunc: func(k, old, new string, d *pluginsdk.ResourceData) bool {
return (new == d.Get(k).(string)) && (old == "*****")
},
},
},
},
},

"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"integration_runtime_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"annotations": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"parameters": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"additional_properties": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceArmDataFactoryLinkedServiceODataCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
dataFactoryName := d.Get("data_factory_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if 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 OData Anonymous %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_odata", *existing.ID)
}
}

odataLinkedService := &datafactory.ODataLinkedService{
Description: utils.String(d.Get("description").(string)),
Type: datafactory.TypeBasicLinkedServiceTypeOData,
ODataLinkedServiceTypeProperties: &datafactory.ODataLinkedServiceTypeProperties{
AuthenticationType: datafactory.ODataAuthenticationTypeAnonymous,
URL: d.Get("url").(string),
},
}

// There are multiple authentication paths. If support for those get added, we can easily add them in
// a similar format to the below while not messing up the other attributes in ODataLinkedServiceTypeProperties
if v, ok := d.GetOk("basic_authentication"); ok {
attrs := v.([]interface{})
if len(attrs) != 0 && attrs[0] != nil {
raw := attrs[0].(map[string]interface{})
odataLinkedService.AuthenticationType = datafactory.ODataAuthenticationTypeBasic
odataLinkedService.UserName = raw["username"].(string)
odataLinkedService.Password = datafactory.SecureString{
Value: utils.String(raw["password"].(string)),
Type: datafactory.TypeSecureString,
}
}
}

if v, ok := d.GetOk("parameters"); ok {
odataLinkedService.Parameters = expandDataFactoryParameters(v.(map[string]interface{}))
}

if v, ok := d.GetOk("integration_runtime_name"); ok {
odataLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string))
}

if v, ok := d.GetOk("additional_properties"); ok {
odataLinkedService.AdditionalProperties = v.(map[string]interface{})
}

if v, ok := d.GetOk("annotations"); ok {
annotations := v.([]interface{})
odataLinkedService.Annotations = &annotations
}

linkedService := datafactory.LinkedServiceResource{
Properties: odataLinkedService,
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil {
return fmt.Errorf("Error creating/updating Data Factory Linked Service OData Anonymous %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 OData Anonymous %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 OData Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
}

d.SetId(*resp.ID)

return resourceArmDataFactoryLinkedServiceODataRead(d, meta)
}

func resourceArmDataFactoryLinkedServiceODataRead(d *schema.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("Error retrieving Data Factory Linked Service OData %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)

odata, ok := resp.Properties.AsODataLinkedService()
if !ok {
return fmt.Errorf("Error classifiying Data Factory Linked Service OData %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeOData, *resp.Type)
}

props := odata.ODataLinkedServiceTypeProperties
d.Set("url", props.URL)
if props.AuthenticationType == datafactory.ODataAuthenticationTypeBasic {
if err := d.Set("basic_authentication", []interface{}{map[string]interface{}{
"username": props.UserName,
// `password` isn't returned from the api so we'll set it to `*****` here to be able to check for diffs during plan
"password": "*****",
}}); err != nil {
return fmt.Errorf("setting `basic_authentication`: %+v", err)
}
}

d.Set("additional_properties", odata.AdditionalProperties)
d.Set("description", odata.Description)

annotations := flattenDataFactoryAnnotations(odata.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("Error setting `annotations`: %+v", err)
}

parameters := flattenDataFactoryParameters(odata.Parameters)
if err := d.Set("parameters", parameters); err != nil {
return fmt.Errorf("Error setting `parameters`: %+v", err)
}

if connectVia := odata.ConnectVia; connectVia != nil {
if connectVia.ReferenceName != nil {
d.Set("integration_runtime_name", connectVia.ReferenceName)
}
}

return nil
}

func resourceArmDataFactoryLinkedServiceODataDelete(d *schema.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("Error deleting Data Factory Linked Service OData %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit cb587a5

Please sign in to comment.