forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_docker_volume.go
102 lines (86 loc) · 2.34 KB
/
resource_docker_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
101
102
package docker
import (
"fmt"
dc "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceDockerVolume() *schema.Resource {
return &schema.Resource{
Create: resourceDockerVolumeCreate,
Read: resourceDockerVolumeRead,
Delete: resourceDockerVolumeDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"driver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"driver_opts": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"mountpoint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDockerVolumeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)
createOpts := dc.CreateVolumeOptions{}
if v, ok := d.GetOk("name"); ok {
createOpts.Name = v.(string)
}
if v, ok := d.GetOk("driver"); ok {
createOpts.Driver = v.(string)
}
if v, ok := d.GetOk("driver_opts"); ok {
createOpts.DriverOpts = mapTypeMapValsToString(v.(map[string]interface{}))
}
var err error
var retVolume *dc.Volume
if retVolume, err = client.CreateVolume(createOpts); err != nil {
return fmt.Errorf("Unable to create volume: %s", err)
}
if retVolume == nil {
return fmt.Errorf("Returned volume is nil")
}
d.SetId(retVolume.Name)
d.Set("name", retVolume.Name)
d.Set("driver", retVolume.Driver)
d.Set("mountpoint", retVolume.Mountpoint)
return nil
}
func resourceDockerVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)
var err error
var retVolume *dc.Volume
if retVolume, err = client.InspectVolume(d.Id()); err != nil && err != dc.ErrNoSuchVolume {
return fmt.Errorf("Unable to inspect volume: %s", err)
}
if retVolume == nil {
d.SetId("")
return nil
}
d.Set("name", retVolume.Name)
d.Set("driver", retVolume.Driver)
d.Set("mountpoint", retVolume.Mountpoint)
return nil
}
func resourceDockerVolumeDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)
if err := client.RemoveVolume(d.Id()); err != nil && err != dc.ErrNoSuchVolume {
return fmt.Errorf("Error deleting volume %s: %s", d.Id(), err)
}
d.SetId("")
return nil
}