Skip to content

Commit

Permalink
azurerm_iothub - Support for public_network_access_enabled (#8586)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Oct 2, 2020
1 parent 87a2424 commit 71abd5a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
18 changes: 18 additions & 0 deletions azurerm/internal/services/iothub/iothub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func resourceArmIotHub() *schema.Resource {
},
},

"public_network_access_enabled": {
Type: schema.TypeBool,
Optional: true,
},

"type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -513,6 +518,15 @@ func resourceArmIotHubCreateUpdate(d *schema.ResourceData, meta interface{}) err
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

// nolint staticcheck
if v, ok := d.GetOkExists("public_network_access_enabled"); ok {
enabled := devices.Disabled
if v.(bool) {
enabled = devices.Enabled
}
props.Properties.PublicNetworkAccess = enabled
}

retention, retentionOk := d.GetOk("event_hub_retention_in_days")
partition, partitionOk := d.GetOk("event_hub_partition_count")
if partitionOk || retentionOk {
Expand Down Expand Up @@ -624,6 +638,10 @@ func resourceArmIotHubRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("file_upload", fileUpload); err != nil {
return fmt.Errorf("setting `file_upload` in IoTHub %q: %+v", id.Name, err)
}

if enabled := properties.PublicNetworkAccess; enabled != "" {
d.Set("public_network_access_enabled", enabled == devices.Enabled)
}
}

d.Set("name", id.Name)
Expand Down
102 changes: 101 additions & 1 deletion azurerm/internal/services/iothub/tests/iothub_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,46 @@ func TestAccAzureRMIotHub_fallbackRoute(t *testing.T) {
})
}

func TestAccAzureRMIotHub_publicAccess(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_iothub", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMIotHubDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMIotHub_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMIotHub_publicAccessEnabled(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMIotHub_publicAccessDisabled(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMIotHub_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMIotHubDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.ResourceClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -242,7 +282,7 @@ provider "azurerm" {
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-iothub-%d"
location = "%s"
}
Expand Down Expand Up @@ -601,3 +641,63 @@ resource "azurerm_iothub" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
}

func testAccAzureRMIotHub_publicAccessEnabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-iothub-%d"
location = "%s"
}
resource "azurerm_iothub" "test" {
name = "acctestIoTHub-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku {
name = "B1"
capacity = "1"
}
public_network_access_enabled = true
tags = {
purpose = "testing"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMIotHub_publicAccessDisabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-iothub-%d"
location = "%s"
}
resource "azurerm_iothub" "test" {
name = "acctestIoTHub-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku {
name = "B1"
capacity = "1"
}
public_network_access_enabled = false
tags = {
purpose = "testing"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/r/iothub.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ The following arguments are supported:

* `route` - (Optional) A `route` block as defined below.

* `public_network_access_enabled` - (Optional) Is the IotHub resource accessible from a public network?

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down

0 comments on commit 71abd5a

Please sign in to comment.