-
Notifications
You must be signed in to change notification settings - Fork 286
/
clientcache.go
67 lines (57 loc) · 2.1 KB
/
clientcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package nutanix
import (
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"strconv"
prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
"github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
v3 "github.com/nutanix-cloud-native/prism-go-client/v3"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
// ClientCache is a map of NutanixDatacenterConfig name to Nutanix client.
type ClientCache struct {
clients map[string]Client
}
// NewClientCache returns a new ClientCache.
func NewClientCache() *ClientCache {
return &ClientCache{
clients: make(map[string]Client),
}
}
// GetNutanixClient returns a Nutanix client for the given NutanixDatacenterConfig.
func (cb *ClientCache) GetNutanixClient(datacenterConfig *anywherev1.NutanixDatacenterConfig, creds credentials.BasicAuthCredential) (Client, error) {
if client, ok := cb.clients[datacenterConfig.Name]; ok {
return client, nil
}
clientOpts := make([]v3.ClientOption, 0)
if datacenterConfig.Spec.AdditionalTrustBundle != "" {
block, _ := pem.Decode([]byte(datacenterConfig.Spec.AdditionalTrustBundle))
certs, err := x509.ParseCertificates(block.Bytes)
if err != nil {
return nil, fmt.Errorf("unable to parse additional trust bundle %s: %v", datacenterConfig.Spec.AdditionalTrustBundle, err)
}
if len(certs) == 0 {
return nil, fmt.Errorf("unable to extract certs from the addtional trust bundle %s", datacenterConfig.Spec.AdditionalTrustBundle)
}
clientOpts = append(clientOpts, v3.WithCertificate(certs[0]))
}
endpoint := datacenterConfig.Spec.Endpoint
port := datacenterConfig.Spec.Port
url := net.JoinHostPort(endpoint, strconv.Itoa(port))
nutanixCreds := prismgoclient.Credentials{
URL: url,
Username: creds.PrismCentral.Username,
Password: creds.PrismCentral.Password,
Endpoint: endpoint,
Port: fmt.Sprintf("%d", port),
Insecure: datacenterConfig.Spec.Insecure,
}
client, err := v3.NewV3Client(nutanixCreds, clientOpts...)
if err != nil {
return nil, fmt.Errorf("error creating nutanix client: %v", err)
}
cb.clients[datacenterConfig.Name] = client.V3
return client.V3, nil
}