Skip to content

Commit

Permalink
Addresss comments
Browse files Browse the repository at this point in the history
Signed-off-by: Shawn Wang <[email protected]>
  • Loading branch information
wsquan171 committed Nov 6, 2023
1 parent 89e9c57 commit b7aae56
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
63 changes: 43 additions & 20 deletions nsxt/resource_nsxt_policy_ldap_identity_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
nsxModel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

var ldapServerTypes = [](string){
nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE,
nsxModel.LdapIdentitySource_RESOURCE_TYPE_OPENLDAPIDENTITYSOURCE,
}
const (
activeDirectoryType = "ActiveDirectory"
openLdapType = "OpenLdap"
)

var ldapServerTypes = []string{activeDirectoryType, openLdapType}

func resourceNsxtPolicyLdapIdentitySource() *schema.Resource {
return &schema.Resource{
Expand All @@ -43,6 +45,7 @@ func resourceNsxtPolicyLdapIdentitySource() *schema.Resource {
Description: "Indicates the type of LDAP server",
Required: true,
ValidateFunc: validation.StringInSlice(ldapServerTypes, false),
ForceNew: true,
},
"domain_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -202,23 +205,39 @@ func resourceNsxtPolicyLdapIdentitySourceProbeAndUpdate(d *schema.ResourceData,
altDomainNames := getStringListFromSchemaList(d, "alternative_domain_names")
ldapServers := getLdapServersFromSchema(d)

var structValue *data.StructValue
obj := nsxModel.LdapIdentitySource{
DisplayName: &displayName,
Description: &description,
Revision: &revision,
Tags: tags,
DomainName: &domainName,
BaseDn: &baseDn,
AlternativeDomainNames: altDomainNames,
LdapServers: ldapServers,
ResourceType: serverType,
var dataValue data.DataValue
var errs []error
if serverType == activeDirectoryType {
obj := nsxModel.ActiveDirectoryIdentitySource{
DisplayName: &displayName,
Description: &description,
Revision: &revision,
Tags: tags,
DomainName: &domainName,
BaseDn: &baseDn,
AlternativeDomainNames: altDomainNames,
LdapServers: ldapServers,
ResourceType: nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE,
}
dataValue, errs = converter.ConvertToVapi(obj, nsxModel.ActiveDirectoryIdentitySourceBindingType())
} else if serverType == openLdapType {
obj := nsxModel.OpenLdapIdentitySource{
DisplayName: &displayName,
Description: &description,
Revision: &revision,
Tags: tags,
DomainName: &domainName,
BaseDn: &baseDn,
AlternativeDomainNames: altDomainNames,
LdapServers: ldapServers,
ResourceType: nsxModel.LdapIdentitySource_RESOURCE_TYPE_OPENLDAPIDENTITYSOURCE,
}
dataValue, errs = converter.ConvertToVapi(obj, nsxModel.OpenLdapIdentitySourceBindingType())
}
dataValue, errs := converter.ConvertToVapi(obj, nsxModel.LdapIdentitySourceBindingType())
if errs != nil {
return errs[0]
}
structValue = dataValue.(*data.StructValue)
structValue := dataValue.(*data.StructValue)

log.Printf("[INFO] Probing LDAP Identity Source with ID %s", id)
probeResult, err := ldapClient.Probeidentitysource(structValue)
Expand Down Expand Up @@ -284,8 +303,12 @@ func resourceNsxtPolicyLdapIdentitySourceRead(d *schema.ResourceData, m interfac

ldapObj := obj.(nsxModel.LdapIdentitySource)
resourceType := ldapObj.ResourceType
if resourceType != nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE &&
resourceType != nsxModel.LdapIdentitySource_RESOURCE_TYPE_OPENLDAPIDENTITYSOURCE {
var dServerType string
if resourceType == nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE {
dServerType = activeDirectoryType
} else if resourceType == nsxModel.LdapIdentitySource_RESOURCE_TYPE_OPENLDAPIDENTITYSOURCE {
dServerType = openLdapType
} else {
return fmt.Errorf("unrecognized LdapIdentitySource Resource Type %s", resourceType)
}

Expand All @@ -296,7 +319,7 @@ func resourceNsxtPolicyLdapIdentitySourceRead(d *schema.ResourceData, m interfac
d.Set("description", ldapObj.Description)
d.Set("revision", ldapObj.Revision)
setPolicyTagsInSchema(d, ldapObj.Tags)
d.Set("type", resourceType)
d.Set("type", dServerType)
d.Set("domain_name", ldapObj.DomainName)
d.Set("base_dn", ldapObj.BaseDn)
d.Set("alternative_domain_names", ldapObj.AlternativeDomainNames)
Expand Down
5 changes: 2 additions & 3 deletions nsxt/resource_nsxt_policy_ldap_identity_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
nsxModel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

var accTestPolicyLdapIdentitySourceCreateAttributes = map[string]string{
Expand All @@ -24,7 +23,7 @@ var accTestPolicyLdapIdentitySourceUpdateAttributes = map[string]string{

func TestAccResourceNsxtPolicyLdapIdentitySource_basic(t *testing.T) {
testResourceName := "nsxt_policy_ldap_identity_source.test"
ldapType := nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE
ldapType := activeDirectoryType

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -92,7 +91,7 @@ func TestAccResourceNsxtPolicyLdapIdentitySource_basic(t *testing.T) {

func TestAccResourceNsxtPolicyLdapIdentitySource_import_basic(t *testing.T) {
testResourceName := "nsxt_policy_ldap_identity_source.test"
ldapType := nsxModel.LdapIdentitySource_RESOURCE_TYPE_ACTIVEDIRECTORYIDENTITYSOURCE
ldapType := activeDirectoryType

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down
6 changes: 3 additions & 3 deletions website/docs/r/policy_ldap_identity_source.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This resource provides a method for the management of LDAP identity sources.
resource "nsxt_policy_ldap_identity_source" "test" {
display_name = "Airius LDAP"
description = "Airius LDAP Identity Source"
type = "ActiveDirectoryIdentitySource"
type = "ActiveDirectory"
domain_name = "airius.com"
base_dn = "DC=airius, DC=com"
Expand All @@ -40,8 +40,8 @@ The following arguments are supported:
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
* `type` - (Required) Indicates the type of the LDAP identity source. Valid options:
* `ActiveDirectoryIdentitySource` - This is an Active Directory identity source.
* `OpenLdapIdentitySource` - This is an OpenLDAP identity source.
* `ActiveDirectory` - This is an Active Directory identity source.
* `OpenLdap` - This is an OpenLDAP identity source.
* `domain_name` - (Required) Authentication domain name. This is the name of the authentication domain. When users log into NSX using an identity of the form "user@domain", NSX uses the domain portion to determine which LDAP identity source to use.
* `base_dn` - (Required) DN of subtree for user and group searches.
* `alternative_domain_names` - (Optional) Additional domains to be directed to this identity source. After parsing the "user@domain", the domain portion is used to select the LDAP identity source to use. Additional domains listed here will also be directed to this LDAP identity source. In Active Directory these are sometimes referred to as Alternative UPN Suffixes.
Expand Down

0 comments on commit b7aae56

Please sign in to comment.