Skip to content

Commit

Permalink
Getting the status implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
vanarajaz committed Jan 21, 2022
1 parent 59c4b11 commit b1f48eb
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 29 deletions.
6 changes: 6 additions & 0 deletions api/instance_api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var (
instanceList = "instance/instanceList"
instanceAttachIso = "instance/attachIso"
instanceDetachIso = "instance/detachIso"
instanceStatus = "instance/vmStatus"
)

// Instance create api
Expand Down Expand Up @@ -56,3 +57,8 @@ func GetInstanceAttachIsoApi(uuid string, isoUUid string) string {
func GetInstanceDetachIsoApi(uuid string, isoUUid string) string {
return EndPoint + "/" + instanceDetachIso + "?uuid=" + uuid + "&isoUuid=" + isoUUid
}

// Instance status api
func GetInstanceStatusApi(uuid string) string {
return EndPoint + "/" + instanceStatus + "?uuid=" + uuid
}
52 changes: 26 additions & 26 deletions examples/instance/instance.tf
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

data "stackbill_compute_offering_list" "all" {
zone_uuid = "74b12720-73ce-49b6-857f-48cdac6dcd3f"
uuid = "c674ac49-32cd-4aae-96f8-25458bded6ad"
}
# data "stackbill_compute_offering_list" "all" {
# zone_uuid = "74b12720-73ce-49b6-857f-48cdac6dcd3f"
# uuid = "c674ac49-32cd-4aae-96f8-25458bded6ad"
# }

# Returns all coffees
output "all_compute_offerings" {
value = data.stackbill_compute_offering_list.all.computeofferings
}
# # Returns all coffees
# output "all_compute_offerings" {
# value = data.stackbill_compute_offering_list.all.computeofferings
# }

resource "stackbill_instance" "my-server" {
compute_offering_uuid = "2f7b1b02-078f-42fb-aece-aa21bfa89e1a"
cpu_core = "3"
# disk_size = 0
hypervisor_name = "string"
# memory = "0"
name = "AzTestingVmFour"
network_uuid = "f215bcd2-8109-484d-91b3-20fc1cc8293b"
# security_group_name = "string"
# ssh_key_name = "string"
# storage_offering_uuid = "string"
template_uuid = "e39172b4-715b-44ac-ada2-7f97f733eecc"
zone_uuid = "74b12720-73ce-49b6-857f-48cdac6dcd3f"
}
# resource "stackbill_instance" "my-server" {
# compute_offering_uuid = "2f7b1b02-078f-42fb-aece-aa21bfa89e1a"
# cpu_core = "3"
# # disk_size = 0
# hypervisor_name = "string"
# # memory = "0"
# name = "AzTestingVmFour"
# network_uuid = "0c7cc302-abe5-4687-8351-26aba55b3d0d"
# # security_group_name = "string"
# # ssh_key_name = "string"
# # storage_offering_uuid = "string"
# template_uuid = "e39172b4-715b-44ac-ada2-7f97f733eecc"
# zone_uuid = "74b12720-73ce-49b6-857f-48cdac6dcd3f"
# }

# resource "stackbill_instance_update_name" "update_name" {
# uuid = "a82c041a-b660-4446-b570-0b387cc95a4c"
Expand All @@ -36,7 +36,7 @@ resource "stackbill_instance" "my-server" {
# memory = "0"
# }

# resource "stackbill_instance_actions" "actions" {
# uuid = "a82c041a-b660-4446-b570-0b387cc95a4c"
# action = "Stop"
# }
resource "stackbill_instance_actions" "actions" {
uuid = "37dd31ca-928f-4a9b-9d2e-6e9fb433bf23"
action = "Stop"
}
25 changes: 22 additions & 3 deletions examples/instance/terraform.tfstate.backup
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
{
"version": 4,
"terraform_version": "1.1.2",
"serial": 13,
"lineage": "eefdd319-0035-01c2-b9c0-6a692ef81e70",
"serial": 1,
"lineage": "cf6e77c3-dc67-c74e-e4d9-4e60b865ea94",
"outputs": {},
"resources": []
"resources": [
{
"mode": "managed",
"type": "stackbill_instance_actions",
"name": "actions",
"provider": "provider[\"stackbill.com/assistanz/stackbill\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"action": "Start",
"id": "37dd31ca-928f-4a9b-9d2e-6e9fb433bf23",
"uuid": "37dd31ca-928f-4a9b-9d2e-6e9fb433bf23"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
]
}
35 changes: 35 additions & 0 deletions instance/instance_actions_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package instance
import (
"context"
"log"
"strings"
"terraform-provider-stackbill/utils"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -41,6 +43,23 @@ func (vs *instanceActionsResource) Create(ctx context.Context, d *schema.Resourc
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if action == START && status == "RUNNING" {
response = strings.Replace(response, "STOPPED", "RUNNING", -1)
break
}
if action == STOP && status == "STOPPED" {
response = strings.Replace(response, "RUNNING", "STOPPED", -1)
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance " + action + " completed...!")
Expand Down Expand Up @@ -74,6 +93,22 @@ func (vs *instanceActionsResource) Update(ctx context.Context, d *schema.Resourc
if err != nil {
return diag.FromErr(err)
}
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if action == START && status == "RUNNING" {
response = strings.Replace(response, "STOPPED", "RUNNING", -1)
break
}
if action == STOP && status == "STOPPED" {
response = strings.Replace(response, "RUNNING", "STOPPED", -1)
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance " + action + " completed...!")
Expand Down
22 changes: 22 additions & 0 deletions instance/instance_api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package instance

import (
"encoding/json"
"errors"
"terraform-provider-stackbill/api"
"terraform-provider-stackbill/auth"
Expand All @@ -21,6 +22,7 @@ type InstanceApi interface {
InstanceIsoActions(map[string]interface{}, interface{}) (string, error)
ListInstances(string, string, interface{}) (string, error)
DeleteInstance(string, interface{}) (string, error)
GetInstanceStatus(string, interface{}) (string, error)
}

//Instance utils Object
Expand Down Expand Up @@ -168,3 +170,23 @@ func (vs *instanceApi) ListInstances(zoneUuid string, uuid string, meta interfac
}
return response, nil
}

// Resize VM
// TODO - Documentation
func (vs *instanceApi) GetInstanceStatus(uuid string, meta interface{}) (string, error) {
// Meta information
m := meta.(*auth.AuthKeys)
apiKey := m.ApiKey
secretKey := m.SecretKey
endPoint := api.GetInstanceStatusApi(uuid)
response, err := httpClient.Get(endPoint, apiKey, secretKey)
if err != nil {
return "", err
}
var resJson map[string]interface{}
if err := json.Unmarshal([]byte(response), &resJson); err != nil {
return "", err
}
status := resJson["status"].(string)
return status, nil
}
25 changes: 25 additions & 0 deletions instance/instance_iso_actions_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"terraform-provider-stackbill/utils"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -41,6 +42,18 @@ func (ac *instanceIsoActionsResource) Create(ctx context.Context, d *schema.Reso
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance iso " + action + " completed...!")
Expand Down Expand Up @@ -74,6 +87,18 @@ func (vs *instanceIsoActionsResource) Update(ctx context.Context, d *schema.Reso
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance iso " + action + " completed...!")
Expand Down
25 changes: 25 additions & 0 deletions instance/instance_reset_sshkey_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"terraform-provider-stackbill/utils"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -50,6 +51,18 @@ func (irs *instanceResetSshkeyResource) Create(ctx context.Context, d *schema.Re
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance Reset sshkey successful...!")
Expand Down Expand Up @@ -80,6 +93,18 @@ func (irs *instanceResetSshkeyResource) Update(ctx context.Context, d *schema.Re
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance Reset sshkey successful...!")
Expand Down
25 changes: 25 additions & 0 deletions instance/instance_resize_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"
"terraform-provider-stackbill/utils"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -50,6 +51,18 @@ func (ir *instanceResizeResource) Create(ctx context.Context, d *schema.Resource
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance resize successful...!")
Expand Down Expand Up @@ -80,6 +93,18 @@ func (ir *instanceResizeResource) Update(ctx context.Context, d *schema.Resource
if err != nil {
return diag.FromErr(err)
}
// Wait to start or stop the instance
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
output := utils.FormatJsonString(response)
log.Println(output)
log.Println("Instance resize successful...!")
Expand Down
13 changes: 13 additions & 0 deletions instance/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"log"
"terraform-provider-stackbill/utils"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -51,6 +52,18 @@ func (vs *instanceResource) Create(ctx context.Context, d *schema.ResourceData,
uuid := instanceObj["uuid"].(string)
// Update the state
d.SetId(uuid)
// Check the status
time.Sleep(20 * time.Second)
for {
status, err := instanceApiObj.GetInstanceStatus(uuid, meta)
if err != nil {
log.Println(err.Error())
}
if status == "RUNNING" {
break
}
time.Sleep(10 * time.Second)
}
}
output := utils.FormatJsonString(response)
log.Println(output)
Expand Down
Binary file modified terraform-provider-stackbill
Binary file not shown.

0 comments on commit b1f48eb

Please sign in to comment.