forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ovh_vrack_publiccloud_attachment.go
172 lines (138 loc) · 5.25 KB
/
resource_ovh_vrack_publiccloud_attachment.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
172
package ovh
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/ovh/go-ovh/ovh"
)
func resourceVRackPublicCloudAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceVRackPublicCloudAttachmentCreate,
Read: resourceVRackPublicCloudAttachmentRead,
Delete: resourceVRackPublicCloudAttachmentDelete,
Schema: map[string]*schema.Schema{
"vrack_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_VRACK_ID", ""),
},
"project_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", ""),
},
},
}
}
func resourceVRackPublicCloudAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vrackId := d.Get("vrack_id").(string)
projectId := d.Get("project_id").(string)
if err := vrackPublicCloudAttachmentExists(vrackId, projectId, config.OVHClient); err == nil {
//set id
d.SetId(fmt.Sprintf("vrack_%s-cloudproject_%s-attach", vrackId, projectId))
return nil
}
params := &VRackAttachOpts{Project: projectId}
r := VRackAttachTaskResponse{}
log.Printf("[DEBUG] Will Attach VRack %s -> PublicCloud %s", vrackId, params.Project)
endpoint := fmt.Sprintf("/vrack/%s/cloudProject", vrackId)
err := config.OVHClient.Post(endpoint, params, &r)
if err != nil {
return fmt.Errorf("Error calling %s with params %s:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for Attachement Task id %d: VRack %s -> PublicCloud %s", r.Id, vrackId, params.Project)
stateConf := &resource.StateChangeConf{
Pending: []string{"init", "todo", "doing"},
Target: []string{"completed"},
Refresh: waitForVRackTaskCompleted(config.OVHClient, vrackId, r.Id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to attach to public cloud (%s): %s", vrackId, params.Project, err)
}
log.Printf("[DEBUG] Created Attachement Task id %d: VRack %s -> PublicCloud %s", r.Id, vrackId, params.Project)
//set id
d.SetId(fmt.Sprintf("vrack_%s-cloudproject_%s-attach", vrackId, params.Project))
return nil
}
func resourceVRackPublicCloudAttachmentRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vrackId := d.Get("vrack_id").(string)
params := &VRackAttachOpts{Project: d.Get("project_id").(string)}
r := VRackAttachTaskResponse{}
endpoint := fmt.Sprintf("/vrack/%s/cloudProject/%s", vrackId, params.Project)
err := config.OVHClient.Get(endpoint, &r)
if err != nil {
return err
}
log.Printf("[DEBUG] Read VRack %s -> PublicCloud %s", vrackId, params.Project)
return nil
}
func resourceVRackPublicCloudAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vrackId := d.Get("vrack_id").(string)
params := &VRackAttachOpts{Project: d.Get("project_id").(string)}
r := VRackAttachTaskResponse{}
endpoint := fmt.Sprintf("/vrack/%s/cloudProject/%s", vrackId, params.Project)
err := config.OVHClient.Delete(endpoint, &r)
if err != nil {
return err
}
log.Printf("[DEBUG] Waiting for Attachment Deletion Task id %d: VRack %s -> PublicCloud %s", r.Id, vrackId, params.Project)
stateConf := &resource.StateChangeConf{
Pending: []string{"init", "todo", "doing"},
Target: []string{"completed"},
Refresh: waitForVRackTaskCompleted(config.OVHClient, vrackId, r.Id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to attach to public cloud (%s): %s", vrackId, params.Project, err)
}
log.Printf("[DEBUG] Removed Attachement id %d: VRack %s -> PublicCloud %s", r.Id, vrackId, params.Project)
d.SetId("")
return nil
}
func vrackPublicCloudAttachmentExists(vrackId, projectId string, c *ovh.Client) error {
type attachResponse struct {
VRack string `json:"vrack"`
Project string `json:"project"`
}
r := attachResponse{}
endpoint := fmt.Sprintf("/vrack/%s/cloudProject/%s", vrackId, projectId)
err := c.Get(endpoint, &r)
if err != nil {
return fmt.Errorf("Error while querying %s: %q\n", endpoint, err)
}
log.Printf("[DEBUG] Read Attachment %s -> VRack:%s, Cloud Project: %s", endpoint, r.VRack, r.Project)
return nil
}
// AttachmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an Attachment Task.
func waitForVRackTaskCompleted(c *ovh.Client, serviceName string, taskId int) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r := VRackAttachTaskResponse{}
endpoint := fmt.Sprintf("/vrack/%s/task/%d", serviceName, taskId)
err := c.Get(endpoint, &r)
if err != nil {
if err.(*ovh.APIError).Code == 404 {
log.Printf("[DEBUG] Task id %d on VRack %s completed", taskId, serviceName)
return taskId, "completed", nil
} else {
return taskId, "", err
}
}
log.Printf("[DEBUG] Pending Task id %d on VRack %s status: %s", r.Id, serviceName, r.Status)
return taskId, r.Status, nil
}
}