-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_transip_private_network_attachment.go
107 lines (94 loc) · 3.29 KB
/
resource_transip_private_network_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
package main
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/repository"
"github.com/transip/gotransip/v6/vps"
)
func resourcePrivateNetworkAttachment() *schema.Resource {
return &schema.Resource{
Create: resourcePrivateNetworkAttachmentCreate,
Read: resourcePrivateNetworkAttachmentRead,
// Update: resourcePrivateNetworkUpdate,
Delete: resourcePrivateNetworkAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"private_network_id": {
Type: schema.TypeString,
Description: "Name of the Private Network.",
Required: true,
ForceNew: true,
},
"vps_id": {
Type: schema.TypeString,
Description: "VPN name the Private Network is attached to.",
Required: true,
ForceNew: true,
},
},
}
}
func resourcePrivateNetworkAttachmentCreate(d *schema.ResourceData, m interface{}) error {
errorStrings := []string{
"has an action running, no modification is allowed",
"is already locked to another action",
"EOF"}
privateNetworkID := d.Get("private_network_id").(string)
vpsID := d.Get("vps_id").(string)
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
err := repository.AttachVps(vpsID, privateNetworkID)
if err != nil {
for _, errorString := range errorStrings {
if strings.Contains(err.Error(), errorString) {
return resource.RetryableError(fmt.Errorf("failed to attach VPS %s to private network %s, VPS busy: %s; retrying", vpsID, privateNetworkID, err))
}
}
return resource.NonRetryableError(fmt.Errorf("failed to attach private network %s to VPS %s: %s", privateNetworkID, vpsID, err))
}
return resource.NonRetryableError(resourcePrivateNetworkAttachmentRead(d, m))
})
}
func resourcePrivateNetworkAttachmentRead(d *schema.ResourceData, m interface{}) error {
privateNetworkID := d.Get("private_network_id").(string)
vpsID := d.Get("vps_id").(string)
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
p, err := repository.GetByName(privateNetworkID)
if err != nil {
return fmt.Errorf("failed to lookup private network %q: %s", d.Id(), err)
}
found := false
for _, vpsName := range p.VpsNames {
if vpsName == vpsID {
found = true
break
}
}
if !found {
d.SetId("")
}
d.SetId(p.Name)
return nil
}
func resourcePrivateNetworkAttachmentDelete(d *schema.ResourceData, m interface{}) error {
privateNetworkID := d.Get("private_network_id").(string)
vpsID := d.Get("vps_id").(string)
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
err := repository.DetachVps(vpsID, privateNetworkID)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf("VPS '%s' has an action running, no modification is allowed", vpsID)) {
return resource.RetryableError(fmt.Errorf("retrying to detach private network %s from VPS %s: %s", privateNetworkID, vpsID, err))
}
}
return nil
})
return nil
}