-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Combustion resource to use instead of the ignition one
- Loading branch information
Showing
4 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |