Skip to content
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

Add data source & resource group_members #148

Closed
wants to merge 6 commits into from
Closed

Add data source & resource group_members #148

wants to merge 6 commits into from

Conversation

stawik-mesa
Copy link
Contributor

@stawik-mesa stawik-mesa commented Sep 8, 2021

This is a proposed resource & datasource which allows to manage all group members of a group in an authoritative way.

Primary used instead of group_member when many groups & members are managed to increase the performance and reduce the number of api requests during the state refresh.

#147

@megan07 megan07 self-requested a review September 14, 2021 19:13
Copy link
Contributor

@megan07 megan07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Sorry for the delay in review, thanks for implementing this!

internal/provider/resource_group_members.go Outdated Show resolved Hide resolved
internal/provider/resource_group_members.go Outdated Show resolved Hide resolved
internal/provider/resource_group_members_test.go Outdated Show resolved Hide resolved
Required: true,
},

"members": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to set the hash here, you can copy over this file from the google provider then add something like this, I used raw["email"] when I was playing around with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for not saying anything yesterday. I'm wondering if we would be better off making this a TypeList and writing a diffSuppressFunc to suppress any diffs on order? TypeSet can sometimes be a nuisance to maintain, and in this case, the diffSuppress might be easier. Do you have thoughts for/against this idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work with TypeList? hashicorp/terraform-plugin-sdk#477
The function is giving me strings and not entries from a list. How will this work? Do you have an example?

I used TypeSet to fix the issue with the unordered list

internal/provider/data_source_group_members_test.go Outdated Show resolved Hide resolved
internal/provider/data_source_group_members.go Outdated Show resolved Hide resolved
internal/provider/data_source_group_members_test.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@stawik-mesa stawik-mesa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@megan07 thank you for the review

I also added the ForceNew: true on the field group_id.

internal/provider/resource_group_members.go Outdated Show resolved Hide resolved
@stawik-mesa stawik-mesa requested a review from megan07 September 15, 2021 07:07
Copy link
Contributor

@megan07 megan07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the extra thoughts, I'm running through tests locally to see how things turn out, so more things popped up this time. Thanks again!

internal/provider/resource_group_members.go Show resolved Hide resolved
"role": member.Role,
"type": member.Type,
"status": member.Status,
"delivery_settings": member.DeliverySettings,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this yesterday, but thought I was able to get it working - apparenlty I didn't? deliverySettings isn't returned by the API when we do a List, so we need to find a way to assign it the value we intended, I think.

Just throwing something together, I got this to work - but there very well might be a friendlier way to do it:

	for i, member := range membersObj.Members {
		deliverySettings := resourceGroupMembers().Schema["members"].Elem.(*schema.Resource).Schema["delivery_settings"].Default.(string)
		if ds := d.Get(fmt.Sprintf("members.%d.delivery_settings", i)); ds != nil {
			deliverySettings = ds.(string)
		}

		members[i] = map[string]interface{}{
			...
			"delivery_settings": deliverySettings,
			...
		}
	}

internal/provider/resource_group_members.go Outdated Show resolved Hide resolved
Required: true,
},

"members": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for not saying anything yesterday. I'm wondering if we would be better off making this a TypeList and writing a diffSuppressFunc to suppress any diffs on order? TypeSet can sometimes be a nuisance to maintain, and in this case, the diffSuppress might be easier. Do you have thoughts for/against this idea?

@stawik-mesa stawik-mesa requested a review from megan07 September 16, 2021 08:24
@@ -213,12 +212,18 @@ func resourceGroupMembersRead(ctx context.Context, d *schema.ResourceData, meta

members := make([]interface{}, len(membersObj.Members))
for i, member := range membersObj.Members {
// Use value if presnet or default as "delivery_settings" is not provided by API
deliverySettings := resourceGroupMembers().Schema["members"].Elem.(*schema.Resource).Schema["delivery_settings"].Default.(string)
if ds := d.Get(fmt.Sprintf("members.%d.delivery_settings", i)); ds != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that I copied this over from when I was testing it as a TypeList, so the index i here needs to actually be the hash of the object that we saved in the Set function. Then, as I was looking into that, I conferred with some other team members and we realized that example I sent you was actually broken there, broken in the sense that it actually ends up as the default Set function. Since I don't work with TypeSet much (or ever) I've spent the day going over this and am still trying to decide if we want a Set or a List. I'm sorry I don't have more for you today, but I'm running through tests and will hopefully have more details for you tomorrow! Thanks for your patience!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I think we will have to keep it TypeSet. There's a bug in the diffSuppressFunc that will prevent us from moving forward with a list. That being said...a coworker and I discussed it and rather than trying to find out the hash here, we can do a configMembers := d.Get("members").(*schema.Set), then loop through there to find the correct configured member and just grab it's deliverySettings. Does that make sense?

configMembers := d.Get("members").(*schema.Set)

	members := make([]interface{}, len(membersObj.Members))
	for i, member := range membersObj.Members {

		// Use value if present or default as "delivery_settings" is not provided by API
		deliverySettings := deliverySettingsDefault

		for _, cm := range configMembers.List() {
			cMem := cm.(map[string]interface{})
			if cMem["email"].(string) == member.Email {
				deliverySettings = cMem["delivery_settings"].(string)
				break
			}
		}

You can also get rid of that Set function that I suggested you create earlier too. So sorry about all the confusion. And also, if you wouldn't mind making the "ALL_MAIL" a constant at the top (I have deliverySettingsDefault here) so we can have it defined in one place, and not have that long line of code we had earlier.

Again, sorry for all the confusion on this! If at any point you don't have the time to finish it off, let me know and I can pick it up! There may need to be some further tweaking in the tests as they seem a bit flakey, but might just be some eventual consistency problems. Thanks so much for your help on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Thanks for all the hints. I applied your suggestions, but if you have time to work on it, it would be great because I think we get the feature then quite faster ;)

@stawik-mesa
Copy link
Contributor Author

thanks @megan07. Close PR, follow-up: #155

@stawik-mesa stawik-mesa deleted the group_members_resource branch September 21, 2021 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants