Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce adapter client #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions adapter/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package adapter

import (
"fmt"

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

var v4ClientCache = v4.NewClientCache(v4.WithSessionAuth(true))

type CachedClientParams = v4.CachedClientParams

func GetClient(cachedClientParams CachedClientParams) (Client, error) {
v4Client, err := v4ClientCache.GetOrCreate(cachedClientParams)
if err != nil {
return nil, fmt.Errorf("failed to create v4 API client: %w", err)
}
return &client{v4Client: v4Client}, nil
}

type Client interface {
Networking() NetworkingClient
Prism() PrismClient
Cluster() ClusterClient
}

type client struct {
v4Client *v4.Client
}
130 changes: 130 additions & 0 deletions adapter/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package adapter

import (
"fmt"

"github.com/google/uuid"
"github.com/nutanix-cloud-native/prism-go-client/utils"
v4 "github.com/nutanix-cloud-native/prism-go-client/v4"
clustersapi "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config"
)

type ClusterClient interface {
GetCluster(cluster string) (*Cluster, error)
}

type Cluster struct {
extID uuid.UUID
}

func (c *Cluster) ExtID() uuid.UUID {
return c.extID
}

func (c *client) Cluster() ClusterClient {
return &clusterClient{
v4Client: c.v4Client,
client: c,
}
}

type clusterClient struct {
v4Client *v4.Client
client Client
}

func (n *clusterClient) GetCluster(cluster string) (*Cluster, error) {
clusterUUID, err := uuid.Parse(cluster)
if err == nil {
return n.getClusterByExtID(clusterUUID)
}

return n.getClusterByName(cluster)
}

func (n *clusterClient) getClusterByName(clusterName string) (*Cluster, error) {
response, err := n.v4Client.ClustersApiInstance.ListClusters(
nil,
nil,
utils.StringPtr(fmt.Sprintf(`name eq '%s'`, clusterName)),
nil,
nil,
nil,
)
if err != nil {
return nil, fmt.Errorf(
"failed to find cluster uuid for cluster %s: %w",
clusterName,
err,
)
}
clusters := response.GetData()
if clusters == nil {
return nil, fmt.Errorf("no cluster found with name %q", clusterName)
}

switch apiClusters := clusters.(type) {
case []clustersapi.Cluster:
if len(apiClusters) == 0 {
return nil, fmt.Errorf("no cluster found with name %q", clusterName)
}
if len(apiClusters) > 1 {
return nil, fmt.Errorf("multiple clusters (%d) found with name %q", len(apiClusters), clusterName)
}

extID := *apiClusters[0].ExtId
clusterUUID, err := uuid.Parse(extID)
if err != nil {
return nil, fmt.Errorf("failed to parse cluster uuid %q for cluster %q: %w", extID, clusterName, err)
}

return &Cluster{
extID: clusterUUID,
}, nil
default:
return nil, fmt.Errorf("unknown response: %+v", clusters)
}
}

func (n *clusterClient) getClusterByExtID(clusterExtID uuid.UUID) (*Cluster, error) {
response, err := n.v4Client.ClustersApiInstance.GetClusterById(
utils.StringPtr(clusterExtID.String()),
)
if err != nil {
return nil, fmt.Errorf(
"failed to find cluster with extID %q: %w",
clusterExtID,
err,
)
}
cluster := response.GetData()
if cluster == nil {
return nil, fmt.Errorf("no cluster found with extID %q", clusterExtID)
}

switch apiCluster := cluster.(type) {
case *clustersapi.Cluster:
if apiCluster.ExtId == nil {
return nil, fmt.Errorf("no extID found for cluster %q", clusterExtID)
}
clusterUUID, err := uuid.Parse(*apiCluster.ExtId)
if err != nil {
return nil,
fmt.Errorf(
"failed to parse cluster extID %q for cluster %q: %w",
*apiCluster.ExtId,
clusterExtID,
err,
)
}

return &Cluster{
extID: clusterUUID,
}, nil
default:
return nil, fmt.Errorf("unknown response: %+v", cluster)
}
}
Loading
Loading