-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_hosting_privatedatabase_whitelist.go
158 lines (133 loc) · 5.06 KB
/
resource_hosting_privatedatabase_whitelist.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
package ovh
import (
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceHostingPrivateDatabaseWhitelist() *schema.Resource {
return &schema.Resource{
Create: resourceHostingPrivateDatabaseWhitelistCreate,
Update: resourceHostingPrivateDatabaseWhitelistUpdate,
Read: resourceHostingPrivateDatabaseWhitelistRead,
Delete: resourceHostingPrivateDatabaseWhitelistDelete,
Importer: &schema.ResourceImporter{
State: resourceHostingPrivateDatabaseWhitelistImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
Description: "The internal name of your private database",
},
"ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The whitelisted IP in your instance",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Custom name for your Whitelisted IP",
},
"service": {
Type: schema.TypeBool,
Required: true,
Description: "Authorize this IP to access service port",
},
"sftp": {
Type: schema.TypeBool,
Required: true,
Description: "Authorize this IP to access SFTP port",
},
},
}
}
func resourceHostingPrivateDatabaseWhitelistCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ip := d.Get("ip").(string)
opts := (&HostingPrivateDatabaseWhitelistCreateOpts{}).FromResource(d)
ds := &HostingPrivateDatabaseWhitelist{}
log.Printf("[DEBUG][Create] HostingPrivateDatabaseWhitelist")
endpoint := fmt.Sprintf("/hosting/privateDatabase/%s/whitelist", url.PathEscape(serviceName))
err := config.OVHClient.Post(endpoint, opts, &ds)
if err != nil {
return fmt.Errorf("failed to create HostingPrivateDatabaseWhitelist: %s", err)
}
log.Printf("[DEBUG][Create][WaitForArchived] HostingPrivateDatabaseWhitelist")
endpoint = fmt.Sprintf("/hosting/privateDatabase/%s/tasks/%d", url.PathEscape(serviceName), ds.TaskId)
err = WaitArchivedHostingPrivateDabaseTask(config.OVHClient, endpoint, 2*time.Minute)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s", serviceName, ip))
return resourceHostingPrivateDatabaseWhitelistRead(d, meta)
}
func resourceHostingPrivateDatabaseWhitelistUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ip := d.Get("ip").(string)
opts := (&HostingPrivateDatabaseWhitelistUpdateOpts{}).FromResource(d)
log.Printf("[DEBUG][Update] HostingPrivateDatabaseWhitelist")
endpoint := fmt.Sprintf("/hosting/privateDatabase/%s/whitelist/%s", url.PathEscape(serviceName), url.PathEscape(ip))
err := config.OVHClient.Put(endpoint, opts, nil)
if err != nil {
return fmt.Errorf("failed to update whitelist: %s", err)
}
d.SetId(fmt.Sprintf("%s/%s", serviceName, ip))
return resourceHostingPrivateDatabaseWhitelistRead(d, meta)
}
func resourceHostingPrivateDatabaseWhitelistRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ip := d.Get("ip").(string)
ds := &HostingPrivateDatabaseWhitelist{}
log.Printf("[DEBUG][Read] HostingPrivateDatabaseWhitelist")
endpoint := fmt.Sprintf("/hosting/privateDatabase/%s/whitelist/%s", url.PathEscape(serviceName), url.PathEscape(ip))
if err := config.OVHClient.Get(endpoint, &ds); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.SetId(fmt.Sprintf("%s/%s", serviceName, ip))
for k, v := range ds.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceHostingPrivateDatabaseWhitelistDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ip := d.Get("ip").(string)
ds := &HostingPrivateDatabaseWhitelist{}
log.Printf("[DEBUG][Delete] HostingPrivateDatabaseWhitelist")
endpoint := fmt.Sprintf("/hosting/privateDatabase/%s/whitelist/%s", url.PathEscape(serviceName), url.PathEscape(ip))
if err := config.OVHClient.Delete(endpoint, ds); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
log.Printf("[DEBUG][Delete][WaitForArchived] HostingPrivateDatabaseWhitelist")
endpoint = fmt.Sprintf("/hosting/privateDatabase/%s/tasks/%d", url.PathEscape(serviceName), ds.TaskId)
err := WaitArchivedHostingPrivateDabaseTask(config.OVHClient, endpoint, 2*time.Minute)
if err != nil {
return err
}
d.SetId("")
return nil
}
func resourceHostingPrivateDatabaseWhitelistImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 2)
log.Printf("[DEBUG][Import] HostingPrivateDatabaseWhitelist givenId: %s", givenId)
if len(splitId) != 2 {
return nil, fmt.Errorf("import Id is not SERVICE_NAME/IP formatted")
}
d.SetId(splitId[0])
d.Set("service_name", splitId[0])
d.Set("ip", splitId[1])
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}