/
resource_cloudamqp_privatelink_azure.go
171 lines (156 loc) · 4.31 KB
/
resource_cloudamqp_privatelink_azure.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
166
167
168
169
170
171
package cloudamqp
import (
"fmt"
"regexp"
"strconv"
"github.com/84codes/go-api/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourcePrivateLinkAzure() *schema.Resource {
return &schema.Resource{
Create: resourcePrivateLinkAzureCreate,
Read: resourcePrivateLinkAzureRead,
Update: resourcePrivateLinkAzureUpdate,
Delete: resourcePrivateLinkAzureDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The CloudAMQP instance identifier",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Status of the PrivateLink [enabled, pending, disabled]",
},
"service_name": {
Type: schema.TypeString,
Computed: true,
Description: "Service name (alias) of the PrivateLink, needed when creating the endpoint",
},
"server_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the server having the PrivateLink enabled",
},
"approved_subscriptions": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "Approved subscriptions to access the endpoint service",
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep in seconds between retries when enable PrivateLink",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout in seconds when enable PrivateLink",
},
},
CustomizeDiff: customdiff.All(
customdiff.ValidateValue("approved_subscriptions", func(value, meta interface{}) error {
for _, v := range value.([]interface{}) {
re := regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
if !re.MatchString(v.(string)) {
return fmt.Errorf("invalid Subscription ID : %v", v)
}
}
return nil
}),
),
}
}
func resourcePrivateLinkAzureCreate(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
params = make(map[string][]interface{})
)
params["approved_subscriptions"] = d.Get("approved_subscriptions").([]interface{})
err := api.EnablePrivatelink(instanceID, params, sleep, timeout)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", instanceID))
return resourcePrivateLinkAzureRead(d, meta)
}
func resourcePrivateLinkAzureRead(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID, _ = strconv.Atoi(d.Id()) // Uses d.Id() to allow import
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)
// Set arguments during import
if d.Get("instance_id").(int) == 0 {
d.Set("instance_id", instanceID)
}
if sleep == 0 && timeout == 0 {
sleep = 10
d.Set("sleep", 10)
timeout = 1800
d.Set("timeout", 1800)
}
data, err := api.ReadPrivatelink(instanceID, sleep, timeout)
if err != nil {
return err
}
for k, v := range data {
if validatePrivateLinkAzureSchemaAttribute(k) {
if k == "alias" {
d.Set("service_name", v)
} else {
d.Set(k, v)
}
}
}
return nil
}
func resourcePrivateLinkAzureUpdate(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
params = make(map[string][]interface{})
)
params["approved_subscriptions"] = d.Get("approved_subscriptions").([]interface{})
err := api.UpdatePrivatelink(instanceID, params)
if err != nil {
return err
}
return nil
}
func resourcePrivateLinkAzureDelete(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
)
err := api.DisablePrivatelink(instanceID)
if err != nil {
return err
}
return nil
}
func validatePrivateLinkAzureSchemaAttribute(key string) bool {
switch key {
case "status",
"server_name",
"alias",
"approved_subscriptions":
return true
}
return false
}