def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        rest_api_params = None
        rest_api_params = self.set_server_status(parsed_args)
        if rest_api_params:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="server_status",
                                              match_key=rest_api_params['match_key'],
                                              match_value=rest_api_params['match_value'])
            json_format = getattr(parsed_args, "json", False)
            if json_format:
                self.app.stdout.write(str(smgrutils.print_rest_response(resp)) + "\n")
            else:
                table_format_output = smgrutils.convert_json_to_table(obj="server", json_resp=resp)
                self.app.stdout.write("\n" + str(smgrutils.print_rest_response(str(table_format_output))) + "\n")
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        rest_api_params = None
        rest_api_params = self.set_server_status(parsed_args)
        if rest_api_params:
            resp = smgrutils.send_REST_request(
                self.smgr_ip,
                self.smgr_port,
                obj="server_status",
                match_key=rest_api_params['match_key'],
                match_value=rest_api_params['match_value'])
            json_format = getattr(parsed_args, "json", False)
            if json_format:
                self.app.stdout.write(
                    str(smgrutils.print_rest_response(resp)) + "\n")
            else:
                table_format_output = smgrutils.convert_json_to_table(
                    obj="server", json_resp=resp)
                self.app.stdout.write("\n" + str(
                    smgrutils.print_rest_response(str(table_format_output))) +
                                      "\n")
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)
        rest_api_params = None
        obj = getattr(parsed_args, "which", None)
        if obj == "server":
            rest_api_params = self.delete_server(parsed_args)
        elif obj == "cluster":
            rest_api_params = self.delete_cluster(parsed_args)
        elif obj == "image":
            rest_api_params = self.delete_image(parsed_args)
        elif obj == "tag":
            rest_api_params = self.delete_tag(parsed_args)

        if rest_api_params:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj=rest_api_params["object"],
                                              match_key=rest_api_params["match_key"],
                                              match_value=rest_api_params["match_value"],
                                              method="DELETE")
            smgrutils.print_rest_response(resp)
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
Esempio n. 4
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        payload = {}
        match_key = None
        match_value = None
        if getattr(parsed_args, "server_id", None):
            match_key = 'id'
            match_value = getattr(parsed_args, "server_id", None)
        elif getattr(parsed_args, "cluster_id", None):
            match_key = 'cluster_id'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "tag", None):
            match_key = 'tag'
            match_value = getattr(parsed_args, "tag", None)
        else:
            pass

        if match_key:
            payload[match_key] = match_value

        if not getattr(parsed_args, "no_confirm", False):
            msg = "Run Inventory on servers matching: (%s:%s)? (y/N) :" % (
                match_key, match_value)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="run_inventory",
                                               payload=payload, method="POST")
            if "Error" not in resp:
                self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
            else:
                self.app.stdout.write("\nError Returned:\n" + str(smgrutils.print_rest_response(resp)) +
                                      "\nPlease check that inventory and monitoring have been configured correctly.\n")
Esempio n. 5
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        # contruct paylod for backend
        payload = {}
        payload['opcode'] = 'issu_finalize'
        payload['old_cluster'] = parsed_args.cluster_id_old
        payload['new_cluster'] = parsed_args.cluster_id_new

        if (not parsed_args.no_confirm):
            msg = "Switch from cluster %s to %s? (y/N) :" %(
                      parsed_args.cluster_id_old, parsed_args.cluster_id_new)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port,
                                           obj="server/provision",
                                           payload=payload, method="POST")
        self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        # contruct paylod for backend
        payload = {}
        payload['opcode'] = 'issu_finalize'
        payload['old_cluster'] = parsed_args.cluster_id_old
        payload['new_cluster'] = parsed_args.cluster_id_new

        if (not parsed_args.no_confirm):
            msg = "Switch from cluster %s to %s? (y/N) :" %(
                      parsed_args.cluster_id_old, parsed_args.cluster_id_new)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port,
                                           obj="server/provision",
                                           payload=payload, method="POST")
        self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
