Skip to content

Commit

Permalink
Add Combustion resource to use instead of the ignition one
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosdo committed Feb 12, 2024
1 parent 8ce864f commit 7f841a1
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
9 changes: 5 additions & 4 deletions libvirt/coreos_ignition_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

type defIgnition struct {
Name string
PoolName string
Content string
Name string
PoolName string
Content string
Combution bool
}

// Creates a new cloudinit with the defaults
Expand Down Expand Up @@ -133,7 +134,7 @@ func (ign *defIgnition) createFile() (string, error) {
file = true
if _, err := os.Stat(ign.Content); err != nil {
var js map[string]interface{}
if errConf := json.Unmarshal([]byte(ign.Content), &js); errConf != nil {
if errConf := json.Unmarshal([]byte(ign.Content), &js); !ign.Combution && errConf != nil {
return "", fmt.Errorf("coreos_ignition 'content' is neither a file "+
"nor a valid json object %s", ign.Content)
}
Expand Down
1 change: 1 addition & 0 deletions libvirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Provider() *schema.Provider {
"libvirt_pool": resourceLibvirtPool(),
"libvirt_cloudinit_disk": resourceCloudInitDisk(),
"libvirt_ignition": resourceIgnition(),
"libvirt_combustion": resourceCombustion(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
91 changes: 91 additions & 0 deletions libvirt/resource_libvirt_combustion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package libvirt

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceCombustion() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCombustionCreate,
ReadContext: resourceCombustionRead,
DeleteContext: resourceCombustionDelete,
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 resourceCombustionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] creating combustion file")
client := meta.(*Client)
if client.libvirt == nil {
return diag.Errorf(LibVirtConIsNil)
}

combustion := newIgnitionDef()

combustion.Name = d.Get("name").(string)
combustion.PoolName = d.Get("pool").(string)
combustion.Content = d.Get("content").(string)
combustion.Combution = true

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

key, err := combustion.CreateAndUpload(client)
if err != nil {
return diag.FromErr(err)
}
d.SetId(key)

return resourceIgnitionRead(ctx, d, meta)
}

func resourceCombustionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
virConn := meta.(*Client).libvirt
if virConn == nil {
return diag.Errorf(LibVirtConIsNil)
}

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

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

return nil
}

func resourceCombustionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Client)
if client.libvirt == nil {
return diag.Errorf(LibVirtConIsNil)
}

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

return diag.FromErr(volumeDelete(ctx, client, key))
}
73 changes: 73 additions & 0 deletions libvirt/resource_libvirt_combustion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package libvirt

import (
"fmt"
"testing"

libvirt "github.com/digitalocean/go-libvirt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccLibvirtCombustion_Basic(t *testing.T) {
var volume libvirt.StorageVol
randomCombustionName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
var config = fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_combustion" "combustion" {
name = "%s"
content = "#!/bin/bash
# combustion: network
echo 'root:$6$3aQC9rrDLHiTf1yR$NoKe9tko0kFIpu0rQ2y/FOO' | chpasswd -e
"
pool = "${libvirt_pool.%s.name}"
}
`, randomPoolName, randomPoolName, randomPoolPath, randomCombustionName, randomPoolName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtCombustionDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckIgnitionVolumeExists("libvirt_combustion.combustion", &volume),
resource.TestCheckResourceAttr(
"libvirt_combustion.combustion", "name", randomCombustionName),
resource.TestCheckResourceAttr(
"libvirt_combustion.combustion", "pool", randomPoolName),
),
},
},
})
}

func testAccCheckLibvirtCombustionDestroy(s *terraform.State) error {
virtConn := testAccProvider.Meta().(*Client).libvirt
for _, rs := range s.RootModule().Resources {
if rs.Type != "libvirt_combustion" {
continue
}
// Try to find the Ignition Volume
ignKey, errKey := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID)
if errKey != nil {
return errKey
}
_, err := virtConn.StorageVolLookupByKey(ignKey)
if err == nil {
return fmt.Errorf(
"Error waiting for CombustionVolume (%s) to be destroyed: %w",
ignKey, err)
}
}
return nil
}

0 comments on commit 7f841a1

Please sign in to comment.