Skip to content

Commit

Permalink
Support for azurerm_kusto_iothub_data_connection missing table_name, …
Browse files Browse the repository at this point in the history
…mapping_rule_name, data_format (#12293)
  • Loading branch information
EliiseS authored Jun 24, 2021
1 parent f0c2f15 commit a93265a
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/kusto_iothub_data_connection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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:
Expand Down

0 comments on commit a93265a

Please sign in to comment.