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 2, 2024
1 parent b983688 commit 84f076c
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 ""
}

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

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")
}
if imageUUID != nil {
imageIntentResponse, err := client.V3.GetImage(ctx, *imageUUID)
if image.UUID != nil {
resp, err := client.V3.GetImage(ctx, *image.UUID)
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)
}
}
foundImageUUID = *imageIntentResponse.Metadata.UUID
} else { // else search by name
imageIntentResponse = resp
} else if image.Name != nil { // else search by name
responseImages, err := client.V3.ListAllImage(ctx, "")
if err != nil {
return "", err
return nil, err
}
// 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) {
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)
} 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)
} else {
foundImageUUID = *foundImages[0].Metadata.UUID
imageIntentResponse = foundImages[0]
}
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")
}
} else {
return nil, fmt.Errorf("input parameters must include either name or uuid")
}
return foundImageUUID, nil
return imageIntentResponse, nil
}

// 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)
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)
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)
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)
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)
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,
},
}

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 84f076c

Please sign in to comment.