Skip to content

Commit 3f06fb4

Browse files
authored
Merge pull request #1 from bushelpowered/add-skip-attributes
Add skip attributes option
2 parents 83bb9f7 + 524b0d1 commit 3f06fb4

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

provider/resource_ldap_object.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ func resourceLDAPObject() *schema.Resource {
6060
},
6161
Optional: true,
6262
},
63+
"skip_attributes": {
64+
Type: schema.TypeSet,
65+
Description: "A list of attributes which will not be tracked by the provider",
66+
Elem: &schema.Schema{Type: schema.TypeString},
67+
Set: schema.HashString,
68+
Optional: true,
69+
},
70+
"select_attributes": {
71+
Type: schema.TypeSet,
72+
Description: "Only attributes in this list will be modified by the provider",
73+
Elem: &schema.Schema{Type: schema.TypeString},
74+
Set: schema.HashString,
75+
Optional: true,
76+
},
6377
},
6478
}
6579
}
@@ -118,6 +132,20 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
118132
}
119133
request.Attribute("objectClass", objectClasses)
120134

135+
// retrieve attributes to skip from HCL
136+
attributesToSkip := []string{"objectClass"}
137+
for _, attr := range (d.Get("skip_attributes").(*schema.Set)).List() {
138+
log.Printf("[DEBUG] ldap_object::create - object %q set to skip: %q", dn, attr.(string))
139+
attributesToSkip = append(attributesToSkip, attr.(string))
140+
}
141+
142+
// retrieve attributes to skip from HCL
143+
attributesToSet := []string{}
144+
for _, attr := range (d.Get("select_attributes").(*schema.Set)).List() {
145+
log.Printf("[DEBUG] ldap_object::create - object %q set to only modify: %q", dn, attr.(string))
146+
attributesToSet = append(attributesToSet, attr.(string))
147+
}
148+
121149
// if there is a non empty list of attributes, loop though it and
122150
// create a new map collecting attribute names and its value(s); we need to
123151
// do this because we could not model the attributes as a map[string][]string
@@ -133,6 +161,13 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
133161
log.Printf("[DEBUG] ldap_object::create - %q has attribute of type %T", dn, attribute)
134162
// each map should only have one entry (see resource declaration)
135163
for name, value := range attribute.(map[string]interface{}) {
164+
if stringSliceContains(attributesToSkip, name) {
165+
continue
166+
}
167+
if len(attributesToSet) > 0 && !stringSliceContains(attributesToSet, name) {
168+
log.Printf("[DEBUG] ldap_object::create - %q skipping unselected attribute", dn, name)
169+
continue
170+
}
136171
log.Printf("[DEBUG] ldap_object::create - %q has attribute[%v] => %v (%T)", dn, name, value, value)
137172
v := toAttributeValue(name, value.(string))
138173
m[name] = append(m[name], v)
@@ -156,6 +191,15 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
156191
return resourceLDAPObjectRead(d, meta)
157192
}
158193

194+
func stringSliceContains(haystack []string, needle string) bool {
195+
for _, h := range haystack {
196+
if needle == h {
197+
return true
198+
}
199+
}
200+
return false
201+
}
202+
159203
func resourceLDAPObjectRead(d *schema.ResourceData, meta interface{}) error {
160204
return readLDAPObject(d, meta, true)
161205
}
@@ -254,18 +298,36 @@ func readLDAPObject(d *schema.ResourceData, meta interface{}, updateState bool)
254298
d.SetId(dn)
255299
d.Set("object_classes", sr.Entries[0].GetAttributeValues("objectClass"))
256300

301+
// retrieve attributes to skip from HCL
302+
attributesToSkip := []string{"objectClass"}
303+
for _, attr := range (d.Get("skip_attributes").(*schema.Set)).List() {
304+
log.Printf("[DEBUG] ldap_object::create - object %q set to skip: %q", dn, attr.(string))
305+
attributesToSkip = append(attributesToSkip, attr.(string))
306+
}
307+
308+
// retrieve attributes to set from HCL
309+
attributesToSet := []string{}
310+
for _, attr := range (d.Get("select_attributes").(*schema.Set)).List() {
311+
log.Printf("[DEBUG] ldap_object::create - object %q set to only modify: %q", dn, attr.(string))
312+
attributesToSet = append(attributesToSet, attr.(string))
313+
}
314+
257315
// now deal with attributes
258316
set := &schema.Set{
259317
F: attributeHash,
260318
}
261319

262320
for _, attribute := range sr.Entries[0].Attributes {
263321
log.Printf("[DEBUG] ldap_object::read - treating attribute %q of %q (%d values: %v)", attribute.Name, dn, len(attribute.Values), attribute.Values)
264-
if attribute.Name == "objectClass" {
322+
if stringSliceContains(attributesToSkip, attribute.Name) {
265323
// skip: we don't treat object classes as ordinary attributes
266324
log.Printf("[DEBUG] ldap_object::read - skipping attribute %q of %q", attribute.Name, dn)
267325
continue
268326
}
327+
if len(attributesToSet) > 0 && !stringSliceContains(attributesToSet, attribute.Name) {
328+
log.Printf("[DEBUG] ldap_object::read - skipping unselected attribute %q of %q", attribute.Name, dn)
329+
continue
330+
}
269331
if len(attribute.Values) == 1 {
270332
// we don't treat the RDN as an ordinary attribute
271333
a := fmt.Sprintf("%s=%s", attribute.Name, attribute.Values[0])

0 commit comments

Comments
 (0)