Skip to content

APIC-EM の API にアクセスしてみる

APIC-EM は GUI 上からデバイスを管理出来ますが、APIC-EM の API へアクセスすることによって管理されている機器の情報を利用することが出来ます。今回は Python を使って APIC-EM の API へアクセスしてみます。

検証環境

検証環境は以下を使いました。トポロジーは 以前の記事 と同じものを使いました。

  • APIC-EM 1.2.0.1594
  • Ubuntu 16.04 LTS
  • Python 2.7

ブラウザ上から API を確認する

ブラウザから API の詳細を確認するには、まずホーム画面の右上にある API をクリックします。

file

Swagger の画面に遷移します。今回、利用する Inventory → network-device をクリックします。

file

すると、network-device API の詳細が表示されます。

file

Python から API を呼び出してみる

Python のプログラム全体は以下の通りです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python

import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

controller_url = 'https://ADDRESS/api/v1/'
auth = {
    'username': 'USERNAME',
    'password': 'PASSWORD'
}

ticket = requests.post(
    controller_url + 'ticket',
    data=json.dumps(auth),
    headers={'content-type': 'application/json'},
    verify=False
)

service_ticket = ticket.json()['response']['serviceTicket']
get_device = requests.get(
    controller_url + 'network-device',
    headers={'X-Auth-Token': service_ticket},
    verify=False
)

print json.dumps(
    get_device.json(),
    indent=4
)

解説

HTTP リクエストの送信には request モジュールを、JSON の取り扱いに json モジュールを使うので import しておきます。

1
2
import requests
import json

そのまま実行すると APIC-EM の既定で利用されている自己証明書に関する警告が表示されてしまう為、下記のようにして警告を無効化しています。

1
2
3
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

APIC-EM へアクセスするのに必要な URL、ユーザ名、パスワードを定義します。API のベースになる URL は「https:/ADDRESS/api/v1/」です (ADDRESS は APIC-EM のアドレス)。

1
2
3
4
5
controller_url = 'https://ADDRESS/api/v1/'
auth = {
    'username': 'USERNAME',
    'password': 'PASSWORD'
}

API を呼び出す為には認証し、トークンを取得しておく必要があります。トークンを取得する為の URL は「https://ADDRESS/api/v1/ticket」です。自己証明書でもアクセスを続行する為に verify=False を指定しています。

1
2
3
4
5
6
ticket = requests.post(
    controller_url + 'ticket',
    data=json.dumps(auth),
    headers={'content-type': 'application/json'},
    verify=False
)

認証に成功するとトークン (serviceTicket) を取得出来ますので、service_ticket に代入しておきます。

1
service_ticket = ticket.json()['response']['serviceTicket']

次は network-device API を呼び出します (GET します)。トークンは X-Auth-Token ヘッダーに格納しておきます。

1
2
3
4
5
get_device = requests.get(
    controller_url + 'network-device',
    headers={'X-Auth-Token': service_ticket},
    verify=False
)

API 呼び出しが成功すれば、後は結果を表示するだけです。

1
2
3
4
print json.dumps(
    get_device.json(),
    indent=4
)

実行例

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
$ python2.7 network-device.py
{
    "version": "1.0",
    "response": [
        {
            "macAddress": "fa:16:3e:9d:5e:57",
            "upTime": "20:12:28.83",
            "bootDateTime": "2016-05-29 12:46:41",
            "lastUpdateTime": 1464604008472,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "2",
            "tunnelUdpPort": null,
            "id": "0481cdcc-f2dc-4ee8-9a9c-dd9ed1274a7e",
            "locationName": null,
            "instanceUuid": "0481cdcc-f2dc-4ee8-9a9c-dd9ed1274a7e",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv1.example.local",
            "memorySize": "347798400",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "079e9cfb-dadf-4fdc-bf38-7c857ad3d774",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:26:48",
            "managementIpAddress": "10.100.4.1",
            "serialNumber": "9VFIDI2JBMQRUID38TI4T",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:e9:36:84",
            "upTime": "20:12:46.40",
            "bootDateTime": "2016-05-29 12:46:42",
            "lastUpdateTime": 1464603986690,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "32617bbf-df08-4267-92f7-06290e601588",
            "locationName": null,
            "instanceUuid": "32617bbf-df08-4267-92f7-06290e601588",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv2.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "164e2b13-b48d-4b72-9a82-c5432c4c6b38",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:26:26",
            "managementIpAddress": "10.0.12.2",
            "serialNumber": "9QOH3V7GIP7QXN0IGB88T",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:34:74:cd",
            "upTime": "20:09:49.39",
            "bootDateTime": "2016-05-29 12:46:42",
            "lastUpdateTime": 1464603959298,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "9e413d48-4d2d-449e-8349-017b6f4af8e9",
            "locationName": null,
            "instanceUuid": "9e413d48-4d2d-449e-8349-017b6f4af8e9",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv3.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "e4c63f3d-35e1-43b8-a02c-6cbc8362d9d9",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:25:59",
            "managementIpAddress": "10.0.23.3",
            "serialNumber": "9DKHCVBX27ZP8C3CQHOOW",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:ae:b4:18",
            "upTime": "20:13:57.92",
            "bootDateTime": "2016-05-29 12:46:43",
            "lastUpdateTime": 1464603930034,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "beb25dc8-5838-44b0-9bc6-fe834c234c2e",
            "locationName": null,
            "instanceUuid": "beb25dc8-5838-44b0-9bc6-fe834c234c2e",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv4.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "a5ba6c8a-733f-4544-b7b0-5ae9136bae8e",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:25:30",
            "managementIpAddress": "10.0.34.4",
            "serialNumber": "9JLHM88D6QR7TEO1NADR5",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:b1:fa:a5",
            "upTime": "20:13:40.61",
            "bootDateTime": "2016-05-29 12:46:44",
            "lastUpdateTime": 1464603837669,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "2",
            "tunnelUdpPort": null,
            "id": "5a276269-f261-478b-88e0-4228fe8e989a",
            "locationName": null,
            "instanceUuid": "5a276269-f261-478b-88e0-4228fe8e989a",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv5.example.local",
            "memorySize": "347798400",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "2d514ec5-cd6b-4249-908a-ca3042ae798b",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:23:57",
            "managementIpAddress": "10.0.45.5",
            "serialNumber": "9734KHDB8OGRH7GTYVH9J",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        }
    ]
}

参考 URL