Skip to content

Commit

Permalink
Merge pull request #212 from yannickstruyf3/bugfix/user_group-enhance…
Browse files Browse the repository at this point in the history
…ments

changed print messages and added filter based on DN
  • Loading branch information
marinsalinas authored Dec 1, 2020
2 parents eb53523 + 0e32b63 commit 088e1af
Showing 1 changed file with 63 additions and 22 deletions.
85 changes: 63 additions & 22 deletions nutanix/data_source_nutanix_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ func dataSourceNutanixUserGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixUserGroupRead,
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Optional: true,
"user_group_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"user_group_name", "user_group_distinguished_name"},
},
"name": {
"user_group_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"uuid"},
ConflictsWith: []string{"user_group_id", "user_group_distinguished_name"},
},
"user_group_distinguished_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"user_group_id", "user_group_name"},
},
"api_version": {
Type: schema.TypeString,
Expand Down Expand Up @@ -197,23 +203,26 @@ func dataSourceNutanixUserGroup() *schema.Resource {
}

func dataSourceNutanixUserGroupRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading User: %s", d.Id())
log.Printf("[DEBUG] Reading Group: %s", d.Id())

// Get client connection
conn := meta.(*Client).API

uuid, iok := d.GetOk("uuid")
name, nok := d.GetOk("name")
uuid, iok := d.GetOk("user_group_id")
name, nok := d.GetOk("user_group_name")
dname, dnok := d.GetOk("user_group_distinguished_name")

if !iok && !nok {
return fmt.Errorf("please provide one of uuid or name attributes")
if !iok && !nok && !dnok {
return fmt.Errorf("please provide one of user_group_id, user_group_distinguished_name or user_group_name attributes")
}

var reqErr error
var resp *v3.UserGroupIntentResponse

if iok {
resp, reqErr = findUserGroupByUUID(conn, uuid.(string))
} else if dnok {
resp, reqErr = findUserGroupByDistinguishedName(conn, dname.(string))
} else {
resp, reqErr = findUserGroupByName(conn, name.(string))
}
Expand All @@ -222,48 +231,48 @@ func dataSourceNutanixUserGroupRead(d *schema.ResourceData, meta interface{}) er
if strings.Contains(fmt.Sprint(reqErr), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return fmt.Errorf("error reading user with error %s", reqErr)
return fmt.Errorf("error reading group with error %s", reqErr)
}

m, c := setRSEntityMetadata(resp.Metadata)

if err := d.Set("metadata", m); err != nil {
return fmt.Errorf("error setting metadata for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting metadata for group UUID(%s), %s", d.Id(), err)
}
if err := d.Set("categories", c); err != nil {
return fmt.Errorf("error setting categories for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting categories for group UUID(%s), %s", d.Id(), err)
}

if err := d.Set("owner_reference", flattenReferenceValues(resp.Metadata.OwnerReference)); err != nil {
return fmt.Errorf("error setting owner_reference for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting owner_reference for group UUID(%s), %s", d.Id(), err)
}
d.Set("api_version", utils.StringValue(resp.APIVersion))
d.Set("name", utils.StringValue(resp.Status.Resources.DisplayName))

if err := d.Set("state", resp.Status.State); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

if err := d.Set("directory_service_user_group", flattenDirectoryServiceUserGroup(resp.Status.Resources.DirectoryServiceUserGroup)); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

if err := d.Set("user_group_type", resp.Status.Resources.UserGroupType); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

if err := d.Set("display_name", resp.Status.Resources.DisplayName); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

if err := d.Set("project_reference_list", flattenArrayReferenceValues(resp.Status.Resources.ProjectsReferenceList)); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

refe := flattenArrayReferenceValues(resp.Status.Resources.AccessControlPolicyReferenceList)

if err := d.Set("access_control_policy_reference_list", refe); err != nil {
return fmt.Errorf("error setting state for image UUID(%s), %s", d.Id(), err)
return fmt.Errorf("error setting state for group UUID(%s), %s", d.Id(), err)
}

d.SetId(*resp.Metadata.UUID)
Expand All @@ -272,7 +281,6 @@ func dataSourceNutanixUserGroupRead(d *schema.ResourceData, meta interface{}) er
}

func flattenDirectoryServiceUserGroup(dsu *v3.DirectoryServiceUserGroup) []interface{} {

if dsu != nil {
directoryServiceUserMap := map[string]interface{}{}

Expand All @@ -289,6 +297,14 @@ func flattenDirectoryServiceUserGroup(dsu *v3.DirectoryServiceUserGroup) []inter
}

func findUserGroupByName(conn *v3.Client, name string) (*v3.UserGroupIntentResponse, error) {
return findUserGroupByAttribute(conn, matchUserGroupByName, name)
}

func findUserGroupByDistinguishedName(conn *v3.Client, name string) (*v3.UserGroupIntentResponse, error) {
return findUserGroupByAttribute(conn, matchUserGroupByDistinguishedName, name)
}

func findUserGroupByAttribute(conn *v3.Client, matches func(*v3.UserGroupIntentResponse, string) bool, targetAttributeValue string) (*v3.UserGroupIntentResponse, error) {
//filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllUserGroup("")
if err != nil {
Expand All @@ -299,7 +315,8 @@ func findUserGroupByName(conn *v3.Client, name string) (*v3.UserGroupIntentRespo

found := make([]*v3.UserGroupIntentResponse, 0)
for _, v := range entities {
if *v.Status.Resources.DisplayName == name {
// if *v.Status.Resources.DisplayName == targetAttributeValue {
if matches(v, targetAttributeValue) {
found = append(found, v)
}
}
Expand All @@ -316,5 +333,29 @@ func findUserGroupByName(conn *v3.Client, name string) (*v3.UserGroupIntentRespo
}

func findUserGroupByUUID(conn *v3.Client, uuid string) (*v3.UserGroupIntentResponse, error) {
log.Printf("finding group via uuid: %s", uuid)
return conn.V3.GetUserGroup(uuid)
}

func matchUserGroupByDistinguishedName(userGroup *v3.UserGroupIntentResponse, name string) bool {
if userGroup != nil &&
userGroup.Status != nil &&
userGroup.Status.Resources != nil &&
userGroup.Status.Resources.DirectoryServiceUserGroup != nil &&
userGroup.Status.Resources.DirectoryServiceUserGroup.DistinguishedName != nil &&
*userGroup.Status.Resources.DirectoryServiceUserGroup.DistinguishedName == name {
return true
}
return false
}

func matchUserGroupByName(userGroup *v3.UserGroupIntentResponse, name string) bool {
if userGroup != nil &&
userGroup.Status != nil &&
userGroup.Status.Resources != nil &&
userGroup.Status.Resources.DisplayName != nil &&
*userGroup.Status.Resources.DisplayName == name {
return true
}
return false
}

0 comments on commit 088e1af

Please sign in to comment.