Skip to content

Commit

Permalink
Added support to add script
Browse files Browse the repository at this point in the history
- Added support to add script at the moment of create the instances
- Update the documentation of the instance
  • Loading branch information
alejandrojnm committed May 5, 2020
2 parents 197425b + 08dad15 commit 807f086
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
"strings"
)
Expand Down Expand Up @@ -74,6 +75,13 @@ func resourceInstance() *schema.Resource {
Description: "An optional list of tags, represented as a key, value pair",
Elem: &schema.Schema{Type: schema.TypeString},
},
"script": {
Type: schema.TypeString,
Optional: true,
Description: "the contents of a script that will be uploaded to /usr/local/bin/civo-user-init-script on your instance, " +
"read/write/executable only by root and then will be executed at the end of the cloud initialization",
ValidateFunc: validation.StringIsNotEmpty,
},
// Computed resource
"initial_password": {
Type: schema.TypeString,
Expand Down Expand Up @@ -151,6 +159,10 @@ func resourceInstanceCreate(d *schema.ResourceData, m interface{}) error {
config.SSHKeyID = attr.(string)
}

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

tfTags := d.Get("tags").(*schema.Set).List()
tags := make([]string, len(tfTags))
for i, tfTag := range tfTags {
Expand Down Expand Up @@ -230,6 +242,7 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {
d.Set("public_ip", resp.PublicIP)
d.Set("pseudo_ip", resp.PseudoIP)
d.Set("status", resp.Status)
d.Set("script", resp.Script)
d.Set("created_at", resp.CreatedAt.UTC().String())
d.Set("notes", resp.Notes)

Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ The following arguments are supported:
* `hostname` - (Required) The Instance hostname.
* `reverse_dns` - (Optional) A fully qualified domain name that should be used as the instance's IP's reverse DNS (optional, uses the hostname if unspecified).
* `size` - (Optional) The name of the size, from the current list, e.g. g2.small (required).
* `public_ip_requiered` - (Optional) This should be either false, true or `move_ip_from:intances_id`.
* `public_ip` - (Optional) This should be either false, true or `move_ip_from:intances_id`.
* `network_id` - (Optional) This must be the ID of the network from the network listing (optional; default network used when not specified).
* `template` - (Optional) The ID for the template to use to build the instance.
* `initial_user` - (Optional) The name of the initial user created on the server (optional; this will default to the template's default_username and fallback to civo).
* `notes` - (Optional) Add some notes to the instance.
* `sshkey_id` - (Optional) 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).
* `firewall_id` - (Optional) The ID of the firewall to use, from the current list. If left blank or not sent, the default firewall will be used (open to all).
* `script` - (Optional) the contents of a script that will be uploaded to /usr/local/bin/civo-user-init-script on your instance, read/write/executable only by root and then will be executed at the end of the cloud initialization
* `tags` - (Optional) An optional list of tags, represented as a key, value pair.

## Attributes Reference
Expand All @@ -60,6 +61,7 @@ The following attributes are exported:
* `public_ip` - The public ip.
* `pseudo_ip` - Is the ip that is used to route the public ip from the internet to the instance using NAT
* `status` - The status of the instance
* `script` - the contents of a script uploaded
* `created_at` - The date of creation of the instance

## Import
Expand Down

0 comments on commit 807f086

Please sign in to comment.