summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_coreos_ignition.go
blob: 8f3e40d276e3536fb314b1916742edbef2d5c387 (plain)
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
package libvirt

import (
	"fmt"
	"log"

	"github.com/hashicorp/terraform/helper/schema"
)

func resourceIgnition() *schema.Resource {
	return &schema.Resource{
		Create: resourceIgnitionCreate,
		Read:   resourceIgnitionRead,
		Delete: resourceIgnitionDelete,
		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"pool": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  "default",
				ForceNew: true,
			},
			"content": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
		},
	}
}

func resourceIgnitionCreate(d *schema.ResourceData, meta interface{}) error {
	log.Printf("[DEBUG] creating ignition file")
	virConn := meta.(*Client).libvirt
	if virConn == nil {
		return fmt.Errorf(LibVirtConIsNil)
	}

	ignition := newIgnitionDef()

	ignition.Name = d.Get("name").(string)
	ignition.PoolName = d.Get("pool").(string)
	ignition.Content = d.Get("content").(string)

	log.Printf("[INFO] ignition: %+v", ignition)

	key, err := ignition.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 resourceIgnitionRead(d, meta)
}

func resourceIgnitionRead(d *schema.ResourceData, meta interface{}) error {
	virConn := meta.(*Client).libvirt
	if virConn == nil {
		return fmt.Errorf(LibVirtConIsNil)
	}

	ign, err := newIgnitionDefFromRemoteVol(virConn, d.Id())
	d.Set("pool", ign.PoolName)
	d.Set("name", ign.Name)

	if err != nil {
		return fmt.Errorf("Error while retrieving remote volume: %s", err)
	}

	return nil
}

func resourceIgnitionDelete(d *schema.ResourceData, meta interface{}) error {
	virConn := meta.(*Client).libvirt
	if virConn == nil {
		return fmt.Errorf(LibVirtConIsNil)
	}

	key, err := getIgnitionVolumeKeyFromTerraformID(d.Id())
	if err != nil {
		return err
	}

	return RemoveVolume(virConn, key)
}