ポリシーの一覧表示と取得
このページでは、ポリシーを一覧表示して取得する方法を示したコードサンプルを示します。
始める前に
Policy API の設定を完了します。
リスト型ポリシー
次の例は、組織内のポリシーを一覧表示する方法を示しています。
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)
print_list_policies_response(list_policies_response)
if 'nextPageToken' not in list_policies_response or not list_policies_response['nextPageToken']:
print('This is the last page.')
break
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)
if 'nextPageToken' in response:
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)