From a93265a4977a9fb434283899d03fb26e8b3f8873 Mon Sep 17 00:00:00 2001 From: Eliise Date: Thu, 24 Jun 2021 13:46:14 +0200 Subject: [PATCH] Support for azurerm_kusto_iothub_data_connection missing table_name, mapping_rule_name, data_format (#12293) --- .../kusto_iothub_data_connection_resource.go | 81 +++++++++++-- ...to_iothub_data_connection_resource_test.go | 109 ++++++++++++------ ...sto_eventhub_data_connection.html.markdown | 2 +- ...kusto_iothub_data_connection.html.markdown | 10 ++ 4 files changed, 157 insertions(+), 45 deletions(-) diff --git a/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource.go b/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource.go index 0ae00520e86b..1270da582bdd 100644 --- a/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource.go +++ b/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource.go @@ -83,6 +83,44 @@ func resourceKustoIotHubDataConnection() *pluginsdk.Resource { ValidateFunc: iothubValidate.IotHubSharedAccessPolicyName, }, + "table_name": { + Type: pluginsdk.TypeString, + ForceNew: true, + Optional: true, + ValidateFunc: validate.EntityName, + }, + + "mapping_rule_name": { + Type: pluginsdk.TypeString, + ForceNew: true, + Optional: true, + ValidateFunc: validate.EntityName, + }, + + "data_format": { + Type: pluginsdk.TypeString, + ForceNew: true, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(kusto.IotHubDataFormatAPACHEAVRO), + string(kusto.IotHubDataFormatAVRO), + string(kusto.IotHubDataFormatCSV), + string(kusto.IotHubDataFormatJSON), + string(kusto.IotHubDataFormatMULTIJSON), + string(kusto.IotHubDataFormatORC), + string(kusto.IotHubDataFormatPARQUET), + string(kusto.IotHubDataFormatPSV), + string(kusto.IotHubDataFormatRAW), + string(kusto.IotHubDataFormatSCSV), + string(kusto.IotHubDataFormatSINGLEJSON), + string(kusto.IotHubDataFormatSOHSV), + string(kusto.IotHubDataFormatTSV), + string(kusto.IotHubDataFormatTSVE), + string(kusto.IotHubDataFormatTXT), + string(kusto.IotHubDataFormatW3CLOGFILE), + }, false), + }, + "event_system_properties": { Type: pluginsdk.TypeSet, Optional: true, @@ -128,17 +166,11 @@ func resourceKustoIotHubDataConnectionCreate(d *pluginsdk.ResourceData, meta int return tf.ImportAsExistsError("azurerm_kusto_iothub_data_connection", id.ID()) } - dataConnection := kusto.IotHubDataConnection{ - Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))), - IotHubConnectionProperties: &kusto.IotHubConnectionProperties{ - IotHubResourceID: utils.String(d.Get("iothub_id").(string)), - ConsumerGroup: utils.String(d.Get("consumer_group").(string)), - SharedAccessPolicyName: utils.String(d.Get("shared_access_policy_name").(string)), - }, - } + iotHubDataConnectionProperties := expandKustoIotHubDataConnectionProperties(d) - if eventSystemProperties, ok := d.GetOk("event_system_properties"); ok { - dataConnection.IotHubConnectionProperties.EventSystemProperties = utils.ExpandStringSlice(eventSystemProperties.(*pluginsdk.Set).List()) + dataConnection := kusto.IotHubDataConnection{ + Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))), + IotHubConnectionProperties: iotHubDataConnectionProperties, } future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ClusterName, id.DatabaseName, id.Name, dataConnection) @@ -183,6 +215,9 @@ func resourceKustoIotHubDataConnectionRead(d *pluginsdk.ResourceData, meta inter if props := dataConnection.IotHubConnectionProperties; props != nil { d.Set("iothub_id", props.IotHubResourceID) d.Set("consumer_group", props.ConsumerGroup) + d.Set("table_name", props.TableName) + d.Set("mapping_rule_name", props.MappingRuleName) + d.Set("data_format", props.DataFormat) d.Set("shared_access_policy_name", props.SharedAccessPolicyName) d.Set("event_system_properties", utils.FlattenStringSlice(props.EventSystemProperties)) } @@ -212,3 +247,29 @@ func resourceKustoIotHubDataConnectionDelete(d *pluginsdk.ResourceData, meta int return nil } + +func expandKustoIotHubDataConnectionProperties(d *pluginsdk.ResourceData) *kusto.IotHubConnectionProperties { + iotHubDataConnectionProperties := &kusto.IotHubConnectionProperties{ + IotHubResourceID: utils.String(d.Get("iothub_id").(string)), + ConsumerGroup: utils.String(d.Get("consumer_group").(string)), + SharedAccessPolicyName: utils.String(d.Get("shared_access_policy_name").(string)), + } + + if tableName, ok := d.GetOk("table_name"); ok { + iotHubDataConnectionProperties.TableName = utils.String(tableName.(string)) + } + + if mappingRuleName, ok := d.GetOk("mapping_rule_name"); ok { + iotHubDataConnectionProperties.MappingRuleName = utils.String(mappingRuleName.(string)) + } + + if df, ok := d.GetOk("data_format"); ok { + iotHubDataConnectionProperties.DataFormat = kusto.IotHubDataFormat(df.(string)) + } + + if eventSystemProperties, ok := d.GetOk("event_system_properties"); ok { + iotHubDataConnectionProperties.EventSystemProperties = utils.ExpandStringSlice(eventSystemProperties.(*pluginsdk.Set).List()) + } + + return iotHubDataConnectionProperties +} diff --git a/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource_test.go b/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource_test.go index e827d799475c..cc59af8c01fd 100644 --- a/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource_test.go +++ b/azurerm/internal/services/kusto/kusto_iothub_data_connection_resource_test.go @@ -31,7 +31,80 @@ func TestAccKustoIotHubDataConnection_basic(t *testing.T) { }) } -func (KustoIotHubDataConnectionResource) basic(data acceptance.TestData) string { +func TestAccKustoIotHubDataConnection_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_kusto_iothub_data_connection", "test") + r := KustoIotHubDataConnectionResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func (KustoIotHubDataConnectionResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := parse.DataConnectionID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.Kusto.DataConnectionsClient.Get(ctx, id.ResourceGroup, id.ClusterName, id.DatabaseName, id.Name) + if err != nil { + return nil, fmt.Errorf("retrieving %s: %v", id.String(), err) + } + + value, ok := resp.Value.AsIotHubDataConnection() + if !ok { + return nil, fmt.Errorf("%s is not an IotHub Data Connection", id.String()) + } + + return utils.Bool(value.IotHubConnectionProperties != nil), nil +} + +func (r KustoIotHubDataConnectionResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_kusto_iothub_data_connection" "test" { + name = "acctestkedc-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + cluster_name = azurerm_kusto_cluster.test.name + database_name = azurerm_kusto_database.test.name + + iothub_id = azurerm_iothub.test.id + consumer_group = azurerm_iothub_consumer_group.test.name + shared_access_policy_name = azurerm_iothub_shared_access_policy.test.name +} +`, r.template(data), data.RandomInteger) +} + +func (r KustoIotHubDataConnectionResource) complete(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_kusto_iothub_data_connection" "test" { + name = "acctestkedc-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + cluster_name = azurerm_kusto_cluster.test.name + database_name = azurerm_kusto_database.test.name + + iothub_id = azurerm_iothub.test.id + consumer_group = azurerm_iothub_consumer_group.test.name + shared_access_policy_name = azurerm_iothub_shared_access_policy.test.name + event_system_properties = ["message-id", "sequence-number", "to"] + mapping_rule_name = "Json_Mapping" + data_format = "MULTIJSON" +} +`, r.template(data), data.RandomInteger) +} + +func (KustoIotHubDataConnectionResource) template(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} @@ -89,37 +162,5 @@ resource "azurerm_iothub_consumer_group" "test" { eventhub_endpoint_name = "events" resource_group_name = azurerm_resource_group.test.name } - -resource "azurerm_kusto_iothub_data_connection" "test" { - name = "acctestkedc-%d" - resource_group_name = azurerm_resource_group.test.name - location = azurerm_resource_group.test.location - cluster_name = azurerm_kusto_cluster.test.name - database_name = azurerm_kusto_database.test.name - - iothub_id = azurerm_iothub.test.id - consumer_group = azurerm_iothub_consumer_group.test.name - shared_access_policy_name = azurerm_iothub_shared_access_policy.test.name - event_system_properties = ["message-id", "sequence-number", "to"] -} -`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger) -} - -func (KustoIotHubDataConnectionResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := parse.DataConnectionID(state.ID) - if err != nil { - return nil, err - } - - resp, err := clients.Kusto.DataConnectionsClient.Get(ctx, id.ResourceGroup, id.ClusterName, id.DatabaseName, id.Name) - if err != nil { - return nil, fmt.Errorf("retrieving %s: %v", id.String(), err) - } - - value, ok := resp.Value.AsIotHubDataConnection() - if !ok { - return nil, fmt.Errorf("%s is not an IotHub Data Connection", id.String()) - } - - return utils.Bool(value.IotHubConnectionProperties != nil), nil +`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger) } diff --git a/website/docs/r/kusto_eventhub_data_connection.html.markdown b/website/docs/r/kusto_eventhub_data_connection.html.markdown index ebe9f2f9e5c7..c946420419df 100644 --- a/website/docs/r/kusto_eventhub_data_connection.html.markdown +++ b/website/docs/r/kusto_eventhub_data_connection.html.markdown @@ -92,7 +92,6 @@ The following arguments are supported: * `database_name` - (Required) Specifies the name of the Kusto Database this data connection will be added to. Changing this forces a new resource to be created. -* `data_format` - (Optional) Specifies the data format of the EventHub messages. Allowed values: `AVRO`, `CSV`, `JSON`, `MULTIJSON`, `PSV`, `RAW`, `SCSV`, `SINGLEJSON`, `SOHSV`, `TSV` and `TXT` * `eventhub_id` - (Required) Specifies the resource id of the EventHub this data connection will use for ingestion. Changing this forces a new resource to be created. @@ -104,6 +103,7 @@ The following arguments are supported: * `mapping_rule_name` - (Optional) Specifies the mapping rule used for the message ingestion. Mapping rule must exist before resource is created. +* `data_format` - (Optional) Specifies the data format of the EventHub messages. Allowed values: `AVRO`, `CSV`, `JSON`, `MULTIJSON`, `PSV`, `RAW`, `SCSV`, `SINGLEJSON`, `SOHSV`, `TSV` and `TXT` ## Attributes Reference diff --git a/website/docs/r/kusto_iothub_data_connection.html.markdown b/website/docs/r/kusto_iothub_data_connection.html.markdown index 7cd78a73d447..1d1297f5815f 100644 --- a/website/docs/r/kusto_iothub_data_connection.html.markdown +++ b/website/docs/r/kusto_iothub_data_connection.html.markdown @@ -75,6 +75,10 @@ resource "azurerm_kusto_iothub_data_connection" "example" { consumer_group = azurerm_iothub_consumer_group.example.name shared_access_policy_name = azurerm_iothub_shared_access_policy.example.name event_system_properties = ["message-id", "sequence-number", "to"] + + table_name = "my-table" + mapping_rule_name = "my-table-mapping" + data_format = "JSON" } ``` @@ -100,6 +104,12 @@ The following arguments are supported: * `event_system_properties` - (Optional) Specifies the System Properties that each IoT Hub message should contain. Changing this forces a new resource to be created. +* `table_name` - (Optional) Specifies the target table name used for the message ingestion. Table must exist before resource is created. + +* `mapping_rule_name` - (Optional) Specifies the mapping rule used for the message ingestion. Mapping rule must exist before resource is created. + +* `data_format` - (Optional) Specifies the data format of the IoTHub messages. Allowed values: `APACHEAVRO`, `AVRO`, `CSV`, `JSON`, `MULTIJSON`, `ORC`, `PARQUET`, `PSV`, `RAW`, `SCSV`, `SINGLEJSON`, `SOHSV`, `TSV`, `TSVE`, `TXT` and `W3CLOGFILE`. + ## Attributes Reference The following attributes are exported: