Skip to content

Commit

Permalink
feat(DataSource): Added new data source
Browse files Browse the repository at this point in the history
- 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
alejandrojnm committed May 17, 2020
1 parent 465ad8b commit 40a1654
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 1 deletion.
159 changes: 159 additions & 0 deletions civo/datasource_intances.go
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
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Provider() terraform.ResourceProvider {
"civo_template": dataSourceTemplate(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_instances_size": dataSourceInstancesSize(),
"civo_instances": dataSourceInstances(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/civo/terraform-provider-civo
require (
cloud.google.com/go v0.54.0 // indirect
github.com/aws/aws-sdk-go v1.29.22 // indirect
github.com/civo/civogo v0.2.5
github.com/civo/civogo v0.2.8
github.com/fatih/color v1.9.0 // indirect
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/hashicorp/go-getter v1.4.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/civo/civogo v0.2.4 h1:V7LLe7aPfQs+XZ4xq+6TOnf9CU7Y9iH3jgfma1WkcFk=
github.com/civo/civogo v0.2.4/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.5 h1:HOObmjirSPoTwJDsZWQeKnEJE00OBvKrC1wjqE4ZhNs=
github.com/civo/civogo v0.2.5/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.8 h1:eKh1UchZagznUc5WibnzMrVfjXG2db8TqFqWUnGruvI=
github.com/civo/civogo v0.2.8/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
118 changes: 118 additions & 0 deletions website/docs/d/instances.html.markdown
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

0 comments on commit 40a1654

Please sign in to comment.