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
|
package libvirt
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCloudInit() *schema.Resource {
return &schema.Resource{
Create: resourceCloudInitCreate,
Read: resourceCloudInitRead,
Delete: resourceCloudInitDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"pool": {
Type: schema.TypeString,
Optional: true,
Default: "default",
ForceNew: true,
},
"local_hostname": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"user_data": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ssh_authorized_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceCloudInitCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] creating cloudinit")
virConn := meta.(*Client).libvirt
if virConn == nil {
return fmt.Errorf(LibVirtConIsNil)
}
cloudInit := newCloudInitDef()
cloudInit.Metadata.LocalHostname = d.Get("local_hostname").(string)
cloudInit.UserDataRaw = d.Get("user_data").(string)
if _, ok := d.GetOk("ssh_authorized_key"); ok {
sshKey := d.Get("ssh_authorized_key").(string)
cloudInit.UserData.SSHAuthorizedKeys = append(
cloudInit.UserData.SSHAuthorizedKeys,
sshKey)
}
cloudInit.Name = d.Get("name").(string)
cloudInit.PoolName = d.Get("pool").(string)
log.Printf("[INFO] cloudInit: %+v", cloudInit)
key, err := cloudInit.CreateAndUpload(virConn)
if err != nil {
return err
}
d.SetId(key)
// make sure we record the id even if the rest of this gets interrupted
d.Partial(true) // make sure we record the id even if the rest of this gets interrupted
d.Set("id", key)
d.SetPartial("id")
// TODO: at this point we have collected more things than the ID, so let's save as many things as we can
d.Partial(false)
return resourceCloudInitRead(d, meta)
}
func resourceCloudInitRead(d *schema.ResourceData, meta interface{}) error {
virConn := meta.(*Client).libvirt
if virConn == nil {
return fmt.Errorf(LibVirtConIsNil)
}
ci, err := newCloudInitDefFromRemoteISO(virConn, d.Id())
if err != nil {
return fmt.Errorf("error while retrieving remote ISO: %s", err)
}
d.Set("pool", ci.PoolName)
d.Set("name", ci.Name)
d.Set("local_hostname", ci.Metadata.LocalHostname)
d.Set("user_data", ci.UserDataRaw)
if len(ci.UserData.SSHAuthorizedKeys) == 1 {
d.Set("ssh_authorized_key", ci.UserData.SSHAuthorizedKeys[0])
}
return nil
}
func resourceCloudInitDelete(d *schema.ResourceData, meta interface{}) error {
virConn := meta.(*Client).libvirt
if virConn == nil {
return fmt.Errorf(LibVirtConIsNil)
}
key, err := getCloudInitVolumeKeyFromTerraformID(d.Id())
if err != nil {
return err
}
return RemoveVolume(virConn, key)
}
|