ConoHa API (v2.0) でRubyからAPIを叩きOSのイメージ一覧を取得する

# 処理の流れ

1. APIのトークンを取得
2. トークンをパラメーターに渡しOSのイメージ一覧を取得するAPIを叩く

## Gemfile

```ruby:Gemfile
# A sample Gemfile
source "https://rubygems.org"

gem 'httpclient', '~> 2.6.0.1'
```

今回はHTTP通信にhttpclientモジュールを使います。
古いhttpclientだとSSL3関係で失敗するので最新のバージョンを指定します。

```
bundle install
```

でモジュールインストール

## トークンを取得するコアロジック　ライブラリを作成

```ruby:conoha2_client.rb
require 'uri'
require 'json'
require 'httpclient'

class Conoha2Client

  HEAD_SUCCESS_HTTP_STATUS_CODE = 200

  @token = nil

  def initialize(opt)
    @auth_url = opt['auth_url']
    @tenant_id = opt['tenant_id']
    @username = opt['username']
    @password = opt['password']
  end

  def request_token
    return @token if !@token.nil?
    body =  build_auth_json

    http_client = HTTPClient.new

    response = http_client.post_content(@auth_url, body, 'Content-Type' => 'application/json')
    response_json_body = JSON.load(response)

    @token = response_json_body['access']['token']['id']
  end


  private

  def build_auth_json
    auth_json = {
        auth: {
            passwordCredentials: {
                username: @username,
                password: @password
            },
        tenantId: @tenant_id
        }
    }
    JSON.dump(auth_json)
  end


end


```


このモジュールを使えば

``Conoha2Client.new(@conf).request_token``　でトークンが取得できます。


## 設定ファイルを記述

パスワード、APIキーなどの情報を設定ファイルに記述します


```conf/conf.json
{
    "conohaV2api" : {
        "auth_url" : "https://identity.tyo1.conoha.io/v2.0/tokens",
        "tenant_id" : "",
        "username" : "",
        "password" : ""
    }
}
```


## APIを叩くプログラムを作成

```ruby:list_os_image.rb
require './conoha2_client'
require 'optparse'

conf_path = './conf/conf.json'

opt = OptionParser.new
Version = "1.0.0"

opt.on('-c CONF_PATH', 'conf_path') {|v| conf_path = v}
opt.parse!(ARGV)

File.open conf_path do |file|
  conf = JSON.load(file.read)
  @conf = conf['conohaV2api']
end

token = Conoha2Client.new(@conf).request_token

images_url = 'https://image-service.tyo1.conoha.io/v2/images'

http_client = HTTPClient.new
response = http_client.get_content(images_url, nil, 'Content-Type' => 'application/json' ,'X-Auth-Token' => token)
response_json_body = JSON.load(response)

response_json_body['images'].each do |image_info|
  p image_info['name']
end
```


## 実行結果

以下のコマンドで実行

```
bundle exec ruby list_os_image.rb
```

```
"gncvmi-hinemos-5.0-centos-7.1"
"gncvmi-fedora-22-x86_64"
"gncvmi-owncloud-8-centos-6.6"
"gncvmi-drupal-7.36-centos-6.6"
"gncvmi-archlinux-x86_64"
"gncvmi-freebsd-10.1-zfs-x86_64"
"gncvmi-wp-4.2-centos-6-6"
"gncvmi-jenkins-1.6-centos-6.6"
"gncvmi-freebsd-10.1-x86_64"
"gncvmi-fedora-21-x86_64"
"gncvmi-debian-8-i386"
"gncvmi-debian-8-amd64"
"gncvmi-debian-7-i386"
"gncvmi-debian-7-amd64"
"gncvmi-gitlab-7.9.1-centos-6.6"
"DEPRECATED_20150617_wp-4.2-centos-6-6"
"DEPRECATED_20150617_debian-8-i386"
"DEPRECATED_20150617_debian-8-amd64"
"DEPRECATED_20150617_debian-7-i386"
"DEPRECATED_20150617_debian-7-amd64"
"DEPRECATED_20150617_freebsd-10.1-x86_64"
"gncvmi-opensuse-13.2-x86_64"
"gncvmi-scientificlinux-7.1-x86_64"
"DEPRECATED_20150617_fedora-21-x86_64"
"gncvmi-ror-4.2-centos-6.6"
"gncvmi-lamp-centos-6.6"
"DEPRECATED_20150617_jenkins-1.6-centos-6.6"
"gncvmi-redmine-3.0-centos-6.6"
"gncvmi-ubuntu-14.04-i386"
"gncvmi-ubuntu-14.04-amd64"
"gncvmi-ubuntu-12.04-i386"
"gncvmi-ubuntu-12.04-amd64"
"gncvmi-centos-7.1-x86_64"
"gncvmi-centos-6.6-x86_64"
"gncvmi-centos-6.6-i386"
```

これでOSのイメージ一覧が取得できます。
ConoHa側に予め用意してあるOSイメージだけでなく自分が保存したOSイメージも格納されてるようです。

## 参考

https://www.conoha.jp/docs/index.html