Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Combustion resource to use instead of the ignition one #1068

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion libvirt/coreos_ignition_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type defIgnition struct {
Name string
PoolName string
Content string

// Toggle the resource to a combustion script.
// Combustion and ignition have very similar boot parameters, only the format changes:
// combustion is a shell script while ignition is JSON.
Combustion bool
}

// Creates a new cloudinit with the defaults
Expand Down Expand Up @@ -133,7 +138,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.Combustion && 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.Combustion = 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
}
Loading