列出和获取政策
本页介绍了如何列出和获取政策。
准备工作
列出政策
以下示例展示了如何列出组织中的政策。
Python
以下示例展示了如何使用 Python 列出政策:
"""Sample script to demonstrate the use of the List method in the Policy API."""
import json
import pprint
import time
import urllib.request
import google.auth.transport.requests
from absl import app
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.policies']
BASE_URL = 'https://cloudidentity.googleapis.com/'
# Change this to the location of the service-account credentials.
SA_FILE = ''
# Enter the administrator to call as here.
ADMIN_EMAIL = ''
PAGE_SIZE = 100
# To list all policies, set FILTER to '';
# To list policies for a specific customer, set FILTER to
# 'customer == "customers/{obfuscated_target_customer_id}"';
# To list policies for a specific Application, such as Gmail, set FILTER to
# 'setting.type.matches("gmail.*")';
# To list policies for a specific Setting, such as service_status, set FILTER to
# 'setting.type.matches(".*service_status")'.
FILTER = ''
def create_delegated_credentials(user_email):
credentials = service_account.Credentials.from_service_account_file(
SA_FILE, scopes=SCOPES
)
delegated_credentials = credentials.with_subject(user_email)
return delegated_credentials
def build_list_policies_request(page_size, filter, page_token, access_token):
list_url = (
BASE_URL
+ 'v1beta1/'
+ 'policies?'
+ 'page_size='
+ str(page_size)
+ '&filter='
+ filter
+ '&page_token='
+ page_token
)
request = urllib.request.Request(list_url)
request.add_header('Authorization', 'Bearer ' + access_token)
return request
def call_list_policies_api(request):
content = urllib.request.urlopen(request).read()
response = json.loads(content)
return response
def call_list_policies_api_till_last_page(access_token):
page_token = ''
# Paginate until the last page.
while True:
list_policies_request = build_list_policies_request(
PAGE_SIZE, FILTER, page_token, access_token
)
list_policies_response = call_list_policies_api(list_policies_request)
if 'nextPageToken' not in list_policies_response:
print('This is the last page.')
break
print_list_policies_response(list_policies_response)
page_token = list_policies_response['nextPageToken']
time.sleep(1)
def print_list_policies_response(response):
pp = pprint.PrettyPrinter(indent=4)
if 'policies' in response:
for policy in response['policies']:
pp.pprint(policy)
print('Next page token: ' + response['nextPageToken'])
def main(unused_argv):
dc = create_delegated_credentials(ADMIN_EMAIL)
dc.refresh(google.auth.transport.requests.Request())
call_list_policies_api_till_last_page(dc.token)
if __name__ == '__main__':
app.run(main)
获取政策
以下示例展示了如何检索特定政策。
Python
以下示例展示了如何使用 Python 检索特定政策:
"""Sample script to demonstrate the use of the get method in the Policy API."""
import json
import pprint
import time
import urllib.request
import google.auth.transport.requests
from absl import app
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.policies']
BASE_URL = 'https://cloudidentity.googleapis.com/'
# Change this to the location of the service-account credentials.
SA_FILE = ''
# Enter the administrator to call as here.
ADMIN_EMAIL = ''
# Set POLICY_NAME to policy.name (policies/{obfuscated_policy_id}) to call
# GetPolicy API.
POLICY_NAME = 'policies/...'
def build_get_policy_request(policy_name, access_token):
list_url = BASE_URL + 'v1beta1/' + policy_name
request = urllib.request.Request(list_url)
request.add_header('Authorization', 'Bearer ' + access_token)
return request
def call_get_policy_api(access_token):
request = build_get_policy_request(POLICY_NAME, access_token)
content = urllib.request.urlopen(request).read()
response = json.loads(content)
print_get_policy_response(response)
def print_get_policy_response(response):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(response)
def main(unused_argv):
dc = create_delegated_credentials(ADMIN_EMAIL)
dc.refresh(google.auth.transport.requests.Request())
call_get_policy_api(dc.token)
if __name__ == '__main__':
app.run(main)