This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_source_openstack_blockstorage_snapshot_v3.go
124 lines (101 loc) · 2.98 KB
/
data_source_openstack_blockstorage_snapshot_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceBlockStorageSnapshotV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceBlockStorageSnapshotV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"most_recent": {
Type: schema.TypeBool,
Optional: true,
},
// Computed values
"description": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceBlockStorageSnapshotV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}
listOpts := snapshots.ListOpts{
Name: d.Get("name").(string),
Status: d.Get("status").(string),
VolumeID: d.Get("volume_id").(string),
}
allPages, err := snapshots.List(client, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query openstack_blockstorage_snapshots_v3: %s", err)
}
allSnapshots, err := snapshots.ExtractSnapshots(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_blockstorage_snapshots_v3: %s", err)
}
if len(allSnapshots) < 1 {
return fmt.Errorf("Your openstack_blockstorage_snapshot_v3 query returned no results. " +
"Please change your search criteria and try again.")
}
var snapshot snapshots.Snapshot
if len(allSnapshots) > 1 {
recent := d.Get("most_recent").(bool)
if recent {
snapshot = dataSourceBlockStorageV3MostRecentSnapshot(allSnapshots)
} else {
log.Printf("[DEBUG] Multiple openstack_blockstorage_snapshot_v3 results found: %#v", allSnapshots)
return fmt.Errorf("Your query returned more than one result. Please try a more " +
"specific search criteria, or set `most_recent` attribute to true.")
}
} else {
snapshot = allSnapshots[0]
}
return dataSourceBlockStorageSnapshotV3Attributes(d, snapshot)
}
func dataSourceBlockStorageSnapshotV3Attributes(d *schema.ResourceData, snapshot snapshots.Snapshot) error {
d.SetId(snapshot.ID)
d.Set("name", snapshot.Name)
d.Set("description", snapshot.Description)
d.Set("size", snapshot.Size)
d.Set("status", snapshot.Status)
d.Set("volume_id", snapshot.VolumeID)
if err := d.Set("metadata", snapshot.Metadata); err != nil {
log.Printf("[DEBUG] Unable to set metadata for snapshot %s: %s", snapshot.ID, err)
}
return nil
}