Skip to content

Commit

Permalink
Add primary & secondary connection string to 'azurerm_iothub_dp… (#5231)
Browse files Browse the repository at this point in the history
Add primary_connection_string and secondary_connection_string to azurerm_iothub_dps_shared_access_policy
  • Loading branch information
Brunhil authored and katbyte committed Dec 31, 2019
1 parent 9a1cc1a commit e8859f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,23 @@ func resourceArmIotHubDPSSharedAccessPolicy() *schema.Resource {
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,
},
},
}
}
Expand Down Expand Up @@ -201,6 +213,11 @@ func resourceArmIotHubDPSSharedAccessPolicyRead(d *schema.ResourceData, meta int
iothubDpsName := id.Path["provisioningServices"]
keyName := id.Path["keys"]

iothubDps, err := client.Get(ctx, iothubDpsName, resourceGroup)
if err != nil {
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) {
Expand All @@ -217,6 +234,22 @@ 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)
}
}
}
}

rights := flattenDpsAccessRights(accessPolicy.Rights)
d.Set("enrollment_read", rights.enrollmentRead)
d.Set("enrollment_write", rights.enrollmentWrite)
Expand Down Expand Up @@ -369,3 +402,7 @@ func flattenDpsAccessRights(r iothub.AccessRightsDescription) dpsAccessRights {

return rights
}

func getSAPConnectionString(iothubDpsHostName string, keyName string, key string) string {
return fmt.Sprintf("HostName=%s;SharedAccessKeyName=%s;SharedAccessKey=%s", iothubDpsHostName, keyName, key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestAccAzureRMIotHubDpsSharedAccessPolicy_basic(t *testing.T) {
resource.TestCheckResourceAttr(data.ResourceName, "registration_read", "false"),
resource.TestCheckResourceAttr(data.ResourceName, "registration_write", "false"),
resource.TestCheckResourceAttr(data.ResourceName, "service_config", "true"),
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"),
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/iothub_dps_shared_access_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ The following attributes are exported:

* `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.

## Import

IoT Hub Device Provisioning Service Shared Access Policies can be imported using the `resource id`, e.g.
Expand Down

0 comments on commit e8859f5

Please sign in to comment.