protected override void ProcessRecord() { ProjectsResource.GetIamPolicyRequest request = Service.Projects.GetIamPolicy(new GetIamPolicyRequest(), $"{Project}"); Policy policy = request.Execute(); WriteObject(policy.Bindings, true); }
protected override void ProcessRecord() { string role = GetRole(); string member = GetMember(); // Remove the role from existing bindings. ProjectsResource.GetIamPolicyRequest getRequest = Service.Projects.GetIamPolicy(new GetIamPolicyRequest(), $"{Project}"); Policy existingPolicy = getRequest.Execute(); bool needToExecuteRequest = false; foreach (Binding binding in existingPolicy.Bindings) { if (string.Equals(role, binding.Role, StringComparison.OrdinalIgnoreCase)) { if (binding.Members.Contains(member, StringComparer.OrdinalIgnoreCase)) { binding.Members.Remove(member); needToExecuteRequest = true; } break; } } if (!needToExecuteRequest) { WriteObject(existingPolicy.Bindings, true); } else { if (ShouldProcess($"{member}", $"Remove IAM policy binding in project '{Project}' for role '{role}'")) { var requestBody = new SetIamPolicyRequest() { Policy = existingPolicy }; ProjectsResource.SetIamPolicyRequest setRequest = Service.Projects.SetIamPolicy(requestBody, $"{Project}"); Policy changedPolicy = setRequest.Execute(); WriteObject(changedPolicy.Bindings, true); } } }
protected override void ProcessRecord() { string role = GetRole(); string member = GetMember(); // We have to search through all the existing bindings and try to insert the role if possible // because otherwise, we will delete all the existing bindings. If the role is already there, // we don't need to execute the request. ProjectsResource.GetIamPolicyRequest getRequest = Service.Projects.GetIamPolicy(new GetIamPolicyRequest(), $"{Project}"); Policy existingPolicy = getRequest.Execute(); bool needToExecuteRequest = true; bool bindingFound = false; foreach (Binding binding in existingPolicy.Bindings) { if (string.Equals(role, binding.Role, StringComparison.OrdinalIgnoreCase)) { bindingFound = true; if (!binding.Members.Contains(member, StringComparer.OrdinalIgnoreCase)) { binding.Members.Add(member); } else { needToExecuteRequest = false; } break; } } if (!bindingFound) { var newBinding = new Binding() { Role = role, Members = new List <string>() { member } }; existingPolicy.Bindings.Add(newBinding); needToExecuteRequest = true; } if (!needToExecuteRequest) { WriteObject(existingPolicy.Bindings, true); } else { var requestBody = new SetIamPolicyRequest() { Policy = existingPolicy }; ProjectsResource.SetIamPolicyRequest setRequest = Service.Projects.SetIamPolicy(requestBody, $"{Project}"); Policy changedPolicy = setRequest.Execute(); WriteObject(changedPolicy.Bindings, true); } }