-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_dedicated_nasha_partition_snapshot.go
134 lines (105 loc) · 3.63 KB
/
resource_dedicated_nasha_partition_snapshot.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
package ovh
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDedicatedNASHAPartitionSnapshot() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDedicatedNASHAPartitionSnapshotCreate,
ReadContext: resourceDedicatedNASHAPartitionSnapshotRead,
DeleteContext: resourceDedicatedNASHAPartitionSnapshotDelete,
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,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceDedicatedNASHAPartitionSnapshotCreate(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)
snapshotType := d.Get("type").(string)
snapshot := &DedicatedNASHAPartitionSnapshot{
Type: snapshotType,
}
resp := &DedicatedNASHATask{}
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/snapshot", serviceName, partitionName)
err := config.OVHClient.Post(endpoint, snapshot, resp)
if err != nil {
return diag.Errorf("calling %s with params %q:\n\t %s", endpoint, snapshot, err.Error())
}
stateConf := resp.StateChangeConf(d, meta)
_, err = stateConf.WaitForStateContext(c)
if err != nil {
return diag.Errorf("waiting for HA-NAS snapshot (%q): %s", snapshot, err.Error())
}
log.Printf("[DEBUG] Created HA-NAS snapshot")
d.SetId(fmt.Sprintf("%s/%s/%s", serviceName, partitionName, snapshotType))
return nil
}
func resourceDedicatedNASHAPartitionSnapshotRead(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 snapshot ID: %s", d.Id())
}
d.Set("service_name", tab[0])
d.Set("partition_name", tab[1])
d.Set("type", tab[2])
}
serviceName := d.Get("service_name").(string)
partitionName := d.Get("partition_name").(string)
snapshotType := d.Get("type").(string)
resp := &DedicatedNASHAPartition{}
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/snapshot/%s", serviceName, partitionName, snapshotType)
err := config.OVHClient.Get(endpoint, resp)
if err != nil {
if err.Error() == fmt.Sprintf("Error 404: \"The requested object (type = %s) does not exist\"", snapshotType) {
d.SetId("")
return nil
} else {
return diag.Errorf("Error calling %s:\n\t '%q'", endpoint, err)
}
}
return nil
}
func resourceDedicatedNASHAPartitionSnapshotDelete(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)
snapshotType := d.Get("type").(string)
endpoint := fmt.Sprintf("/dedicated/nasha/%s/partition/%s/snapshot/%s", serviceName, partitionName, snapshotType)
resp := &DedicatedNASHATask{}
err := config.OVHClient.Delete(endpoint, resp)
if err != nil {
return diag.Errorf("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 shapshot delete: %s", err)
}
log.Printf("[DEBUG] Deleted HA-NAS shapshot")
return nil
}