-
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_instances - Upgarde the civogo lib to 0.2.8 BREAKING CHANGE: No Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
465ad8b
commit 40a1654
Showing
5 changed files
with
281 additions
and
1 deletion.
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,159 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/civo/terraform-provider-civo/internal/datalist" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
// Data source to get and filter all instances size | ||
// use to define the size in resourceInstance | ||
func dataSourceInstances() *schema.Resource { | ||
dataListConfig := &datalist.ResourceConfig{ | ||
RecordSchema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"hostname": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"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, | ||
}, | ||
}, | ||
FilterKeys: []string{ | ||
"id", | ||
"hostname", | ||
"public_ip", | ||
"private_ip", | ||
"pseudo_ip", | ||
"size", | ||
"template", | ||
"created_at", | ||
}, | ||
SortKeys: []string{ | ||
"id", | ||
"hostname", | ||
"public_ip", | ||
"private_ip", | ||
"pseudo_ip", | ||
"size", | ||
"template", | ||
"created_at", | ||
}, | ||
ResultAttributeName: "instances", | ||
FlattenRecord: flattenDataSourceInstances, | ||
GetRecords: getDataSourceInstances, | ||
} | ||
|
||
return datalist.NewResource(dataListConfig) | ||
|
||
} | ||
|
||
func getDataSourceInstances(m interface{}) ([]interface{}, error) { | ||
apiClient := m.(*civogo.Client) | ||
|
||
instance := []interface{}{} | ||
partialInstances, err := apiClient.ListInstances(1, 200) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERR] error retrieving sizes: %s", err) | ||
} | ||
|
||
for _, partialInstance := range partialInstances.Items { | ||
instance = append(instance, partialInstance) | ||
} | ||
|
||
return instance, nil | ||
} | ||
|
||
func flattenDataSourceInstances(instance, m interface{}) (map[string]interface{}, error) { | ||
|
||
i := instance.(civogo.Instance) | ||
|
||
flattenedInstance := map[string]interface{}{} | ||
flattenedInstance["id"] = i.ID | ||
flattenedInstance["hostname"] = i.Hostname | ||
flattenedInstance["reverse_dns"] = i.ReverseDNS | ||
flattenedInstance["size"] = i.Size | ||
flattenedInstance["network_id"] = i.NetworkID | ||
flattenedInstance["template"] = i.TemplateID | ||
flattenedInstance["initial_user"] = i.InitialUser | ||
flattenedInstance["notes"] = i.Notes | ||
flattenedInstance["sshkey_id"] = i.SSHKey | ||
flattenedInstance["firewall_id"] = i.FirewallID | ||
flattenedInstance["tags"] = i.Tags | ||
flattenedInstance["script"] = i.Script | ||
flattenedInstance["initial_password"] = i.InitialPassword | ||
flattenedInstance["private_ip"] = i.PublicIP | ||
flattenedInstance["public_ip"] = i.PrivateIP | ||
flattenedInstance["pseudo_ip"] = i.PseudoIP | ||
flattenedInstance["status"] = i.Status | ||
flattenedInstance["created_at"] = i.CreatedAt.UTC().String() | ||
|
||
return flattenedInstance, nil | ||
} |
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
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,118 @@ | ||
--- | ||
layout: "civo" | ||
page_title: "Civo: instances" | ||
sidebar_current: "docs-civo-datasource-instances" | ||
description: |- | ||
Retrieve information on Instances. | ||
--- | ||
|
||
# civo_instances | ||
|
||
Get information on Instances for use in other resources, with the ability to filter and sort the results. | ||
If no filters are specified, all Instances will be returned. | ||
|
||
This data source is useful if the Instances in question are not managed by Terraform or you need to | ||
utilize any of the Instances' data. | ||
|
||
Note: You can use the [`civo_instance`](/docs/providers/civo/d/instance.html) data source to obtain metadata | ||
about a single instance if you already know the `id`, unique `hostname`, or unique `tag` to retrieve. | ||
|
||
## Example Usage | ||
|
||
Use the `filter` block with a `key` string and `values` list to filter images. | ||
|
||
For example to find all instances with size `g2.small`: | ||
|
||
```hcl | ||
data "civo_instances" "small-size" { | ||
filter { | ||
key = "size" | ||
values = [g2.small] | ||
} | ||
} | ||
``` | ||
|
||
You can filter and sort the results as well: | ||
|
||
```hcl | ||
data "digitalocean_droplets" "small-with-backups" { | ||
filter { | ||
key = "size" | ||
values = [g2.small] | ||
} | ||
sort { | ||
key = "created_at" | ||
direction = "desc" | ||
} | ||
} | ||
``` | ||
if you don't know the size you can use the [`civo_instances_size`](/docs/providers/civo/d/instances_size.html) data source to obtain metadata | ||
and use in this way: | ||
|
||
```hcl | ||
data "civo_instances_size" "small" { | ||
filter { | ||
key = "name" | ||
values = ["small"] | ||
} | ||
} | ||
data "digitalocean_droplets" "small-with-backups" { | ||
filter { | ||
key = "size" | ||
values = [data.civo_instances_size.small.sizes[1].name] | ||
} | ||
sort { | ||
key = "created_at" | ||
direction = "desc" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Argument Reference | ||
|
||
* `filter` - (Optional) Filter the results. | ||
The `filter` block is documented below. | ||
|
||
* `sort` - (Optional) Sort the results. | ||
The `sort` block is documented below. | ||
|
||
`filter` supports the following arguments: | ||
|
||
* `key` - (Required) Filter the Instances by this key. This may be one of '`id`, `hostname`, `public_ip`, `private_ip`, | ||
`pseudo_ip`, `size`, `template` or `created_at`. | ||
|
||
* `values` - (Required) A list of values to match against the `key` field. Only retrieves Instances | ||
where the `key` field takes on one or more of the values provided here. | ||
|
||
`sort` supports the following arguments: | ||
|
||
* `key` - (Required) Sort the Droplets by this key. This may be one of `id`, `hostname`, `public_ip`, `private_ip`, | ||
`pseudo_ip`, `size`, `template` or `created_at`. | ||
|
||
* `direction` - (Required) The sort direction. This may be either `asc` or `desc`. | ||
|
||
## Attributes Reference | ||
|
||
* `instances` - A list of Instances satisfying any `filter` and `sort` criteria. Each instance has the following attributes: | ||
|
||
* `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 |