AWS 㨠GCP 㨠Azure ã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªï¼Pythonï¼ã®ããã¥ã¡ã³ãã®ããã
æ¯åè¿·åã«ãªãã®ã§ã¾ã¨ãã¨ãã¾ãã
AWS
æ¦è¦
ã®ä¸ã«ãé«ã¬ãã«ã® Resource 㨠ä½ã¬ãã«ã® Client ãããã¾ãã
ãã¹ã¦ã®ãµã¼ãã¹ã§ Resource ãå®è£
ããã¦ããããã§ã¯ãªãã®ã§ãçµ±ä¸æ§ãæ±ãã¦å人çã«ã¯ Client ã使ã£ã¦ãã¾ããã§ããResource 㯠Collection ã® all()ï¼ec2.instances.all()ï¼ã filter() ãå¼ã¶ã¨ã¤ãã¬ã¼ã¿ãè¿ã£ã¦ãã¦ãããã for ã§åãã°ããã®ã§ãã¡ãã¡ paginator ã®å¦çãæ¸ããªãã¦ããã®ã¯ä¾¿å©ã§ããã
Resource
- ãªãã¡ã¬ã³ã¹
- https://boto3.amazonaws.com/v1/documentation/api/latest/index.html#api-reference
- 使ããããµã¼ãã¹ãæ¢ãã¦ããã®ä¸ã«
Resourcesã®ãªã³ã¯ãããã°å®è£ ããã¦ãã¾ããããã«ãªããã°å®è£ ããã¦ã¾ããããã¨ãããã¨ãªã®ã§Clientã使ãã¾ãããã - ã¤ã³ã¹ã¿ã³ã¹ãªã©ã®ä¸è¦§ãå¾ãã¨ãã«ä½¿ã Collection ã®ããã¥ã¡ã³ãã¯ã
Resourcesã®ãªã³ã¯ã®å ã®Service Resourceã®ãªã³ã¯ã®å ã«ããã¾ãã
- 使ããããµã¼ãã¹ãæ¢ãã¦ããã®ä¸ã«
- https://boto3.amazonaws.com/v1/documentation/api/latest/index.html#api-reference
- ãµã³ãã«ã³ã¼ã
- Resource ã Action ã®ãã¼ã¸ã«ã³ã¼ãçãæ²è¼ããã¦ããã®ã§ããããåèã«ããã¨ããã§ãã
Client
- ãªãã¡ã¬ã³ã¹
- https://boto3.amazonaws.com/v1/documentation/api/latest/index.html#api-reference
- 使ããããµã¼ãã¹ãæ¢ãã¦ããã®ä¸ã®
Clientã®ãªã³ã¯ããªãã¡ã¬ã³ã¹ã§ãã
- 使ããããµã¼ãã¹ãæ¢ãã¦ããã®ä¸ã®
- https://boto3.amazonaws.com/v1/documentation/api/latest/index.html#api-reference
- ãµã³ãã«ã³ã¼ã
- Client ããã®ã¡ã½ããã®ãã¼ã¸ã«ã³ã¼ãçãæ²è¼ããã¦ããã®ã§ããããåèã«ããã¨ããã§ãã
tips
ããããã®ã¢ã«ã¦ã³ãã§ãªã«ãããã
ããããã®ã¢ã«ã¦ã³ãã§ãªã«ããããã¨ãã«ãããããã®ã¢ã«ã¦ã³ãã®ã¯ã¬ãã³ã·ã£ã«ãç¨æããã®ã¯ããã¸ããªã®ã§ãOrganizations ãå°å ¥ãã¦ããã®ãåæã§ãããassume role ã使ãã¨ããã§ãã
ãããªæãã®ãç¨æãã¦ã
def assumed_session( *, management_session: boto3.session.Session, assumed_account_id: str, role_name='OrganizationAccountAccessRole', ) -> boto3.session.Session: res = management_session.client('sts').assume_role( RoleArn=f"arn:aws:iam::{assumed_account_id}:role/{role_name}", RoleSessionName=f"{assumed_account_id}@{role_name}"[:64], ) return boto3.session.Session( aws_access_key_id=res['Credentials']['AccessKeyId'], aws_secret_access_key=res['Credentials']['SecretAccessKey'], aws_session_token=res['Credentials']['SessionToken'], )
管çã¢ã«ã¦ã³ãã®ã»ãã·ã§ã³ã¨ã¡ã³ãã¼ã¢ã«ã¦ã³ãã® ID ãæ¸¡ãã¨ãã¡ã³ãã¼ã¢ã«ã¦ã³ãã®ã»ãã·ã§ã³ãè¿ãã®ã§ãããã使ã£ã¦ Resource ã Client ãä½ã£ã¦å¦çãã¾ãã
ããã«ãä»»æã®é¢æ°ãåã㦠concurrent.futures.ThreadPoolExecutor ã使ã£ã¦ä¸¦åå®è¡ãã便å©é¢æ°ãç¨æãã¦ããã¨ããããæãã¾ãã
- è¤æ°ã®ã¢ã«ã¦ã³ãã® IAM ã¦ã¼ã¶ã¼ã®æ å ±ãå¾ãã¹ã¯ãªãã
GCP
æ¦è¦
- https://github.com/googleapis/google-cloud-python (
google.cloud) - https://github.com/googleapis/google-api-python-client (
googleapiclient)
ã® 2 種é¡ãããã¾ãã
https://cloud.google.com/apis/docs/client-libraries-explained ã«ä¾ãã°ã
ä¸é¨ã® Google Cloud APIs ã§ã¯ãè¨èªã«ãã£ã¦ã¯ Cloud ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªãå©ç¨ã§ãã¾ããããããã® API ã®ããããã使ç¨ããéã«å¸æããè¨èªã® Cloud ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªãåå¨ããªãå ´åã¯ã以åã®ã¹ã¿ã¤ã«ã®ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªï¼Google API ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªï¼ãå¼ãç¶ã使ç¨ã§ãã¾ãã
ã¨ã®ãã¨ãªã®ã§ã使ããããµã¼ãã¹ãé«ã¬ãã«ã® google-cloud-python ã§å®è£ ããã¦ããã°ããããããã§ãªããã°ä½ã¬ãã«ã® google-api-python-client ã使ãã¨ããããã§ãã
ããæï¼ä»ã¯æ¹åããã¦ãããç¥ãã¾ããï¼ãgoogle-cloud-python ãæå³ããéãã«åããªãã¦ããã£ããã¨ãããã®ã§ãèªå㯠google-api-python-client ã使ãããã«ãã¦ãã¾ãã
google-cloud-python
- ã¬ãã¸ããª
- ãªãã¡ã¬ã³ã¹
- https://cloud.google.com/python/docs/reference?hl=en
- æ¬æã«å©ç¨å¯è½ãªãµã¼ãã¹ã®ãªã³ã¯ã並ãã§ã¾ãããæ¥æ¬èªçã¯ç½ ã§ããEnglish ã«åãæ¿ãã¾ãããã
- 2024-05-08 æç¹ã®æ¥æ¬èªçã¯ãå©ç¨å¯è½ãªãµã¼ãã¹ããã¹ã¦åæããã¦ãããããªã³ã¯å ã«é£ãã§ãå 容ãèããã¼ã¸ï¼google-cloud-python ã®ãªãã¡ã¬ã³ã¹ãã¼ã¸ã§ã¯ãªããAPI ã®èª¬æã®ä¸ã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã®ä¸ç¯ï¼ã表示ããã¡ããã¾ãã
- ãããã¯ãæ¬æã§ã¯ãªãå·¦æ±ã®ä¸è¦§ãã使ããããµã¼ãã¹ãè¦ã¤ãã¦ã OK ã§ãã
- æ¬æã«å©ç¨å¯è½ãªãµã¼ãã¹ã®ãªã³ã¯ã並ãã§ã¾ãããæ¥æ¬èªçã¯ç½ ã§ããEnglish ã«åãæ¿ãã¾ãããã
- https://cloud.google.com/python/docs/reference?hl=en
- ãµã³ãã«ã³ã¼ã
- https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-SERVICENAME/samples/ ã«ããã¾ãã
- google-cloud-python åãµã¼ãã¹ã® Overview ã® Code samples and snippets ãã辿ãããã®ãããã¾ãã
google-api-python-client
- ã¬ãã¸ããª
- ãªãã¡ã¬ã³ã¹
- https://github.com/googleapis/google-api-python-client/blob/main/docs/dyn/index.md
- 使ããããµã¼ãã¹ãè¦ã¤ãã¦ãã ããã
- ãªã³ã¯å ã®ãã¼ã¸ã¯ããããæ®ºé¢¨æ¯ãªã®ã§ãå¾è¿°ã® API ãã®ãã®ã®ãªãã¡ã¬ã³ã¹ããããã¦èªãã ã»ããããã¨æãã¾ãã
- 使ããããµã¼ãã¹ãè¦ã¤ãã¦ãã ããã
- https://github.com/googleapis/google-api-python-client/blob/main/docs/dyn/index.md
- ãµã³ãã«ã³ã¼ã
- https://github.com/googleapis/google-api-python-client/tree/main/samples
- Pagination
- https://github.com/googleapis/google-api-python-client/blob/main/docs/pagination.md
- ã¤ã³ã¹ã¿ã³ã¹ã®ä¸è¦§ã¨ããçµæãããããããå ´åã«ããããã«ã¼ããåãã¦ãã¹ã¦ã®çµæãå¾ãã¢ã¬ã§ãã
- ã¨ãã«ãhttps://github.com/googleapis/google-api-python-client/tree/main/docs ã¯ããããã人ã®ã¬ããããæããããããã¥ã¡ã³ãã§ãããå 容çã«ãä¸èªãã¦ãããã»ããããã§ãã
API
- ãªãã¡ã¬ã³ã¹
- https://developers.google.com/apis-explorer
- ããããé£ã¹ã¾ãã
- https://developers.google.com/apis-explorer
tips
update ã¯ä¸¸ãã¨ç½®ãæãã®å ´åãå¤ãã®ã§æ³¨æãã¾ããã
update ã¡ã½ããã¯ãã¼ãã¶ã¾ããã¨ç·å ¥ãæ¿ãã®å ´åãå¤ãã§ããããã¥ã¡ã³ãã«æ³¨ææ¸ããããã¯ãã§ãããè¦è½ã¨ãã¦é¨åæ´æ°ã®ã¤ããã§ä¸é¨ã®ãã©ã¡ã¼ã¿ã ãæ·»ã㦠update ããã¨ãæå®ããªãã£ããã©ã¡ã¼ã¿ã®å¤ãæ¶ãã¡ãããã¨ãããã®ã§æ³¨æãã¾ãããã
å¤ãã® API ã§ã¯é¨åæ´æ°ã® patch ã¡ã½ãããæä¾ããã¦ããã®ã§ããã使ãããã¾ãç¾å¨ã®å¤ãå¾ãå¾ãä¸é¨ã®å¤ã夿´ãã¦ãã¹ã¦ update ã¡ã½ããã«æ·»ããããã¾ãããã
gcloud ã® credentials ãæåãã
API ã¢ã¯ã»ã¹ã«å¿
è¦ãª credentials ã¯è²ã
ãªæ¹æ³ã§ç¨æã§ãã¾ãããäºã gcloud auth login ãã¦çæããã credentials ãæåãããã¨ãã§ãã¾ãã
#!/usr/bin/env python3 import json import os import sqlite3 import sys import google.auth.transport.requests import google.oauth2.credentials def credentials_from_gcloud() -> google.oauth2.credentials.Credentials: """gcloud ã® credentials ãæåãã""" con = sqlite3.connect(os.path.expanduser('~/.config/gcloud/credentials.db')) con.row_factory = sqlite3.Row rows = con.execute(''' SELECT * FROM credentials WHERE account_id LIKE "%@gmail.com" -- é©å½ã«å¤ãã¦ä½¿ã£ã¦ãã ããã ''').fetchall() assert len(rows) == 1 cred_data = json.loads(rows[0]['value']) cred = google.oauth2.credentials.Credentials( 'gcloud', client_id=cred_data['client_id'], client_secret=cred_data['client_secret'], token_uri=cred_data['token_uri'], refresh_token=cred_data['refresh_token'], ) try: cred.refresh(google.auth.transport.requests.Request()) except google.auth.exceptions.RefreshError as e: print('æéåãã¦ãã¿ããã ãã gcloud auth login ãã¦ããã', file=sys.stderr) raise e return cred credentials = credentials_from_gcloud()
ããã¸ã§ã¯ãã®ä¸è¦§ãå¾ãæ¹æ³
Resource Manager API ã® projects.list ã§ããã¸ã§ã¯ãã®ä¸è¦§ãå¾ããã¨ãã§ãã¾ãã
ããGoogle Workspaceï¼æ§ GSuiteï¼ãå©ç¨ãã¦ãã¦çµç¹ãªã½ã¼ã¹ãåå¨ããå ´åãä½ãèããã« projects.list ããã¨ãã³ãã ããæ°ã®ããã¸ã§ã¯ããè¿ã£ã¦ããããããã¾ããã
ããã¯ãApps Script ããã¸ã§ã¯ãã使ãããã¨åæã« GCP ã®ããã¸ã§ã¯ãã使ãããããã çµç¹/system-gsuite/apps-script/ ã®ãã©ã«ãã®ä¸ã«é
ç½®ãããããã§ãã
ãªã®ã§ããªã½ã¼ã¹ã®ç®¡ç ã§ apps-script ãã©ã«ãã® IDï¼æ°åã§ãï¼ã調ã¹ã¦ããã®ãã©ã«ã ID ã親ã¨ããããã¸ã§ã¯ãã¯é¤å¤ããããã«ããã°ããã§ãã
import googleapiclient.discovery projects: list[dict] = [] # é¤å¤ãã apps-script ãã©ã«ãã® ID apps_script_folder_id = '999999999999' crm_client = googleapiclient.discovery.build( 'cloudresourcemanager', 'v3', credentials=credentials, ) projects_r = crm_client.projects() request = projects_r.list( filter=f"NOT parent.id={apps_script_folder_id}" ) while request is not None: response = request.execute() projects.extend(response['projects']) request = projects_r.list_next(request, response) for project in projects: print(project['projectId'])
ã¾ããè«æ±å ã¢ã«ã¦ã³ãã«ãªã³ã¯ããã¦ããããã¸ã§ã¯ãã®ä¸è¦§ï¼ï¼é課éã®éè¯ããã¸ã§ã¯ãã¯é¤å¤ãããï¼ãªããCloud Billing API ã® billingAccounts.projects.list ã使ãã®ãããã¨æãã¾ãã
gcloud ãã¢ã¯ã»ã¹ãã¦ãã API ãç¥ããã
--verbosity=debug ãæå®ãã㨠API ã®ã¨ã³ããã¤ã³ãã表示ããã¾ãã
ä¾ãã°ãgcloud alpha services quota list ãã¢ã¯ã»ã¹ãã¦ãã API ãç¥ãããæã¯ããã®ããã«å®è¡ããã¨ã
$ gcloud --verbosity=debug alpha services quota list --service='bigquery.googleapis.com' --consumer='projects/oreno-project'` DEBUG: Running [gcloud.alpha.services.quota.list] with arguments: [--consumer: "projects/oreno-project", --service: "bigquery.googleapis.com", --verbosity: "debug"] DEBUG: Starting new HTTPS connection (1): serviceusage.googleapis.com:443 DEBUG: https://serviceusage.googleapis.com:443 "GET /v1beta1/projects/oreno-project/services/bigquery.googleapis.com/consumerQuotaMetrics?alt=json HTTP/1.1" 200 None ...
ã¨åºåãããService Usage API ã® v1beta1 ã® services.consumerQuotaMetrics ã«ã¢ã¯ã»ã¹ãã¦ãããã¨ããããã¾ãã
Azure
æ¦è¦
ãµã¼ãã¹ãã¨ã«ããã±ã¼ã¸ãåããã¦ãã¦ãã¡ãããããããããã¾ãã
Python API ãã©ã¦ã¶ã¼ ã§æ¢ãã§ãããã
API
tips
ãµã³ãã«ã³ã¼ã
azure-sdk-for-python ã® sdk/*/*/generated_samples/ ã«ããã®ã§åèã«ããã¨ããã§ãã
ã¯ã¬ãã³ã·ã£ã«
az login ã§ãã°ã¤ã³ããç¶æ
ãªã azure.identity.DefaultAzureCredential() ã§å¾ãããã
åºæ¬å½¢
ä¾ãã° Subscriptions ã ã¨ãããªæãã
# XxxClient ã¯ãAPI ãªãã¡ã¬ã³ã¹ã® Management / azure.mgmt.subscription / Overview ãè¦ã㨠XxxClient ã®ä¸è¦§ãããã subscription_client = azure.mgmt.subscription.SubscriptionClient(credential=DefaultAzureCredential()) # subscriptions ã¨ãã¯ãAPI ãªãã¡ã¬ã³ã¹ã® Management / azure.mgmt.subscription / XxxClient ã® Variables ã«ä¸è¦§ãããã # get ã list ã¨ãã®ã¡ã½ããã¯ãAPI ãªãã¡ã¬ã³ã¹ã® Management / azure.mgmt.subscription / operations / XxxOperations ã«ä¸è¦§ãããã res = subscription_client.subscriptions.get(subscription_id='xxx') # çµæï¼ã®è¦ç´ ï¼ã¯ as_dict() ã§ dict ã«å¤æã§ããã£ã½ã print(res.as_dict())
API ãã¼ã¸ã§ã³ã®æå®
çµè«ããè¨ãã¨ãAPI ãã¼ã¸ã§ã³ã®æå®ã¯ããªãã
ãµã¼ãã¹ã«ãã£ã¦ã¯è¤æ°ã®ãã¼ã¸ã§ã³ã®å®è£ ããã£ããããã
ä¾ãã° azure.mgmt.resource.ResourceManagementClient ã ã¨ãããããã ã
ææ°ãã¼ã¸ã§ã³ã ã使ãã°ããããã§ã¯ãªãããã¼ã¸ã§ã³ã«ãã£ã¦å®è£ ããã¦ãããã®ã墿¸ãããµã¼ãã¹ãããï¼
ãã¼ã¸ã§ã³ã®æå®ã¯ XxxClient ã® api_version ã§æå®ã§ããã®ã§ãå¿
è¦ãªãµã¼ãã¹ã«å¿ãã¦ããã§ç´°ãããã¼ã¸ã§ã³æå®â¦ããå¿
è¦ã¯ãªãã¦ããã¼ã¸ã§ã³æå®ããªããã° ããæãã«ææ°ã®ãã¼ã¸ã§ã³ã使ã£ã¦ãããã¿ãããå°ãªãã¨ã 2025-05 æç¹ã®ããã±ã¼ã¸ã§ã¯ã
使ç¨ãã¦ãã API ãã¼ã¸ã§ã³ã®ç¢ºèªã¯ logging.basicConfig(level=logging.DEBUG) ããã¨ãªã¯ã¨ã¹ããã URL ã表示ããããã§ãããã§ç¢ºèªããã®ãæã£åãæ©ãã
Graph API
AD ã°ã«ã¼ãã¨ã㯠Graph API ã使ãã¿ããã
- https://learn.microsoft.com/ja-jp/graph/overview
- https://github.com/microsoftgraph/msgraph-sdk-python
docs/ã®ä¸ã«ã¡ãã£ã¨ãµã³ãã«ã³ã¼ããããã