Skip to content

Commit

Permalink
refactor: Make available complete image info, instead of UUID only
Browse files Browse the repository at this point in the history
We plan to make decisions based on some of the information.
  • Loading branch information
dlipovetsky committed Dec 3, 2024
1 parent fec1de5 commit 236044f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
10 changes: 10 additions & 0 deletions api/v1beta1/nutanix_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ type NutanixResourceIdentifier struct {
Name *string `json:"name,omitempty"`
}

func (i NutanixResourceIdentifier) String() string {
if i.Type == NutanixIdentifierUUID && i.UUID != nil {
return *i.UUID
}
if i.Type == NutanixIdentifierName && i.Name != nil {
return *i.Name
}
return ""

Check warning on line 84 in api/v1beta1/nutanix_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/nutanix_types.go#L77-L84

Added lines #L77 - L84 were not covered by tests
}

type NutanixCategoryIdentifier struct {
// key is the Key of category in PC.
// +optional
Expand Down
39 changes: 21 additions & 18 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,46 +298,49 @@ func GetSubnetUUID(ctx context.Context, client *prismclientv3.Client, peUUID str
return foundSubnetUUID, nil
}

// GetImageUUID returns the UUID of the image with the given name
func GetImageUUID(ctx context.Context, client *prismclientv3.Client, imageName, imageUUID *string) (string, error) {
var foundImageUUID string
// GetImageByNameOrUUID returns an image. If no UUID is provided, returns the unique image with the name.
// Returns an error if no image has the UUID, if no image has the name, or more than one image has the name.
func GetImageByNameOrUUID(ctx context.Context, client *prismclientv3.Client, image infrav1.NutanixResourceIdentifier) (*prismclientv3.ImageIntentResponse, error) {
var imageIntentResponse *prismclientv3.ImageIntentResponse

Check warning on line 304 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L303-L304

Added lines #L303 - L304 were not covered by tests

if imageUUID == nil && imageName == nil {
return "", fmt.Errorf("image name or image uuid must be passed in order to retrieve the image")
if image.UUID == nil && image.Name == nil {
return nil, fmt.Errorf("image name or image uuid must be passed in order to retrieve the image")

Check warning on line 307 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L306-L307

Added lines #L306 - L307 were not covered by tests
}
if imageUUID != nil {
imageIntentResponse, err := client.V3.GetImage(ctx, *imageUUID)
if image.UUID != nil {
resp, err := client.V3.GetImage(ctx, *image.UUID)

Check warning on line 310 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L309-L310

Added lines #L309 - L310 were not covered by tests
if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
return "", fmt.Errorf("failed to find image with UUID %s: %v", *imageUUID, err)
return nil, fmt.Errorf("failed to find image with UUID %s: %v", *image.UUID, err)

Check warning on line 313 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L313

Added line #L313 was not covered by tests
}
}
foundImageUUID = *imageIntentResponse.Metadata.UUID
} else { // else search by name
imageIntentResponse = resp
} else if image.Name != nil { // else search by name

Check warning on line 317 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L316-L317

Added lines #L316 - L317 were not covered by tests
responseImages, err := client.V3.ListAllImage(ctx, "")
if err != nil {
return "", err
return nil, err

Check warning on line 320 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L320

Added line #L320 was not covered by tests
}
// Validate filtered Images
foundImages := make([]*prismclientv3.ImageIntentResponse, 0)
for _, s := range responseImages.Entities {
imageSpec := s.Spec
if strings.EqualFold(*imageSpec.Name, *imageName) {
if strings.EqualFold(*imageSpec.Name, *image.Name) {

Check warning on line 326 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L326

Added line #L326 was not covered by tests
foundImages = append(foundImages, s)
}
}
if len(foundImages) == 0 {
return "", fmt.Errorf("failed to retrieve image by name %s", *imageName)
return nil, fmt.Errorf("failed to retrieve image by name %s", *image.Name)

Check warning on line 331 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L331

Added line #L331 was not covered by tests
} else if len(foundImages) > 1 {
return "", fmt.Errorf("more than one image found with name %s", *imageName)
return nil, fmt.Errorf("more than one image found with name %s", *image.Name)

Check warning on line 333 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L333

Added line #L333 was not covered by tests
} else {
foundImageUUID = *foundImages[0].Metadata.UUID
imageIntentResponse = foundImages[0]

Check warning on line 335 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L335

Added line #L335 was not covered by tests
}
if foundImageUUID == "" {
return "", fmt.Errorf("failed to retrieve image by name or uuid. Verify input parameters")
if imageIntentResponse == nil {
return nil, fmt.Errorf("failed to retrieve image by name or uuid. Verify input parameters")

Check warning on line 338 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L337-L338

Added lines #L337 - L338 were not covered by tests
}
} else {
return nil, fmt.Errorf("input parameters must include either name or uuid")

Check warning on line 341 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L340-L341

Added lines #L340 - L341 were not covered by tests
}
return foundImageUUID, nil
return imageIntentResponse, nil

Check warning on line 343 in controllers/helpers.go

View check run for this annotation

Codecov / codecov/patch

controllers/helpers.go#L343

Added line #L343 was not covered by tests
}

// HasTaskInProgress returns true if the given task is in progress
Expand Down
18 changes: 10 additions & 8 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,15 @@ func getDiskList(rctx *nctx.MachineContext) ([]*prismclientv3.VMDisk, error) {
}

func getSystemDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) {
nodeOSImageName := rctx.NutanixMachine.Spec.Image.Name
nodeOSImageUUID, err := GetImageUUID(rctx.Context, rctx.NutanixClient, nodeOSImageName, rctx.NutanixMachine.Spec.Image.UUID)
nodeOSImage, err := GetImageByNameOrUUID(rctx.Context, rctx.NutanixClient, rctx.NutanixMachine.Spec.Image)

Check warning on line 778 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L778

Added line #L778 was not covered by tests
if err != nil {
errorMsg := fmt.Errorf("failed to get the image UUID for image named %q: %w", *nodeOSImageName, err)
errorMsg := fmt.Errorf("failed to get system disk image %q: %w", rctx.NutanixMachine.Spec.Image, err)

Check warning on line 780 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L780

Added line #L780 was not covered by tests
rctx.SetFailureStatus(capierrors.CreateMachineError, errorMsg)
return nil, err
}

systemDiskSizeMib := GetMibValueOfQuantity(rctx.NutanixMachine.Spec.SystemDiskSize)
systemDisk, err := CreateSystemDiskSpec(nodeOSImageUUID, systemDiskSizeMib)
systemDisk, err := CreateSystemDiskSpec(*nodeOSImage.Metadata.UUID, systemDiskSizeMib)

Check warning on line 786 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L786

Added line #L786 was not covered by tests
if err != nil {
errorMsg := fmt.Errorf("error occurred while creating system disk spec: %w", err)
rctx.SetFailureStatus(capierrors.CreateMachineError, errorMsg)
Expand All @@ -795,10 +794,13 @@ func getSystemDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) {
}

func getBootstrapDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) {
bootstrapImageName := rctx.NutanixMachine.Spec.BootstrapRef.Name
bootstrapImageUUID, err := GetImageUUID(rctx.Context, rctx.NutanixClient, &bootstrapImageName, nil)
bootstrapImageRef := infrav1.NutanixResourceIdentifier{
Type: infrav1.NutanixIdentifierName,
Name: ptr.To(rctx.NutanixMachine.Spec.BootstrapRef.Name),
}
bootstrapImage, err := GetImageByNameOrUUID(rctx.Context, rctx.NutanixClient, bootstrapImageRef)

Check warning on line 801 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L797-L801

Added lines #L797 - L801 were not covered by tests
if err != nil {
errorMsg := fmt.Errorf("failed to get the image UUID for image named %q: %w", bootstrapImageName, err)
errorMsg := fmt.Errorf("failed to get bootstrap disk image %q: %w", bootstrapImageRef, err)

Check warning on line 803 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L803

Added line #L803 was not covered by tests
rctx.SetFailureStatus(capierrors.CreateMachineError, errorMsg)
return nil, err
}
Expand All @@ -813,7 +815,7 @@ func getBootstrapDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error)
},
DataSourceReference: &prismclientv3.Reference{
Kind: ptr.To(strings.ToLower(infrav1.NutanixMachineBootstrapRefKindImage)),
UUID: ptr.To(bootstrapImageUUID),
UUID: bootstrapImage.Metadata.UUID,

Check warning on line 818 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L818

Added line #L818 was not covered by tests
},
}

Expand Down
7 changes: 5 additions & 2 deletions test/e2e/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ func (t testHelper) createUUIDNMT(ctx context.Context, clusterName, namespace st
clusterUUID, err := controllers.GetPEUUID(ctx, t.nutanixClient, &clusterVarValue, nil)
Expect(err).ToNot(HaveOccurred())

imageUUID, err := controllers.GetImageUUID(ctx, t.nutanixClient, &imageVarValue, nil)
image, err := controllers.GetImageByNameOrUUID(ctx, t.nutanixClient, infrav1.NutanixResourceIdentifier{
Type: infrav1.NutanixIdentifierName,
Name: ptr.To(imageVarValue),
})
Expect(err).ToNot(HaveOccurred())

subnetUUID, err := controllers.GetSubnetUUID(ctx, t.nutanixClient, clusterUUID, &subnetVarValue, nil)
Expand All @@ -260,7 +263,7 @@ func (t testHelper) createUUIDNMT(ctx context.Context, clusterName, namespace st
MemorySize: resource.MustParse(defaultMemorySize),
Image: infrav1.NutanixResourceIdentifier{
Type: infrav1.NutanixIdentifierUUID,
UUID: ptr.To(imageUUID),
UUID: image.Metadata.UUID,
},
Cluster: infrav1.NutanixResourceIdentifier{
Type: infrav1.NutanixIdentifierUUID,
Expand Down

0 comments on commit 236044f

Please sign in to comment.