Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Data Source: 'azurerm_iothub_dps_shared_access_policy' #5516

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package iothub

import (
"fmt"
"regexp"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceIotHubDPSSharedAccessPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceIotHubDPSSharedAccessPolicyRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[a-zA-Z0-9!._-]{1,64}`), ""+
"The shared access policy key name must not be empty, and must not exceed 64 characters in length. The shared access policy key name can only contain alphanumeric characters, exclamation marks, periods, underscores and hyphens."),
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"iothub_dps_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.IoTHubName,
},

"primary_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},

"primary_connection_string": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},

"secondary_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},

"secondary_connection_string": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
},
}
}

func dataSourceIotHubDPSSharedAccessPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.DPSResourceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

keyName := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
iothubDpsName := d.Get("iothub_dps_name").(string)

iothubDps, err := client.Get(ctx, iothubDpsName, resourceGroup)
if err != nil {
if utils.ResponseWasNotFound(iothubDps.Response) {
return fmt.Errorf("Error: IotHub DPS %q (Resource Group %q) was not found", iothubDpsName, resourceGroup)
}

return fmt.Errorf("Error retrieving IotHub DPS %q (Resource Group %q): %+v", iothubDpsName, resourceGroup, err)
}

accessPolicy, err := client.ListKeysForKeyName(ctx, iothubDpsName, keyName, resourceGroup)
if err != nil {
if utils.ResponseWasNotFound(accessPolicy.Response) {
return fmt.Errorf("Error: Shared Access Policy %q (IotHub DPS %q / Resource Group %q) was not found", keyName, iothubDpsName, resourceGroup)
}

return fmt.Errorf("Error loading Shared Access Policy %q (IotHub DPS %q / Resource Group %q): %+v", keyName, iothubDpsName, resourceGroup, err)
}

d.Set("name", keyName)
d.Set("resource_group_name", resourceGroup)

resourceID := fmt.Sprintf("%s/keys/%s", *iothubDps.ID, keyName)
d.SetId(resourceID)

d.Set("primary_key", accessPolicy.PrimaryKey)
d.Set("secondary_key", accessPolicy.SecondaryKey)

primaryConnectionString := ""
secondaryConnectionString := ""
if iothubDps.Properties != nil && iothubDps.Properties.DeviceProvisioningHostName != nil {
hostname := iothubDps.Properties.DeviceProvisioningHostName
if primary := accessPolicy.PrimaryKey; primary != nil {
primaryConnectionString = getSAPConnectionString(*hostname, keyName, *primary)
}
if secondary := accessPolicy.SecondaryKey; secondary != nil {
secondaryConnectionString = getSAPConnectionString(*hostname, keyName, *secondary)
}
}
d.Set("primary_connection_string", primaryConnectionString)
d.Set("secondary_connection_string", secondaryConnectionString)

return nil
}
5 changes: 3 additions & 2 deletions azurerm/internal/services/iothub/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func (r Registration) Name() string {
// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_iothub_dps": dataSourceArmIotHubDPS(),
"azurerm_iothub_shared_access_policy": dataSourceArmIotHubSharedAccessPolicy(),
"azurerm_iothub_dps": dataSourceArmIotHubDPS(),
"azurerm_iothub_dps_shared_access_policy": dataSourceIotHubDPSSharedAccessPolicy(),
"azurerm_iothub_shared_access_policy": dataSourceArmIotHubSharedAccessPolicy(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,19 @@ func resourceArmIotHubDPSSharedAccessPolicyRead(d *schema.ResourceData, meta int
d.Set("primary_key", accessPolicy.PrimaryKey)
d.Set("secondary_key", accessPolicy.SecondaryKey)

if props := iothubDps.Properties; props != nil {
if host := props.ServiceOperationsHostName; host != nil {
if pKey := accessPolicy.PrimaryKey; pKey != nil {
if err := d.Set("primary_connection_string", getSAPConnectionString(*host, keyName, *pKey)); err != nil {
return fmt.Errorf("error setting `primary_connection_string`: %v", err)
}
}

if sKey := accessPolicy.SecondaryKey; sKey != nil {
if err := d.Set("secondary_connection_string", getSAPConnectionString(*host, keyName, *sKey)); err != nil {
return fmt.Errorf("error setting `secondary_connection_string`: %v", err)
}
}
primaryConnectionString := ""
secondaryConnectionString := ""
if iothubDps.Properties != nil && iothubDps.Properties.DeviceProvisioningHostName != nil {
hostname := iothubDps.Properties.DeviceProvisioningHostName
if primary := accessPolicy.PrimaryKey; primary != nil {
primaryConnectionString = getSAPConnectionString(*hostname, keyName, *primary)
}
if secondary := accessPolicy.SecondaryKey; secondary != nil {
secondaryConnectionString = getSAPConnectionString(*hostname, keyName, *secondary)
}
}
d.Set("primary_connection_string", primaryConnectionString)
d.Set("secondary_connection_string", secondaryConnectionString)

rights := flattenDpsAccessRights(accessPolicy.Rights)
d.Set("enrollment_read", rights.enrollmentRead)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMIotHubDpsSharedAccessPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMIotHubDpsSharedAccessPolicy_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMIotHubDpsSharedAccessPolicyExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "primary_key"),
resource.TestCheckResourceAttrSet(data.ResourceName, "primary_connection_string"),
resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_key"),
resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_connection_string"),
),
},
},
})
}

func testAccDataSourceAzureRMIotHubDpsSharedAccessPolicy_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_iothub_dps" "test" {
name = "acctestIoTDPS-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"

sku {
name = "S1"
capacity = "1"
}
}
resource "azurerm_iothub_dps_shared_access_policy" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
iothub_dps_name = "${azurerm_iothub_dps.test.name}"
name = "acctest"
service_config = true
}
data "azurerm_iothub_dps_shared_access_policy" "test" {
name = "${azurerm_iothub_dps_shared_access_policy.test.name}"
iothub_dps_name = "${azurerm_iothub_dps.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@
<a href="/docs/providers/azurerm/d/iothub_dps.html">azurerm_iothub_dps</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/iothub_dps_shared_access_policy.html">azurerm_iothub_dps_shared_access_policy</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/iothub_shared_access_policy.html">azurerm_iothub_shared_access_policy</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/iothub_dps_shared_access_policy.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
subcategory: "IoT Hub"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_iothub_dps_shared_access_policy"
description: |-
Gets information about an existing IotHub Device Provisioning Service Shared Access Policy
---

# Data Source: azurerm_iothub_dps_shared_access_policy

Use this data source to access information about an existing IotHub Device Provisioning Service Shared Access Policy

## Example Usage

```hcl
data "azurerm_iothub_dps_shared_access_policy" "example" {
name = "example"
resource_group_name = azurerm_resource_group.example.name
iothub_dps_name = azurerm_iothub_dps.example.name
}
```

## Argument Reference

The following arguments are supported:

* `name` - Specifies the name of the IotHub Shared Access Policy.

* `resource_group_name` - Specifies the name of the resource group under which the IotHub Shared Access Policy resource exists.

* `iothub_dps_name` - Specifies the name of the IoT Hub Device Provisioning service to which the Shared Access Policy belongs.

## Attributes Reference

The following attributes are exported:

* `id` - The ID of the IoT Hub Device Provisioning Service Shared Access Policy.

* `primary_key` - The primary key used to create the authentication token.

* `primary_connection_string` - The primary connection string of the Shared Access Policy.

* `secondary_key` - The secondary key used to create the authentication token.

* `secondary_connection_string` - The secondary connection string of the Shared Access Policy.