Я действительно нуб на Python, и сейчас я пытаюсь получить и отфильтровать результат запроса get-iam-policy на GCP, используя это:
from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
# REQUIRED: The resource for which the policy is being requested.
# See the operation documentation for the appropriate value for this field.
resource = 'my-resource' # TODO: Update placeholder value.
get_iam_policy_request_body = {
# TODO: Add desired entries to the request body.
}
request = service.projects().getIamPolicy(resource=resource, body=get_iam_policy_request_body)
response = request.execute()
pprint(response)
Он работает нормально и я получаю такой результат:
"version": 1,
"etag": "VfRwfrGwlFjdc=",
"bindings": [
{
"role": "roles/cloudasset.serviceAgent",
"members": [
"serviceAccount:service-999999999999@gcp-sa-cloudasset.iam.gserviceaccount.com"
]
},
{
"role": "roles/cloudbuild.builds.builder",
"members": [
"serviceAccount:999999999999@cloudbuild.gserviceaccount.com"
]
},
{
"role": "roles/cloudbuild.serviceAgent",
"members": [
"serviceAccount:service-999999999999@gcp-sa-cloudbuild.iam.gserviceaccount.com"
]
},
{
"role": "roles/cloudfunctions.serviceAgent",
"members": [
"serviceAccount:service-999999999999@gcf-admin-robot.iam.gserviceaccount.com"
]
},
{
"role": "roles/cloudsql.client",
"members": [
"serviceAccount:myproject-backup@myproject.iam.gserviceaccount.com"
]
},
{
"role": "roles/compute.serviceAgent",
"members": [
"serviceAccount:service-999999999999@compute-system.iam.gserviceaccount.com"
]
},
{
"role": "roles/container.serviceAgent",
"members": [
"serviceAccount:service-999999999999@container-engine-robot.iam.gserviceaccount.com"
]
},
{
"role": "roles/containeranalysis.ServiceAgent",
"members": [
"serviceAccount:service-999999999999@container-analysis.iam.gserviceaccount.com"
]
},
{
"role": "roles/containerscanning.ServiceAgent",
"members": [
"serviceAccount:service-999999999999@gcp-sa-containerscanning.iam.gserviceaccount.com"
]
},
{
"role": "roles/containerthreatdetection.serviceAgent",
"members": [
"serviceAccount:service-999999999999@gcp-sa-ktd-control.iam.gserviceaccount.com"
]
},
{
"role": "roles/editor",
"members": [
"serviceAccount:999999999999-compute@developer.gserviceaccount.com",
"serviceAccount:999999999999@cloudservices.gserviceaccount.com",
"serviceAccount:myproject@appspot.gserviceaccount.com",
"serviceAccount:service-999999999999@containerregistry.iam.gserviceaccount.com"
]
},
{
"role": "roles/owner",
"members": [
"serviceAccount:terraform@myproject.iam.gserviceaccount.com",
"user:myuser@acme.com"
]
},
{
"role": "roles/servicenetworking.serviceAgent",
"members": [
"serviceAccount:service-999999999999@service-networking.iam.gserviceaccount.com"
]
},
{
"role": "roles/websecurityscanner.serviceAgent",
"members": [
"serviceAccount:service-999999999999@gcp-sa-websecurityscanner.iam.gserviceaccount.com"
]
}
]
}
Но я не знаю, как выполнить фильтрацию для получения только членов группы «роли / владелец». В моем случае:
{
"role": "roles/owner",
"members": [
"serviceAccount:terraform@myproject.iam.gserviceaccount.com",
"user:myuser@acme.com"
]
}
и поместите их в переменную для последующей печати или повторного использования
есть ли у кого-нибудь представление о том, как это сделать?
Есть хороший день.