Esempio n. 7
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        try:
            image_id = getattr(parsed_args, "id", None)
            image_version = getattr(parsed_args, "version", None)
            image_type = getattr(parsed_args, "type", None)
            image_category = getattr(parsed_args, "category", None)
            openstack_sku = ''
            if getattr(parsed_args, "openstack_sku", None):
                openstack_sku = getattr(parsed_args, "openstack_sku", None)
            kickstart = kickseed = ''
            if getattr(parsed_args, "kickstart", None):
                kickstart = getattr(parsed_args, "kickstart", None)
            # end args.kickstart
            if getattr(parsed_args, "kickseed", None):
                kickseed = getattr(parsed_args, "kickseed", None)
            # end args.kickseed
            file_name = getattr(parsed_args, "file_name", None)
            payload = dict()
            payload['id'] = image_id
            payload['version'] = image_version
            payload['type'] = image_type
            payload['category'] = image_category
            payload["file"] = (pycurl.FORM_FILE, file_name)
            if openstack_sku:
                payload["openstack_sku"] = openstack_sku
            if kickstart:
                payload["kickstart"] = (pycurl.FORM_FILE, kickstart)
            if kickseed:
                payload["kickseed"] = (pycurl.FORM_FILE, kickseed)
            for param in payload:
                if not payload[param]:
                    self.app.stdout.write("\nMissing mandatory parameter: " +
                                          str(param) + "\n")
                    sys.exit(0)
            if image_id:
                resp = smgrutils.send_REST_request(self.smgr_ip,
                                                   self.smgr_port,
                                                   obj="image/upload",
                                                   payload=payload,
                                                   method="PUT")
                self.app.stdout.write(
                    "\n" + str(smgrutils.print_rest_response(resp)) + "\n")
        except Exception as e:
            sys.exit("Exception: %s : Error uploading image" % e.message)
Esempio n. 8
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        match_key = None
        match_value = None
        payload = {}
        if getattr(parsed_args, "server_id", None):
            match_key = 'id'
            match_value = getattr(parsed_args, "server_id", None)
        elif getattr(parsed_args, "cluster_id", None):
            match_key = 'cluster_id'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "server_id", None):
            match_key = 'tag'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "server_id", None):
            match_key = 'where'
            match_value = getattr(parsed_args, "cluster_id", None)
        else:
            pass

        payload['base_image_id'] = getattr(parsed_args, "base_image_id", None)
        payload['package_image_id'] = getattr(parsed_args, "package_image_id", None)

        if getattr(parsed_args, "no_reboot", None):
            payload['no_reboot'] = "y"
        if match_key:
            payload[match_key] = match_value

        if not getattr(parsed_args, "no_confirm", None):
            if getattr(parsed_args, "base_image_id", None):
                image_str = getattr(parsed_args, "base_image_id", None)
            else:
                image_str = "configured"
            msg = "Reimage servers (%s:%s) with %s? (y/N) :" % (
                match_key, match_value, image_str)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        if payload:
            #self.app.print_error_message_and_quit("Payload = " + str(payload) + "\n\n")
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="server/reimage",
                                               payload=payload, method="POST")
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
    def take_action(self, parsed_args):
        detail = getattr(parsed_args, 'detail', None)
        rest_api_params = {}

        # end except
        if parsed_args.which == "server":
            rest_api_params = self.show_server(parsed_args)
        elif parsed_args.which == "image":
            rest_api_params = self.show_image(parsed_args)
        elif parsed_args.which == "cluster":
            rest_api_params = self.show_cluster(parsed_args)
        elif parsed_args.which == 'tag':
            rest_api_params = self.show_tag()
        elif parsed_args.which == 'all':
            rest_api_params = self.show_all()
        elif parsed_args.which == 'monitoring':
            rest_api_params = mon_querying_obj.show_mon_details(parsed_args)
        elif parsed_args.which == 'inventory':
            rest_api_params = inv_querying_obj.show_inv_details(parsed_args)
        elif parsed_args.which == "columns":
            rest_api_params = self.show_columns(parsed_args)

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)
        resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, rest_api_params=rest_api_params,
                                          detail=detail, method='GET')

        json_format = getattr(parsed_args, "json", False)

        detail = getattr(parsed_args, "detail", None)
        if detail:
            json_format = True

        if (parsed_args.which == 'monitoring' or parsed_args.which == 'inventory') and not rest_api_params["select"]:
            json_format = True
        elif parsed_args.which == 'all':
            json_format = True
        elif parsed_args.which == "columns":
            json_format = True

        if resp.startswith("Error"):
            self.app.stdout.write(resp + "\n")
        elif json_format:
            self.app.stdout.write(str(smgrutils.print_rest_response(resp)) + "\n")
        else:
            table_format_output = smgrutils.convert_json_to_table(parsed_args.which, resp, rest_api_params["select"])
            self.app.stdout.write(str(table_format_output) + "\n")
