2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SoftLayer CLIを拡張する: 別モジュールとして拡張する

Last updated at Posted at 2015-01-18

前の記事(SoftLayer CLIを拡張する: Virtual Serverの稼働ホストをsl vs listで表示する)では、オリジナルのCLIのモジュールを変更して機能を追加しました。ただ通常は、既存のモジュールはそのままで、新しいモジュールを作成して試行錯誤したい場合が多いと思います。
その方法がこちら。

===== 1/19 追加

GitHub版ではファイルの構成がかなり変わっています。
SoftLayer APIパッケージのPyPI版とGitHub版の違い - Qiita

以下のファイルを変更することで、同じように別モジュールとして拡張が可能です。

  • managers/vs2.py
  • CLI/routes.py
  • CLI/virt/list2.py

=====

Managerの変更

  • オリジナルのmanagers/vs.pyをmanagers/vs2.pyにコピー
  • VSManagerをVSManager2に変更
  • 変更後コンパイルを実行
  • managers/__init__.pyを変更

managers/vs2.pyの変更

$ sudo cp managers/vs.py managers/vs2.py 
$ sudo sed -i.orig -e "s/VSManager/VSManager2/g" managers/vs2.py
$ ls -l managers/vs*.py*
-rw-r--r--  1 root  wheel  26164 Jan 17 23:42 managers/vs.py
-rw-r--r--  1 root  wheel  23958 Jan  4 23:12 managers/vs.pyc
-rw-r--r--  1 root  wheel  26170 Jan 19 00:05 managers/vs2.py
-rw-r--r--  1 root  wheel  26164 Jan 18 23:45 managers/vs2.py.orig

コンパイル

$ sudo python -m compileall managers/vs2.py
Compiling managers/vs2.py ...

manager/__init__.pyの変更

以下のように、VSManager2をクラスとしてimportするように変更

$ diff -U 5 managers/__init__.py.orig managers/__init__.py
--- managers/__init__.py.orig	2015-01-19 00:11:20.000000000 +0900
+++ managers/__init__.py	2015-01-18 23:44:10.000000000 +0900
@@ -23,11 +23,12 @@
 from SoftLayer.managers.ordering import OrderingManager  # NOQA
 from SoftLayer.managers.sshkey import SshKeyManager  # NOQA
 from SoftLayer.managers.ssl import SSLManager  # NOQA
 from SoftLayer.managers.ticket import TicketManager  # NOQA
 from SoftLayer.managers.vs import VSManager  # NOQA
+from SoftLayer.managers.vs2 import VSManager2  # NOQA

 __all__ = ['CCIManager', 'DNSManager', 'FirewallManager', 'HardwareManager',
            'ImageManager', 'MessagingManager', 'MetadataManager', 'CDNManager',
            'NetworkManager', 'SshKeyManager', 'SSLManager', 'TicketManager',
-           'VSManager', 'ISCSIManager', 'LoadBalancerManager',
+           'VSManager', 'VSManager2', 'ISCSIManager', 'LoadBalancerManager',
            'OrderingManager']

CLIの変更

  • CLI/modules/vs.pyをCLI/modules/vs2.py にコピー
  • Usage, Table, データ取得の定義を変更
  • VSManagerをVSManager2に変更
  • コンパイル

CLI/modules/vs2.pyの変更

CLI/modules/vs.pyをCLI/modules/vs2.py にコピー

$ sudo cp CLI/modules/vs.py CLI/modules/vs2.py 

以下のように変更。

$ diff -U 5 CLI/modules/vs.py CLI/modules/vs2.py
--- CLI/modules/vs.py	2015-01-18 03:22:24.000000000 +0900
+++ CLI/modules/vs2.py	2015-01-18 09:22:40.000000000 +0900
@@ -1,7 +1,7 @@
 """
-usage: sl vs [<command>] [<args>...] [options]
+usage: sl vs2 [<command>] [<args>...] [options]

 Manage, delete, order compute instances

 The available commands are:
   cancel          Cancel a running virtual server
@@ -41,11 +41,11 @@
 from SoftLayer import utils


 class ListVSIs(environment.CLIRunnable):
     """
-usage: sl vs list [--hourly | --monthly] [--sortby=SORT_COLUMN] [--tags=TAGS]
+usage: sl vs2 list [--hourly | --monthly] [--sortby=SORT_COLUMN] [--tags=TAGS]
                   [options]

 List virtual servers

 Examples:
@@ -92,11 +92,11 @@
                                     tags=tags)

         table = formatting.Table([
             'id', 'datacenter', 'host',
             'cores', 'memory', 'primary_ip',
-            'backend_ip', 'active_transaction', 'owner'
+            'backend_ip', 'location', 'active_transaction', 'owner'
         ])
         table.sortby = args.get('--sortby') or 'host'

         for guest in guests:
             guest = utils.NestedDict(guest)
@@ -106,10 +106,11 @@
                 guest['fullyQualifiedDomainName'],
                 guest['maxCpu'],
                 formatting.mb_to_gb(guest['maxMemory']),
                 guest['primaryIpAddress'] or formatting.blank(),
                 guest['primaryBackendIpAddress'] or formatting.blank(),
+                utils.lookup(guest, 'location', 'pathString') or formatting.blank(),
                 formatting.active_txn(guest),
                 utils.lookup(guest, 'billingItem', 'orderItem', 'order',
                              'userRecord', 'username') or formatting.blank(),
             ])

その後、VSManagerをVSManager2に変更。

$ sudo sed -i.orig -e "s/VSManager/VSManager2/g" CLI/modules/vs2.py

コンパイル

$ sudo python -m compileall CLI/modules/vs2.py
Compiling CLI/modules/vs2.py ...
$ ls -l CLI/modules/vs2.py*
-rw-r--r--  1 root  wheel  42394 Jan 18 03:14 CLI/modules/vs2.py
-rw-r--r--  1 root  wheel  36269 Jan 18 03:18 CLI/modules/vs2.pyc

これで

  • sl vs listでは通常のVirtual Serverのテーブルが、
  • sl vs2 listでVirtual Serverの稼働ホストのフィールドが追加されたテーブルが
    出力されます。

さらに拡張したい場合は、managers/vs2.py、CLI/modules/vs2.pyを編集すればよいので、オリジナルのCLIを変更することなく試すことができます。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?