Softlayer勉強会#2

19
API を理解しクラウドをコード Python )で操作する #02 ~SoftLayer ~ 株式会社 MNU 雪本修一

Transcript of Softlayer勉強会#2

Page 1: Softlayer勉強会#2

APIを理解しクラウドをコード(Python)で操作する #02

~SoftLayer編~

株式会社MNU 雪本修一

Page 2: Softlayer勉強会#2

自己紹介

• 雪本修一 1974.8.3生まれ

• 株式会社MNU 代表取締役

• 電気通信大学認定ベンチャー企業

• 連絡先:[email protected]

Page 3: Softlayer勉強会#2

今日の内容

• 開発環境の構築

• Pythonの簡単な使い方

• SoftLayer APIの使い方とドキュメントの見方について

• SoftLayer APIを使ったプログラミング

Page 4: Softlayer勉強会#2

開発環境の構築

Page 5: Softlayer勉強会#2

開発環境の構築

• Pythonのインストール

• windows

• Python.orgからダウンロードしてインストール

• mac

• homebrewでインストールすると楽チン

Page 6: Softlayer勉強会#2

Pythonのインストール(続き)• python.org(https://www.python.org/) にアクセスする.

• "Downloads" - "Windows" と進む.

• Latest Python 2 Release - Python 2.7.8 を選択する.

• Download から Windows x86 MSI Installer (2.7.8) (sig) を選択し,python-2.7.8.msi をダウンロードする.

• ダウンロードした python-2.7.8.msi を使って Python をインストールする.

• インストールが終わったら環境変数にパスを追加する.

• 参考:Windows 環境変数 Path の設定方法

• PATH = $PATH;C:\Python27;C:\Python27\Scripts;

• コマンドプロンプトで

• > phython

• $ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)”

• ~/.bashrc

!

• brew install python —framework

!

• easy_install pip

export PATH=/usr/local/bin:$PATH

export PATH=/usr/local/share/python:$PATH

Windows Mac

Page 7: Softlayer勉強会#2

ライブラリなどのインストール• distribute のインストール

• https://pypi.python.org/pypi/distribute/0.7.3

• > python ez_setup.py

• > easy_install pip

!• https://pypi.python.org/pypi/virtualenv/

1.9.1

• > cd virtualenv-1.9.1> python setup.py install

• > python -m virtualenv C:¥venv33

• > C:¥venv33¥Scripts¥activate

• (venv33) C:¥> deactivate

• virtualenvのインストールvirtualenvwrapperのインストール

!

!

• virtualenvの作成

!

• virtualenvの環境に移動

!

• virtualenvのディアクティベート

Windows Mac

pip install virtualenv virtualenvwrapper

source /usr/local/bin/virtualenvwrapper.sh

mkvirtualenv softlayer

workon softlayer

deactivate

Page 8: Softlayer勉強会#2

iPythonのインストール

• iPython

• コマンドラインでpythonを実行する時に何かと便利なライブラリ.タブで補完も行えるようになる.

• 以下のコマンドでインストール

• pip install ipython

Page 9: Softlayer勉強会#2

Pythonの基本的な文法

Page 10: Softlayer勉強会#2

Pythonの文法• インデント

for (int i = 0; i < 100; ++i) { for (int j = 0; j < 100; ++j) { if (i * j % 10 == 0) printf("%d %d\n", i, j); } }

for i in range(100): for j in range(100): if i * j % 10 == 0: print "%d %d" % (i, j)

C言語 Python

Page 11: Softlayer勉強会#2

辞書• 辞書(ディクショナリ)

!

!

• 値の取得

{キー1:値1, キー2:値2, …} dict = {"python":75, "ruby":82}

辞書オブジェクト[キー] dict[“python”]

Page 12: Softlayer勉強会#2

for文• 繰り返し構文

dict = [ {“python”:75, “ruby”:82}, {“python”:11, ”ruby”:99}  ] !

for value in dict: ... print value

Page 13: Softlayer勉強会#2

Softlayer APIについて

Page 14: Softlayer勉強会#2

APIキーの取得• https://control.softlayer.com

• アカウントとapiキーを取得

Page 15: Softlayer勉強会#2

softlayerAPI実装の準備• SoftLayer API Python Clientのインストール

• pip install softlayer

• https://softlayer-api-python-client.readthedocs.org/en/latest/

Page 16: Softlayer勉強会#2

サンプルの実装

Page 17: Softlayer勉強会#2

APIドキュメント

• http://sldn.softlayer.com/reference/services/SoftLayer_Account

• maskの実装

• 戻り値を使う側で指定して受け取ることが出来る

Page 18: Softlayer勉強会#2

Softlayer APIを使った 簡単な実装

Page 19: Softlayer勉強会#2

• インスタンスを作成

• アカウント情報を取得

• 利用金額の取得