-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_dedicated_nasha_partition_access.go
143 lines (114 loc) · 3.92 KB
/
resource_dedicated_nasha_partition_access.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
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDedicatedNASHAPartitionAccess() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDedicatedNASHAPartitionAccessCreate,
ReadContext: resourceDedicatedNASHAPartitionAccessRead,
DeleteContext: resourceDedicatedNASHAPartitionAccessDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"partition_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Default: "readwrite",
Optional: true,
ForceNew: true,
},
},
}
}
func resourceDedicatedNASHAPartitionAccessCreate(c context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
partitionName := d.Get("partition_name").(string)
ipsubnet, _ := d.Get("ip").(string)
access := &DedicatedNASHAPartitionAccess{
IP: d.Get("ip").(string),
Type: d.Get("type").(string),
}
resp := &DedicatedNASHATask{}
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/access", serviceName, partitionName)
err := config.OVHClient.Post(endpoint, access, resp)
if err != nil {
return diag.Errorf("calling %s with params %v:\n\t %q", endpoint, access, err)
}
stateConf := resp.StateChangeConf(d, meta)
_, err = stateConf.WaitForStateContext(c)
if err != nil {
return diag.Errorf("waiting for HA-NAS partition access (%v): %s", access, err)
}
log.Printf("[DEBUG] Created HA-NAS partition access")
d.SetId(fmt.Sprintf("%s/%s/%s", serviceName, partitionName, url.PathEscape(ipsubnet)))
return nil
}
func resourceDedicatedNASHAPartitionAccessRead(c context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
if strings.Contains(d.Id(), "/") {
tab := strings.Split(d.Id(), "/")
if len(tab) != 3 {
return diag.Errorf("can't parse access partition ID: %s", d.Id())
}
d.Set("service_name", tab[0])
d.Set("partition_name", tab[1])
ip, _ := url.PathUnescape(tab[2])
d.Set("ip", ip)
}
serviceName := d.Get("service_name").(string)
partitionName := d.Get("partition_name").(string)
ipsubnet, _ := d.Get("ip").(string)
resp := &DedicatedNASHAPartitionAccess{}
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/access/%s", serviceName, partitionName, url.PathEscape(ipsubnet))
err := config.OVHClient.Get(endpoint, resp)
if err != nil {
if err.Error() == fmt.Sprintf("Error 404: \"The requested object (ip = %s) does not exist\"", ipsubnet) ||
err.Error() == fmt.Sprintf("Error 404: \"The requested object (partitionName = %s) does not exist\"", partitionName) {
d.SetId("")
return nil
}
return diag.Errorf("Error calling %s:\n\t '%q'", endpoint, err)
}
d.Set("type", resp.Type)
return nil
}
func resourceDedicatedNASHAPartitionAccessDelete(c context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
partitionName := d.Get("partition_name").(string)
ipsubnet, _ := d.Get("ip").(string)
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/access/%s", serviceName, partitionName, url.PathEscape(ipsubnet))
resp := &DedicatedNASHATask{}
err := config.OVHClient.Delete(endpoint, resp)
if err != nil {
return diag.Errorf("Error calling %s:\n\t %q", endpoint, err)
}
stateConf := resp.StateChangeConf(d, meta)
_, err = stateConf.WaitForStateContext(c)
if err != nil {
return diag.Errorf("waiting for HA-NAS partition access: %s", err)
}
return nil
}