From d02f79b2043869d98c7ccdcc650206a3b2ed13e6 Mon Sep 17 00:00:00 2001
From: Ivan Kolodyazhny <e0ne@e0ne.info>
Date: Wed, 6 Aug 2014 13:58:43 +0300
Subject: [PATCH] RPC client lazy initialization

All *Command classes are instantiating for each cinder-manage command
execution. RPC client should be initialized for VolumeCommands usage.
Some custom RPC clients (oslo.messaging backends) could init RPC
connection after client initialization so it is no need to create RPC
client for every cinder-manage command invoke.

Related-Bug: #1348787
Change-Id: I7b3388dded6eeec972d9949538e1608c3a538734
---
 bin/cinder-manage | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/bin/cinder-manage b/bin/cinder-manage
index 0b25ae3ab..064cbdc20 100755
--- a/bin/cinder-manage
+++ b/bin/cinder-manage
@@ -249,11 +249,17 @@ class VersionCommands(object):
 class VolumeCommands(object):
     """Methods for dealing with a cloud in an odd state."""
 
-    def __init__(self, *args, **kwargs):
-        super(VolumeCommands, self).__init__(*args, **kwargs)
-        rpc.init(CONF)
-        target = messaging.Target(topic=CONF.volume_topic)
-        self.client = rpc.get_client(target)
+    def __init__(self):
+        self._client = None
+
+    @property
+    def rpc_client(self):
+        if not rpc.initialized():
+            rpc.init(CONF)
+            target = messaging.Target(topic=CONF.volume_topic)
+            self._client = rpc.get_client(target)
+
+        return self._client
 
     @args('volume_id',
           help='Volume ID to be deleted')
@@ -276,7 +282,7 @@ class VolumeCommands(object):
             print(_("Detach volume from instance and then try again."))
             return
 
-        cctxt = self.client.prepare(server=host)
+        cctxt = self.rpc_client.prepare(server=host)
         cctxt.cast(ctxt, "delete_volume", volume_id=volume['id'])
 
     @args('--currenthost', required=True, help='Existing volume host name')
-- 
2.45.2