Skip to content

Commit

Permalink
Merge pull request #989 from ksamoray/compute_mgr_creds
Browse files Browse the repository at this point in the history
Fix login credential type identifier parsing
  • Loading branch information
ksamoray authored Oct 13, 2023
2 parents c92f82e + f1e6000 commit 9094cc9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
11 changes: 11 additions & 0 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,14 @@ func getElemOrEmptyMapFromSchema(d *schema.ResourceData, key string) map[string]
}
return make(map[string]interface{})
}

func getElemOrEmptyMapFromMap(d map[string]interface{}, key string) map[string]interface{} {
e := d[key]
if e != nil {
elems := e.([]interface{})
if len(elems) > 0 {
return elems[0].(map[string]interface{})
}
}
return make(map[string]interface{})
}
30 changes: 22 additions & 8 deletions nsxt/resource_nsxt_compute_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func resourceNsxtComputeManagerRead(d *schema.ResourceData, m interface{}) error

func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.StructValue) error {
converter := bindings.NewTypeConverter()
elem := make(map[string]interface{})
parentElem := getElemOrEmptyMapFromSchema(d, "credential")

base, errs := converter.ConvertToGolang(credential, model.LoginCredentialBindingType())
if errs != nil {
Expand All @@ -407,35 +407,48 @@ func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.Struct
credType := base.(model.LoginCredential).CredentialType

switch credType {
case "saml_login":
case model.SamlTokenLoginCredential__TYPE_IDENTIFIER:
elem := getElemOrEmptyMapFromMap(parentElem, "saml_login")
entry, errs := converter.ConvertToGolang(credential, model.SamlTokenLoginCredentialBindingType())
if errs != nil {
return errs[0]
}
credEntry := entry.(model.SamlTokenLoginCredential)
elem["thumbprint"] = credEntry.Thumbprint
elem["token"] = credEntry.Token
parentElem["saml_login"] = []interface{}{elem}

case "session_login":
case model.SessionLoginCredential__TYPE_IDENTIFIER:
elem := getElemOrEmptyMapFromMap(parentElem, "session_login")
entry, errs := converter.ConvertToGolang(credential, model.SessionLoginCredentialBindingType())
if errs != nil {
return errs[0]
}
credEntry := entry.(model.SessionLoginCredential)
elem["session_id"] = credEntry.SessionId
elem["thumbprint"] = credEntry.Thumbprint
parentElem["session_login"] = []interface{}{elem}

case "username_password_login":
case model.UsernamePasswordLoginCredential__TYPE_IDENTIFIER:
elem := getElemOrEmptyMapFromMap(parentElem, "username_password_login")
entry, errs := converter.ConvertToGolang(credential, model.UsernamePasswordLoginCredentialBindingType())
if errs != nil {
return errs[0]
}
credEntry := entry.(model.UsernamePasswordLoginCredential)
elem["username"] = credEntry.Username
// Normally NSX won't return credEntry.Username
if credEntry.Username != nil {
elem["username"] = credEntry.Username
}
// Normally NSX won't return credEntry.Password
if credEntry.Password != nil {
elem["password"] = credEntry.Password
}
elem["thumbprint"] = credEntry.Thumbprint
elem["password"] = credEntry.Password
parentElem["username_password_login"] = []interface{}{elem}

case "verifiable_asymmetric_login":
case model.VerifiableAsymmetricLoginCredential__TYPE_IDENTIFIER:
elem := getElemOrEmptyMapFromMap(parentElem, "verifiable_asymmetric_login")
entry, errs := converter.ConvertToGolang(credential, model.VerifiableAsymmetricLoginCredentialBindingType())
if errs != nil {
return errs[0]
Expand All @@ -444,12 +457,13 @@ func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.Struct
elem["asymmetric_credential"] = credEntry.AsymmetricCredential
elem["credential_key"] = credEntry.CredentialKey
elem["credential_verifier"] = credEntry.CredentialVerifier
parentElem["verifiable_asymmetric_login"] = []interface{}{elem}

default:
return errors.New("no valid credential found")
}

d.Set("credential", elem)
d.Set("credential", []interface{}{parentElem})
return nil
}

Expand Down

0 comments on commit 9094cc9

Please sign in to comment.