Esempio n. 10
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)
        match_key = None
        match_value = None
        match_param = None
        payload = {}

        if getattr(parsed_args, "server_id", None):
            match_key = 'id'
            match_value = getattr(parsed_args, "server_id", None)
        elif getattr(parsed_args, "cluster_id", None):
            match_key = 'cluster_id'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "tag", None):
            match_key = 'tag'
            match_value = getattr(parsed_args, "tag", None)
        elif getattr(parsed_args, "where", None):
            match_key = 'where'
            match_value = getattr(parsed_args, "where", None)
        else:
            pass

        if match_key:
            payload[match_key] = match_value
        if getattr(parsed_args, "net_boot", None):
            payload['net_boot'] = "y"

        if not getattr(parsed_args, "no_confirm", None):
            msg = "Restart servers (%s:%s)? (y/N) :" % (match_key, match_value)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip,
                                               self.smgr_port,
                                               obj="server/restart",
                                               payload=payload,
                                               method="POST")
            self.app.stdout.write("\n" +
                                  str(smgrutils.print_rest_response(resp)) +
                                  "\n")
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        try:
            image_id = getattr(parsed_args, "id", None)
            image_version = getattr(parsed_args, "version", None)
            image_type = getattr(parsed_args, "type", None)
            image_category = getattr(parsed_args, "category", None)
            openstack_sku = ''
            if getattr(parsed_args, "openstack_sku", None):
                openstack_sku = getattr(parsed_args, "openstack_sku", None)
            kickstart = kickseed = ''
            if getattr(parsed_args, "kickstart", None):
                kickstart = getattr(parsed_args, "kickstart", None)
            # end args.kickstart
            if getattr(parsed_args, "kickseed", None):
                kickseed = getattr(parsed_args, "kickseed", None)
            # end args.kickseed
            file_name = getattr(parsed_args, "file_name", None)
            payload = dict()
            payload['id'] = image_id
            payload['version'] = image_version
            payload['type'] = image_type
            payload['category'] = image_category
            payload["file"] = (pycurl.FORM_FILE, file_name)
            if openstack_sku:
                payload["openstack_sku"] = openstack_sku
            if kickstart:
                payload["kickstart"] = (pycurl.FORM_FILE, kickstart)
            if kickseed:
                payload["kickseed"] = (pycurl.FORM_FILE, kickseed)
            for param in payload:
                if not payload[param]:
                    self.app.stdout.write("\nMissing mandatory parameter: " + str(param) + "\n")
                    sys.exit(0)
            if image_id:
                resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="image/upload",
                                                   payload=payload, method="PUT")
                self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
        except Exception as e:
            sys.exit("Exception: %s : Error uploading image" % e.message)
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)
        match_key = None
        match_value = None
        match_param = None
        payload = {}

        if getattr(parsed_args, "server_id", None):
            match_key = 'id'
            match_value = getattr(parsed_args, "server_id", None)
        elif getattr(parsed_args, "cluster_id", None):
            match_key = 'cluster_id'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "tag", None):
            match_key = 'tag'
            match_value = getattr(parsed_args, "tag", None)
        elif getattr(parsed_args, "where", None):
            match_key = 'where'
            match_value = getattr(parsed_args, "where", None)
        else:
            pass

        if match_key:
            payload[match_key] = match_value
        if getattr(parsed_args, "net_boot", None):
            payload['net_boot'] = "y"

        if not getattr(parsed_args, "no_confirm", None):
            msg = "Restart servers (%s:%s)? (y/N) :" % (
                match_key, match_value)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="server/restart",
                                               payload=payload, method="POST")
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
Esempio n. 13
0
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        # contruct paylod for backend
        payload = {}
        payload['opcode'] = 'issu'
        payload['old_cluster'] = parsed_args.cluster_id_old
        payload['new_cluster'] = parsed_args.cluster_id_new
        payload['new_image'] = parsed_args.new_image
        if parsed_args.compute_all:
            payload['compute_tag'] = "all_computes"
        elif parsed_args.tag:
            payload['compute_tag'] = parsed_args.tag
        elif parsed_args.server_id:
            payload['compute_tag'] = "__server__id__" + parsed_args.server_id
        else:
            payload['compute_tag'] = ""

        if (not parsed_args.no_confirm):
            msg = "Upgrade cluster %s to %s, Contrail Image:%s? (y/N) :" %(
                         parsed_args.cluster_id_old, parsed_args.cluster_id_new,
                         parsed_args.new_image)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port,
                                           obj="server/provision",
                                           payload=payload, method="POST")
        self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
