/
data_source_volume.go
100 lines (84 loc) · 2 KB
/
data_source_volume.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
package triton
import (
"context"
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/joyent/triton-go/compute"
)
func dataSourceVolume() *schema.Resource {
return &schema.Resource{
Read: dataSourceVolumeRead,
Schema: map[string]*schema.Schema{
"filesystem_path": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"networks": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
},
"type": {
Description: "Type of volume",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
c, err := client.Compute()
if err != nil {
return err
}
input := &compute.ListVolumesInput{}
if name, hasName := d.GetOk("name"); hasName {
input.Name = name.(string)
}
if state, hasState := d.GetOk("state"); hasState {
input.State = state.(string)
}
if size, hasSize := d.GetOk("size"); hasSize {
input.Size = strconv.Itoa(size.(int))
}
volumes, err := c.Volumes().List(context.Background(), input)
if err != nil {
return err
}
if len(volumes) == 0 {
return fmt.Errorf("Your query returned no results. Please change " +
"your search criteria and try again.")
}
if len(volumes) > 1 {
log.Printf("[DEBUG] triton_volume - %d results found", len(volumes))
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}
var volume *compute.Volume = volumes[0]
return tritonVolumeToTerraformVolume(d, volume)
}