Skip to content

Commit

Permalink
- Added the new data source snapshot
Browse files Browse the repository at this point in the history
- fix error in snapshot resource doc

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 21, 2020
1 parent e32df07 commit 96f9e41
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
120 changes: 120 additions & 0 deletions civo/datasource_snapshot.go
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
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Provider() terraform.ResourceProvider {
"civo_volume": dataSourceVolume(),
"civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
"civo_snapshot": dataSourceSnapshot(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
46 changes: 46 additions & 0 deletions website/docs/d/snapshot.html.markdown
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.
2 changes: 1 addition & 1 deletion website/docs/r/snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The following attributes are exported:
* `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 the this date will be the next execution date.
* `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.

Expand Down

0 comments on commit 96f9e41

Please sign in to comment.