This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
/
resource_vagrant.go
125 lines (106 loc) · 3.02 KB
/
resource_vagrant.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
125
package main
import (
"fmt"
"strings"
"errors"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCoreOSBoxVagrant() *schema.Resource {
return &schema.Resource{
Create: CreateVagrant,
Delete: DeleteVagrant,
Exists: ExistsVagrant,
Read: ReadVagrant,
Schema: map[string]*schema.Schema{
"channel": &schema.Schema{
Type: schema.TypeString,
Description: "CoreOS update channel",
Default: "stable",
Optional: true,
ForceNew: true,
},
"version": &schema.Schema{
Type: schema.TypeString,
Description: "CoreOS version: current or version number",
Default: "current",
Optional: true,
ForceNew: true,
},
"hypervisor": &schema.Schema{
Type: schema.TypeString,
Description: "Vagrant provider: virtualbox or vmware",
Default: "virtualbox",
Optional: true,
ForceNew: true,
},
"version_out": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Version string read from CoreOS releases",
},
"box_string": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Box information string. Ami id, gce path, vagrant box url",
},
},
}
}
func CreateVagrant(d *schema.ResourceData, meta interface{}) error {
version, err := getVerInfo(d)
if err != nil {
return err
}
d.Set("version_out", version)
box_info, err := getVagrant(d)
if err != nil {
return err
}
d.Set("box_string", strings.TrimSpace(box_info))
d.SetId(getID(d))
return nil
}
func DeleteVagrant(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
func ExistsVagrant(d *schema.ResourceData, meta interface{}) (bool, error) {
return getID(d) == d.Id(), nil
}
func ReadVagrant(d *schema.ResourceData, meta interface{}) error {
version, err := getVerInfo(d)
if err != nil {
return err
}
d.Set("version_out", version)
box_info, err := getVagrant(d)
if err != nil {
return err
}
d.Set("box_string", strings.TrimSpace(box_info))
d.SetId(getID(d))
return nil
}
func getVagrant(d *schema.ResourceData) (val string, err error) {
switch d.Get("hypervisor").(string) {
case "virtualbox":
return getVirtualboxVagrant(d)
case "vmware":
return getVmwareVagrant(d)
}
return "", errors.New("Unknown vagrant hypervisor")
}
func getVirtualboxVagrant(d *schema.ResourceData) (val string, err error) {
url := fmt.Sprintf(
"http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json",
d.Get("channel").(string),
d.Get("version").(string))
return url, nil
}
func getVmwareVagrant(d *schema.ResourceData) (val string, err error) {
url := fmt.Sprintf(
"http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant_vmware_fusion.json.json",
d.Get("channel").(string),
d.Get("version").(string))
return url, nil
}