Skip to content

Commit

Permalink
add tests, enhance DiffSuppressFunc for data_json
Browse files Browse the repository at this point in the history
  • Loading branch information
l-with committed Aug 17, 2024
1 parent e52dfc3 commit 110ba80
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
25 changes: 8 additions & 17 deletions ldap/resource_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"github.com/go-ldap/ldap/v3"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -36,6 +35,9 @@ func resourceLDAPEntry() *schema.Resource {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
if d.Id() == "" {
return false
}
var oldLdapEntry client.LdapEntry
json.Unmarshal([]byte(oldValue), &oldLdapEntry.Entry)
var newLdapEntry client.LdapEntry
Expand Down Expand Up @@ -143,8 +145,9 @@ func resourceLDAPEntryImport(_ context.Context, d *schema.ResourceData, _ interf

func resourceLDAPEntryRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
cl := m.(*client.Client)
// return diag.Errorf("should not read")

id := d.Id()
dn := d.Get(attributeNameDn).(string)

var attributes *[]string
{
Expand All @@ -157,29 +160,20 @@ func resourceLDAPEntryRead(ctx context.Context, d *schema.ResourceData, m interf
attributes = client.GetAttributeNames(&ldapEntry)
}

ldapEntry, err := cl.ReadEntryByDN(id, "("+dummyFilter+")", attributes)
ldapEntry, err := cl.ReadEntryByDN(dn, "("+dummyFilter+")", attributes)
if err != nil {
if err.(*ldap.Error).ResultCode == ldap.LDAPResultNoSuchObject {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
ignoreAndBase64Encode := getIgnoreAndBase64encode(d)
ignoreRDNAttributes := client.GetRDNAttributes(ldapEntry, id)
ignoreRDNAttributes := client.GetRDNAttributes(ldapEntry, dn)
if ignoreRDNAttributes != nil {
*ignoreAndBase64Encode.IgnoreAttributes = append(*ignoreAndBase64Encode.IgnoreAttributes, *ignoreRDNAttributes...)
}
client.IgnoreAndBase64encodeAttributes(ldapEntry, ignoreAndBase64Encode)

err = d.Set(attributeNameDn, id)
if err != nil {
return diag.FromErr(err)
}

jsonData, err := json.Marshal(ldapEntry.Entry)
if err != nil {
return diag.Errorf("error marshaling JSON for %q: %s", id, err)
return diag.Errorf("error marshaling JSON for %q: %s", dn, err)
}

err = d.Set(attributeNameDataJson, string(jsonData))
Expand Down Expand Up @@ -214,9 +208,6 @@ func resourceLDAPEntryCreate(ctx context.Context, d *schema.ResourceData, m inte

d.SetId(dn)

//return diag.FromErr(errors.New("test"))
//time.Sleep(120 * time.Second)

return resourceLDAPEntryRead(ctx, d, m)
}

Expand Down
73 changes: 65 additions & 8 deletions ldap/resource_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestAccResourceLdapEntry(t *testing.T) {
if err != nil {
return err
}
_, uuidInEntry := ldapEntry.Entry["uuid"]
if uuidInEntry {
return errors.New("uuid: expected to be ignored, got '" + ldapEntry.Entry["uuid"][0] + "'")
_, uidInEntry := ldapEntry.Entry["uid"]
if uidInEntry {
return errors.New("uid: expected to be ignored, got '" + ldapEntry.Entry["uid"][0] + "'")
}
return nil
},
Expand All @@ -47,6 +47,12 @@ func TestAccResourceLdapEntry(t *testing.T) {
if ldapEntry.Entry["cn"][0] != "Jim Mit" {
return errors.New("cn: expected 'Jim Mit', got '" + ldapEntry.Entry["cn"][0] + "'")
}
if ldapEntry.Entry["street"][0] != "Street" {
return errors.New("street: expected 'Street', got '" + ldapEntry.Entry["street"][0] + "'")
}
if ldapEntry.Entry["userPassword"][0] != "userPassword" {
return errors.New("userPassword: expected 'userPassword', got '" + ldapEntry.Entry["userPassword"][0] + "'")
}
return nil
},
),
Expand All @@ -57,6 +63,29 @@ func TestAccResourceLdapEntry(t *testing.T) {
),
),
},
{
Config: testAccResourceEntryIgnoreAttributes(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("ldap_entry.users_example_com", "dn", "ou=users,dc=example,dc=com"),
//resource.TestCheckResourceAttr("ldap_entry.user_jimmit2", "dn", "uid=jimmit02,ou=users,dc=example,dc=com"),
resource.TestCheckResourceAttrWith(
"ldap_entry.user_jimmit",
"data_json",
func(value string) error {
var ldapEntry client.LdapEntry
err := json.Unmarshal([]byte(value), &ldapEntry.Entry)
if err != nil {
return err
}
_, userPasswordInEntry := ldapEntry.Entry["userPassword"]
if userPasswordInEntry {
return errors.New("userPassword: expected to be ignored, got '" + ldapEntry.Entry["userPassword"][0] + "'")
}
return nil
},
),
),
},
},
})
}
Expand All @@ -73,11 +102,13 @@ resource "ldap_entry" "users_example_com" {
resource "ldap_entry" "user_jimmit" {
dn = "uid=jimmit01,${ldap_entry.users_example_com.dn}"
data_json = jsonencode({
objectClass = ["inetOrgPerson"]
ou = ["users"]
givenName = ["Jim"]
sn = ["Mit"]
cn = ["Jim Mit"]
objectClass = ["inetOrgPerson"]
ou = ["users"]
givenName = ["Jim"]
sn = [ldap_entry.users_example_com.id]
cn = ["Jim Mit"]
userPassword = ["userPassword"]
street = ["Street"]
})
}
Expand All @@ -94,3 +125,29 @@ data "ldap_entries" "search" {
}
`)
}

func testAccResourceEntryIgnoreAttributes() string {
return fmt.Sprintf(`
resource "ldap_entry" "users_example_com" {
dn = "ou=users,dc=example,dc=com"
data_json = jsonencode({
objectClass = ["organizationalUnit"]
})
}
resource "ldap_entry" "user_jimmit" {
dn = "uid=jimmit01,${ldap_entry.users_example_com.dn}"
ignore_attributes = [
"userPassword"
]
data_json = jsonencode({
objectClass = ["inetOrgPerson"]
ou = ["users"]
givenName = ["Jim"]
sn = [ldap_entry.users_example_com.id]
cn = ["Jim Mit"]
street = ["Street"]
})
}
`)
}

0 comments on commit 110ba80

Please sign in to comment.