github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/adminorg_administration.go (about)

     1  /*
     2   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  	"github.com/vmware/go-vcloud-director/v2/util"
    13  )
    14  
    15  // LdapConfigure allows to configure LDAP mode in use by the Org
    16  func (adminOrg *AdminOrg) LdapConfigure(settings *types.OrgLdapSettingsType) (*types.OrgLdapSettingsType, error) {
    17  	util.Logger.Printf("[DEBUG] Configuring LDAP mode for Org name %s", adminOrg.AdminOrg.Name)
    18  
    19  	// Xmlns field is not mandatory when `types.OrgLdapSettingsType` is set as part of whole
    20  	// `AdminOrg` structure but it must be set when directly updating LDAP. For that reason
    21  	// `types.OrgLdapSettingsType` Xmlns struct tag has 'omitempty' set
    22  	settings.Xmlns = types.XMLNamespaceVCloud
    23  
    24  	href := adminOrg.AdminOrg.HREF + "/settings/ldap"
    25  	_, err := adminOrg.client.ExecuteRequest(href, http.MethodPut, types.MimeOrgLdapSettings,
    26  		"error updating LDAP settings: %s", settings, nil)
    27  	if err != nil {
    28  		return nil, fmt.Errorf("error updating LDAP mode for Org name '%s': %s", adminOrg.AdminOrg.Name, err)
    29  	}
    30  
    31  	ldapSettings, err := adminOrg.GetLdapConfiguration()
    32  	if err != nil {
    33  		return nil, fmt.Errorf("error retrieving LDAP configuration:  %s", err)
    34  	}
    35  
    36  	return ldapSettings, nil
    37  }
    38  
    39  // LdapDisable wraps LdapConfigure to disable LDAP configuration for org
    40  func (adminOrg *AdminOrg) LdapDisable() error {
    41  	_, err := adminOrg.LdapConfigure(&types.OrgLdapSettingsType{OrgLdapMode: types.LdapModeNone})
    42  	return err
    43  }
    44  
    45  // GetLdapConfiguration retrieves LDAP configuration structure
    46  func (adminOrg *AdminOrg) GetLdapConfiguration() (*types.OrgLdapSettingsType, error) {
    47  	util.Logger.Printf("[DEBUG] Reading LDAP configuration for Org name %s", adminOrg.AdminOrg.Name)
    48  
    49  	ldapSettings := &types.OrgLdapSettingsType{}
    50  
    51  	href := adminOrg.AdminOrg.HREF + "/settings/ldap"
    52  
    53  	_, err := adminOrg.client.ExecuteRequest(href, http.MethodGet, types.MimeOrgLdapSettings,
    54  		"error getting LDAP settings: %s", nil, ldapSettings)
    55  
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return ldapSettings, nil
    61  }