-
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.
- Added the new data source snapshot
- fix error in snapshot resource doc Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
e32df07
commit 96f9e41
Showing
4 changed files
with
168 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,120 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/gorhill/cronexpr" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"log" | ||
"time" | ||
) | ||
|
||
// Data source to get from the api a specific snapshot | ||
// using the id or the name | ||
func dataSourceSnapshot() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceSnapshotRead, | ||
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 | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"safe": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"cron_timing": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"hostname": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"template_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"region": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"size_gb": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"next_execution": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"requested_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"completed_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSnapshotRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
var searchBy string | ||
|
||
if id, ok := d.GetOk("id"); ok { | ||
log.Printf("[INFO] Getting the snapshot key by id") | ||
searchBy = id.(string) | ||
} else if name, ok := d.GetOk("name"); ok { | ||
log.Printf("[INFO] Getting the snapshot key by label") | ||
searchBy = name.(string) | ||
} | ||
|
||
snapShot, err := apiClient.FindSnapshot(searchBy) | ||
if err != nil { | ||
fmt.Errorf("[ERR] failed to retrive snapshot: %s", err) | ||
return err | ||
} | ||
|
||
d.SetId(snapShot.ID) | ||
d.Set("name", snapShot.Name) | ||
d.Set("instance_id", snapShot.InstanceID) | ||
d.Set("safe", snapShot.Safe) | ||
d.Set("cron_timing", snapShot.Cron) | ||
d.Set("hostname", snapShot.Hostname) | ||
d.Set("template_id", snapShot.Template) | ||
d.Set("region", snapShot.Region) | ||
d.Set("size_gb", snapShot.SizeGigabytes) | ||
d.Set("state", snapShot.State) | ||
|
||
nextExecution := time.Time{} | ||
if snapShot.Cron != "" { | ||
nextExecution = cronexpr.MustParse(snapShot.Cron).Next(time.Now().UTC()) | ||
} | ||
|
||
d.Set("next_execution", nextExecution.String()) | ||
d.Set("requested_at", snapShot.RequestedAt.UTC().String()) | ||
d.Set("completed_at", snapShot.CompletedAt.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,46 @@ | ||
--- | ||
layout: "civo" | ||
page_title: "Civo: civo_snapshot" | ||
sidebar_current: "docs-civo-datasource-snapshot" | ||
description: |- | ||
Get information about a Civo snapshot. | ||
--- | ||
|
||
# civo_snapshot | ||
|
||
Snapshots are saved instances of a block storage volume. Use this data | ||
source to retrieve the ID of a Civo snapshot for use in other | ||
resources. | ||
|
||
## Example Usage | ||
|
||
Get the snapshot: | ||
|
||
```hcl | ||
data "civo_snapshot" "mysql-vm" { | ||
name = "mysql-vm" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `id` - (Optional) The ID of the snapshot. | ||
* `name` - (Optional) The name of the snapshot. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` The ID of the Droplet snapshot. | ||
* `name` - The name of the snapshot. | ||
* `instance_id` - The ID of the Instance from which the snapshot was be taken. | ||
* `safe` - If is `true` the instance will be shut down during the snapshot if id `false` them not. | ||
* `cron_timing` - A string with the cron format. | ||
* `hostname` - The hostname of the instance. | ||
* `template_id` - The template id. | ||
* `region` - The region where the snapshot was take. | ||
* `size_gb` - The size of the snapshot in GB. | ||
* `state` - The status of the snapshot. | ||
* `next_execution` - if cron was define this date will be the next execution date. | ||
* `requested_at` - The date where the snapshot was requested. | ||
* `completed_at` - The date where the snapshot was completed. |
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