This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from scaleway/terraform-provider-scaleway
/
data_source_volume.go
78 lines (64 loc) · 1.56 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
package scaleway
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/nicolai86/scaleway-sdk"
)
func dataSourceScalewayVolume() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayVolumeRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "the name of the volume",
},
"size_in_gb": {
Type: schema.TypeInt,
Computed: true,
Description: "the size of the volume in GB",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "the type of backing storage",
},
"server": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceScalewayVolumeRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Client).scaleway
name := d.Get("name").(string)
volumes, err := client.GetVolumes()
if err != nil {
if serr, ok := err.(api.APIError); ok {
log.Printf("[DEBUG] Error obtaining Volumes: %q\n", serr.APIMessage)
}
return fmt.Errorf("Error obtaining Volumes: %+v", err)
}
var volume *api.Volume
for _, v := range *volumes {
if v.Name == name {
volume = &v
break
}
}
if volume == nil {
return fmt.Errorf("Couldn't locate a Volume with the name %q!", name)
}
d.SetId(volume.Identifier)
d.Set("name", volume.Name)
d.Set("size_in_gb", int(uint64(volume.Size)/gb))
d.Set("type", volume.VolumeType)
if volume.Server != nil {
d.Set("server", volume.Server.Identifier)
} else {
d.Set("server", "")
}
return nil
}