Skip to content

Commit

Permalink
fix: use wrapper errors to clearly denote issues in client building
Browse files Browse the repository at this point in the history
  • Loading branch information
faiq committed Jan 3, 2024
1 parent 847d34a commit d092c2d
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
if prismCentralInfo.Port == 0 {
return nil, fmt.Errorf("cannot get credentials if Prism Port is not set")
}
credentialRef := prismCentralInfo.CredentialRef
if credentialRef == nil {
return nil, fmt.Errorf("credentialRef must be set on prismCentral attribute for cluster %s in namespace %s", nutanixCluster.Name, nutanixCluster.Namespace)
credentialRef, err := GetCredentialRefForCluster(nutanixCluster)
if err != nil {
//nolint:wrapcheck // error is alredy wrapped
return nil, err
}
// If namespace is empty, use the cluster namespace
if credentialRef.Namespace == "" {
Expand All @@ -94,7 +95,7 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
// Add env provider for CAPX manager
npe, err := n.getManagerNutanixPrismEndpoint()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create prism endpoint %w", err)
}
// If namespaces is not set, set it to the namespace of the CAPX manager
if npe.CredentialRef.Namespace == "" {
Expand Down Expand Up @@ -123,7 +124,7 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
// fetch endpoint details
me, err := env.GetManagementEndpoint(envTypes.Topology{})
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get management endpoint object %w", err)
}
creds := prismgoclient.Credentials{
URL: me.Address.Host,
Expand Down Expand Up @@ -155,26 +156,25 @@ func (n *NutanixClientHelper) GetClient(cred prismgoclient.Credentials, addition
}
cli, err := nutanixClientV3.NewV3Client(cred, clientOpts...)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create new nutanix client %w", err)
}

// Check if the client is working
_, err = cli.V3.GetCurrentLoggedInUser(context.Background())
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get current logged in user with client %w", err)
}

return cli, nil
}

func (n *NutanixClientHelper) getManagerNutanixPrismEndpoint() (*credentialTypes.NutanixPrismEndpoint, error) {
npe := &credentialTypes.NutanixPrismEndpoint{}
config, err := n.readEndpointConfig()
if err != nil {
return npe, err
return nil, fmt.Errorf("failed to read config %w", err)
}
if err = json.Unmarshal(config, npe); err != nil {
return npe, err
return nil, fmt.Errorf("failed to unmarshal config %w", err)
}
if npe.CredentialRef == nil {
return nil, fmt.Errorf("credentialRef must be set on CAPX manager")
Expand All @@ -196,16 +196,16 @@ func GetCredentialRefForCluster(nutanixCluster *infrav1.NutanixCluster) (*creden
if nutanixCluster == nil {
return nil, fmt.Errorf("cannot get credential reference if nutanix cluster object is nil")
}
prismCentralinfo := nutanixCluster.Spec.PrismCentral
if prismCentralinfo == nil {
prismCentralInfo := nutanixCluster.Spec.PrismCentral
if prismCentralInfo == nil {
return nil, nil
}
if prismCentralinfo.CredentialRef == nil {
if prismCentralInfo.CredentialRef == nil {
return nil, fmt.Errorf("credentialRef must be set on prismCentral attribute for cluster %s in namespace %s", nutanixCluster.Name, nutanixCluster.Namespace)
}
if prismCentralinfo.CredentialRef.Kind != credentialTypes.SecretKind {
if prismCentralInfo.CredentialRef.Kind != credentialTypes.SecretKind {
return nil, nil
}

return prismCentralinfo.CredentialRef, nil
return prismCentralInfo.CredentialRef, nil
}

0 comments on commit d092c2d

Please sign in to comment.