Skip to content

Commit

Permalink
Merge pull request #490 from MusicDin/fix/instance-img-project
Browse files Browse the repository at this point in the history
Fix cached image being searched only in default project
  • Loading branch information
simondeziel authored Jul 4, 2024
2 parents 4fcee29 + 7562b6f commit 3c99340
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
50 changes: 50 additions & 0 deletions internal/image/resource_cached_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ func TestAccCachedImage_project(t *testing.T) {
})
}

func TestAccCachedImage_instanceFromImageFingerprint(t *testing.T) {
projectName := acctest.GenerateName(2, "")
instanceName := acctest.GenerateName(2, "")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCachedImage_instanceFromImageFingerprint(projectName, instanceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_project.project1", "name", projectName),
resource.TestCheckResourceAttr("lxd_cached_image.img1", "project", projectName),
resource.TestCheckResourceAttr("lxd_cached_image.img1", "source_image", acctest.TestCachedImageSourceImage),
resource.TestCheckResourceAttr("lxd_cached_image.img1", "source_remote", acctest.TestCachedImageSourceRemote),
resource.TestCheckResourceAttr("lxd_instance.inst", "name", instanceName),
resource.TestCheckResourceAttr("lxd_instance.inst", "project", projectName),
),
},
},
})
}

func testAccCachedImage_basic() string {
return fmt.Sprintf(`
resource "lxd_cached_image" "img1" {
Expand Down Expand Up @@ -298,10 +321,37 @@ func testAccCachedImage_project(project string) string {
resource "lxd_project" "project1" {
name = "%s"
}
resource "lxd_cached_image" "img1" {
source_remote = "%s"
source_image = "%s"
project = lxd_project.project1.name
}
`, project, acctest.TestCachedImageSourceRemote, acctest.TestCachedImageSourceImage)
}

func testAccCachedImage_instanceFromImageFingerprint(project string, instanceName string) string {
return fmt.Sprintf(`
resource "lxd_project" "project1" {
name = "%s"
config = {
"features.images" = true
"features.profiles" = false
}
}
resource "lxd_cached_image" "img1" {
source_remote = "%s"
source_image = "%s"
project = lxd_project.project1.name
}
resource "lxd_instance" "inst" {
name = "%s"
project = lxd_project.project1.name
image = lxd_cached_image.img1.fingerprint
running = false
}
`, project, acctest.TestCachedImageSourceRemote, acctest.TestCachedImageSourceImage, instanceName)
}
14 changes: 10 additions & 4 deletions internal/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,16 @@ func (r InstanceResource) Create(ctx context.Context, req resource.CreateRequest
image = imageParts[1]
}

imageServer, err := r.provider.ImageServer(imageRemote)
if err != nil {
resp.Diagnostics.Append(errors.NewImageServerError(err))
return
var imageServer lxd.ImageServer
if imageRemote == "" {
// Use the instance server as an image server if image remote is empty.
imageServer = server
} else {
imageServer, err = r.provider.ImageServer(imageRemote)
if err != nil {
resp.Diagnostics.Append(errors.NewImageServerError(err))
return
}
}

// Extract profiles, devices, config and limits.
Expand Down

0 comments on commit 3c99340

Please sign in to comment.