-
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 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
1 parent
69cb929
commit c97d499
Showing
4 changed files
with
151 additions
and
2 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,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 | ||
} |
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,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. |
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