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

cached_image: add virtual-machine image alias support #325

Merged
merged 2 commits into from
Aug 10, 2023
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
3 changes: 3 additions & 0 deletions docs/resources/cached_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ resource "lxd_instance" "test1" {

* `source_image` - *Required* - Fingerprint or alias of image to pull.

* `type` - *Optional* - Type of image to cache. Must be one of `container` or `virtual-machine`.
Defaults to `container`.

* `aliases` - *Optional* - A list of aliases to assign to the image after
pulling.

Expand Down
24 changes: 23 additions & 1 deletion lxd/resource_lxd_cached_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func resourceLxdCachedImage() *schema.Resource {
Default: "",
},

"type": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: "container",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "container" && value != "virtual-machine" {
errors = append(errors, fmt.Errorf(
"Only container and virtual-machine are supported values for 'type'"))
}
return
},
},

// Computed attributes

"architecture": {
Expand Down Expand Up @@ -105,9 +120,16 @@ func resourceLxdCachedImageCreate(d *schema.ResourceData, meta interface{}) erro
return err
}

var imageType string
if v, ok := d.GetOk("type"); ok && v != "" {
imageType = v.(string)
} else {
return fmt.Errorf("Missing image type")
}

image := d.Get("source_image").(string)
// has the user provided an fingerprint or alias?
aliasTarget, _, _ := imgServer.GetImageAlias(image)
aliasTarget, _, _ := imgServer.GetImageAliasType(imageType, image)
if aliasTarget != nil {
image = aliasTarget.Target
}
Expand Down
46 changes: 43 additions & 3 deletions lxd/resource_lxd_cached_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"strings"
"testing"

"github.com/dustinkirkland/golang-petname"

petname "github.com/dustinkirkland/golang-petname"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

Expand All @@ -34,6 +33,25 @@ func TestAccCachedImage_basic(t *testing.T) {
})
}

func TestAccCachedImage_basicVM(t *testing.T) {
var img api.Image

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCachedImage_basicVM(),
Check: resource.ComposeTestCheckFunc(
testAccCachedImageExists(t, "lxd_cached_image.img1vm", &img),
resourceAccCachedImageCheckAttributes("lxd_cached_image.img1vm", &img),
testAccCachedImageIsVM(&img),
),
},
},
})
}

func TestAccCachedImage_alias(t *testing.T) {
var img api.Image
alias1 := strings.ToLower(petname.Generate(2, "-"))
Expand Down Expand Up @@ -290,6 +308,16 @@ func resourceAccCachedImageCheckAttributes(n string, img *api.Image) resource.Te
}
}

func testAccCachedImageIsVM(img *api.Image) resource.TestCheckFunc {
return func(s *terraform.State) error {
if img.Type != "virtual-machine" {
return fmt.Errorf("Cached image is not a virtual-machine as requested")
}

return nil
}
}

func testAccCachedImage_basic() string {
return fmt.Sprintf(`
resource "lxd_cached_image" "img1" {
Expand All @@ -301,6 +329,18 @@ resource "lxd_cached_image" "img1" {
`)
}

func testAccCachedImage_basicVM() string {
return fmt.Sprintf(`
resource "lxd_cached_image" "img1vm" {
source_remote = "images"
source_image = "alpine/3.16"
type = "virtual-machine"

copy_aliases = true
}
`)
}

func testAccCachedImage_aliases(aliases ...string) string {
return fmt.Sprintf(`
resource "lxd_cached_image" "img2" {
Expand Down Expand Up @@ -377,7 +417,7 @@ resource "lxd_project" "project1" {
config = {
"features.storage.volumes" = false
"features.images" = false
"features.storage.buckets" = false
"features.storage.buckets" = false
"features.profiles" = false
}
}
Expand Down
Loading