-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DataSource): Added new data source
- Added the new data source civo_instance to get one instance - Rename the file datasource_intances.go for datasource_instances.go BREAKING CHANGE: No Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
40a1654
commit 559594a
Showing
4 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"log" | ||
) | ||
|
||
// Data source to get from the api a specific template | ||
// using the code of the image | ||
func dataSourceInstance() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceInstanceRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
ExactlyOneOf: []string{"id", "hostname"}, | ||
}, | ||
"hostname": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
ExactlyOneOf: []string{"id", "hostname"}, | ||
}, | ||
// computed attributes | ||
"reverse_dns": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"size": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"network_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"template": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"initial_user": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"notes": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sshkey_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"firewall_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": { | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"script": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"initial_password": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"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, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceInstanceRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
var foundImage *civogo.Instance | ||
|
||
if id, ok := d.GetOk("id"); ok { | ||
log.Printf("[INFO] Getting the instance by id") | ||
image, err := apiClient.FindInstance(id.(string)) | ||
if err != nil { | ||
fmt.Errorf("[ERR] failed to retrive instance: %s", err) | ||
return err | ||
} | ||
|
||
foundImage = image | ||
} else if hostname, ok := d.GetOk("hostname"); ok { | ||
log.Printf("[INFO] Getting the instance by hostname") | ||
image, err := apiClient.FindInstance(hostname.(string)) | ||
if err != nil { | ||
fmt.Errorf("[ERR] failed to retrive instance: %s", err) | ||
return err | ||
} | ||
|
||
foundImage = image | ||
} | ||
|
||
d.SetId(foundImage.ID) | ||
d.Set("hostname", foundImage.Hostname) | ||
d.Set("reverse_dns", foundImage.ReverseDNS) | ||
d.Set("size", foundImage.Size) | ||
d.Set("initial_user", foundImage.InitialUser) | ||
d.Set("initial_password", foundImage.InitialPassword) | ||
d.Set("sshkey_id", foundImage.SSHKey) | ||
d.Set("tags", foundImage.Tags) | ||
d.Set("private_ip", foundImage.PrivateIP) | ||
d.Set("public_ip", foundImage.PublicIP) | ||
d.Set("pseudo_ip", foundImage.PseudoIP) | ||
d.Set("status", foundImage.Status) | ||
d.Set("script", foundImage.Script) | ||
d.Set("created_at", foundImage.CreatedAt.UTC().String()) | ||
d.Set("notes", foundImage.Notes) | ||
|
||
return nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
layout: "civo" | ||
page_title: "DigitalOcean: civo_instance" | ||
sidebar_current: "docs-civo-datasource-instance" | ||
description: |- | ||
Get information on a Instance. | ||
--- | ||
|
||
# civo_instance | ||
|
||
Get information on a Instance for use in other resources. This data source provides | ||
all of the Instance's properties as configured on your Civo account. This | ||
is useful if the Instance in question is not managed by Terraform or you need to | ||
utilize any of the Instance's data. | ||
|
||
**Note:** This data source returns a single Instance. When specifying a `hostname`, an | ||
error is triggered if more than one Instance is found. | ||
|
||
## Example Usage | ||
|
||
Get the Instance by hostname: | ||
|
||
```hcl | ||
data "civo_instance" "myhostaname" { | ||
hostname = "myhostname.com" | ||
} | ||
output "instance_output" { | ||
value = data.civo_instance.myhostaname.public_ip | ||
} | ||
``` | ||
|
||
Get the Droplet by id: | ||
|
||
```hcl | ||
data "civo_instance" "myhostaname" { | ||
id = "6f283ab7-c37e-42f9-9b4b-f80aea8c006d" | ||
} | ||
``` | ||
## Argument Reference | ||
|
||
One of following the arguments must be provided: | ||
|
||
* `id` - (Optional) The ID of the Instance | ||
* `hostname` - (Optional) The hostname of the Instance. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The ID of the Droplet. | ||
* `hostname` - The Instance hostname. | ||
* `reverse_dns` - A fully qualified domain name. | ||
* `size` - The name of the size. | ||
* `public_ip_requiered` - This should be either false, true or `move_ip_from:intances_id`. | ||
* `network_id` - This will be the ID of the network. | ||
* `template` - The ID for the template to used to build the instance. | ||
* `initial_user` - The name of the initial user created on the server. | ||
* `notes` - The notes of the instance. | ||
* `sshkey_id` - The ID SSH. | ||
* `firewall_id` - The ID of the firewall used. | ||
* `tags` - An optional list of tags | ||
* `initial_password` - Instance initial password | ||
* `private_ip` - The private ip. | ||
* `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 |