CSR1000VとPostmanでREST-API

久しぶりにやり直したらCLIが改良されていたのと、設定をすぐ忘れるので自分用のメモです。SSH接続はできている前提。マニュアルやフォーラム等ではcURLの例が多いので、ここではPostmanの例とします。

公式はこのあたり。
[設定](http://www.cisco.com/c/en/us/td/docs/routers/csr1000/software/configuration/b_CSR1000v_Configuration_Guide/b_CSR1000v_Configuration_Guide_chapter_01101.html)
[APIリファレンス](http://www.cisco.com/c/en/us/td/docs/routers/csr1000/software/restapi/restapi.html)

```Cisco:IOS-version
CSR-1#show flash: | i .pkg
 17  289154944 Jan 31 2017 23:05:47 +00:00 /bootflash/csr1000v-mono-universalk9.03.16.05.S.155-3.S5-ext.SPA.pkg
CSR-1#
CSR-1#show version | i 15
Cisco IOS Software, CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 15.5(3)S5, RELEASE SOFTWARE (fc2)
```

##設定
出た当初よりずいぶんすっきりしました。

```Cisco:IOS-config
transport-map type persistent webui https-webui
 secure-server
!
username cisco privilege 15 password 0 cisco
!
virtual-service csr_mgmt
 ip shared host-interface GigabitEthernet1
 activate
!
no ip http server
ip http authentication local
ip http secure-server
!         
transport type persistent webui input https-webui
```

REST-APIサービス起動とオプション設定。

```Cisco:IOS-rest
CSR-1(config)#remote-management 
CSR-1(cfg-remote-mgmt)#?
  default      Set a command to its defaults
  exit         exit remote management configuration submode
  no           Negate a command or set its defaults
  pnsc         Prime Network Services Controller and Policy Agent configuration
  restful-api  RESTful API

CSR-1(cfg-remote-mgmt)#rest
CSR-1(cfg-remote-mgmt)#restful-api ?
  autosave        Automatic configuration saving interval
  local-port      Local port for secure access to RESTful API
  single-session  Limit RESTful API to one session at a time
  <cr>
```

##確認

```Cisco:IOS-show
CSR-1#show virtual-service detail 
Virtual service csr_mgmt detail
  State                 : Activated
  Package information
    Name                : iosxe-remote-mgmt.03.16.05.S.155-3.S5-ext.ova
    Path                : bootflash:/iosxe-remote-mgmt.03.16.05.S.155-3.S5-ext.ova
    Application
      Name              : csr_mgmt
      Installed version : 03.16.04
      Description       : CSR-MGMT
    Signing
      Key type          : Cisco development key
      Method            : SHA-1
    Licensing
      Name              : Not Available
      Version           : Not Available

  Detailed guest status
    
----------------------------------------------------------------------
Process               Status            Uptime           # of restarts
----------------------------------------------------------------------
nginx                  UP         0Y 0W 0D  1: 2: 2        0
climgr                 UP         0Y 0W 0D  1: 2: 2        0
restful_api            UP         0Y 0W 0D  1: 2: 2        0
fcgicpa                Down      
pnscag                 Down      
pnscdme                Down      
----------------------------------------------------------------------
Feature         Status                 Configuration
----------------------------------------------------------------------
Restful API   Disabled, UP             
PNSC          Disabled, Down

Network stats:
 eth0: RX  packets:1868, TX  packets:6
 eth1: RX  packets:185, TX  packets:139

Coredump file(s): lost+found
 
  Activated profile name: None
  Resource reservation
    Disk                : 756 MB
    Memory              : 512 MB
    CPU                 : 5% system CPU

  Attached devices
    Type              Name        Alias            
    ---------------------------------------------
    NIC               ieobc_1     ieobc            
    NIC               dp_1_33     net2             
    Disk              _rootfs                      
    Disk              /opt/var                     
    Disk              /opt/va...                   
    Serial/shell                  serial0          
    Serial/aux                    serial1          
    Serial/Syslog                 serial2          
    Serial/Trace                  serial3          
    Watchdog          watchdo...                   

  Network interfaces
    MAC address             Attached to interface           
    ------------------------------------------------------
    54:0E:00:0B:0C:02       ieobc_1                         
    00:50:56:A2:22:D3       VirtualPortGroup33              

  Guest interface
  ---
  Interface: eth1
  ip address: 10.71.130.57/21

  ---

  Guest routes
  ---
  Address/Mask                         Next Hop                          Intf.
-------------------------------------------------------------------------------

  ---

  Resource admission (without profile) : passed
    Disk space    : 756MB
    Memory        : 512MB 
    CPU           : 5% system CPU 
    VCPUs         : Not specified
```

##POSTMANでToken取得
- トークンサービスのAPIにPOST
- ベーシック認証（ヘッダは自動補完してくれます）
- ヘッダに Accept Application/json を追加

![SS 2017-03-10 13.47.56.png](https://qiita-image-store.s3.amazonaws.com/0/157325/ef631bae-02e8-dfaf-043e-d4d32cff0601.png)

![SS 2017-03-10 13.48.27.png](https://qiita-image-store.s3.amazonaws.com/0/157325/6829b5f0-155c-4bcc-7232-07f4441334f4.png)

これで実行すると、以下のようにtoken-idが返ってきます。以降はこれをX-Auth-Tokenとしてヘッダに入れると、読み書きにアクセスできます。

![SS 2017-03-10 13.48.42.png](https://qiita-image-store.s3.amazonaws.com/0/157325/8f291b73-8f8f-4751-df3c-ff61763e2a15.png)

##POSTMANでインターフェース情報取得する例

![SS 2017-03-10 13.56.03.png](https://qiita-image-store.s3.amazonaws.com/0/157325/f628846a-51be-c7c3-2a3b-2cf2123b8e4c.png)

以上です。