/
servicebus.go
165 lines (134 loc) · 4.51 KB
/
servicebus.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
154
155
156
157
158
159
160
161
162
163
164
165
package azure
import (
"fmt"
"log"
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
)
//validation
func ValidateServiceBusNamespaceName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{4,48}[a-zA-Z0-9]$"),
"The namespace name can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number and be between 6 and 50 characters long.",
)
}
func ValidateServiceBusQueueName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9][\w-./~]{0,258}([a-zA-Z0-9])?$`),
"The topic name can contain only letters, numbers, periods, hyphens, tildas, forward slashes and underscores. The namespace must start and end with a letter or number and be less then 260 characters long.",
)
}
func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z0-9][-._a-zA-Z0-9]{0,48}([a-zA-Z0-9])?$"),
"The name can contain only letters, numbers, periods, hyphens and underscores. The name must start and end with a letter or number and be less then 50 characters long.",
)
}
func ValidateServiceBusTopicName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-._~a-zA-Z0-9]{0,258}([a-zA-Z0-9])?$"),
"The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter, and it must end with a letter or number and be less then 260 characters long.",
)
}
func ValidateServiceBusAuthorizationRuleName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z0-9][-._a-zA-Z0-9]{0,48}([a-zA-Z0-9])?$"),
"The name can contain only letters, numbers, periods, hyphens and underscores. The name must start and end with a letter or number and be less the 50 characters long.",
)
}
func ExpandServiceBusAuthorizationRuleRights(d *schema.ResourceData) *[]servicebus.AccessRights {
rights := make([]servicebus.AccessRights, 0)
if d.Get("listen").(bool) {
rights = append(rights, servicebus.Listen)
}
if d.Get("send").(bool) {
rights = append(rights, servicebus.Send)
}
if d.Get("manage").(bool) {
rights = append(rights, servicebus.Manage)
}
return &rights
}
func FlattenServiceBusAuthorizationRuleRights(rights *[]servicebus.AccessRights) (listen, send, manage bool) {
//zero (initial) value for a bool in go is false
if rights != nil {
for _, right := range *rights {
switch right {
case servicebus.Listen:
listen = true
case servicebus.Send:
send = true
case servicebus.Manage:
manage = true
default:
log.Printf("[DEBUG] Unknown Authorization Rule Right '%s'", right)
}
}
}
return listen, send, manage
}
//shared schema
func MergeSchema(a map[string]*schema.Schema, b map[string]*schema.Schema) map[string]*schema.Schema {
s := map[string]*schema.Schema{}
for k, v := range a {
s[k] = v
}
for k, v := range b {
s[k] = v
}
return s
}
func ServiceBusAuthorizationRuleSchemaFrom(s map[string]*schema.Schema) map[string]*schema.Schema {
authSchema := map[string]*schema.Schema{
"listen": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"send": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"manage": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"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,
},
}
return MergeSchema(s, authSchema)
}
func ServiceBusAuthorizationRuleCustomizeDiff(d *schema.ResourceDiff, _ interface{}) error {
listen, hasListen := d.GetOk("listen")
send, hasSend := d.GetOk("send")
manage, hasManage := d.GetOk("manage")
if !hasListen && !hasSend && !hasManage {
return fmt.Errorf("One of the `listen`, `send` or `manage` properties needs to be set")
}
if manage.(bool) && !listen.(bool) && !send.(bool) {
return fmt.Errorf("if `manage` is set both `listen` and `send` must be set to true too")
}
return nil
}