Esempio n. 14
0
    def take_action(self, parsed_args):
        detail = getattr(parsed_args, 'detail', None)
        rest_api_params = {}

        # end except
        if parsed_args.which == "server":
            rest_api_params = self.show_server(parsed_args)
        elif parsed_args.which == "image":
            rest_api_params = self.show_image(parsed_args)
        elif parsed_args.which == "cluster":
            rest_api_params = self.show_cluster(parsed_args)
        elif parsed_args.which == 'tag':
            rest_api_params = self.show_tag()
        elif parsed_args.which == 'all':
            rest_api_params = self.show_all()
        elif parsed_args.which == 'monitoring':
            rest_api_params = mon_querying_obj.show_mon_details(parsed_args)
        elif parsed_args.which == 'inventory':
            rest_api_params = inv_querying_obj.show_inv_details(parsed_args)
        elif parsed_args.which == "columns":
            rest_api_params = self.show_columns(parsed_args)

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)
        resp = smgrutils.send_REST_request(self.smgr_ip,
                                           self.smgr_port,
                                           rest_api_params=rest_api_params,
                                           detail=detail,
                                           method='GET')

        json_format = getattr(parsed_args, "json", False)

        detail = getattr(parsed_args, "detail", None)
        if detail:
            json_format = True

        if (parsed_args.which == 'monitoring' or parsed_args.which
                == 'inventory') and not rest_api_params["select"]:
            json_format = True
        elif parsed_args.which == 'all':
            json_format = True
        elif parsed_args.which == "columns":
            json_format = True

        if resp.startswith("Error"):
            self.app.stdout.write(resp + "\n")
        elif json_format:
            self.app.stdout.write(
                str(smgrutils.print_rest_response(resp)) + "\n")
        else:
            table_format_output = smgrutils.convert_json_to_table(
                parsed_args.which, resp, rest_api_params["select"])
            self.app.stdout.write(str(table_format_output) + "\n")
