CloudAutomatorがAPIで操作出るようになりました!
Cloud AutomatorのREST APIを公開しました!
これまではブラウザぽちぽちだったのでありがたい。(ミスが減るね!)
せっかくなのでさわってみます。
前提条件
- 業務用のPCがWindowsなので、PowerShellでやってみます
- Invoke-RestMethodコマンド(curlのようなもの)で操作します
事前準備
REST APIキーの発行
管理コンソールからAPIキーを取得します。
方法は、冒頭のブログ記事を確認してください。
ヘッダーに指定するのは共通なので、ここで変数に設定します。
$apiKey = "********************************"
$headers = @{"Authorization" = "Bearer " + $apiKey}
アカウントの管理
アカウントの作成
APIの仕様をドキュメントで確認します。
URLを指定します。
$url="https://manager.cloudautomator.com/api/v1/aws_accounts"
BODY部をヒアストリングで生成します。
$account_name="api-test"
$aws_id="111122223333"
$access_key="XXXXXXXXXXXXXXXXXXXX"
$secret_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$body = @"
{
"name": "$account_name",
"account_number": "$aws_id",
"access_key_id": "$access_key",
"secret_access_key": "$secret_key"
}
"@
アカウントを作成します。
Invoke-RestMethod `
-Uri $url `
-Method Post `
-Headers $headers `
-Body $body `
-ContentType "application/json" `
| ConvertTo-Json
{
"data": {
"id": "1231",
"type": "aws_accounts",
"attributes": {
"name": "api-test"
}
}
}
作成したアカウントの確認
まずは、一覧を表示します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$url="https://manager.cloudautomator.com/api/v1/aws_accounts"
アカウントの一覧を表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": [
{
"id": "1231",
"type": "aws_accounts",
"attributes": "@{name=api-test}"
},
{
"id": "***",
"type": "aws_accounts",
"attributes": "@{name=****}"
}
],
"links": {
},
"meta": {
"total": 2
}
}
次に、作成したアカウントを確認します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$account_id="1231"
$url="https://manager.cloudautomator.com/api/v1/aws_accounts/$account_id"
アカウントを表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": {
"id": "1231",
"type": "aws_accounts",
"attributes": {
"name": "api-test"
}
}
}
作成したアカウントの編集
アカウント名を変更してみます。(アクセスキーの変更も同じ要領で可能です)
APIの仕様をドキュメントで確認します。
URLを指定します。
$account_id="1231"
$url="https://manager.cloudautomator.com/api/v1/aws_accounts/$account_id"
BODY部をヒアストリングで生成します。
変更したい部分のみを記述します。
$account_name="api-test-2"
$body = @"
{
"name": "$account_name"
}
"@
アカウントを編集します。
Invoke-RestMethod `
-Uri $url `
-Method Patch `
-Headers $headers `
-Body $body `
-ContentType "application/json" `
| ConvertTo-Json
{
"data": {
"id": "1231",
"type": "aws_accounts",
"attributes": {
"name": "api-test-2"
}
}
}
ジョブの作成と管理
ジョブの作成
APIの仕様をドキュメントで確認します。
URLを指定します。
$url="https://manager.cloudautomator.com/api/v1/jobs"
BODY部をヒアストリングで生成します。
$job_name="Daily_Backup"
$account_id="1231"
$rule_type="cron"
$hour="2"
$minutes="0"
$schedule_type="weekly"
$weekly_schedule='["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]'
$action_type="create_image"
$region="ap-northeast-1"
$specify_image_instance="identifier"
$instance_id="i-xxxxxxxxxxxxxxxxx"
$generation="1"
$image_name="test-ami"
$description="Test for CloudAutomator API"
$reboot_instance="true"
$additional_tag_key="hogehoge"
$additional_tag_value="fugafuga"
$add_same_tag_to_snapshot="true"
$trace_status="true"
$recreate_image_if_ami_status_failed="true"
$body = @"
{
"name": "$job_name",
"aws_account_id": "$account_id",
"rule_type": "$rule_type",
"rule_value": {
"hour": "$hour",
"minutes": $minutes,
"schedule_type": "$schedule_type",
"weekly_schedule": $weekly_schedule
},
"action_type": "$action_type",
"action_value": {
"region": "$region",
"specify_image_instance": "$specify_image_instance",
"instance_id": "$instance_id",
"generation": "$generation",
"image_name": "$image_name",
"description": "$description",
"reboot_instance": "$reboot_instance",
"additional_tag_key": "$additional_tag_key",
"additional_tag_value": "$additional_tag_value",
"add_same_tag_to_snapshot": "$add_same_tag_to_snapshot",
"trace_status": "$trace_status",
"recreate_image_if_ami_status_failed": "$recreate_image_if_ami_status_failed"
}
}
"@
ジョブを作成します。
Invoke-RestMethod `
-Uri $url `
-Method Post `
-Headers $headers `
-Body $body `
-ContentType "application/json" `
| ConvertTo-Json
{
"data": {
"id": "10338",
"type": "trigger_jobs",
"attributes": {
"name": "Daily_Backup",
"aws_account_id": 1231,
"rule_type": "cron",
"rule_value": "@{hour=2; minutes=0; schedule_type=weekly; weekly_schedule=[\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"sunday\"]; time_zone=Tokyo; next_occurrence=2017-07-07 17:00:00 UTC}",
"action_type": "create_image",
"action_value": "@{instance_id=i-xxxxxxxxxxxxxxxxx; region=ap-northeast-1; trace_status=true; additional_tag_key=hogehoge; additional_tag_value=fugafuga; description=Test for CloudAutomator API; generation=1; add_same_tag_to_snapshot=true; image_name=test-ami; reboot_instance=true; recreate_image_if_ami_status_failed=true; specify_image_instance=identifier}",
"active": true,
"created_at": "2017-07-07T21:52:58.931+09:00",
"updated_at": "2017-07-07T21:52:58.951+09:00"
}
}
}
ジョブの確認
まずは、一覧を表示します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$url="https://manager.cloudautomator.com/api/v1/jobs"
ジョブの一覧を表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": [
{
"id": "10338",
"type": "trigger_jobs",
"attributes": "@{name=Daily_Backup; aws_account_id=1231; rule_type=cron; rule_value=; action_type=create_image; action_value=; active=True; created_at=2017-07-07T21:52:58.931+09:00; updated_at=2017-07-07T21:52:58.951+09:00}"
},
{
...
}
],
"links": {
...
},
"meta": {
"total": 14
}
}
次に、作成したジョブを確認します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$job_id="10338"
$url="https://manager.cloudautomator.com/api/v1/jobs/$job_id"
ジョブを表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": {
"id": "10338",
"type": "trigger_jobs",
"attributes": {
"name": "Daily_Backup",
"aws_account_id": 1231,
"rule_type": "cron",
"rule_value": "@{hour=2; minutes=0; time_zone=Tokyo; schedule_type=weekly; next_occurrence=2017-07-07 17:00:00 UTC; weekly_schedule=[\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"sunday\"]}",
"action_type": "create_image",
"action_value": "@{region=ap-northeast-1; generation=1; image_name=test-ami; description=Test for CloudAutomator API; instance_id=i-xxxxxxxxxxxxxxxxx; trace_status=true; reboot_instance=true; additional_tag_key=hogehoge; additional_tag_value=fugafuga; specify_image_instance=identifier; add_same_tag_to_snapshot=true; recreate_image_if_ami_status_failed=true}",
"active": true,
"created_at": "2017-07-07T21:52:58.931+09:00",
"updated_at": "2017-07-07T21:52:58.951+09:00"
}
}
}
ジョブの変更
ジョブの実行時刻を変更してみます。
APIの仕様をドキュメントで確認します。
URLを指定します。
$job_id="10338"
$url="https://manager.cloudautomator.com/api/v1/jobs/$job_id"
BODY部をヒアストリングで生成します。
$hour="3"
$body = @"
{
"rule_value": {
"hour": "$hour"
}
}
"@
ジョブを変更します。
Invoke-RestMethod `
-Uri $url `
-Method Patch `
-Headers $headers `
-Body $body `
-ContentType "application/json" `
| ConvertTo-Json
{
"data": {
"id": "10338",
"type": "trigger_jobs",
"attributes": {
"name": "Daily_Backup",
"aws_account_id": 1231,
"rule_type": "cron",
"rule_value": "@{hour=3; minutes=0; time_zone=Tokyo; schedule_type=weekly; next_occurrence=2017-07-07 18:00:00 UTC; weekly_schedule=[\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"sunday\"]}",
"action_type": "create_image",
"action_value": "@{region=ap-northeast-1; generation=1; image_name=test-ami; description=Test for CloudAutomator API; instance_id=i-xxxxxxxxxxxxxxxxx; trace_status=true; reboot_instance=true; additional_tag_key=hogehoge; additional_tag_value=fugafuga; specify_image_instance=identifier; add_same_tag_to_snapshot=true; recreate_image_if_ami_status_failed=true}",
"active": true,
"created_at": "2017-07-07T21:52:58.931+09:00",
"updated_at": "2017-07-07T22:21:06.857+09:00"
}
}
}
ログの確認
一覧を表示します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$url="https://manager.cloudautomator.com/api/v1/logs"
ログの一覧を表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": [
{
"id": "xxxxx",
"type": "trigger_job_logs",
"attributes": "@{name=XXXXXXXX; result=; status=completed; action_type=create_image; action_value=; aws_account_id=xxx; rule_type=cron; rule_value=; started_at=2014-12-06T02:00:01.147+09:00; finished_at=2014-12-06T03:27:58.493+09:00; created_at=2014-12-06T02:00:01.152+09:00; updated_at=2014-12-06T03:27:58.500+09:00; job_id=xxxx}"
},
{
...
}
],
"links": {
...
},
"meta": {
"total": 347
}
}
次に、特定のジョブのログを確認します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$log_id="xxxxx"
$url="https://manager.cloudautomator.com/api/v1/logs/$log_id"
ログを表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": {
"id": "xxxxx",
"type": "trigger_job_logs",
"attributes": {
"name": "XXXXXXXXXXXX",
"result": "@{error_code=OperationSuccess.AMIsCreated; error_message=AMIs created: AMIs=[ami-xxxxxxxx]}",
"status": "completed",
"action_type": "create_image",
"action_value": "@{region=ap-northeast-1; generation=2; image_name=CloudAutomator; description=%Y%m%d-TestBackup; instance_id=i-xxxxxxxx; trace_status=true; reboot_instance=true; record_set_value=; additional_tag_key=; additional_tag_value=; specify_image_instance=identifier; final_cluster_snapshot_identifier=}",
"aws_account_id": xxx,
"rule_type": "cron",
"rule_value": "@{hour=2; minutes=0; time_zone=Tokyo; schedule_type=weekly; next_occurrence=2014-12-05 17:00:00 UTC; weekly_schedule=[\"sunday\", \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"\"]}",
"started_at": "2014-12-06T02:00:01.147+09:00",
"finished_at": "2014-12-06T03:27:58.493+09:00",
"created_at": "2014-12-06T02:00:01.152+09:00",
"updated_at": "2014-12-06T03:27:58.500+09:00",
"job_id": xxxx
}
}
}
ログに紐づくリソース操作結果を確認します。
APIの仕様をドキュメントで確認します。
URLを指定します。
$log_id="xxxxx"
$url="https://manager.cloudautomator.com/api/v1/logs/$log_id/resource_operation_results"
ログを表示します。
Invoke-RestMethod `
-Uri $url `
-Method Get `
-Headers $headers `
| ConvertTo-Json
{
"data": [
{
"id": "xxxxxxxx",
"type": "resource_operation_results",
"attributes": "@{message=; result=completed; affected_resource_id=ami-xxxxxxxx; target_resource_id=i-xxxxxxxxxxxxxxxxx; created_at=2017-07-08T03:05:09.632+09:00; updated_at=2017-07-08T03:05:09.632+09:00; job_id=xxxxx; log_id=xxxxxxx}"
}
],
"links": {
},
"meta": {
"total": 1
}
}
感想
- ジョブのオンオフ機能が欲しい
- ジョブの後処理を設定できる機能が欲しい
- JSON地獄がしんどいw
以上です。