-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new resource "azurerm_data_factory_linked_service_azure_search" (#12122)
- Loading branch information
Showing
4 changed files
with
527 additions
and
2 deletions.
There are no files selected for viewing
250 changes: 250 additions & 0 deletions
250
azurerm/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
package datafactory | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" | ||
"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/tf/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceDataFactoryLinkedServiceAzureSearch() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Create: resourceDataFactoryLinkedServiceAzureSearchCreateUpdate, | ||
Read: resourceDataFactoryLinkedServiceAzureSearchRead, | ||
Update: resourceDataFactoryLinkedServiceAzureSearchCreateUpdate, | ||
Delete: resourceDataFactoryLinkedServiceAzureSearchDelete, | ||
|
||
// TODO: replace this with an importer which validates the ID during import | ||
Importer: pluginsdk.DefaultImporter(), | ||
|
||
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_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.DataFactoryID, | ||
}, | ||
|
||
"url": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"search_service_key": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"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, | ||
}, | ||
}, | ||
|
||
"encrypted_credential": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDataFactoryLinkedServiceAzureSearchCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).DataFactory.LinkedServiceClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
dataFactoryId, err := parse.DataFactoryID(d.Get("data_factory_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := parse.NewLinkedServiceID(subscriptionId, dataFactoryId.ResourceGroup, dataFactoryId.FactoryName, 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 %s: %+v", id, err) | ||
} | ||
} | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return tf.ImportAsExistsError("azurerm_data_factory_linked_service_azure_search", id.ID()) | ||
} | ||
} | ||
|
||
searchLinkedService := &datafactory.AzureSearchLinkedService{ | ||
AzureSearchLinkedServiceTypeProperties: &datafactory.AzureSearchLinkedServiceTypeProperties{ | ||
URL: d.Get("url").(string), | ||
Key: &datafactory.SecureString{ | ||
Type: datafactory.TypeTypeSecureString, | ||
Value: utils.String(d.Get("search_service_key").(string)), | ||
}, | ||
}, | ||
Description: utils.String(d.Get("description").(string)), | ||
Type: datafactory.TypeBasicLinkedServiceTypeAzureSearch, | ||
} | ||
|
||
if v, ok := d.GetOk("parameters"); ok { | ||
searchLinkedService.Parameters = expandDataFactoryParameters(v.(map[string]interface{})) | ||
} | ||
|
||
if v, ok := d.GetOk("integration_runtime_name"); ok { | ||
searchLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("additional_properties"); ok { | ||
searchLinkedService.AdditionalProperties = v.(map[string]interface{}) | ||
} | ||
|
||
if v, ok := d.GetOk("annotations"); ok { | ||
annotations := v.([]interface{}) | ||
searchLinkedService.Annotations = &annotations | ||
} | ||
|
||
linkedService := datafactory.LinkedServiceResource{ | ||
Properties: searchLinkedService, | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { | ||
return fmt.Errorf("creating/updating %s: %+v", id, err) | ||
} | ||
|
||
d.SetId(id.ID()) | ||
|
||
return resourceDataFactoryLinkedServiceAzureSearchRead(d, meta) | ||
} | ||
|
||
func resourceDataFactoryLinkedServiceAzureSearchRead(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).DataFactory.LinkedServiceClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
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 %s: %+v", id, err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
d.Set("data_factory_id", parse.NewDataFactoryID(subscriptionId, id.ResourceGroup, id.FactoryName).ID()) | ||
|
||
linkedService, ok := resp.Properties.AsAzureSearchLinkedService() | ||
if !ok { | ||
return fmt.Errorf("classifiying %s: Expected: %q", id, datafactory.TypeBasicLinkedServiceTypeAzureSearch) | ||
} | ||
|
||
if prop := linkedService.AzureSearchLinkedServiceTypeProperties; prop != nil { | ||
url := "" | ||
if v, ok := prop.URL.(string); ok { | ||
url = v | ||
} | ||
d.Set("url", url) | ||
|
||
encryptedCredential := "" | ||
if v, ok := prop.EncryptedCredential.(string); ok { | ||
encryptedCredential = v | ||
} | ||
d.Set("encrypted_credential", encryptedCredential) | ||
} | ||
|
||
d.Set("additional_properties", linkedService.AdditionalProperties) | ||
d.Set("description", linkedService.Description) | ||
|
||
if err := d.Set("annotations", flattenDataFactoryAnnotations(linkedService.Annotations)); err != nil { | ||
return fmt.Errorf("setting `annotations`: %+v", err) | ||
} | ||
|
||
if err := d.Set("parameters", flattenDataFactoryParameters(linkedService.Parameters)); err != nil { | ||
return fmt.Errorf("setting `parameters`: %+v", err) | ||
} | ||
|
||
integrationRuntimeName := "" | ||
if linkedService.ConnectVia != nil && linkedService.ConnectVia.ReferenceName != nil { | ||
integrationRuntimeName = *linkedService.ConnectVia.ReferenceName | ||
} | ||
d.Set("integration_runtime_name", integrationRuntimeName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceDataFactoryLinkedServiceAzureSearchDelete(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 | ||
} | ||
|
||
if _, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", id, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.