-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
data_source_eventhub_namespace_authorization_rule.go
123 lines (100 loc) · 3.46 KB
/
data_source_eventhub_namespace_authorization_rule.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
116
117
118
119
120
121
122
123
package eventhub
import (
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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 dataSourceEventHubNamespaceAuthorizationRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceEventHubNamespaceAuthorizationRuleRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubAuthorizationRuleName(),
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubNamespaceName(),
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"listen": {
Type: schema.TypeBool,
Computed: true,
},
"send": {
Type: schema.TypeBool,
Computed: true,
},
"manage": {
Type: schema.TypeBool,
Computed: true,
},
"primary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"primary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"secondary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func dataSourceEventHubNamespaceAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.NamespacesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
namespaceName := d.Get("namespace_name").(string)
resp, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("EventHub Authorization Rule %q (Resource Group %q / Namespace Name %q) was not found", name, resourceGroup, namespaceName)
}
return fmt.Errorf("retrieving EventHub Authorization Rule %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("retrieving EventHub Authorization Rule %q (Resource Group %q): `id` was nil", name, resourceGroup)
}
d.SetId(*resp.ID)
d.Set("name", name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resourceGroup)
if props := resp.AuthorizationRuleProperties; props != nil {
listen, send, manage := azure.FlattenEventHubAuthorizationRuleRights(props.Rights)
d.Set("manage", manage)
d.Set("listen", listen)
d.Set("send", send)
}
keysResp, err := client.ListKeys(ctx, resourceGroup, namespaceName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule List Keys %s: %+v", name, err)
}
d.Set("primary_key", keysResp.PrimaryKey)
d.Set("secondary_key", keysResp.SecondaryKey)
d.Set("primary_connection_string", keysResp.PrimaryConnectionString)
d.Set("secondary_connection_string", keysResp.SecondaryConnectionString)
return nil
}