forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iothub_dps_shared_access_policy_data_source.go
115 lines (93 loc) · 3.64 KB
/
iothub_dps_shared_access_policy_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package iothub
import (
"fmt"
"time"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/iothub/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/iothub/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func dataSourceIotHubDPSSharedAccessPolicy() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceIotHubDPSSharedAccessPolicyRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.IotHubSharedAccessPolicyName,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"iothub_dps_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.IoTHubName,
},
"primary_key": {
Type: pluginsdk.TypeString,
Sensitive: true,
Computed: true,
},
"primary_connection_string": {
Type: pluginsdk.TypeString,
Sensitive: true,
Computed: true,
},
"secondary_key": {
Type: pluginsdk.TypeString,
Sensitive: true,
Computed: true,
},
"secondary_connection_string": {
Type: pluginsdk.TypeString,
Sensitive: true,
Computed: true,
},
},
}
}
func dataSourceIotHubDPSSharedAccessPolicyRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.DPSResourceClient
subscriptionId := meta.(*clients.Client).IoTHub.DPSResourceClient.SubscriptionID
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id := parse.NewDpsSharedAccessPolicyID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iothub_dps_name").(string), d.Get("name").(string))
iothubDps, err := client.Get(ctx, id.ProvisioningServiceName, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(iothubDps.Response) {
return fmt.Errorf("Error: IotHub DPS %q (Resource Group %q) was not found", id.ProvisioningServiceName, id.ResourceGroup)
}
return fmt.Errorf("retrieving IotHub DPS %q (Resource Group %q): %+v", id.ProvisioningServiceName, id.ResourceGroup, err)
}
accessPolicy, err := client.ListKeysForKeyName(ctx, id.ProvisioningServiceName, id.KeyName, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(accessPolicy.Response) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("loading %s: %+v", id, err)
}
d.Set("name", id.KeyName)
d.Set("resource_group_name", id.ResourceGroup)
d.SetId(id.ID())
d.Set("primary_key", accessPolicy.PrimaryKey)
d.Set("secondary_key", accessPolicy.SecondaryKey)
primaryConnectionString := ""
secondaryConnectionString := ""
if iothubDps.Properties != nil && iothubDps.Properties.ServiceOperationsHostName != nil {
hostname := iothubDps.Properties.ServiceOperationsHostName
if primary := accessPolicy.PrimaryKey; primary != nil {
primaryConnectionString = getSAPConnectionString(*hostname, id.KeyName, *primary)
}
if secondary := accessPolicy.SecondaryKey; secondary != nil {
secondaryConnectionString = getSAPConnectionString(*hostname, id.KeyName, *secondary)
}
}
d.Set("primary_connection_string", primaryConnectionString)
d.Set("secondary_connection_string", secondaryConnectionString)
return nil
}