-
Notifications
You must be signed in to change notification settings - Fork 16
/
data_source_transip_vps.go
158 lines (149 loc) · 4.14 KB
/
data_source_transip_vps.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/repository"
"github.com/transip/gotransip/v6/vps"
)
func dataSourceVps() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The unique VPS name.",
Required: true,
},
"description": {
Type: schema.TypeString,
Description: "The name that can be set by customer.",
Computed: true,
},
"product_name": {
Type: schema.TypeString,
Description: "The product name.",
Computed: true,
},
"operating_system": {
Type: schema.TypeString,
Description: "The VPS OperatingSystem.",
Computed: true,
},
"disk_size": {
Type: schema.TypeInt,
Description: "The VPS disk size in kB.",
Computed: true,
},
"memory_size": {
Type: schema.TypeInt,
Description: "The VPS memory size in kB.",
Computed: true,
},
"cpus": {
Type: schema.TypeInt,
Description: "The VPS cpu count.",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "The VPS status, either 'created', 'installing', 'running', 'stopped' or 'paused'.",
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Description: "The VPS main ipAddress.",
Computed: true,
},
"mac_address": {
Type: schema.TypeString,
Description: "The VPS macaddress.",
Computed: true,
},
"is_locked": {
Type: schema.TypeBool,
Description: "Whether or not another process is already doing stuff with this VPS.",
Computed: true,
},
"is_blocked": {
Type: schema.TypeBool,
Description: "If the VPS is administratively blocked.",
Computed: true,
},
"is_customer_locked": {
Type: schema.TypeBool,
Description: "If this VPS is locked by the customer.",
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Description: "The name of the availability zone the VPS is in.",
Computed: true,
},
"tags": {
Type: schema.TypeList,
Description: "The custom tags added to this VPS.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ipv4_addresses": {
Type: schema.TypeList,
Description: "All IPV4 addresses associated with this VPS.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ipv6_address": {
Type: schema.TypeList,
Description: "All IPV6 addresses associated with this VPS.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceVpsRead(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
client := m.(repository.Client)
repository := vps.Repository{Client: client}
v, err := repository.GetByName(name)
if err != nil {
return fmt.Errorf("failed to lookup vps %q: %s", name, err)
}
ipAddresses, err := repository.GetIPAddresses(name)
if err != nil {
return fmt.Errorf("failed to lookup vps %q: %s", name, err)
}
var ipv4Addresses []string
var ipv6Addresses []string
for _, address := range ipAddresses {
if len(address.Address) == 4 {
ipv4Addresses = append(ipv4Addresses, address.Address.String())
}
if len(address.Address) == 16 {
ipv6Addresses = append(ipv6Addresses, address.Address.String())
}
}
d.SetId(v.Name)
d.Set("description", v.Description)
d.Set("product_name", v.ProductName)
d.Set("operating_system", v.OperatingSystem)
d.Set("disk_size", v.DiskSize)
d.Set("memory_size", v.MemorySize)
d.Set("cpus", v.CPUs)
d.Set("status", v.Status)
d.Set("ip_address", v.IPAddress)
d.Set("mac_address", v.MacAddress)
d.Set("is_locked", v.IsLocked)
d.Set("is_blocked", v.IsBlocked)
d.Set("is_customer_locked", v.IsCustomerLocked)
d.Set("availability_zone", v.AvailabilityZone)
d.Set("tags", v.Tags)
d.Set("ipv4_addresses", ipv4Addresses)
d.Set("ipv6_addresses", ipv6Addresses)
return nil
}