Skip to content

Commit

Permalink
r/azurerm_kusto_eventhub_data_connection: allow for empty table_name …
Browse files Browse the repository at this point in the history
…and data_format (#10913)

Fixes #10838
  • Loading branch information
favoretti authored Mar 10, 2021
1 parent 9e804ca commit 0429810
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func resourceKustoEventHubDataConnection() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
ValidateFunc: eventhubValidate.EventHubID,
},

"consumer_group": {
Expand Down Expand Up @@ -120,17 +120,6 @@ func resourceKustoEventHubDataConnection() *schema.Resource {
}, false),
},
},
CustomizeDiff: func(d *schema.ResourceDiff, _ interface{}) error {
_, hasTableName := d.GetOk("table_name")
_, hasMappingRuleName := d.GetOk("mapping_rule_name")
_, hasDataFormat := d.GetOk("data_format")

if !(utils.AllEquals(hasTableName, hasMappingRuleName, hasDataFormat)) {
return fmt.Errorf("if one of the target table properties `table_name`, `mapping_rule_name` or `data_format` are set, the other values must also be defined")
}

return nil
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,112 @@ func TestAccKustoEventHubDataConnection_basic(t *testing.T) {
})
}

func (KustoEventHubDataConnectionResource) basic(data acceptance.TestData) string {
func TestAccKustoEventHubDataConnection_unboundMapping1(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_eventhub_data_connection", "test")
r := KustoEventHubDataConnectionResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.unboundMapping1(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccKustoEventHubDataConnection_unboundMapping2(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_eventhub_data_connection", "test")
r := KustoEventHubDataConnectionResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.unboundMapping2(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (KustoEventHubDataConnectionResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.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.AsEventHubDataConnection()
if !ok {
return nil, fmt.Errorf("%s is not an EventHubDataConnection", id.String())
}

return utils.Bool(value.EventHubConnectionProperties != nil), nil
}

func (r KustoEventHubDataConnectionResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_kusto_eventhub_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
eventhub_id = azurerm_eventhub.test.id
consumer_group = azurerm_eventhub_consumer_group.test.name
}
`, r.template(data), data.RandomInteger)
}

func (r KustoEventHubDataConnectionResource) unboundMapping1(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_kusto_eventhub_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
eventhub_id = azurerm_eventhub.test.id
consumer_group = azurerm_eventhub_consumer_group.test.name
mapping_rule_name = "Json_Mapping"
}
`, r.template(data), data.RandomInteger)
}

func (r KustoEventHubDataConnectionResource) unboundMapping2(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_kusto_eventhub_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
eventhub_id = azurerm_eventhub.test.id
consumer_group = azurerm_eventhub_consumer_group.test.name
mapping_rule_name = "Json_Mapping"
data_format = "MULTIJSON"
}
`, r.template(data), data.RandomInteger)
}

func (KustoEventHubDataConnectionResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand Down Expand Up @@ -82,35 +187,5 @@ resource "azurerm_eventhub_consumer_group" "test" {
eventhub_name = azurerm_eventhub.test.name
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_kusto_eventhub_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
eventhub_id = azurerm_eventhub.test.id
consumer_group = azurerm_eventhub_consumer_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (KustoEventHubDataConnectionResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.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.AsEventHubDataConnection()
if !ok {
return nil, fmt.Errorf("%s is not an EventHubDataConnection", id.String())
}

return utils.Bool(value.EventHubConnectionProperties != nil), nil
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

0 comments on commit 0429810

Please sign in to comment.