forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add expiryDetail to google_cloud_identity_group_membership (GoogleClo…
- Loading branch information
Showing
5 changed files
with
160 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
mmv1/templates/terraform/custom_update/cloud_identity_group_membership.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<%# The license inside this block applies to this file. | ||
# Copyright 2023 Google Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-%> | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
d.Partial(true) | ||
|
||
if d.HasChange("roles") { | ||
url, err := tpgresource.ReplaceVars(d, config, "{{CloudIdentityBasePath}}{{name}}:modifyMembershipRoles") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
// Return object for modifyMembershipRoles (we build request object from scratch, without using `obj`) | ||
b, a := d.GetChange("roles") | ||
before := b.(*schema.Set) | ||
after := a.(*schema.Set) | ||
|
||
ignoreUpdateR := make(map[string]struct{}) | ||
addRoleList := after.Difference(before).List() | ||
removeRoleList := before.Difference(after).List() | ||
|
||
var updateRolesParams []map[string]interface{} | ||
for _, addR := range addRoleList { | ||
ar := addR.(map[string]interface{})["name"].(string) | ||
ae := addR.(map[string]interface{})["expiry_detail"].([]interface {}) | ||
for _, removeR := range removeRoleList { | ||
if ar == removeR.(map[string]interface{})["name"].(string) { | ||
ignoreUpdateR[ar] = struct{}{} | ||
var updateR map[string]interface{} | ||
if len(ae) == 0 { | ||
updateR = map[string]interface{}{"name": ar} | ||
} else { | ||
updateR = map[string]interface{}{"name": ar, "expiry_detail": ae[0]} | ||
} | ||
updateP := map[string]interface{}{"field_mask": "expiryDetail.expire_time", "membership_role": updateR} | ||
updateRolesParams = append(updateRolesParams, updateP) | ||
} | ||
} | ||
} | ||
|
||
var addRoles []map[string]interface{} | ||
for _, r := range addRoleList { | ||
name := r.(map[string]interface{})["name"].(string) | ||
if _, ignore := ignoreUpdateR[name]; ignore { | ||
continue | ||
} | ||
expiryDetail := r.(map[string]interface{})["expiry_detail"].([]interface {}) | ||
if len(expiryDetail) == 0 { | ||
addRoles = append(addRoles, map[string]interface{}{"name": name}) | ||
} else { | ||
addRoles = append(addRoles, map[string]interface{}{"name": name, "expiry_detail": expiryDetail[0]}) | ||
} | ||
} | ||
var removeRoles []string | ||
for _, r := range removeRoleList { | ||
name := r.(map[string]interface{})["name"].(string) | ||
if _, ignore := ignoreUpdateR[name]; ignore { | ||
continue | ||
} | ||
removeRoles = append(removeRoles, name) | ||
} | ||
|
||
// ref: https://cloud.google.com/identity/docs/reference/rest/v1/groups.memberships/modifyMembershipRoles#request-body | ||
// Only single operation per request is allowed. | ||
if len(removeRoles) > 0 { | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: map[string]interface{}{"removeRoles": removeRoles}, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error removing GroupMembership %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished removing GroupMembership %q: %#v", d.Id(), res) | ||
} | ||
} | ||
if len(updateRolesParams) > 0 { | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: map[string]interface{}{"updateRolesParams": updateRolesParams}, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error updating GroupMembership %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished updating GroupMembership %q: %#v", d.Id(), res) | ||
} | ||
} | ||
if len(addRoles) > 0 { | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: map[string]interface{}{"addRoles": addRoles}, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error adding GroupMembership %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished adding GroupMembership %q: %#v", d.Id(), res) | ||
} | ||
} | ||
} | ||
|
||
d.Partial(false) | ||
|
||
return resourceCloudIdentityGroupMembershipRead(d, meta) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
mmv1/templates/terraform/update_encoder/cloud_identity_group_membership.go.erb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters