-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from barryib/user-datasource
feat: Add datasource for LDAP user
- Loading branch information
Showing
5 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# ldap_user Data Source | ||
|
||
`ldap_user` is a data source for retrieving an LDAP user. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "ldap_user" "user" { | ||
ou = "OU=MyOU,DC=domain,DC=tld" | ||
name = "MyUser" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `ou` - (Required) OU where LDAP user will be search. | ||
* `name` - (Optional) The name of the LDAP user. | ||
* `sam_account_name` - (Optional) The sAMAccountName of the LDAP user. | ||
* `user_principal_name` - (Optional) The userPrincipalName of the LDAP user. | ||
|
||
## Attribute Reference | ||
|
||
* `id` - LDAP user DN. | ||
* `description` - Description attribute for the LDAP user. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ldap | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Ouest-France/goldap" | ||
"github.com/go-ldap/ldap/v3" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceLDAPUser() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceLDAPUserRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ou": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "sam_account_name", "user_principal_name"}, | ||
}, | ||
"sam_account_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"user_principal_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceLDAPUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
return resourceLDAPUserRead(context.WithValue(ctx, CallerTypeKey, DatasourceCaller), d, m) | ||
} | ||
|
||
func resourceLDAPUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*goldap.Client) | ||
|
||
user, err := client.ReadUser(d.Get("ou").(string), d.Get("name").(string), d.Get("sam_account_name").(string), d.Get("user_principal_name").(string)) | ||
|
||
if err != nil { | ||
if err.(*ldap.Error).ResultCode == ldap.LDAPResultNoSuchObject { | ||
// Object doesn't exist | ||
|
||
// If Read is called from a datasource, return an error | ||
if ctx.Value(CallerTypeKey) == DatasourceCaller { | ||
return diag.FromErr(err) | ||
} | ||
|
||
// If not a call from datasource, remove the resource from the state | ||
// and cleanly return | ||
d.SetId("") | ||
return nil | ||
} | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(user["distinguishedName"][0]) | ||
|
||
if val, ok := user["name"]; ok { | ||
if err := d.Set("name", val[0]); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
if val, ok := user["sAMAccountName"]; ok { | ||
if err := d.Set("sam_account_name", val[0]); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
if val, ok := user["userPrincipalName"]; ok { | ||
if err := d.Set("user_principal_name", val[0]); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
if val, ok := user["description"]; ok { | ||
if err := d.Set("description", val[0]); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
return diag.FromErr(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters