From cdb609643c45412c497b67b5fcc0f06cf4907113 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Mon, 2 Oct 2023 11:10:59 +0200 Subject: [PATCH] Fix login credential type identifier parsing Values used for type identifier parsing were incorrect Signed-off-by: Kobi Samoray --- nsxt/resource_nsxt_compute_manager.go | 31 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/nsxt/resource_nsxt_compute_manager.go b/nsxt/resource_nsxt_compute_manager.go index d5fdebc83..9640ec40d 100644 --- a/nsxt/resource_nsxt_compute_manager.go +++ b/nsxt/resource_nsxt_compute_manager.go @@ -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 { @@ -407,7 +418,8 @@ 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] @@ -415,8 +427,10 @@ func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.Struct 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] @@ -424,8 +438,10 @@ func setCredentialValuesInSchema(d *schema.ResourceData, credential *data.Struct 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] @@ -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] @@ -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 }