forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_namespace_authorization_rule_resource.go
153 lines (125 loc) · 4.98 KB
/
relay_namespace_authorization_rule_resource.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package relay
import (
"fmt"
"log"
"time"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/relay/sdk/2017-04-01/namespaces"
"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 resourceRelayNamespaceAuthorizationRule() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceRelayNamespaceAuthorizationRuleCreateUpdate,
Read: resourceRelayNamespaceAuthorizationRuleRead,
Update: resourceRelayNamespaceAuthorizationRuleCreateUpdate,
Delete: resourceRelayNamespaceAuthorizationRuleDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := namespaces.ParseAuthorizationRuleID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
// function takes a schema map and adds the authorization rule properties to it
Schema: authorizationRuleSchemaFrom(map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": azure.SchemaResourceGroupName(),
}),
CustomizeDiff: pluginsdk.CustomizeDiffShim(authorizationRuleCustomizeDiff),
}
}
func resourceRelayNamespaceAuthorizationRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Relay.NamespacesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for Relay Namespace Authorization Rule creation.")
resourceId := namespaces.NewAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.GetAuthorizationRule(ctx, resourceId)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", resourceId, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_relay_namespace_authorization_rule", resourceId.ID())
}
}
parameters := namespaces.AuthorizationRule{
Name: utils.String(resourceId.AuthorizationRuleName),
Properties: namespaces.AuthorizationRuleProperties{
Rights: expandAuthorizationRuleRights(d),
},
}
if _, err := client.CreateOrUpdateAuthorizationRule(ctx, resourceId, parameters); err != nil {
return fmt.Errorf("creating/updating %s: %+v", resourceId, err)
}
d.SetId(resourceId.ID())
return resourceRelayNamespaceAuthorizationRuleRead(d, meta)
}
func resourceRelayNamespaceAuthorizationRuleRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Relay.NamespacesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := namespaces.ParseAuthorizationRuleID(d.Id())
if err != nil {
return err
}
resp, err := client.GetAuthorizationRule(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
keysResp, err := client.ListKeys(ctx, *id)
if err != nil {
return fmt.Errorf("listing keys for %s: %+v", id, err)
}
d.Set("name", id.AuthorizationRuleName)
d.Set("namespace_name", id.NamespaceName)
d.Set("resource_group_name", id.ResourceGroupName)
if model := resp.Model; model != nil {
listen, send, manage := flattenAuthorizationRuleRights(model.Properties.Rights)
d.Set("manage", manage)
d.Set("listen", listen)
d.Set("send", send)
}
d.Set("primary_key", keysResp.Model.PrimaryKey)
d.Set("primary_connection_string", keysResp.Model.PrimaryConnectionString)
d.Set("secondary_key", keysResp.Model.SecondaryKey)
d.Set("secondary_connection_string", keysResp.Model.SecondaryConnectionString)
return nil
}
func resourceRelayNamespaceAuthorizationRuleDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Relay.NamespacesClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := namespaces.ParseAuthorizationRuleID(d.Id())
if err != nil {
return err
}
if _, err = client.DeleteAuthorizationRule(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
return nil
}