Esempio n. 15
0
    def take_action(self, parsed_args, remaining_args=None):

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")

        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        smgr_obj = getattr(parsed_args, "object", None)
        payload = None

        try:
            if getattr(parsed_args, "file_name",
                       None) and smgr_obj in self.smgr_objects:
                payload = json.load(open(parsed_args.file_name))
                if smgr_obj == "tag":
                    self.verify_added_tags(smgr_obj, payload)
                else:
                    for obj_payload in payload[str(smgr_obj)]:
                        if "tag" in obj_payload and smgr_obj == "server":
                            self.verify_added_tags(smgr_obj, obj_payload)
                        if "id" not in obj_payload and smgr_obj != "tag":
                            self.app.print_error_message_and_quit(
                                "No id specified for object being added")
            elif not (getattr(parsed_args, "id", None) or getattr(parsed_args, "mac_address", None)) \
                    and smgr_obj != "tag" and smgr_obj != "dhcp_subnet":
                # Check if parsed args has id for object
                self.app.print_error_message_and_quit(
                    "\nYou need to specify the id or mac_address to add an object"
                    " (Arguement --id/--mac_address).\n")
            elif smgr_obj not in self.smgr_objects:
                self.app.print_error_message_and_quit("\nThe object: " +
                                                      str(smgr_obj) +
                                                      " is not a valid one.\n")
            elif smgr_obj == "tag":
                payload = self.add_tag(parsed_args, remaining_args)
                self.verify_added_tags(smgr_obj, payload)
            else:
                payload = {}
                payload[smgr_obj] = list()
                # Collect object payload from parsed_args and remaining args
                payload[smgr_obj].append(
                    self.add_object(smgr_obj, parsed_args, remaining_args))
                # Verify tags and mandatory params added for given object
                for obj_payload in payload[smgr_obj]:
                    if "tag" in obj_payload and smgr_obj == "server":
                        self.verify_added_tags(smgr_obj, obj_payload)
                    mandatory_params_set = set(self.mandatory_params[smgr_obj])
                    added_params_set = set(obj_payload.keys())
                    if mandatory_params_set.difference(added_params_set):
                        self.app.stdout.write(
                            "\nMandatory parameters for object " +
                            str(smgr_obj) + " not entered\n")
                        self.app.print_error_message_and_quit(
                            "\nList of missing mandatory parameters are: " +
                            str(
                                list(
                                    mandatory_params_set.difference(
                                        added_params_set))) + "\n")
            # Merge obj_payload with ini defaults, in code defaults (same func)
            self.merge_with_defaults(smgr_obj, payload)
        except ValueError as e:
            self.app.stdout.write("\nError in CLI Format - ValueError: " +
                                  str(e) + "\n")
            self.app.stdout.write("\nError Message: " + str(e.message) + "\n")
            self.app.stdout.write("\nPayload: " + str(payload) + "\n")
        except Exception as e:
            self.app.stdout.write("\nException here:" + str(e) + "\n")
        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip,
                                               self.smgr_port,
                                               obj=smgr_obj,
                                               payload=payload,
                                               method="PUT")
            smgrutils.print_rest_response(resp)
            self.app.stdout.write("\n" +
                                  str(smgrutils.print_rest_response(resp)) +
                                  "\n")
        else:
            self.app.stdout.write("\nNo payload for object " + str(smgr_obj) +
                                  "\nPlease enter params\n")
