CLI for SO: Add check for datacenter 29/329/1
authorPhilip Joseph <philip.joseph@riftio.com>
Wed, 14 Sep 2016 18:10:39 +0000 (18:10 +0000)
committerPhilip Joseph <philip.joseph@riftio.com>
Wed, 14 Sep 2016 18:10:39 +0000 (18:10 +0000)
Signed-off-by: Philip Joseph <philip.joseph@riftio.com>
rwlaunchpad/plugins/rwlaunchpadtasklet/scripts/onboard_pkg [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index b616ccf..6a39775
@@ -72,6 +72,10 @@ class OnboardPkgRcConnError(OnboardPkgError):
     pass
 
 
+class OnboardPkgDcError(OnboardPkgError):
+    pass
+
+
 class OnboardPkgAcctError(OnboardPkgError):
     pass
 
@@ -121,6 +125,12 @@ class OnboardPackage:
                               passwd=self._password,
                               ip=self._ip,
                               port=self._rport)
+        self._oper_url = "curl -k {header} --user \"{user}:{passwd}\" https://{ip}:{port}/api/operational". \
+                       format(header=self._headers,
+                              user=self._user,
+                              passwd=self._password,
+                              ip=self._ip,
+                              port=self._rport)
 
     @property
     def log(self):
@@ -180,10 +190,12 @@ class OnboardPackage:
 
     def _exec_cmd(self, cmd):
         self.log.debug("Execute command: {}".format(cmd))
-        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE, shell=True)
         (output, err) = proc.communicate()
         rc = proc.returncode
-        self.log.debug("Command exec status: {}, {}, {}".format(rc, output, err))
+        self.log.debug("Command exec status: {}\nSTDOUT: {}\nSTDERR: {}".
+                       format(rc, output, err))
         if rc != 0:
             raise OnboardPkgCmdError("Command {} failed ({}): {}".
                                             format(cmd, rc, err))
@@ -263,7 +275,31 @@ class OnboardPackage:
             self.log.debug("No NSD ID provided for instantiation")
             return
 
-        # TODO: Add check to see if datacenter is valid
+        # Check to see if datacenter is valid
+        if self._dc:
+            dc_url = "{url}/datacenters". format(url=self._oper_url)
+            output = self._exec_cmd(dc_url)
+            if (output is None) or (len(output) == 0):
+                # Account not found
+                raise OnboardPkgDcError("Datacenter {} provided is not valid".
+                                        format(self._dc))
+            found = False
+            js = json.loads(output)
+            if "ro-accounts" in js["rw-launchpad:datacenters"]:
+                for ro in js["rw-launchpad:datacenters"]["ro-accounts"]:
+                    if "datacenters" in ro:
+                        for dc in ro["datacenters"]:
+                            if dc["uuid"] == self._dc:
+                                self.log.debug("Found datacenter {}".format(dc))
+                                found = True
+                                break
+                    if found:
+                        break
+
+            if found is False:
+                raise OnboardPkgDcError("Datacenter {} provided is not valid".
+                                        format(self._dc))
+
 
         # Check cloud account is valid, if provided
         if self._account: