Skip to content

Commit

Permalink
Get rid of FIQL in Nutanix API list methods (#8997)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiantum authored Nov 22, 2024
1 parent 112571b commit 195ea65
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 218 deletions.
56 changes: 37 additions & 19 deletions internal/pkg/nutanix/prismclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
"github.com/nutanix-cloud-native/prism-go-client/utils"
v3 "github.com/nutanix-cloud-native/prism-go-client/v3"

"github.com/aws/eks-anywhere/pkg/providers/nutanix"
Expand Down Expand Up @@ -44,30 +43,39 @@ func NewPrismClient(endpoint, port string, insecure bool) (PrismClient, error) {

// GetImageUUIDFromName retrieves the image uuid from the given image name.
func (c *client) GetImageUUIDFromName(ctx context.Context, imageName string) (*string, error) {
res, err := c.V3.ListImage(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", imageName)),
})
if err != nil || len(res.Entities) == 0 {
res, err := c.V3.ListAllImage(ctx, "")
if err != nil {
return nil, fmt.Errorf("failed to list images: %v", err)
}

images := make([]*v3.ImageIntentResponse, 0)

for _, image := range res.Entities {
if image.Spec.Name != nil && *image.Spec.Name == imageName {
images = append(images, image)
}
}

if len(images) == 0 {
return nil, fmt.Errorf("failed to find image by name %q: %v", imageName, err)
}

if len(res.Entities) > 1 {
return nil, fmt.Errorf("found more than one (%v) image with name %q", len(res.Entities), imageName)
if len(images) > 1 {
return nil, fmt.Errorf("found more than one (%v) image with name %q", len(images), imageName)
}

return res.Entities[0].Metadata.UUID, nil
return images[0].Metadata.UUID, nil
}

// GetClusterUUIDFromName retrieves the cluster uuid from the given cluster name.
//
//nolint:gocyclo
func (c *client) GetClusterUUIDFromName(ctx context.Context, clusterName string) (*string, error) {
res, err := c.V3.ListCluster(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", clusterName)),
})
res, err := c.V3.ListAllCluster(ctx, "")
if err != nil {
return nil, fmt.Errorf("failed to find cluster by name %q: %v", clusterName, err)
return nil, fmt.Errorf("failed to list clusters: %v", err)
}

entities := make([]*v3.ClusterIntentResponse, 0)
for _, entity := range res.Entities {
if entity.Status != nil && entity.Status.Resources != nil && entity.Status.Resources.Config != nil {
Expand Down Expand Up @@ -97,16 +105,26 @@ func (c *client) GetClusterUUIDFromName(ctx context.Context, clusterName string)

// GetSubnetUUIDFromName retrieves the subnet uuid from the given subnet name.
func (c *client) GetSubnetUUIDFromName(ctx context.Context, subnetName string) (*string, error) {
res, err := c.V3.ListSubnet(ctx, &v3.DSMetadata{
Filter: utils.StringPtr(fmt.Sprintf("name==%s", subnetName)),
})
if err != nil || len(res.Entities) == 0 {
res, err := c.V3.ListAllSubnet(ctx, "", nil)
if err != nil {
return nil, fmt.Errorf("failed to list subnets: %v", err)
}

subnets := make([]*v3.SubnetIntentResponse, 0)

for _, subnet := range res.Entities {
if subnet.Spec.Name != nil && *subnet.Spec.Name == subnetName {
subnets = append(subnets, subnet)
}
}

if len(subnets) == 0 {
return nil, fmt.Errorf("failed to find subnet by name %q: %v", subnetName, err)
}

if len(res.Entities) > 1 {
return nil, fmt.Errorf("found more than one (%v) subnet with name %q", len(res.Entities), subnetName)
if len(subnets) > 1 {
return nil, fmt.Errorf("found more than one (%v) subnet with name %q", len(subnets), subnetName)
}

return res.Entities[0].Metadata.UUID, nil
return subnets[0].Metadata.UUID, nil
}
9 changes: 5 additions & 4 deletions pkg/providers/nutanix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package nutanix
import (
"context"

prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
v3 "github.com/nutanix-cloud-native/prism-go-client/v3"
)

type Client interface {
GetSubnet(ctx context.Context, uuid string) (*v3.SubnetIntentResponse, error)
ListAllHost(ctx context.Context) (*v3.HostListResponse, error)
ListSubnet(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.SubnetListIntentResponse, error)
ListAllSubnet(ctx context.Context, filter string, clientSideFilters []*prismgoclient.AdditionalFilter) (*v3.SubnetListIntentResponse, error)
GetImage(ctx context.Context, uuid string) (*v3.ImageIntentResponse, error)
ListImage(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ImageListIntentResponse, error)
ListAllImage(ctx context.Context, filter string) (*v3.ImageListIntentResponse, error)
GetCluster(ctx context.Context, uuid string) (*v3.ClusterIntentResponse, error)
ListCluster(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ClusterListIntentResponse, error)
ListAllCluster(ctx context.Context, filter string) (*v3.ClusterListIntentResponse, error)
GetProject(ctx context.Context, uuid string) (*v3.Project, error)
ListProject(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ProjectListResponse, error)
ListAllProject(ctx context.Context, filter string) (*v3.ProjectListResponse, error)
GetCurrentLoggedInUser(ctx context.Context) (*v3.UserIntentResponse, error)
ListCategories(ctx context.Context, getEntitiesRequest *v3.CategoryListMetadata) (*v3.CategoryKeyListResponse, error)
GetCategoryKey(ctx context.Context, name string) (*v3.CategoryKeyStatus, error)
Expand Down
99 changes: 50 additions & 49 deletions pkg/providers/nutanix/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions pkg/providers/nutanix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func TestNutanixProviderSetupAndValidateCreate(t *testing.T) {
},
},
}
mockClient.EXPECT().ListCluster(gomock.Any(), gomock.Any()).Return(clusters, nil).AnyTimes()
mockClient.EXPECT().ListAllCluster(gomock.Any(), gomock.Any()).Return(clusters, nil).AnyTimes()
subnets := &v3.SubnetListIntentResponse{
Entities: []*v3.SubnetIntentResponse{
{
Expand All @@ -400,11 +400,16 @@ func TestNutanixProviderSetupAndValidateCreate(t *testing.T) {
},
Spec: &v3.Subnet{
Name: utils.StringPtr("prism-subnet"),
ClusterReference: &v3.Reference{
Kind: utils.StringPtr("cluster"),
UUID: utils.StringPtr("a15f6966-bfc7-4d1e-8575-224096fc1cda"),
Name: utils.StringPtr("prism-cluster"),
},
},
},
},
}
mockClient.EXPECT().ListSubnet(gomock.Any(), gomock.Any()).Return(subnets, nil).AnyTimes()
mockClient.EXPECT().ListAllSubnet(gomock.Any(), gomock.Any(), nil).Return(subnets, nil).AnyTimes()
images := &v3.ImageListIntentResponse{
Entities: []*v3.ImageIntentResponse{
{
Expand All @@ -415,9 +420,17 @@ func TestNutanixProviderSetupAndValidateCreate(t *testing.T) {
Name: utils.StringPtr("prism-image"),
},
},
{
Metadata: &v3.Metadata{
UUID: utils.StringPtr("a15f6966-bfc7-4d1e-8575-224096fc1cdd"),
},
Spec: &v3.Image{
Name: utils.StringPtr("prism-image-1-19"),
},
},
},
}
mockClient.EXPECT().ListImage(gomock.Any(), gomock.Any()).Return(images, nil).AnyTimes()
mockClient.EXPECT().ListAllImage(gomock.Any(), gomock.Any()).Return(images, nil).AnyTimes()
mockClient.EXPECT().ListAllHost(gomock.Any()).Return(fakeHostList(), nil).AnyTimes()
mockCertValidator := mockCrypto.NewMockTlsValidator(ctrl)
mockCertValidator.EXPECT().ValidateCert(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
Expand Down
Loading

0 comments on commit 195ea65

Please sign in to comment.