Skip to content

Commit

Permalink
Merge pull request #7 from barryib/add-importer
Browse files Browse the repository at this point in the history
feat: add importer for `ldap_group`
  • Loading branch information
pablo-ruth authored Oct 29, 2020
2 parents 3f1a233 + 0862f9c commit 2aaf635
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/resources/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ resource "ldap_group" "group" {
## Attribute Reference

* `id` - LDAP group DN.

## Import

LDAP group can be imported using the full LDAP DN (`id`), e.g.

```
$ terraform import ldap_group.example CN=MyGroup,OU=MyOU,DC=domain,DC=tld
```
18 changes: 17 additions & 1 deletion ldap/resource_ldap_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ldap

import (
"fmt"
"strings"

"github.com/Ouest-France/goldap"
"github.com/go-ldap/ldap/v3"
Expand All @@ -14,6 +15,9 @@ func resourceLDAPGroup() *schema.Resource {
Read: resourceLDAPGroupRead,
Update: resourceLDAPGroupUpdate,
Delete: resourceLDAPGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"ou": &schema.Schema{
Expand Down Expand Up @@ -66,14 +70,26 @@ func resourceLDAPGroupCreate(d *schema.ResourceData, m interface{}) error {
func resourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))
dn := d.Id()

attributes, err := client.ReadGroup(dn)
if err != nil {
if err.(*ldap.Error).ResultCode == 32 {
// Object doesn't exist
d.SetId("")
return nil
}
return err
}

if err := d.Set("name", attributes["name"][0]); err != nil {
return err
}

// Remove the `CN=<group-name>,` from the DN to get the OU
ou := strings.ReplaceAll(dn, fmt.Sprintf("CN=%s,", attributes["name"][0]), "")
if err := d.Set("ou", ou); err != nil {
return err
}

desc := ""
Expand Down

0 comments on commit 2aaf635

Please sign in to comment.