From f0dd1886212c2185907011e441c0e3b8c3de191a Mon Sep 17 00:00:00 2001 From: Christophe Jauffret Date: Wed, 2 Oct 2024 16:25:46 +0200 Subject: [PATCH] remove FIQL filter to avoid bug --- README.md | 2 +- builder/nutanix/driver.go | 47 +++++++++++++++++---------------------- version/version.go | 2 +- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 1c4ce8d..b1b4c4a 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Then, run [`packer init`](https://www.packer.io/docs/commands/init). packer { required_plugins { nutanix = { - version = ">= 0.9.3" + version = ">= 0.9.4" source = "github.com/nutanix-cloud-native/nutanix" } } diff --git a/builder/nutanix/driver.go b/builder/nutanix/driver.go index dfbd120..96936be 100644 --- a/builder/nutanix/driver.go +++ b/builder/nutanix/driver.go @@ -7,7 +7,6 @@ import ( "io" "log" "net/http" - "net/url" "path" "strings" "time" @@ -64,9 +63,7 @@ type nutanixImage struct { } func findProjectByName(ctx context.Context, conn *v3.Client, name string) (*v3.Project, error) { - encodedName := url.QueryEscape(name) - filter := fmt.Sprintf("name==%s", encodedName) - resp, err := conn.V3.ListAllProject(ctx, filter) + resp, err := conn.V3.ListAllProject(ctx, "") if err != nil { return nil, err } @@ -96,9 +93,7 @@ func findProjectByName(ctx context.Context, conn *v3.Client, name string) (*v3.P } func findClusterByName(ctx context.Context, conn *v3.Client, name string) (*v3.ClusterIntentResponse, error) { - encodedName := url.QueryEscape(name) - filter := fmt.Sprintf("name==%s", encodedName) - resp, err := conn.V3.ListAllCluster(ctx, filter) + resp, err := conn.V3.ListAllCluster(ctx, "") if err != nil { return nil, err } @@ -132,9 +127,7 @@ func findSubnetByUUID(ctx context.Context, conn *v3.Client, uuid string) (*v3.Su } func findSubnetByName(ctx context.Context, conn *v3.Client, name string) ([]*v3.SubnetIntentResponse, error) { - encodedName := url.QueryEscape(name) - filter := fmt.Sprintf("name==%s", encodedName) - resp, err := conn.V3.ListAllSubnet(ctx, filter, getEmptyClientSideFilter()) + resp, err := conn.V3.ListAllSubnet(ctx, "", nil) if err != nil { return nil, err } @@ -187,9 +180,7 @@ func findGPUByName(ctx context.Context, conn *v3.Client, name string) (*v3.VMGpu } func sourceImageExists(ctx context.Context, conn *v3.Client, name string, uri string) (*v3.ImageIntentResponse, error) { - encodedName := url.QueryEscape(name) - filter := fmt.Sprintf("name==%s", encodedName) - resp, err := conn.V3.ListAllImage(ctx, filter) + resp, err := conn.V3.ListAllImage(ctx, "") if err != nil { return nil, err } @@ -218,9 +209,7 @@ func findImageByUUID(ctx context.Context, conn *v3.Client, uuid string) (*v3.Ima } func findImageByName(ctx context.Context, conn *v3.Client, name string) (*v3.ImageIntentResponse, error) { - encodedName := url.QueryEscape(name) - filter := fmt.Sprintf("name==%s", encodedName) - resp, err := conn.V3.ListAllImage(ctx, filter) + resp, err := conn.V3.ListAllImage(ctx, "") if err != nil { return nil, err } @@ -984,23 +973,33 @@ func (d *NutanixDriver) SaveVMDisk(ctx context.Context, diskUUID string, index i // When force_deregister, check if image already exists if d.Config.ForceDeregister { log.Println("force_deregister is set, check if image already exists") - ImageList, err := conn.V3.ListAllImage(ctx, fmt.Sprintf("name==%s", name)) + resp, err := conn.V3.ListAllImage(ctx, "") if err != nil { return nil, fmt.Errorf("error while ListAllImage, %s", err.Error()) } - if *ImageList.Metadata.TotalMatches == 0 { + + entities := resp.Entities + + found := make([]*v3.ImageIntentResponse, 0) + for _, v := range entities { + if *v.Spec.Name == name { + found = append(found, v) + } + } + + if len(found) == 0 { log.Println("image with given Name not found, no need to deregister") - } else if *ImageList.Metadata.TotalMatches > 1 { + } else if len(found) > 1 { log.Println("more than one image with given Name found, will not deregister") - } else if *ImageList.Metadata.TotalMatches == 1 { + } else if len(found) == 1 { log.Println("exactly one image with given Name found, will deregister") - resp, err := conn.V3.DeleteImage(ctx, *ImageList.Entities[0].Metadata.UUID) + resp, err := conn.V3.DeleteImage(ctx, *found[0].Metadata.UUID) if err != nil { return nil, fmt.Errorf("error while Deleting Image, %s", err.Error()) } - log.Printf("deleting image %s...\n", *ImageList.Entities[0].Metadata.UUID) + log.Printf("deleting image %s...\n", *found[0].Metadata.UUID) err = checkTask(ctx, conn, resp.Status.ExecutionContext.TaskUUID.(string)) if err != nil { @@ -1049,7 +1048,3 @@ func (d *NutanixDriver) SaveVMDisk(ctx context.Context, diskUUID string, index i } } - -func getEmptyClientSideFilter() []*client.AdditionalFilter { - return make([]*client.AdditionalFilter, 0) -} diff --git a/version/version.go b/version/version.go index 0545434..7f472a5 100644 --- a/version/version.go +++ b/version/version.go @@ -6,7 +6,7 @@ import ( var ( // Version is the main version number that is being run at the moment. - Version = "0.9.3" + Version = "0.9.4" // VersionPrerelease is A pre-release marker for the Version. If this is "" // (empty string) then it means that it is a final release. Otherwise, this