Esempio n. 16
0
    def take_action(self, parsed_args, remaining_args=None):

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")

        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        smgr_obj = getattr(parsed_args, "object", None)
        payload = None

        try:
            if getattr(parsed_args, "file_name", None) and smgr_obj in self.smgr_objects:
                payload = json.load(open(parsed_args.file_name))
                if smgr_obj == "tag":
                    self.verify_added_tags(smgr_obj, payload)
                else:
                    for obj_payload in payload[str(smgr_obj)]:
                        if "tag" in obj_payload and smgr_obj == "server":
                            self.verify_added_tags(smgr_obj, obj_payload)
                        if "id" not in obj_payload and smgr_obj != "tag":
                            self.app.print_error_message_and_quit("No id specified for object being added")
            elif (
                not (getattr(parsed_args, "id", None) or getattr(parsed_args, "mac_address", None))
                and smgr_obj != "tag"
            ):
                # 1. Check if parsed args has id for object
                self.app.print_error_message_and_quit(
                    "\nYou need to specify the id or mac_address to add an object" " (Arguement --id/--mac_address).\n"
                )
            elif smgr_obj not in self.smgr_objects:
                self.app.print_error_message_and_quit("\nThe object: " + str(smgr_obj) + " is not a valid one.\n")
            elif smgr_obj == "tag":
                payload = self.add_tag(parsed_args, remaining_args)
                self.verify_added_tags(smgr_obj, payload)
            else:
                payload = {}
                # 2. Check that id duplicate doesn't exist for this added object
                resp = smgrutils.send_REST_request(
                    self.smgr_ip, self.smgr_port, obj=smgr_obj, detail=True, method="GET"
                )
                existing_objects_dict = json.loads(resp)
                existing_objects = existing_objects_dict[smgr_obj]
                obj_id_list = list()
                added_obj_id = getattr(parsed_args, "id", None)
                for ex_obj in existing_objects:
                    obj_id_list.append(ex_obj["id"])
                if added_obj_id in obj_id_list:
                    self.app.print_error_message_and_quit(
                        "\n" + str(smgr_obj) + " with this id already exists. You cannot add it again.\n"
                    )
                payload[smgr_obj] = list()
                # 3. Collect object payload from parsed_args and remaining args
                payload[smgr_obj].append(self.add_object(smgr_obj, parsed_args, remaining_args))
                # 4. Merge obj_payload with ini defaults, in code defaults (same func)
                self.merge_with_defaults(smgr_obj, payload)
                # 5. Verify tags and mandatory params added for given object
                for obj_payload in payload[smgr_obj]:
                    if "tag" in obj_payload and smgr_obj == "server":
                        self.verify_added_tags(smgr_obj, obj_payload)
                    mandatory_params_set = set(self.mandatory_params[smgr_obj])
                    added_params_set = set(obj_payload.keys())
                    if mandatory_params_set.difference(added_params_set):
                        self.app.stdout.write("\nMandatory parameters for object " + str(smgr_obj) + " not entered\n")
                        self.app.print_error_message_and_quit(
                            "\nList of missing mandatory parameters are: "
                            + str(list(mandatory_params_set.difference(added_params_set)))
                            + "\n"
                        )
        except ValueError as e:
            self.app.stdout.write("\nError in CLI Format - ValueError: " + str(e) + "\n")
            self.app.stdout.write("\nError Message: " + str(e.message) + "\n")
            self.app.stdout.write("\nPayload: " + str(payload) + "\n")
        except Exception as e:
            self.app.stdout.write("\nException here:" + str(e) + "\n")
        if payload:
            resp = smgrutils.send_REST_request(
                self.smgr_ip, self.smgr_port, obj=smgr_obj, payload=payload, method="PUT"
            )
            smgrutils.print_rest_response(resp)
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
        else:
            self.app.stdout.write("\nNo payload for object " + str(smgr_obj) + "\nPlease enter params\n")
