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 volume to get one volume, by name or by id
- Fix error in the doc for the instance creation

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 19, 2020
1 parent 69cb929 commit c97d499
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
83 changes: 83 additions & 0 deletions civo/datasource_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 domain
// using the id or the name of the domain
func dataSourceVolume() *schema.Resource {
return &schema.Resource{
Read: dataSourceVolumeRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
},
// Computed resource
"size_gb": {
Type: schema.TypeInt,
Computed: true,
},
"bootable": {
Type: schema.TypeBool,
Computed: true,
},
"mount_point": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

var foundVolume *civogo.Volume

if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the volume by id")
volume, err := apiClient.FindVolume(id.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive volume: %s", err)
return err
}

foundVolume = volume
} else if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the volume by name")
volume, err := apiClient.FindVolume(name.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive volume: %s", err)
return err
}

foundVolume = volume
}

d.SetId(foundVolume.ID)
d.Set("name", foundVolume.Name)
d.Set("size_gb", foundVolume.SizeGigabytes)
d.Set("bootable", foundVolume.Bootable)
d.Set("mount_point", foundVolume.MountPoint)
d.Set("created_at", foundVolume.CreatedAt.UTC().String())

return nil
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
"civo_dns_domain_name": dataSourceDnsDomainName(),
"civo_dns_domain_record": dataSourceDnsDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
65 changes: 65 additions & 0 deletions website/docs/d/volume.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
layout: "civo"
page_title: "Civo: civo_volume"
sidebar_current: "docs-civo-datasource-volume"
description: |-
Get information on a volume.
---

# civo_volume

Get information on a volume for use in other resources. This data source provides
all of the volumes properties as configured on your Civo account. This is
useful if the volume in question is not managed by Terraform or you need to utilize
any of the volumes data.

An error is triggered if the provided volume name does not exist.

## Example Usage

Get the volume:

```hcl
data "civo_volume" "mysql" {
name = "database-mysql"
}
```

Reuse the data about a volume to attach it to a Instance:

```hcl
data "civo_volume" "mysql" {
name = "database-mysql"
}
resource "civo_instance" "mysql-server" {
hostname = "mysql.domain.com"
tags = ["mysql", "db"]
notes = "this is a note for the server"
size = element(data.civo_instances_size.small.sizes, 0).name
template = element(data.civo_template.debian.templates, 0).id
}
resource "civo_volume_attachment" "foobar" {
instance_id = civo_instance.mysql-server.id
volume_id = data.civo_volume.mysql.id
}
```

## Argument Reference

The following arguments are supported:

* `id` - (Optional) The unique identifier for the volume.
* `name` - (Optional) The name of the volume.

## Attributes Reference

The following attributes are exported:

* `id` - The unique identifier for the volume.
* `name` - Name of the volume.
* `size_gb` - The size of the volume.
* `bootable` - if is bootable or not.
* `mount_point` - The mount point of the volume.
* `created_at` - The date of the creation of the volume.
4 changes: 2 additions & 2 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ resource "civo_instance" "my-test-instance" {
hostname = "foo.com"
tags = ["python", "nginx"]
notes = "this is a note for the server"
size = data.civo_size.small.id
template = data.civo_template.debian.id
size = element(data.civo_instances_size.small.sizes, 0).name
template = element(data.civo_template.debian.templates, 0).id
}
```

Expand Down

0 comments on commit c97d499

Please sign in to comment.