@@ -26,39 +26,33 @@ import (
2626// cluster authentication credentials from environment variables.
2727type EnvCredentialProvider struct {}
2828
29- // ConvertClusterName changes any characters that should not be used in
30- // environment variables to an underscore (_).
31- func ConvertClusterName (cluster string ) string {
32- cluster = strings .ToUpper (cluster )
33-
34- r , _ := regexp .Compile ("[^A-Z_0-9]+" )
35- cluster = r .ReplaceAllString (cluster , "_" )
29+ // NewEnvCredentialProvider creates a new instance of EnvCredentialProvider.
30+ func NewEnvCredentialProvider () * EnvCredentialProvider {
31+ return & EnvCredentialProvider {}
32+ }
3633
37- return cluster
34+ // Refresh is a no-op for the EnvCredentialProvider since it does not maintain a state.
35+ func (e * EnvCredentialProvider ) Refresh () error {
36+ return nil
3837}
3938
4039// getEnvVarsForCluster generates the names for the environment variables
4140// that have to be used for providing login credentials of a Nutanix cluster.
42- func getEnvVarsForCluster (cluster string , isPC bool ) (string , string ) {
43- var evU , evP string
44-
41+ func (e * EnvCredentialProvider ) getEnvVarsForCluster (cluster string , isPC bool ) (string , string ) {
4542 if isPC {
46- evU = "PC_USERNAME"
47- evP = "PC_PASSWORD"
48- } else {
49- evU = "PE_USERNAME_" + ConvertClusterName (cluster )
50- evP = "PE_PASSWORD_" + ConvertClusterName (cluster )
43+ return "PC_USERNAME" , "PC_PASSWORD"
5144 }
5245
53- return evU , evP
46+ convertedCluster := e .convertClusterName (cluster )
47+ return "PE_USERNAME_" + convertedCluster , "PE_PASSWORD_" + convertedCluster
5448}
5549
5650// getCreds reads the appropriate environment variables and returns the username and password
5751// for the provided cluster name.
5852//
5953// If isPC is true the credentials for Prism Central will be returned, otherwise those for Prism Element.
60- func getCreds (cluster string , isPC bool ) (string , string , error ) {
61- evU , evP := getEnvVarsForCluster (cluster , isPC )
54+ func ( e * EnvCredentialProvider ) getCreds (cluster string , isPC bool ) (string , string , error ) {
55+ evU , evP := e . getEnvVarsForCluster (cluster , isPC )
6256
6357 username := os .Getenv (evU )
6458 if username == "" {
@@ -73,14 +67,19 @@ func getCreds(cluster string, isPC bool) (string, string, error) {
7367 return username , password , nil
7468}
7569
76- // GetPCCreds returns the username and password for the specified Prism Central cluster
77- func (evCP * EnvCredentialProvider ) GetPCCreds (cluster string ) (string , string , error ) {
78-
79- return getCreds (cluster , true )
70+ // GetPCCreds retrieves the username and password for a given cluster from environment variables.
71+ func (e * EnvCredentialProvider ) GetPCCreds (cluster string ) (string , string , error ) {
72+ return e .getCreds (cluster , true )
8073}
8174
82- // GetPECreds returns the username and password for the specified Prism Element cluster
83- func (evCP * EnvCredentialProvider ) GetPECreds (cluster string ) (string , string , error ) {
75+ // GetPECreds retrieves the username and password for a given cluster from environment variables.
76+ func (e * EnvCredentialProvider ) GetPECreds (cluster string ) (string , string , error ) {
77+ return e .getCreds (cluster , false )
78+ }
8479
85- return getCreds (cluster , false )
80+ // convertClusterName converts the cluster name to uppercase and replaces non-alphanumeric characters with underscores.
81+ func (e * EnvCredentialProvider ) convertClusterName (cluster string ) string {
82+ cluster = strings .ToUpper (cluster )
83+ r , _ := regexp .Compile ("[^A-Z_0-9]+" )
84+ return r .ReplaceAllString (cluster , "_" )
8685}
0 commit comments