Skip to content

Commit

Permalink
add vm desired_status
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalmf committed Aug 17, 2024
1 parent aa0c8c6 commit 64970ad
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 21 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ resource "idcloudhost_vm" "myvm" {
# (optional) assign to floating ip network. if not, vm doesnt have public ip
float_ip_address = idcloudhost_float_ip.myfloatip.address
# add plan will ignored. changing vcpu & ram require desired_status = "stopped"
desired_status = "stopped" # "stopped", "running"
# (optional). if unset will use your user default location
# this field overwrite "default_location"
Expand All @@ -125,7 +128,7 @@ resource "idcloudhost_vm" "myvm" {

## Next Development
- Resource LB Network(Load Balancer)
- Resource VM add desired_status
- Resource VM add desired_status (v1.2.0)
- ✅ Specific resource location (v1.1.0)
- ✅ Support terraform import (v1.1.0)
- ✅ Docs (v1.0.6)
Expand Down
69 changes: 59 additions & 10 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,88 @@
This is my personal idcloudhost terraform provider. It allows managing resources within compute and storage resources.

## Example Usage

### 1. Load providers
```hcl
# 1. Load providers
terraform {
required_providers {
idcloudhost = {
source = "rizalmf/idcloudhost"
version = "=1.0.0"
version = "~> 1.0.0"
}
}
}
```

# 2. Configure the idcloudhost provider
### 2. Configure the idcloudhost provider
```hcl
provider "idcloudhost" {
# you can obtain by create new access as API Token on idcloudhost dashboard
apikey="XXXXXXXXXXXXXXXXX"
## optional. if unset will use your user default location
default_location="jkt01" # jkt01(SouthJKT-a), jkt02(NorthJKT-a), jkt03(WestJKT-a), sgp01(Singapore)
}
```

# 3. Create s3 bucket storage
### 3. Create s3 bucket storage
```hcl
# changable field:
# id = STORAGE NAME
# - billing_account_id
resource "idcloudhost_s3" "mybucket" {
name = "mybucket"
billing_account_id = 000000
}
```

# 4. Create a VPC network
### 4. Create a VPC network
```hcl
# changable field:
# id = PRIVATE NETWORK UUID
# - name
resource "idcloudhost_private_network" "myprivatenetwork" {
# network_uuid = <Computed>
name = "mynetwork"
# (optional). if unset will use your user default location
# this field overwrite "default_location"
# you can not change location on update
location = "jkt01" # jkt01(SouthJKT-a), jkt02(NorthJKT-a), jkt03(WestJKT-a), sgp01(Singapore)
lifecycle {
ignore_changes = [ location ]
}
}
```

# 5. Create Floating IP
### 5. Create Floating IP
```hcl
# changable field:
# id = FLOAT IP ADDRESS
# - name
# - billing_account_id
resource "idcloudhost_float_ip" "myfloatip" {
# address = <Computed>
name = "myfloatnetwork"
billing_account_id = 000000
# (optional). if unset will use your user default location
# this field overwrite "default_location"
# you can not change location on update
location = "jkt01" # jkt01(SouthJKT-a), jkt02(NorthJKT-a), jkt03(WestJKT-a), sgp01(Singapore)
lifecycle {
ignore_changes = [ location ]
}
}
```

# 6. Create vm
### 6. Create vm
```hcl
# changable field:
# id = VM UUID
# - name
# - ram
# - vcpu
Expand All @@ -59,9 +95,9 @@ resource "idcloudhost_vm" "myvm" {
name = "myvm"
billing_account_id = 000000
username = "myusername"
password = "mypassword"
password = "Mypassword1"
os_name = "ubuntu"
os_version = "22.04"
os_version = "22.04-lts"
vcpu = 2
ram = 2048 #mb
disks = 20 #gb
Expand All @@ -71,5 +107,18 @@ resource "idcloudhost_vm" "myvm" {
# (optional) assign to floating ip network. if not, vm doesnt have public ip
float_ip_address = idcloudhost_float_ip.myfloatip.address
# add plan will ignored. changing vcpu & ram require desired_status = "stopped"
desired_status = "stopped" # "stopped", "running"
# (optional). if unset will use your user default location
# this field overwrite "default_location"
# you can not change location on update
location = "jkt01" # jkt01(SouthJKT-a), jkt02(NorthJKT-a), jkt03(WestJKT-a), sgp01(Singapore)
lifecycle {
ignore_changes = [ location, os_name, os_version, username, password, billing_account_id ]
}
}
```
64 changes: 54 additions & 10 deletions provider/resource_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,34 +378,78 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
config := m.(*Config)
apiKey := config.ApiKey
baseUrl := config.BaseUrl
// path := "/v1/user-resource/vm"
defaultLocation := config.DefaultLocation
path := "/user-resource/vm"
version := "/v1"
fullUrl := baseUrl + version + path
location := d.Get("location").(string)
if defaultLocation != "" {
fullUrl = baseUrl + version + "/" + defaultLocation + path
}
if location != "" {
fullUrl = baseUrl + version + "/" + location + path
}
// fullUrl := baseUrl + path

uuid := d.Id()
disks_uuid := d.Get("disks_uuid").(string)
name := d.Get("name").(string)
ram := d.Get("ram").(int)
vcpu := d.Get("vcpu").(int)
disks := d.Get("disks").(int)
// desired_status := d.Get("desired_status").(string)
// float_ip_address := d.Get("float_ip_address").(string)
desired_status := d.Get("desired_status").(string)

if d.HasChange("desired_status") {
unregisteredStatus := true
statusPath := "start"
if desired_status == "stopped" {
unregisteredStatus = false
statusPath = "stop"
}

if desired_status == "running" {
unregisteredStatus = false
}
if desired_status != "" && unregisteredStatus {
return diag.FromErr(fmt.Errorf("unregistered desired_status"))
}
if !unregisteredStatus {
version = "/v1"
path = "/user-resource/vm/" + statusPath
fullUrl = baseUrl + version + path
if defaultLocation != "" {
fullUrl = baseUrl + version + "/" + defaultLocation + path
}
if location != "" {
fullUrl = baseUrl + version + "/" + location + path
}
client := &http.Client{}
form := url.Values{}
form.Add("uuid", uuid)
req, err := http.NewRequest("POST", fullUrl, strings.NewReader(form.Encode()))
if err != nil {
return diag.FromErr(err)
}
req.PostForm = form
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("apikey", apiKey)
resp, err := client.Do(req)

if err != nil {
return diag.FromErr(err)
}

if resp.StatusCode > 299 || resp.StatusCode < 200 {
bodyBytes, _ := io.ReadAll(resp.Body)
return diag.FromErr(fmt.Errorf(string(bodyBytes)))
}
defer resp.Body.Close()
}
}

if d.HasChanges("name", "ram", "vcpu") {
path = "/user-resource/vm"
version = "/v1"
fullUrl = baseUrl + version + path
if defaultLocation != "" {
fullUrl = baseUrl + version + "/" + defaultLocation + path
}
if location != "" {
fullUrl = baseUrl + version + "/" + location + path
}
client := &http.Client{}
form := url.Values{}
form.Add("uuid", uuid)
Expand Down

0 comments on commit 64970ad

Please sign in to comment.