Skip to content

Commit

Permalink
Merge branch 'add-skip-attributes'
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverisaac committed Feb 8, 2021
2 parents d7df031 + 524b0d1 commit ce8953f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions provider/resource_ldap_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func resourceLDAPObject() *schema.Resource {
Set: schema.HashString,
Optional: true,
},
"select_attributes": {
Type: schema.TypeSet,
Description: "Only attributes in this list will be modified by the provider",
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -132,6 +139,13 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
attributesToSkip = append(attributesToSkip, attr.(string))
}

// retrieve attributes to skip from HCL
attributesToSet := []string{}
for _, attr := range (d.Get("select_attributes").(*schema.Set)).List() {
log.Printf("[DEBUG] ldap_object::create - object %q set to only modify: %q", dn, attr.(string))
attributesToSet = append(attributesToSet, attr.(string))
}

// if there is a non empty list of attributes, loop though it and
// create a new map collecting attribute names and its value(s); we need to
// do this because we could not model the attributes as a map[string][]string
Expand All @@ -150,6 +164,10 @@ func resourceLDAPObjectCreate(d *schema.ResourceData, meta interface{}) error {
if stringSliceContains(attributesToSkip, name) {
continue
}
if len(attributesToSet) > 0 && !stringSliceContains(attributesToSet, name) {
log.Printf("[DEBUG] ldap_object::create - %q skipping unselected attribute", dn, name)
continue
}
log.Printf("[DEBUG] ldap_object::create - %q has attribute[%v] => %v (%T)", dn, name, value, value)
v := toAttributeValue(name, value.(string))
m[name] = append(m[name], v)
Expand Down Expand Up @@ -287,6 +305,13 @@ func readLDAPObject(d *schema.ResourceData, meta interface{}, updateState bool)
attributesToSkip = append(attributesToSkip, attr.(string))
}

// retrieve attributes to set from HCL
attributesToSet := []string{}
for _, attr := range (d.Get("select_attributes").(*schema.Set)).List() {
log.Printf("[DEBUG] ldap_object::create - object %q set to only modify: %q", dn, attr.(string))
attributesToSet = append(attributesToSet, attr.(string))
}

// now deal with attributes
set := &schema.Set{
F: attributeHash,
Expand All @@ -299,6 +324,10 @@ func readLDAPObject(d *schema.ResourceData, meta interface{}, updateState bool)
log.Printf("[DEBUG] ldap_object::read - skipping attribute %q of %q", attribute.Name, dn)
continue
}
if len(attributesToSet) > 0 && !stringSliceContains(attributesToSet, attribute.Name) {
log.Printf("[DEBUG] ldap_object::read - skipping unselected attribute %q of %q", attribute.Name, dn)
continue
}
if len(attribute.Values) == 1 {
// we don't treat the RDN as an ordinary attribute
a := fmt.Sprintf("%s=%s", attribute.Name, attribute.Values[0])
Expand Down

0 comments on commit ce8953f

Please sign in to comment.