Skip to content

Commit

Permalink
data.azuread_users: rename show_all to return_all, add nil check
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Sep 2, 2021
1 parent b0dd8c4 commit 6afa9ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
8 changes: 5 additions & 3 deletions docs/data-sources/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ When authenticated with a user principal, this data source does not require any

## Example Usage

*Look up by group name*
```terraform
data "azuread_groups" "groups" {
display_names = ["group-a", "group-b"]
}
```

*Look up all groups*
```terraform
data "azuread_groups" "allGroups" {
show_all = true
return_all = true
}
```

Expand All @@ -34,9 +36,9 @@ The following arguments are supported:

* `display_names` - (Optional) The display names of the groups.
* `object_ids` - (Optional) The object IDs of the groups.
* `show_all` - (Optional) A flag to denote if all groups should be fetched and returned.
* `return_all` - (Optional) A flag to denote if all groups should be fetched and returned.

~> One of `display_names`, `object_ids` or `show_all` should be specified. Either of the first two _may_ be specified as an empty list, in which case no results will be returned.
~> One of `display_names`, `object_ids` or `return_all` should be specified. Either of the first two _may_ be specified as an empty list, in which case no results will be returned.

## Attributes Reference

Expand Down
29 changes: 17 additions & 12 deletions internal/services/groups/groups_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func groupsDataSource() *schema.Resource {
Type: schema.TypeList,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"display_names", "object_ids", "show_all"},
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validate.UUID,
Expand All @@ -46,18 +46,18 @@ func groupsDataSource() *schema.Resource {
Type: schema.TypeList,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"display_names", "object_ids", "show_all"},
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validate.NoEmptyStrings,
},
},

"show_all": {
Description: "Boolean flag for no filter on groups returned.",
"return_all": {
Description: "Retrieve all groups with no filter",
Type: schema.TypeBool,
Optional: true,
ExactlyOneOf: []string{"display_names", "object_ids", "show_all"},
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"},
},
},
}
Expand All @@ -69,21 +69,26 @@ func groupsDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inte

var groups []msgraph.Group
var expectedCount int
var showAll = d.Get("show_all").(bool)
var returnAll = d.Get("return_all").(bool)

var displayNames []interface{}
if v, ok := d.GetOk("display_names"); ok {
displayNames = v.([]interface{})
}

if showAll {
if returnAll {
result, _, err := client.List(ctx, odata.Query{})
if err != nil || len(*result) < 1 {
return tf.ErrorDiagPathF(err, "show_all", "No groups found.")
if err != nil {
return tf.ErrorDiagF(err, "Could not retrieve groups")
}
if result == nil {
return tf.ErrorDiagF(errors.New("API returned nil result"), "Bad API Response")
}
if len(*result) == 0 {
return tf.ErrorDiagPathF(err, "return_all", "No groups found")
}

groups = append(groups, (*result)[0])

groups = append(groups, *result...)
} else if len(displayNames) > 0 {
expectedCount = len(displayNames)
for _, v := range displayNames {
Expand Down Expand Up @@ -121,7 +126,7 @@ func groupsDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inte
}
}

if len(groups) != expectedCount && !showAll {
if !returnAll && len(groups) != expectedCount {
return tf.ErrorDiagF(fmt.Errorf("Expected: %d, Actual: %d", expectedCount, len(groups)), "Unexpected number of groups returned")
}

Expand Down
10 changes: 5 additions & 5 deletions internal/services/groups/groups_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func TestAccGroupsDataSource_noNames(t *testing.T) {
})
}

func TestAccGroupsDataSource_allNames(t *testing.T) {
func TestAccGroupsDataSource_returnAll(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_groups", "test")

data.DataSourceTest(t, []resource.TestStep{
{
Config: GroupsDataSource{}.showAll(),
Config: GroupsDataSource{}.returnAll(),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("display_names.#").Exists(),
check.That(data.ResourceName).Key("object_ids.#").Exists(),
Expand Down Expand Up @@ -114,10 +114,10 @@ data "azuread_groups" "test" {
`
}

func (GroupsDataSource) showAll() string {
func (GroupsDataSource) returnAll() string {
return `
data "azuread_groups" "test" {
show_all = true
return_all = true
}
`
}
}

0 comments on commit 6afa9ab

Please sign in to comment.