Skip to content

Commit

Permalink
feat: Add templates module
Browse files Browse the repository at this point in the history
- Can handler the template
- Add data source civo_template
- Add new feature to the instances, can import now one instance, fixx error to add notes to a instance
- Fix error message in the resource_loadbalance
- Update to civogo v0.2.2

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Mar 31, 2020
1 parent 04c84fe commit 79ff73a
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 12 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ $ make testacc TESTARGS='-run=TestAccCivoDomain_Basic'

For information about writting acceptance tests, see the main Terraform [contributing guide](https://github.com/hashicorp/terraform/blob/master/.github/CONTRIBUTING.md#writing-acceptance-tests).

Progress
Progress resource
----------------------
-~~Provider~~
-~~Instances~~
Expand All @@ -83,10 +83,15 @@ Progress
-~~SSH keys~~
-~~Domain names~~
-~~Domain records~~
-~~Templates~~
-~~Snapshots~~
- Regions
- Quotas
- Sizes
- Kubernetes Clusters
- Kubernetes Applications
- Snapshots
- Templates

Progress data source
----------------------
-~~Templates~~
89 changes: 89 additions & 0 deletions civo/datasource_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package civo

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

func dataSourceTemplate() *schema.Resource {
return &schema.Resource{
Read: dataSourceTemplateRead,
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "code of the image",
ValidateFunc: validation.NoZeroValues,
},
// computed attributes
"name": {
Type: schema.TypeString,
Computed: true,
Description: "name of the image",
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
Description: "volume_id of the image",
},
"image_id": {
Type: schema.TypeString,
Computed: true,
Description: "image_id of the image",
},
"short_description": {
Type: schema.TypeString,
Computed: true,
Description: "short_description of the image",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "description of the image",
},
"default_username": {
Type: schema.TypeString,
Computed: true,
Description: "default_username of the image",
},
"cloud_config": {
Type: schema.TypeString,
Computed: true,
Description: "cloud_config of the image",
},
},
}
}

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

code, hasCode := d.GetOk("code")

if !hasCode {
return fmt.Errorf("`code` must be assigned")
}

if hasCode {
image, err := apiClient.GetTemplateByCode(code.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive template: %s", err)
return err
}

d.SetId(image.ID)
d.Set("code", image.Code)
d.Set("name", image.Name)
d.Set("volume_id", image.VolumeID)
d.Set("image_id", image.ImageID)
d.Set("short_description", image.ShortDescription)
d.Set("description", image.Description)
d.Set("default_username", image.DefaultUsername)
d.Set("cloud_config", image.CloudConfig)
}

return nil
}
4 changes: 4 additions & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("CIVO_TOKEN", ""),
},
},
DataSourcesMap: map[string]*schema.Resource{
"civo_template": dataSourceTemplate(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
Expand All @@ -25,6 +28,7 @@ func Provider() terraform.ResourceProvider {
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
"civo_ssh_key": resourceSSHKey(),
"civo_template": resourceTemplate(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
35 changes: 27 additions & 8 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func resourceInstance() *schema.Resource {
"public_ip_requiered": {
Type: schema.TypeString,
Optional: true,
Description: "This should be either none, create",
Description: "This should be either false, true or `move_ip_from:intances_id`",
},
"network_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -73,6 +73,10 @@ func resourceInstance() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
// Computed resource
"initial_password": {
Type: schema.TypeString,
Computed: true,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -98,10 +102,9 @@ func resourceInstance() *schema.Resource {
Read: resourceInstanceRead,
Update: resourceInstanceUpdate,
Delete: resourceInstanceDelete,
//Exists: resourceExistsItem,
//Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
//},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

Expand Down Expand Up @@ -169,12 +172,28 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
if resp.Status != "ACTIVE" {
return resource.RetryableError(fmt.Errorf("[WARN] expected instance to be created but was in state %s", resp.Status))
} else {
/*
Once the instance is created, we check if the object firewall_id,
if it is, then we set the firewall id to the instances
*/
if attr, ok := d.GetOk("firewall_id"); ok {
_, errInstance := apiClient.SetInstanceFirewall(instance.ID, attr.(string))
if errInstance != nil {
return resource.NonRetryableError(fmt.Errorf("[WARN] failed to set firewall to the instance: %s", errInstance))
}
}

/*
Once the instance is created, we check if the object notes,
if it is, then we add the note to the instances
*/
if attr, ok := d.GetOk("notes"); ok {
resp.Notes = attr.(string)
_, errInstance := apiClient.UpdateInstance(resp)
if errInstance != nil {
return resource.NonRetryableError(fmt.Errorf("[WARN] failed to set note to the instance: %s", errInstance))
}
}
}

return resource.NonRetryableError(resourceInstanceRead(d, m))
Expand All @@ -186,7 +205,7 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {

resp, err := apiClient.GetInstance(d.Id())
if err != nil {
// check if the droplet no longer exists.
// check if the instance no longer exists.
log.Printf("[WARN] civo instance (%s) not found", d.Id())
d.SetId("")
return nil
Expand All @@ -196,6 +215,7 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {
d.Set("reverse_dns", resp.ReverseDNS)
d.Set("size", resp.Size)
d.Set("initial_user", resp.InitialUser)
d.Set("initial_password", resp.InitialPassword)
d.Set("sshkey_id", resp.SSHKey)
d.Set("tags", resp.Tags)
d.Set("private_ip", resp.PrivateIP)
Expand Down Expand Up @@ -243,7 +263,6 @@ func resourceInstanceUpdate(d *schema.ResourceData, m interface{}) error {

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

}

if d.HasChange("notes") {
Expand Down Expand Up @@ -307,7 +326,7 @@ func resourceInstanceDelete(d *schema.ResourceData, m interface{}) error {

_, err := apiClient.DeleteInstance(d.Id())
if err != nil {
log.Printf("[INFO] Civo instance (%s) was delete", d.Id())
log.Printf("[INFO] civo instance (%s) was delete", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion civo/resource_loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func resourceLoadBalancerUpdate(d *schema.ResourceData, m interface{}) error {

_, err := apiClient.UpdateLoadBalancer(d.Id(), conf)
if err != nil {
fmt.Errorf("[ERR] failed to update load balancer: %s", err)
fmt.Errorf("[WARN] failed to update load balancer: %s", err)
return err
}

Expand Down
Loading

0 comments on commit 79ff73a

Please sign in to comment.