-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/groups_datasource show_all_flag #520
Changes from all commits
e04031c
6fdbce1
7a7f596
b9bcc79
b0dd8c4
944a7a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ func groupsDataSource() *schema.Resource { | |
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
ExactlyOneOf: []string{"display_names", "object_ids"}, | ||
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"}, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validate.UUID, | ||
|
@@ -46,12 +46,19 @@ func groupsDataSource() *schema.Resource { | |
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
ExactlyOneOf: []string{"display_names", "object_ids"}, | ||
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"}, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validate.NoEmptyStrings, | ||
}, | ||
}, | ||
|
||
"return_all": { | ||
Description: "Retrieve all groups with no filter", | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ExactlyOneOf: []string{"display_names", "object_ids", "return_all"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -62,13 +69,27 @@ func groupsDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inte | |
|
||
var groups []msgraph.Group | ||
var expectedCount int | ||
var returnAll = d.Get("return_all").(bool) | ||
|
||
var displayNames []interface{} | ||
if v, ok := d.GetOk("display_names"); ok { | ||
displayNames = v.([]interface{}) | ||
} | ||
|
||
if len(displayNames) > 0 { | ||
if returnAll { | ||
result, _, err := client.List(ctx, odata.Query{}) | ||
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") | ||
} | ||
Comment on lines
+84
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check for nil pointers before dereferencing as doing |
||
if len(*result) == 0 { | ||
return tf.ErrorDiagPathF(err, "return_all", "No groups found") | ||
} | ||
|
||
groups = append(groups, *result...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This notation is a useful shortcut to iterating with a for loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't know this one - Thank you for that - I will read through all these comments :) |
||
} else if len(displayNames) > 0 { | ||
expectedCount = len(displayNames) | ||
for _, v := range displayNames { | ||
displayName := v.(string) | ||
|
@@ -105,7 +126,7 @@ func groupsDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inte | |
} | ||
} | ||
|
||
if len(groups) != expectedCount { | ||
if !returnAll && len(groups) != expectedCount { | ||
return tf.ErrorDiagF(fmt.Errorf("Expected: %d, Actual: %d", expectedCount, len(groups)), "Unexpected number of groups returned") | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to check errors separately so it's more obvious when they occur