Esempio n. 17
0
    def take_action(self, parsed_args, remaining_args=None):

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")

        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        smgr_obj = getattr(parsed_args, "object", None)
        if not smgr_obj:
            self.app.print_error_message_and_quit("\nNo object entered for editing\n")
        payload = None

        try:
            if getattr(parsed_args, "file_name", None) and smgr_obj in self.smgr_objects:
                payload = json.load(open(parsed_args.file_name))
                if smgr_obj == "tag":
                    self.verify_edited_tags(smgr_obj, payload)
                else:
                    for obj_payload in payload[str(smgr_obj)]:
                        if "tag" in obj_payload and smgr_obj == "server":
                            self.verify_edited_tags(smgr_obj, obj_payload)
                        if "id" not in obj_payload and smgr_obj != "tag":
                            self.app.print_error_message_and_quit("No id specified for object being edited")
            elif not (getattr(parsed_args, "id", None) or getattr(parsed_args, "mac_address", None)) \
                    and smgr_obj != "tag":
                # 1. Check if parsed args has id for object
                self.app.print_error_message_and_quit(
                    "\nYou need to specify the id or mac_address to edit an object (Arguement --id/--mac_address).\n")
            elif smgr_obj not in self.smgr_objects:
                self.app.print_error_message_and_quit(
                    "\nThe object: " + str(smgr_obj) + " is not a valid one.\n")
            elif smgr_obj == "tag":
                payload = self.edit_tag(parsed_args, remaining_args)
                self.verify_edited_tags(smgr_obj, payload)
            else:
                payload = {}
                # 2. Check that id exists for this added object
                resp = smgrutils.send_REST_request(
                    self.smgr_ip, self.smgr_port,
                    obj=smgr_obj, detail=True, method="GET")
                existing_objects_dict = json.loads(resp)
                existing_objects = existing_objects_dict[smgr_obj]
                obj_id_list = list()
                edited_obj_id = getattr(parsed_args, "id", None)
                for ex_obj in existing_objects:
                    obj_id_list.append(ex_obj["id"])
                    if edited_obj_id == ex_obj["id"]:
                        edited_obj_config = ex_obj
                if edited_obj_id not in obj_id_list:
                    self.app.print_error_message_and_quit(
                        "\n" + str(smgr_obj) + " with this id doesn't already exist. You need to add it first.\n")
                payload[smgr_obj] = list()
                # 3. Collect object payload from parsed_args and remaining args
                payload[smgr_obj].append(self.edit_object(smgr_obj, parsed_args, remaining_args))
                # 4. Verify tags and mandatory params added for given object
                for obj_payload in payload[smgr_obj]:
                    if "tag" in obj_payload and smgr_obj == "server":
                        self.verify_edited_tags(smgr_obj, obj_payload)
        except ValueError as e:
            self.app.print_error_message_and_quit("\nError in CLI Format - ValueError: " + str(e) + "\n")
        except Exception as e:
            self.app.print_error_message_and_quit("\nException here:" + str(e) + "\n")
        if payload:
            resp = smgrutils.send_REST_request(
                self.smgr_ip, self.smgr_port, obj=smgr_obj, payload=payload, method="PUT")
            smgrutils.print_rest_response(resp)
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
        else:
            self.app.stdout.write("\nNo payload for object " + str(smgr_obj) + "\nPlease enter params\n")
    def take_action(self, parsed_args):
        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")
        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        provision_params = {}
        payload = {}
        match_key = None
        match_value = None
        match_param = None
        contrail_img_id = None
        tasks = None
        if getattr(parsed_args, "server_id", None):
            match_key = 'id'
            match_value = getattr(parsed_args, "server_id", None)
        elif getattr(parsed_args, "cluster_id", None):
            match_key = 'cluster_id'
            match_value = getattr(parsed_args, "cluster_id", None)
        elif getattr(parsed_args, "tag", None):
            match_key = 'tag'
            match_value = getattr(parsed_args, "tag", None)
        elif getattr(parsed_args, "where", None):
            match_key = 'where'
            match_value = getattr(parsed_args, "where", None)
        elif getattr(parsed_args, "interactive", None):
            provision_params = self.get_provision_params()
        elif getattr(parsed_args, "provision_params_file", None):
            provision_params = json.load(open(getattr(parsed_args, "provision_params_file", None)))
        else:
            pass

        if getattr(parsed_args, "contrail_image_id", None):
            contrail_img_id = getattr(parsed_args, "contrail_image_id", None)
        payload['package_image_id'] = getattr(parsed_args, "package_image_id", None)
        if contrail_img_id != None:
            payload['contrail_image_id'] = contrail_img_id
        if match_key and match_value:
            payload[match_key] = match_value
        if provision_params:
            payload['provision_parameters'] = provision_params

        if getattr(parsed_args, "tasks", None):
            tasks =  getattr(parsed_args, "tasks", None)
        if tasks != None:
            payload['tasks'] = tasks

	if getattr(parsed_args, "no_run", None):
            payload['no_run'] = 1;

        if not getattr(parsed_args, "no_confirm", None):
            if getattr(parsed_args, "package_image_id", None):
                pkg_id_str = getattr(parsed_args, "package_image_id", None)
            else:
                pkg_id_str = "configured package"
            if match_key:
                if tasks:
                    msg = "Provision servers (%s:%s) with %s and tasks %s? (y/N) :" % (
                        match_key, match_value, pkg_id_str, tasks)
                else:
                    msg = "Provision servers (%s:%s) with %s? (y/N) :" % (
                    match_key, match_value, pkg_id_str)
            else:
                msg = "Provision servers with %s? (y/N) :" % (pkg_id_str)
            user_input = raw_input(msg).lower()
            if user_input not in ["y", "yes"]:
                sys.exit()
        # end if

        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj="server/provision",
                                               payload=payload, method="POST")
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
Esempio n. 19
0
    def take_action(self, parsed_args, remaining_args=None):

        try:
            self.smgr_ip = self.smgr_port = None
            smgr_dict = self.app.get_smgr_config()

            if smgr_dict["smgr_ip"]:
                self.smgr_ip = smgr_dict["smgr_ip"]
            else:
                self.app.report_missing_config("smgr_ip")
            if smgr_dict["smgr_port"]:
                self.smgr_port = smgr_dict["smgr_port"]
            else:
                self.app.report_missing_config("smgr_port")

        except Exception as e:
            sys.exit("Exception: %s : Error getting smgr config" % e.message)

        smgr_obj = getattr(parsed_args, "object", None)
        payload = None

        try:
            if getattr(parsed_args, "file_name", None) and smgr_obj in self.smgr_objects:
                payload = json.load(open(parsed_args.file_name))
                if smgr_obj == "tag":
                    self.verify_added_tags(smgr_obj, payload)
                elif smgr_obj == "config":
                    for obj in payload.keys():
                        for obj_payload in payload[obj]:
                            if "id" not in obj_payload:
                                self.app.print_error_message_and_quit("No id specified for object %s being added\n" % (obj))
                else:
                    for obj_payload in payload[str(smgr_obj)]:
                        if "tag" in obj_payload and smgr_obj == "server":
                            self.verify_added_tags(smgr_obj, obj_payload)
                        if "id" not in obj_payload and (smgr_obj != "tag" and smgr_obj != "dhcp_host" and smgr_obj != "dhcp_subnet"):
                            self.app.print_error_message_and_quit("No id specified for object being added\n")
            elif not (getattr(parsed_args, "id", None) or getattr(parsed_args, "mac_address", None)) \
                    and smgr_obj != "tag" and smgr_obj != "dhcp_subnet":
                # Check if parsed args has id for object
                self.app.print_error_message_and_quit("\nYou need to specify the id or mac_address to add an object"
                                                      " (Arguement --id/--mac_address).\n")
            elif smgr_obj not in self.smgr_objects:
                self.app.print_error_message_and_quit(
                    "\nThe object: " + str(smgr_obj) + " is not a valid one.\n")
            elif smgr_obj == "tag":
                payload = self.add_tag(parsed_args, remaining_args)
                self.verify_added_tags(smgr_obj, payload)
            else:
                payload = {}
                payload[smgr_obj] = list()
                # Collect object payload from parsed_args and remaining args
                payload[smgr_obj].append(self.add_object(smgr_obj, parsed_args, remaining_args))
                # Verify tags and mandatory params added for given object
                for obj_payload in payload[smgr_obj]:
                    if "tag" in obj_payload and smgr_obj == "server":
                        self.verify_added_tags(smgr_obj, obj_payload)
                    mandatory_params_set = set(self.mandatory_params[smgr_obj])
                    added_params_set = set(obj_payload.keys())
                    if mandatory_params_set.difference(added_params_set):
                        self.app.stdout.write("\nMandatory parameters for object " + str(smgr_obj) + " not entered\n")
                        self.app.print_error_message_and_quit("\nList of missing mandatory parameters are: " + str(list(
                            mandatory_params_set.difference(added_params_set))) + "\n")
            # Merge obj_payload with ini defaults, in code defaults (same func)
            self.merge_with_defaults(smgr_obj, payload)
        except ValueError as e:
            self.app.stdout.write("\nError in CLI Format - ValueError: " + str(e) + "\n")
            self.app.stdout.write("\nError Message: " + str(e.message) + "\n")
            self.app.stdout.write("\nPayload: " + str(payload) + "\n")
        except Exception as e:
            self.app.stdout.write("\nException here:" + str(e) + "\n")
        if payload:
            resp = smgrutils.send_REST_request(self.smgr_ip, self.smgr_port, obj=smgr_obj, payload=payload, method="PUT")
            smgrutils.print_rest_response(resp)
            self.app.stdout.write("\n" + str(smgrutils.print_rest_response(resp)) + "\n")
        else:
            self.app.stdout.write("\nNo payload for object " + str(smgr_obj) + "\nPlease enter params\n")