api
python3
IDCFクラウド

IDCFクラウドのAPI Docsが公開されたのでpython3から色々と利用してみる

IDCFクラウドのAPIドキュメントサイト、API Docsが公開されたので色々と試してみました。
ちょうどILB APIのRubyコードを読んでいた最中だったのですが、このサイトが公開されてとてもすんなり理解できたのですぐに実行させることができました。

環境変数にAPIキーとシークレットキー

export IDCF_APIKEY=確認したAPIキー
export IDCF_SECRETKEY=確認したシークレットキー

インフィニットLB一覧を表示する

# -*- conding:utf8 -*-

import os
import json
import requests
import hashlib,hmac
import time
import base64

METHOD='GET'
QUERY_STRING = ''
APIKEY=os.getenv("IDCF_APIKEY")
SECRET_KEY=os.getenv("IDCF_SECRETKEY")
ENDPOINT='https://ilb.jp-east-2.idcfcloud.com'
ENDPOINT_URI='/api/v1/loadbalancers'
EXPIRATION_SECONDS = 30

expiration = int(time.time()) + EXPIRATION_SECONDS
#print(expiration)

message = METHOD + "\n" + ENDPOINT_URI + "\n" + APIKEY + "\n" + str(expiration) + "\n" + QUERY_STRING
secret = SECRET_KEY

signature=hmac.new(bytes(secret, 'ascii'), bytes(message, 'ascii'), hashlib.sha256).digest()
sig=base64.b64encode(signature)

head = {'X-IDCF-APIKEY': APIKEY, \
        'X-IDCF-Expires' : str(expiration), \
        'X-IDCF-Signature' : str(sig,'utf-8') }
#print(head)
#URL生成
uri = ENDPOINT + ENDPOINT_URI + '?' + QUERY_STRING
print(uri)
res = requests.get(uri, headers=head)
data = res.json()
print(json.dumps(data,sort_keys=True,indent=4,separators=(',', ': ')))

インフィニットLB一覧実行結果

[
    {
        "account_id": "XXXXXXXXXXXXXXXXXXXXx",
        "configs": [
            {
                "algorithm": "roundrobin",
                "backend_protocol": "http",
                "connection_timeout": 60,
                "created_at": "2017-11-10T12:33:40.000+09:00",
                "frontend_protocol": "http",
                "healthcheck": {
                    "check_interval": 30,
                    "healthy_threshold": 3,
                    "path": "/.lbstat",
                    "timeout": 30,
                    "type": "http",
                    "unhealthy_threshold": 2
                },
                "id": "XXXXXXXXXXXXXXXXXXXXXxx",
                "loadbalancer_id": "XXXXXXXXXXXXXXXXXXXXX",
                "port": 80,
                "servers": [
                    {
                        "config_id": "XXXXXXXXXXXXXXXXXXXXX",
                        "created_at": "2017-11-10T12:33:39.000+09:00",
                        "id": "XXXXXXXXXXXXXXX",
                        "ipaddress": "10.XX.XX.XX",
                        "loadbalancer_id": "XXXXXXXXXXXXXX",
                        "port": 80,
                        "state": "Running",
                        "updated_at": "2017-11-10T12:33:43.000+09:00"
                    }
                ],
                "state": "Running",
                "stickiness_policy": "lb_cookie",
                "updated_at": "2017-11-10T12:33:43.000+09:00"
            }
        ],
        "created_at": "2017-03-24T09:19:37.000+09:00",
        "fqdn": "example.ilb.jp-east-2.idcfcloud.net",
        "id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "name": "TESTILB",
        "network": {
            "account_id": "XXXXXXXXXXXXXXXXX",
            "domain": "XXXXXXXXXXXXXX",
            "domainid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "gateway": "10.1.0.1",
            "id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "name": "weber-network1",
            "networkcidr": "10.1.0.0/22",
            "networkoffering_id": "XXXXXXXXXXXXXXXXXXXXXXX",
            "networkoffering_name": "DefaultIsolatedNetworkOffering",
            "related": "XXXXXXXXXXXXXXXXXXXXXXXX",
            "state": "Implemented",
            "traffic_type": "Guest",
            "zone_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
            "zone_name": "weber"
        },
        "network_id": "XXXXXXXXXXXXXXXXXXXXXX",
        "network_name": "weber-network1",
        "state": "Running",
        "updated_at": "2017-11-10T12:33:48.000+09:00",
        "zone_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "zone_name": "weber"
    }
]

DNS Zone一覧を表示させる

# -*- conding:utf8 -*-

import os
import json
import requests
import hashlib,hmac
import time
import base64

METHOD='GET'
QUERY_STRING = ''
APIKEY=os.getenv("IDCF_APIKEY")
SECRET_KEY=os.getenv("IDCF_SECRETKEY")
ENDPOINT='https://dns.idcfcloud.com'
ENDPOINT_URI='/api/v1/zones'
EXPIRATION_SECONDS = 30

expiration = int(time.time()) + EXPIRATION_SECONDS
#print(expiration)

message = METHOD + "\n" + ENDPOINT_URI + "\n" + APIKEY + "\n" + str(expiration) + "\n" + QUERY_STRING
secret = SECRET_KEY

signature=hmac.new(bytes(secret, 'ascii'), bytes(message, 'ascii'), hashlib.sha256).digest()
sig=base64.b64encode(signature)

head = {'X-IDCF-APIKEY': APIKEY, \
        'X-IDCF-Expires' : str(expiration), \
        'X-IDCF-Signature' : str(sig,'utf-8') }
#print(head)
#URL生成
uri = ENDPOINT + ENDPOINT_URI + '?' + QUERY_STRING
print(uri)
res = requests.get(uri, headers=head)
data = res.json()
print(json.dumps(data,sort_keys=True,indent=4,separators=(',', ': ')))

DNS Zone一覧実行結果

[
    {
        "authenticated": "1",
        "created_at": "2016-12-02T13:10:13+09:00",
        "default_ttl": 3600,
        "description": "",
        "name": "example.com",
        "updated_at": "2016-12-02T13:10:13+09:00",
        "uuid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    },
    {
        "authenticated": "1",
        "created_at": "2017-04-18T17:01:55+09:00",
        "default_ttl": 3600,
        "description": "",
        "name": "example.jp",
        "updated_at": "2017-04-18T17:01:55+09:00",
        "uuid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxxx"
    }
]

便利ですね!