-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_transip_private_network.go
140 lines (121 loc) · 4.16 KB
/
resource_transip_private_network.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6"
"github.com/transip/gotransip/v6/repository"
"github.com/transip/gotransip/v6/vps"
)
func resourcePrivateNetwork() *schema.Resource {
return &schema.Resource{
Create: resourcePrivateNetworkCreate,
Read: resourcePrivateNetworkRead,
Update: resourcePrivateNetworkUpdate,
Delete: resourcePrivateNetworkDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Description: "The custom name that can be set by customer.",
Required: true,
},
"name": {
Type: schema.TypeString,
Description: "The unique private network name",
Computed: true,
},
"is_blocked": {
Type: schema.TypeString,
Description: "If the Private Network is administratively blocked.",
Computed: true,
},
"is_locked": {
Type: schema.TypeString,
Description: "When locked, another process is already working with this private network.",
Computed: true,
},
"vps_names": {
Type: schema.TypeList,
Description: "The VPSes in this private network.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourcePrivateNetworkCreate(d *schema.ResourceData, m interface{}) error {
description := d.Get("description").(string)
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
err := repository.Order(description)
if err != nil {
return fmt.Errorf("failed to order private network %s: %s", description, err)
}
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
// The set description in the Terraform resource is not the same as the name used to query details about a private network.
// You'll need the unique name Transip generates to get the private network details.
all, err := repository.GetAll()
if err != nil {
return resource.NonRetryableError(fmt.Errorf("failed to get all private networks: %s", err))
}
for _, privateNetwork := range all {
if privateNetwork.Description == description {
d.SetId(privateNetwork.Name)
}
}
if d.Id() == "" {
return resource.RetryableError(fmt.Errorf("Failed to set ID for private network %s", description))
}
return resource.NonRetryableError(resourcePrivateNetworkRead(d, m))
})
}
func resourcePrivateNetworkRead(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
p, err := repository.GetByName(d.Id())
if err != nil {
return fmt.Errorf("failed to lookup private network %q: %s", d.Id(), err)
}
var vpsNames []string
for _, vpsName := range p.VpsNames {
vpsNames = append(vpsNames, vpsName)
}
d.Set("name", d.Id())
d.Set("description", p.Description)
d.Set("is_blocked", p.IsBlocked)
d.Set("is_locked", p.IsLocked)
d.Set("vps_names", vpsNames)
return nil
}
func resourcePrivateNetworkDelete(d *schema.ResourceData, m interface{}) error {
description := d.Get("description")
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
err := repository.Cancel(d.Id(), gotransip.CancellationTimeImmediately)
if err != nil {
return fmt.Errorf("failed to cancel private network %s with id %q: %s", description, d.Id(), err)
}
return nil
}
func resourcePrivateNetworkUpdate(d *schema.ResourceData, m interface{}) error {
description := d.Get("description").(string)
client := m.(repository.Client)
repository := vps.PrivateNetworkRepository{Client: client}
privateNetwork := vps.PrivateNetwork{
Name: d.Id(),
Description: description,
IsBlocked: d.Get("is_blocked").(bool),
IsLocked: d.Get("is_locked").(bool),
VpsNames: d.Get("vps_names").([]string),
}
err := repository.Update(privateNetwork)
if err != nil {
return fmt.Errorf("failed to update private network %s with id %q: %s", description, d.Id(), err)
}
return resourcePrivateNetworkRead(d, m)
}