Skip to content

Commit

Permalink
Merge pull request #491 from MusicDin/fix/instance-image-remote
Browse files Browse the repository at this point in the history
Fix cached image not found if instance remote is set
  • Loading branch information
simondeziel authored Jul 8, 2024
2 parents f7f0752 + 8f6bcb3 commit 9639ed3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 22 additions & 4 deletions internal/image/resource_cached_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,24 @@ func TestAccCachedImage_instanceFromImageFingerprint(t *testing.T) {
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCachedImage_instanceFromImageFingerprint(projectName, instanceName),
// Create an instance from the cached image and do not set instance
// remote. Test will succeed only if the image is searched in the
// remote and project where instance is created.
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),
),
},
{
// Create an instance from the cached image and set instance's remote.
// Test will succeed only if the image is searched in the remote and
// project where instance is created.
Config: testAccCachedImage_instanceFromImageFingerprint(projectName, instanceName, "local"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_project.project1", "name", projectName),
resource.TestCheckResourceAttr("lxd_cached_image.img1", "project", projectName),
Expand Down Expand Up @@ -330,7 +347,7 @@ resource "lxd_cached_image" "img1" {
`, project, acctest.TestCachedImageSourceRemote, acctest.TestCachedImageSourceImage)
}

func testAccCachedImage_instanceFromImageFingerprint(project string, instanceName string) string {
func testAccCachedImage_instanceFromImageFingerprint(project string, instanceName string, instanceRemote string) string {
return fmt.Sprintf(`
resource "lxd_project" "project1" {
name = "%s"
Expand All @@ -349,9 +366,10 @@ resource "lxd_cached_image" "img1" {
resource "lxd_instance" "inst" {
name = "%s"
project = lxd_project.project1.name
image = lxd_cached_image.img1.fingerprint
remote = "%s"
project = lxd_project.project1.name
running = false
}
`, project, acctest.TestCachedImageSourceRemote, acctest.TestCachedImageSourceImage, instanceName)
`, project, acctest.TestCachedImageSourceRemote, acctest.TestCachedImageSourceImage, instanceName, instanceRemote)
}
5 changes: 3 additions & 2 deletions internal/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,17 @@ func (r InstanceResource) Create(ctx context.Context, req resource.CreateRequest
return
}

var imageRemote string
var imageServer lxd.ImageServer

// Evaluate image remote.
image := plan.Image.ValueString()
imageRemote := remote
imageParts := strings.SplitN(image, ":", 2)
if len(imageParts) == 2 {
imageRemote = imageParts[0]
image = imageParts[1]
}

var imageServer lxd.ImageServer
if imageRemote == "" {
// Use the instance server as an image server if image remote is empty.
imageServer = server
Expand Down

0 comments on commit 9639ed3

Please sign in to comment.