Skip to content

Commit

Permalink
Fix login credential type identifier parsing
Browse files Browse the repository at this point in the history
Values used for type identifier parsing were incorrect

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Oct 12, 2023
1 parent 173db5e commit cdb6096
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions nsxt/resource_nsxt_compute_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,20 @@ func resourceNsxtComputeManagerRead(d *schema.ResourceData, m interface{}) error
return nil
}

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{})
}

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,25 +418,30 @@ 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]
Expand All @@ -434,8 +450,10 @@ func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.Struct
elem["username"] = credEntry.Username
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 +462,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 cdb6096

Please sign in to comment.