diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index 844ad82..51dc88d 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -22,9 +22,6 @@ jobs: - name: Run Golang CI Lint uses: golangci/golangci-lint-action@v2 - - name: Clean Lint - run: rm -rf golangci-lint-* - - name: Import GPG key id: import_gpg uses: crazy-max/ghaction-import-gpg@v2 diff --git a/docs/data-sources/group.md b/docs/data-sources/group.md index a23a508..d810b91 100644 --- a/docs/data-sources/group.md +++ b/docs/data-sources/group.md @@ -15,6 +15,7 @@ data "ldap_group" "group" { * `name` - (Required) LDAP group name. * `ou` - (Required) OU where LDAP group will be search. +* `scope` - (Optional) LDAP search scope (1: BaseObject, 2: SingleLevel, 3: WholeSubtree) Defaults to `1`. ## Attribute Reference diff --git a/go.mod b/go.mod index 365c114..9689c50 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Ouest-France/terraform-provider-ldap go 1.18 require ( - github.com/Ouest-France/goldap v0.5.5 + github.com/Ouest-France/goldap v0.6.0 github.com/go-ldap/ldap/v3 v3.4.3 github.com/hashicorp/terraform-plugin-sdk/v2 v2.15.0 ) diff --git a/go.sum b/go.sum index eaf6af7..6b499b8 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e h1:ZU22z/2YRFLyf/P4ZwUYSdNCWsMEI0VeyrFoI2rAhJQ= github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/Ouest-France/goldap v0.5.5 h1:7WOWMf9UXL+ZnP/43O+eUX5NCtUzjSbrmwxr+7ck5Ms= -github.com/Ouest-France/goldap v0.5.5/go.mod h1:XuJUZlzUQJPDX1KNlTqnjXdJcKs0Vdlp7nsoefb7zuU= +github.com/Ouest-France/goldap v0.6.0 h1:dWBLGUwKa7H3e9WHidUqzeQo85UNo6pGHqnKA5Q1KdA= +github.com/Ouest-France/goldap v0.6.0/go.mod h1:HekerH+zN6sfJbhlK7UvWMSLP4lOtccjCDOofKahT5A= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= diff --git a/ldap/data_source_resource_ldap_group.go b/ldap/data_source_resource_ldap_group.go index 04bf640..69e1d64 100644 --- a/ldap/data_source_resource_ldap_group.go +++ b/ldap/data_source_resource_ldap_group.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/Ouest-France/goldap" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -29,6 +30,12 @@ func dataSourceLDAPGroup() *schema.Resource { Type: schema.TypeString, Required: true, }, + "scope": { + Description: "LDAP search scope", + Type: schema.TypeInt, + Optional: true, + Default: 0, + }, "description": { Description: "Description attribute for the LDAP", Type: schema.TypeString, @@ -52,9 +59,30 @@ func dataSourceLDAPGroup() *schema.Resource { } func dataSourceLDAPGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string)) - d.SetId(dn) + // Get scope + scope := d.Get("scope").(int) + if scope < 0 || scope > 2 { + return diag.FromErr(fmt.Errorf("scope must be between 0 and 2, got %d", scope)) + } + + if scope == 0 { + // If scope is 0, we keep the old code to ensure backward compatibility + dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string)) + + d.SetId(dn) + } else { + // If scope is 1 or 2, we search the group DN given the group name and the OU + client := m.(*goldap.Client) + + // Search group + dn, err := client.SearchGroupByName(d.Get("name").(string), d.Get("ou").(string), scope) + if err != nil { + return diag.FromErr(err) + } + + d.SetId(dn) + } // Add context key to signal the Read is called from a datasource return resourceLDAPGroupRead(context.WithValue(ctx, CallerTypeKey, DatasourceCaller), d, m)