[autoscaler] GCP: only call setIamPolicy if necessary (#3782)

This commit is contained in:
James Casbon
2019-02-04 00:16:00 +00:00
committed by Richard Liaw
parent b8cc176b4d
commit 976f018dab
+9 -1
View File
@@ -127,7 +127,6 @@ def _configure_project(config):
assert config["provider"]["project_id"] is not None, (
"'project_id' must be set in the 'provider' section of the autoscaler"
" config. Notice that the project id must be globally unique.")
project = _get_project(project_id)
if project is None:
@@ -386,20 +385,29 @@ def _add_iam_policy_binding(service_account, roles):
policy = crm.projects().getIamPolicy(resource=project_id).execute()
already_configured = True
for role in roles:
role_exists = False
for binding in policy["bindings"]:
if binding["role"] == role:
if member_id not in binding["members"]:
binding["members"].append(member_id)
already_configured = False
role_exists = True
if not role_exists:
already_configured = False
policy["bindings"].append({
"members": [member_id],
"role": role,
})
if already_configured:
# In some managed environments, an admin needs to grant the
# roles, so only call setIamPolicy if needed.
return
result = crm.projects().setIamPolicy(
resource=project_id, body={
"policy": policy,