Skip to content

Commit 1f4e58d

Browse files
committed
initial commit
0 parents  commit 1f4e58d

12 files changed

+956
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.iml
2+
vendor

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
Copyright (c) 2016-present Andriy Tarasenko (https://github.com/andrexus)
3+
4+
MIT License
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
terraform-provider-arubacloud
2+
==========================
3+
4+
Terraform provider for Arubacloud
5+
6+
## Description
7+
8+
With this custom terraform provider plugin you can manage your Arubacloud resources.
9+
10+
## Usage
11+
12+
Add plugin binary to your ~/.terraformrc file
13+
```
14+
providers {
15+
arubacloud = "/path/to/your/bin/terraform-provider-arubacloud"
16+
}
17+
```
18+
19+
### Provider Configuration
20+
21+
```
22+
provider "arubacloud" {
23+
username = "${var.arubacloud_username}"
24+
password = "${var.arubacloud_password}"
25+
dc_number = "${var.arubacloud_dc_number}"
26+
}
27+
```
28+
29+
##### Argument Reference
30+
31+
The following arguments are required.
32+
33+
* `username` - username for accessing Arubacloud Control Panel (like AWI-12345).
34+
* `password` - password for accessing Arubacloud Control Panel.
35+
* `dc_number_server` - Number of the datacenter (1-6)
36+
* 1 -> DC1 -> Italy
37+
* 2 -> DC2 -> Italy
38+
* 3 -> DC3 -> Czech Republic
39+
* 4 -> DC4 -> France
40+
* 5 -> DC5 -> Germany
41+
* 6 -> DC6 -> UK
42+
43+
### Resource Configuration
44+
45+
#### `arubacloud_server_smart`
46+
47+
```
48+
resource "arubacloud_server_smart" "smart-server-example" {
49+
smart_size = "MEDIUM"
50+
name = "smart-1"
51+
admin_password = "${var.admin_password}"
52+
os_template_name = "Ubuntu Server 16.04 LTS 64bit"
53+
note = "created with arubacloud terraform provider"
54+
}
55+
56+
output "smart-server-example public ip" {
57+
value = "${arubacloud_server_smart.smart-server-example.public_ip}"
58+
}
59+
```
60+
##### Argument Reference
61+
62+
The following arguments are supported.
63+
64+
* `smart_size` (Required) Smart server size (SMALL, MEDIUM, LARGE, EXTRALARGE)
65+
* `name` (Required) Name of the virtual machine
66+
* `admin_password` (Required) Admin password for accessing a VM
67+
* `os_template_name` (Required) Operating System template name
68+
* `note` (Optional) Free text
69+
70+
`public_ip` is a computed property which could be used in _output_ section
71+
72+
#### `arubacloud_server_pro`
73+
74+
```
75+
resource "arubacloud_server_pro" "pro-server-example-1" {
76+
hypervisor = "Low Cost Hyper-V"
77+
name = "srv-pro-1"
78+
admin_password = "${var.admin_password}"
79+
os_template_name = "CentOS 7.x 64bit"
80+
note = "my first terraformed arubacloud pro server"
81+
cpu_quantity = 4
82+
ram_quantity = 8
83+
virtual_disks = [20, 50, 50]
84+
}
85+
```
86+
87+
##### Argument Reference
88+
89+
The following arguments are supported.
90+
91+
* `hypervisor` (Optional) VM Hypervisor (`VMWare` _(default)_, `Hyper-V`, `Low Cost Hyper-V`)
92+
* `name` (Required) Name of the virtual machine
93+
* `admin_password` (Required) Admin password for accessing a VM
94+
* `os_template_name` (Required) Operating System template name
95+
* `note` (Optional) Free text
96+
* `cpu_quantity` (Optional) Number of Virtual CPU(s)
97+
* `ram_quantity` (Optional) RAM in GB
98+
* `virtual_disks` (Optional) Array of Hard Disks in GB. Default is [10]. You can add up to 4 HDDs
99+
* `purchased_ip_resource_id` (Optional) By default **no public IP is assigned**. You can use `arubacloud_purchased_ip` resource for assigning a public IP. See example below
100+
101+
#### `arubacloud_purchased_ip`
102+
103+
```
104+
resource "arubacloud_purchased_ip" "example" {}
105+
106+
output "ip" {
107+
value = "${arubacloud_purchased_ip.example.ip}"
108+
}
109+
```
110+
##### Argument Reference
111+
112+
All properties are computed
113+
114+
* `ip`
115+
* `subnet_mask`
116+
* `gateway`
117+
* `gateway_ip_v6`
118+
* `prefix_ip_v6`
119+
* `subnet_prefix_ip_v6`
120+
* `start_range_ip_v6`
121+
* `end_range_ip_v6`
122+
* `server_id`
123+
124+
### More examples
125+
126+
Purchase a new IP and assign it to a PRO server:
127+
128+
```
129+
resource "arubacloud_purchased_ip" "ip-srv-pro" {}
130+
131+
resource "arubacloud_server_pro" "cloud-srv-pro" {
132+
name = "srv-pro-terraformed"
133+
admin_password = "${var.admin_password}"
134+
os_template_name = "Ubuntu Server 14.04 LTS 64bit"
135+
virtual_disks = [10, 80, 100]
136+
purchased_ip_resource_id = "${arubacloud_purchased_ip.ip-srv-pro.id}"
137+
}
138+
139+
output "ip cloud-srv-pro" {
140+
value = "${arubacloud_server_pro.cloud-srv-pro.public_ip}"
141+
}
142+
```
143+
144+
145+
## Contribution
146+
147+
Want a new feature? Do it and send a pull request.
148+
149+
150+
## Licence
151+
152+
[MIT License](https://raw.githubusercontent.com/andrexus/terraform-provider-goarubacloud/master/LICENSE.txt)
153+
154+
## Author
155+
156+
[andrexus](https://github.com/andrexus)

arubacloud/config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package arubacloud
2+
3+
import (
4+
"log"
5+
"github.com/andrexus/goarubacloud"
6+
)
7+
8+
type Config struct {
9+
DatacenterRegion int
10+
Username string
11+
Password string
12+
}
13+
14+
// Client() returns a new client for accessing Arubacloud.
15+
func (c *Config) Client() (*goarubacloud.Client, error) {
16+
client := goarubacloud.NewClient(goarubacloud.DataCenterRegion(c.DatacenterRegion), c.Username, c.Password)
17+
18+
log.Printf("[INFO] Arubacloud Client configured for URL: %s", client.BaseURL.String())
19+
20+
return client, nil
21+
}

arubacloud/provider.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package arubacloud
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
"github.com/hashicorp/terraform/terraform"
8+
)
9+
10+
const (
11+
provider_name = "terraform-provider-arubacloud"
12+
provider_version string = "v0.1.0"
13+
)
14+
15+
// Provider returns a schema.Provider for Arubacloud.
16+
func Provider() terraform.ResourceProvider {
17+
return &schema.Provider{
18+
Schema: map[string]*schema.Schema{
19+
"dc_number": &schema.Schema{
20+
Type: schema.TypeInt,
21+
Required: true,
22+
DefaultFunc: schema.EnvDefaultFunc("ARUBACLOUD_REGION", nil),
23+
Description: "The DC number for API operations.",
24+
},
25+
"username": &schema.Schema{
26+
Type: schema.TypeString,
27+
Required: true,
28+
DefaultFunc: schema.EnvDefaultFunc("ARUBACLOUD_USERNAME", nil),
29+
Description: "Username for API operations.",
30+
},
31+
"password": &schema.Schema{
32+
Type: schema.TypeString,
33+
Required: true,
34+
DefaultFunc: schema.EnvDefaultFunc("ARUBACLOUD_PASSWORD", nil),
35+
Description: "Password for API operations.",
36+
},
37+
},
38+
39+
ResourcesMap: map[string]*schema.Resource{
40+
"arubacloud_server_pro": resourceArubacloudServerPro(),
41+
"arubacloud_server_smart": resourceArubacloudServerSmart(),
42+
"arubacloud_purchased_ip": resourceArubacloudPurchasedIP(),
43+
"arubacloud_vlan": resourceArubacloudVLAN(),
44+
},
45+
46+
ConfigureFunc: providerConfigure,
47+
}
48+
}
49+
50+
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
51+
log.Printf("[DEBUG] Configure %s. Version %s", provider_name, provider_version)
52+
config := Config{
53+
DatacenterRegion: d.Get("dc_number").(int),
54+
Username: d.Get("username").(string),
55+
Password: d.Get("password").(string),
56+
}
57+
58+
return config.Client()
59+
}

arubacloud/resource_cloud_server.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package arubacloud
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
"time"
8+
9+
"github.com/andrexus/goarubacloud"
10+
"github.com/hashicorp/terraform/helper/schema"
11+
)
12+
13+
func resourceArubacloudServerCreate(createRequest goarubacloud.CloudServerCreator, d *schema.ResourceData, meta interface{}) error {
14+
client := meta.(*goarubacloud.Client)
15+
16+
log.Printf("[DEBUG] Cloud server create configuration: %#v", createRequest)
17+
18+
cloud_server, _, err := client.CloudServers.Create(createRequest)
19+
20+
if err != nil {
21+
return fmt.Errorf("Error creating cloud server: %s", err)
22+
}
23+
24+
// Assign the cloud_servers id
25+
d.SetId(strconv.Itoa(cloud_server.ServerId))
26+
27+
log.Printf("[INFO] Cloud server ID: %s", d.Id())
28+
29+
err = goarubacloud.WaitForServerCreationDone(client, cloud_server.ServerId)
30+
if err != nil {
31+
return fmt.Errorf(
32+
"Error waiting for server (%s) creation is done. Error: %s", d.Id(), err)
33+
}
34+
35+
return resourceArubacloudServerProRead(d, meta)
36+
}
37+
38+
func resourceArubacloudServerRead(d *schema.ResourceData, meta interface{}) error {
39+
client := meta.(*goarubacloud.Client)
40+
41+
id, err := strconv.Atoi(d.Id())
42+
if err != nil {
43+
return fmt.Errorf("invalid cloud server id: %v", err)
44+
}
45+
46+
// Retrieve the cloud server details for updating the state
47+
cloud_server, _, err := client.CloudServers.Get(id)
48+
if err != nil {
49+
error_response := err.(*goarubacloud.ErrorResponse)
50+
// check if the cloud server no longer exists.
51+
if error_response.ResultCode == 15 { // resultCode 15 means not found
52+
log.Printf("[WARN] Arubacloud server (%s) not found", d.Id())
53+
d.SetId("")
54+
return nil
55+
}
56+
57+
return fmt.Errorf("Error retrieving cloud server: %s", err)
58+
}
59+
60+
d.Set("name", cloud_server.Name)
61+
d.Set("cpu_quantity", cloud_server.CPUQuantity)
62+
d.Set("ram_quantity", cloud_server.RAMQuantity)
63+
d.Set("dc_number", cloud_server.DatacenterId)
64+
d.Set("note", cloud_server.Note)
65+
d.Set("hypervisor", cloud_server.HypervisorType)
66+
67+
setNetworkAdaptersData(d, cloud_server.NetworkAdapters)
68+
69+
public_ip, err := cloud_server.GetPublicIpAddress()
70+
if err == nil {
71+
d.Set("public_ip", public_ip)
72+
}
73+
74+
return nil
75+
}
76+
77+
func resourceArubacloudServerDelete(d *schema.ResourceData, meta interface{}) error {
78+
client := meta.(*goarubacloud.Client)
79+
80+
id, err := strconv.Atoi(d.Id())
81+
if err != nil {
82+
return fmt.Errorf("invalid cloud server id: %v", err)
83+
}
84+
85+
client.CloudServerActions.PowerOff(id)
86+
87+
err = goarubacloud.WaitForServerStatus(client, id, goarubacloud.OFF)
88+
89+
if err != nil {
90+
return fmt.Errorf(
91+
"Error waiting for cloud server to be shut down for destroy (%s): %s", d.Id(), err)
92+
}
93+
94+
log.Printf("[INFO] Deleting cloud server: %s", d.Id())
95+
96+
// Destroy the cloud server
97+
_, err = client.CloudServers.Delete(id)
98+
99+
// Handle remotely destroyed servers
100+
error_response := err.(*goarubacloud.ErrorResponse)
101+
// check if the cloud server no longer exists.
102+
if error_response.ResultCode == 15 { // resultCode 15 means not found
103+
return nil
104+
}
105+
106+
if err != nil {
107+
return fmt.Errorf("Error deleting cloud server: %s", err)
108+
}
109+
110+
// Wait for resources to be released (like assigned purchased IPs)
111+
time.Sleep(30 * time.Second)
112+
113+
return nil
114+
}
115+
116+
func setNetworkAdaptersData(d *schema.ResourceData, network_adapters []goarubacloud.NetworkAdapter) error {
117+
networkAdaptersData := make([]map[string]interface{}, 0, len(network_adapters))
118+
119+
for _, network_adapter := range network_adapters {
120+
if network_adapter.Id != 0 {
121+
networkAdaptersData = append(networkAdaptersData, map[string]interface{}{
122+
"id": network_adapter.Id,
123+
"mac_address": network_adapter.MacAddress,
124+
})
125+
}
126+
}
127+
128+
return d.Set("network_adapters", networkAdaptersData)
129+
}

0 commit comments

Comments
 (0)