Skip to content

Commit

Permalink
- Add the instances red function
Browse files Browse the repository at this point in the history
- Add all possible option to the resource

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jan 17, 2020
1 parent e018fda commit db611b6
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Terraform Provider ![Travis build](https://travis-ci.org/civo/terraform-provider
Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.10.x
- [Go](https://golang.org/doc/install) 1.11 (to build the provider plugin)
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
- [Go](https://golang.org/doc/install) 1.13 (to build the provider plugin)

Building The Provider
---------------------
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/cli v1.0.0 h1:iGBIsUe3+HZ/AD/Vd7DErOt5sU9fa8Uj7A2s1aggv1Y=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
Expand Down
152 changes: 140 additions & 12 deletions provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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"
"regexp"
)

Expand All @@ -27,23 +29,79 @@ func resourceInstance() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
"hostname": {
Type: schema.TypeString,
Required: true,
Description: "The name of the resource, also acts as it's unique ID",
Description: "A fully qualified domain name that should be set as the instance's hostname (required)",
ForceNew: true,
ValidateFunc: validateName,
},
"description": {
"reverse_dns": {
Type: schema.TypeString,
Required: true,
Description: "A description of an item",
Optional: true,
Description: "A fully qualified domain name that should be used as the instance's IP's reverse DNS (optional, uses the hostname if unspecified)",
},
"size": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the size, from the current list, e.g. g2.small (required)",
},
"public_ip_requiered": {
Type: schema.TypeString,
Optional: true,
Description: "This should be either none, create",
},
"network_id": {
Type: schema.TypeString,
Optional: true,
Description: "This must be the ID of the network from the network listing (optional; default network used when not specified)",
},
"template": {
Type: schema.TypeString,
Optional: true,
Description: "The ID for the template to use to build the instance",
},
"initial_user": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the initial user created on the server (optional; this will default to the template's default_username and fallback to civo)",
},
"sshkey_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of an already uploaded SSH public key to use for login to the default user (optional; if one isn't provided a random password will be set and returned in the initial_password field)",
},
"tags": {
Type: schema.TypeSet,
Type: schema.TypeString,
Optional: true,
Description: "An optional list of tags, represented as a key, value pair",
Elem: &schema.Schema{Type: schema.TypeString},
Description: "An optional tags",
},
//"tags": {
// Type: schema.TypeSet,
// Optional: true,
// Description: "An optional list of tags, represented as a key, value pair",
// Elem: &schema.Schema{Type: schema.TypeString},
//},
// Computed resource
"private_ip": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
},
"pseudo_ip": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceInstanceCreate,
Expand All @@ -66,19 +124,89 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
return err
}

config.Hostname = d.Get("name").(string)
config.Hostname = d.Get("hostname").(string)

if attr, ok := d.GetOk("reverse_dns"); ok {
config.ReverseDNS = attr.(string)
}

if attr, ok := d.GetOk("size"); ok {
config.Size = attr.(string)
}

if attr, ok := d.GetOk("size"); ok {
config.Size = attr.(string)
}

if attr, ok := d.GetOk("network_id"); ok {
config.NetworkID = attr.(string)
}

if attr, ok := d.GetOk("template"); ok {
config.TemplateID = attr.(string)
}

if attr, ok := d.GetOk("initial_user"); ok {
config.InitialUser = attr.(string)
}

if attr, ok := d.GetOk("sshkey_id"); ok {
config.SSHKeyID = attr.(string)
}

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

_, err = apiClient.CreateInstance(config)
instance, err := apiClient.CreateInstance(config)
if err != nil {
fmt.Errorf("failed to create instance: %s", err)
return err
}

d.SetId(config.Hostname)
return nil
d.SetId(instance.ID)

return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
resp, err := apiClient.GetInstance(instance.ID)

if err != nil {
return resource.NonRetryableError(fmt.Errorf("error geting instance: %s", err))
}

if resp.Status != "ACTIVE" {
return resource.RetryableError(fmt.Errorf("expected instance to be created but was in state %s", resp.Status))
}

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

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

resp, err := apiClient.GetInstance(d.Id())
if err != nil {
// check if the droplet no longer exists.
log.Printf("[WARN] Civo instance (%s) not found", d.Id())
d.SetId("")
return nil
}

d.Set("hostname", resp.Hostname)
d.Set("reverse_dns", resp.ReverseDNS)
d.Set("size", resp.Size)
d.Set("network_id", resp.NetworkID)
d.Set("template", resp.TemplateID)
d.Set("initial_user", resp.InitialUser)
d.Set("sshkey_id", resp.SSHKey)
d.Set("tags", resp.Tags)
d.Set("private_ip", resp.PrivateIP)
d.Set("public_ip", resp.PublicIP)
d.Set("pseudo_ip", resp.PseudoIP)
d.Set("status", resp.Status)
d.Set("created_at", resp.CreatedAt)

return nil
}

Expand Down

0 comments on commit db611b6

Please sign in to comment.