Skip to content

Commit

Permalink
- Remove tags from the instance creation for now
Browse files Browse the repository at this point in the history
- Add resource_network.go and resource_volume.go

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jan 24, 2020
1 parent 47fc803 commit 935ec41
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/civo/terraform-provider-civo

require (
github.com/aws/aws-sdk-go v1.25.39
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8
github.com/civo/civogo v0.0.0-20200123124629-f66cbcbb6f2d
github.com/hashicorp/terraform-plugin-sdk v1.3.0
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4 h1:bRX4UokNiGQxrB2uP/M
github.com/civo/civogo v0.0.0-20200116211328-a6d635388fa4/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8 h1:v6uezjqyqgivBSCcPH9R9oC1XgyRK11WoHYYPX//x9Y=
github.com/civo/civogo v0.0.0-20200121103424-a69bef1d9ea8/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/civo/civogo v0.0.0-20200123124629-f66cbcbb6f2d h1:uEPSnpfTL+S7OpW2p69TZ6+2Jo5dDD3WEgUhHbpye84=
github.com/civo/civogo v0.0.0-20200123124629-f66cbcbb6f2d/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
"civo_volume": resourceVolume(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
8 changes: 4 additions & 4 deletions provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
config.SSHKeyID = attr.(string)
}

if attr, ok := d.GetOk("tags"); ok {
//config.Tags = attr.(*schema.Set).List()
config.Tags = attr.(string)
}
//if attr, ok := d.GetOk("tags"); ok {
// //config.Tags = attr.(*schema.Set).List()
// config.Tags = attr.(string)
//}

instance, err := apiClient.CreateInstance(config)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions provider/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func resourceNetworkUpdate(d *schema.ResourceData, m interface{}) error {
if d.HasChange("label") {
config := &civogo.NetworkConfig{Label: d.Get("label").(string)}

//_, err := apiClient.RenameNetwork(config, d.Id())
_, err := apiClient.RenameNetwork(config)
_, err := apiClient.RenameNetwork(config, d.Id())
if err != nil {
log.Printf("[WARN] An error occurred while rename the network (%s)", d.Id())
}
Expand Down
186 changes: 186 additions & 0 deletions provider/resource_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package provider

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"time"
)

func resourceVolume() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name for the volume",
ValidateFunc: validateName,
},
"size_gb": {
Type: schema.TypeInt,
Required: true,
Description: "Size for the volume in GB",
},
"bootable": {
Type: schema.TypeBool,
Required: true,
Description: "Mark like bootable this volume",
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
Description: "Attach to a instance",
},
// Computed resource
"mount_point": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceVolumeCreate,
Read: resourceVolumeRead,
Update: resourceVolumeUpdate,
Delete: resourceVolumeDelete,
//Exists: resourceExistsItem,
//Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
//},
}
}

func resourceVolumeCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

config := &civogo.VolumeConfig{Name: d.Get("name").(string), SizeGB: d.Get("size_gb").(int), Bootable: d.Get("bootable").(bool)}

volume, err := apiClient.NewVolume(config)
if err != nil {
return fmt.Errorf("failed to create a new config: %s", err)
}

d.SetId(volume.ID)

if d.Get("instance_id").(string) != "" {
_, err := apiClient.AttachVolume(d.Id(), d.Get("instance_id").(string))
if err != nil {
log.Printf("[WARN] An error occurred while trying to attach the volume (%s)", d.Id())
}
}

return resourceNetworkRead(d, m)
}

func resourceVolumeRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

CurrentVolume := civogo.Volume{}

resp, err := apiClient.ListVolumes()
if err != nil {
return fmt.Errorf("failed to create a new config: %s", err)
}

for _, volume := range resp {
if volume.ID == d.Id() {
CurrentVolume = volume
}
}

d.Set("name", CurrentVolume.Name)
d.Set("size_gb", CurrentVolume.SizeGB)
d.Set("bootable", CurrentVolume.Bootable)
d.Set("instance_id", CurrentVolume.InstanceID)
d.Set("mount_point", CurrentVolume.MountPoint)
d.Set("created_at", CurrentVolume.CreatedAt)

return nil
}

func resourceVolumeUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

if d.HasChange("instance_id") {
if d.Get("instance_id").(string) != "" {
_, err := apiClient.AttachVolume(d.Id(), d.Get("instance_id").(string))
if err != nil {
log.Printf("[WARN] An error occurred while trying to attach the volume (%s)", d.Id())
}

return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
CurrentVolume := civogo.Volume{}

resp, err := apiClient.ListVolumes()
if err != nil {
return resource.NonRetryableError(fmt.Errorf("failed to get all volume: %s", err))
}

for _, volume := range resp {
if volume.ID == d.Id() {
CurrentVolume = volume
}
}

if CurrentVolume.MountPoint != "" {
return resource.RetryableError(fmt.Errorf("expected volume to be mount but was in state %s", CurrentVolume.MountPoint))
}

return resource.NonRetryableError(resourceVolumeRead(d, m))
})
}

if d.Get("instance_id").(string) == "" {
_, err := apiClient.DetachVolume(d.Id())
if err != nil {
log.Printf("[WARN] An error occurred while trying to detach volume (%s)", d.Id())
}

return resourceVolumeRead(d, m)
}

}

if d.HasChange("size_gb") {
if d.Get("instance_id").(string) != "" {
_, err := apiClient.DetachVolume(d.Id())
if err != nil {
log.Printf("[WARN] An error occurred while trying to detach volume (%s)", d.Id())
}

time.Sleep(10 * time.Second)

newSize := d.Get("size_gb").(int)
_, err = apiClient.ResizeVolume(d.Id(), newSize)
if err != nil {
log.Printf("[INFO] Civo volume (%s) size not change", d.Id())
}

time.Sleep(2 * time.Second)

_, err = apiClient.AttachVolume(d.Id(), d.Get("instance_id").(string))
if err != nil {
log.Printf("[WARN] An error occurred while trying to attach the volume (%s)", d.Id())
}

}

}

return resourceVolumeRead(d, m)
}

func resourceVolumeDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

_, err := apiClient.DeleteVolume(d.Id())
if err != nil {
log.Printf("[INFO] Civo volume (%s) was delete", d.Id())
}
return nil
}

0 comments on commit 935ec41

Please sign in to comment.