def activate_node(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/activator/agent/<node name>
            Response: {
                "name": "<name of the node>",
                "reboot": "<boolean value indicating whether a reboot is needed>",
            }
        """

        logging.debug('activate_node called')
        try:
            node_name = rpc.req_params['node']
            reboot_needed = self.processor.activate_node(node_name)
            reply = {}
            reply['name'] = node_name
            reply['reboot'] = reboot_needed
            rpc.rep_status = CMHTTPErrors.get_ok_status()
            rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def set_property(self, rpc):
        """
            Request: POST http://<cm-vip:port>/cm/v1.0/properties/<property-name>
                {
                    "value": "<value of the property>"
                }
            Response: http status set correctly
               {
                    "change-uuid": "<uuid>"
               }
        """

        logging.debug('set_property called')
        try:
            if not rpc.req_body:
                rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
            else:
                request = json.loads(rpc.req_body)
                name = rpc.req_params['property']
                value = request['value']
                uuid_value = self.processor.set_property(name, value)
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                reply = {}
                reply['change-uuid'] = uuid_value
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def reboot_node(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/reboot?node-name=<node name>
            Response: {
                "node-name": "<name of the node>",
            }
        """

        logging.debug('reboot_node called')
        try:
            if not rpc.req_filter:
                rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
            else:
                node_name = rpc.req_filter.get('node-name', None)
                if isinstance(node_name, list):
                    node_name = node_name[0]
                self.processor.reboot_request(node_name)
                reply = {}
                reply['node-name'] = node_name
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def delete_property(self, rpc):
        """
            Request: DELETE http://<cm-vip:port>/cm/v1.0/properties/<property-name>
            Response: http response with proper status
                {
                    "change-uuid": "<uuid>"
                }
        """

        logging.debug('delete_property called')
        try:
            prop = rpc.req_params['property']
            uuid_value = self.processor.delete_property(prop)
            rpc.rep_status = CMHTTPErrors.get_ok_status()
            reply = {}
            reply['change-uuid'] = uuid_value
            rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def get_property(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/properties/
                             <property-name>?snapshot=<snapshot name>
            Response: {
                "name": "<name of the property>",
                "value": "<value of the property>",
            }
        """

        logging.debug('get_property called')
        try:
            snapshot_name = rpc.req_filter.get('snapshot', None)
            if isinstance(snapshot_name, list):
                snapshot_name = snapshot_name[0]
            prop_name = rpc.req_params['property']
            value = self.processor.get_property(prop_name, snapshot_name)
            reply = {}
            reply['name'] = prop_name
            reply['value'] = value
            rpc.rep_status = CMHTTPErrors.get_ok_status()
            rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def activate(self, rpc):
        """
            Request: POST http://<cm-vip:port>/cm/v1.0/activator/<node-name>
            Response: http response with proper status
            {
                "change-uuid": "<uuid>"
            }
        """

        logging.debug('activate called')
        try:
            node_name = rpc.req_params.get('node', None)
            uuid_value = self.processor.activate(node_name)
            rpc.rep_status = CMHTTPErrors.get_ok_status()
            reply = {}
            reply['change-uuid'] = uuid_value
            rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def list_snapshots(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/snapshots
            Response: {
                "snapshots": [
                    "<name of the snapshot>",
                    ....
                ]
            }
        """

        logging.debug('list_snapshots called')
        try:
            snapshots = self.processor.list_snapshots()
            if not bool(snapshots):
                rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            else:
                reply = {}
                reply['snapshots'] = snapshots
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
Ejemplo n.º 8
0
 def _get_api(self, rpc):
     logging.debug('_get_api called')
     api = None
     try:
         version = rpc.req_params['api']
         api = self.rest_api_factory.get_api(version)
         if not api:
             rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     except KeyError:
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
     return api
    def set_properties(self, rpc):
        """
            Request: POST http://<cm-vip:port>/cm/v1.0/properties
                {
                    "overwrite": True|False,
                    "properties": [
                        {
                            "name": "<name of the property>",
                            "value": "<value of the property>"
                        },
                        {
                            "name": "<name of the property>",
                            "value": "<value of the property>"
                        },
                        ....
                    ]
                }
            Response:
                {
                     "change-uuid": "<uuid>"
                }
        """

        logging.debug('set_properties called')
        try:
            if not rpc.req_body:
                rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
            else:
                request = json.loads(rpc.req_body)
                overwrite = False
                if 'overwrite' in request:
                    overwrite = request['overwrite']
                items = request['properties']
                data = {}
                for entry in items:
                    name = entry['name']
                    value = entry['value']
                    data[name] = value
                uuid_value = self.processor.set_properties(data, overwrite)
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                reply = {}
                reply['change-uuid'] = uuid_value
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def get_properties(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/properties?
                             prop-name-filter=<filter>&snapshot=<snapshot name>
            Response: {
                "properties": [
                    {
                        "name": "<name of the property>",
                        "value": "<value of the property>"
                    },
                    {
                        "name": "<name of the property>",
                        "value": "<value of the property>"
                    }
                    ....
                ]
            }
        """

        logging.debug('get_properties called')
        try:
            prop_name_filter = rpc.req_filter.get('prop-name-filter', '')
            if isinstance(prop_name_filter, list):
                prop_name_filter = prop_name_filter[0]
            snapshot_name = rpc.req_filter.get('snapshot', None)
            if isinstance(snapshot_name, list):
                snapshot_name = snapshot_name[0]
            result = self.processor.get_properties(prop_name_filter, snapshot_name)
            if not bool(result):
                rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            else:
                reply = {}
                items = []
                for key, value in result.iteritems():
                    tmp = {}
                    tmp['name'] = key
                    tmp['value'] = value
                    items.append(tmp)
                reply['properties'] = items
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
Ejemplo n.º 11
0
 def get_apis(self, rpc):
     logging.debug('get_apis called')
     if rpc.req_method != 'GET':
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
         rpc.rep_status += ', only GET operation is possible'
     else:
         self.rest_api_factory.get_apis(rpc)
Ejemplo n.º 12
0
 def handle_snapshots(self, rpc):
     logging.debug('handle_snapshots called')
     if rpc.req_method == 'GET':
         self.list_snapshots(rpc)
     else:
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
         rpc.rep_status += ', only GET is possible to this resource'
Ejemplo n.º 13
0
 def handle_agent_activate(self, rpc):
     logging.debug('handle_agent_activate called')
     if rpc.req_method == 'GET':
         self.activate_node(rpc)
     else:
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
         rpc.rep_status += ', only GET is possible to this resource'
Ejemplo n.º 14
0
 def handle_activator_enable(self, rpc):
     logging.debug('handle_activator_enable called')
     if rpc.req_method == 'POST':
         self.set_automatic_activation_state(rpc, True)
     else:
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
         rpc.rep_status += ', only POST is possible to this resource'
    def restore_snapshot(self, rpc):
        """
            Request: POST http://<cm-vip:port>/cm/v1.0/snapshots/<snapshot name>
            Response: http response with proper status
        """

        logging.debug('restore_snapshot called')
        try:
            snapshot_name = rpc.req_params['snapshot']
            self.processor.restore_snapshot(snapshot_name)
            rpc.rep_status = CMHTTPErrors.get_ok_status()
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def set_automatic_activation_state(self, rpc, state):
        """
            Request: POST http://<cm-vip:port>/cm/v1.0/activator/disable
            or       POST http://<cm-vip:port>/cm/v1.0/activator/enable
            Response: http response with proper status
        """

        logging.debug('set_automatic_activation_state called')
        try:
            self.processor.set_automatic_activation_state(state)
            rpc.rep_status = CMHTTPErrors.get_ok_status()
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def delete_properties(self, rpc):
        """
            Request: DELETE http://<cm-vip:port>/cm/v1.0/properties?prop-name-filter=<filter>
                {
                    'properties': [ <prop-name>,
                                    <prop-name>,
                                    ....
                                  ]
                }
            Response: http response with proper status
               {
                    "change-uuid": "<uuid>"
               }
        """

        logging.debug('delete_properties called')
        try:
            if not rpc.req_filter and not rpc.req_body:
                rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
            else:
                if rpc.req_filter:
                    arg = rpc.req_filter.get('prop-name-filter', '')
                    if isinstance(arg, list):
                        arg = arg[0]
                else:
                    body = json.loads(rpc.req_body)
                    arg = body['properties']
                uuid_value = self.processor.delete_properties(arg)
                rpc.rep_status = CMHTTPErrors.get_ok_status()
                reply = {}
                reply['change-uuid'] = uuid_value
                rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
    def get_changes_states(self, rpc):
        """
            Request: GET http://<cm-vip:port>/cm/v1.0/changes?changd-uuid-filter=<filter>
            Response: {
                "<change-uuid>"" : {
                                    "state": "<state>",
                                    "failed-plugins": { "plugin-name":"error",  ... }
                ...
            }
        """

        logging.debug('get_changes_states called')
        try:
            reply = {}
            changemonitor = self.processor.changemonitor
            change_uuid_value = None
            change_uuid_list = rpc.req_filter.get('change-uuid-filter', None)
            if change_uuid_list:
                change_uuid_value = change_uuid_list[0]
                state = changemonitor.get_change_state(change_uuid_value)
                reply[change_uuid_value] = {}
                reply[change_uuid_value]["state"] = state.state
                reply[change_uuid_value]["failed-plugins"] = state.failed_plugins
            else:
                changes = changemonitor.get_all_changes_states()
                for change_uuid, state in changes.iteritems():
                    reply[change_uuid] = {}
                    reply[change_uuid]["state"] = state.state
                    reply[change_uuid]["failed-plugins"] = state.failed_plugins

            rpc.rep_status = CMHTTPErrors.get_ok_status()
            rpc.rep_body = json.dumps(reply)
        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except KeyError:
            rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
Ejemplo n.º 19
0
 def handle_snapshot(self, rpc):
     logging.debug('handle_snapshot called')
     if rpc.req_method == 'GET':
         self.create_snapshot(rpc)
     elif rpc.req_method == 'POST':
         self.restore_snapshot(rpc)
     elif rpc.req_method == 'DELETE':
         self.delete_snapshot(rpc)
     else:
         rpc.rep_status = CMHTTPErrors.get_request_not_ok_status()
         rpc.rep_status += ', only GET/POST/DELETE are possible to this resource'
Ejemplo n.º 20
0
 def get_apis(self, rpc):
     """
         Request: GET http://<cm-vip:port>/cm/apis
         Response:
             {
                 "versions": [
                     {
                         "id": "<version>",
                         "href": "<http address for the api>"
                         "min-version": "<mimimum version required to support this version>",
                         "status": "<supported|deprecated|unsupported|current>"
                     },
                     ...
                 ]
             }
     """
     try:
         logging.debug('get_apis called')
         reply = {}
         versions = []
         for key, value in self.apis.iteritems():
             version = {}
             version['id'] = value.get_version()
             version['href'] = '%s%s/' % (self.base_url, key)
             version['min-version'] = value.get_minimum_version()
             version['status'] = value.get_status()
             versions.append(version)
         reply['versions'] = versions
         rpc.rep_status = CMHTTPErrors.get_ok_status()
         rpc.rep_body = json.dumps(reply)
         logging.debug('returning, rpc=%s', str(rpc))
     except Exception as exp:  # pylint: disable=broad-except
         logging.error('Got exception %s', str(exp))
         rpc.rep_status = CMHTTPErrors.get_internal_error_status()
         rpc.rep_status += ','
         rpc.rep_status += str(exp)
Ejemplo n.º 21
0
 def delete_properties(self, rpc):
     logging.error('delete_properties not implemented')
     rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     raise cmerror.CMError('Not implemented')
Ejemplo n.º 22
0
 def reboot_node(self, rpc):
     logging.error('reboot_node not implemented')
     rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     raise cmerror.CMError('Not implemented')
Ejemplo n.º 23
0
 def get_changes_states(self, rpc):
     logging.error('get_changes_states not implemented')
     rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     raise cmerror.CMError('Not implemented')
Ejemplo n.º 24
0
 def set_automatic_activation_state(self, rpc, state):
     logging.error('set_automatic_activation_state not implemented')
     rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     raise cmerror.CMError('Not implemented')
Ejemplo n.º 25
0
    def __call__(self, environ, start_response):
        logging.debug('Handling request started, environ=%s', str(environ))
        # for debug, print environment
        # pprint.pprint(environ)

        # For request and resonse data
        rpc = cmhttprpc.HTTPRPC()
        rpc.rep_status = CMHTTPErrors.get_ok_status()

        # get the interesting fields
        rpc.req_method = environ['REQUEST_METHOD']
        path = environ['PATH_INFO']
        try:
            rpc.req_filter = urlparse.parse_qs(
                urllib.unquote(environ['QUERY_STRING']))
        except KeyError as exp:
            rpc.req_filter = {}
        content_type = environ['CONTENT_TYPE']
        try:
            content_size = environ['CONTENT_LENGTH']
        except KeyError:
            content_size = None

        try:
            # get the action to be done
            action = ''
            actions, _ = self.mapper.routematch(path)
            if actions and isinstance(actions, dict):
                action = actions.get('action', '')
                for key, value in actions.iteritems():
                    if key != 'action':
                        rpc.req_params[key] = value
            else:
                rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
                raise cmerror.CMError('The requested url is not found')

            # get the body if available
            if content_size and int(content_size):
                size = int(content_size)
                if content_type == 'application/json':
                    totalread = 0
                    while totalread < size:
                        data = environ['wsgi.input'].read()
                        totalread += len(data)
                        rpc.req_body += data
                else:
                    rpc.rep_status = CMHTTPErrors.get_unsupported_content_type_status(
                    )
                    raise cmerror.CMError('Only json content is supported')

            # check the action
            try:
                logging.info('Calling %s with rpc=%s', action, str(rpc))
                actionfunc = getattr(self, action)
                actionfunc(rpc)
            except AttributeError as attrerror:
                rpc.reply_status = CMHTTPErrors.get_resource_not_found_status()
                raise cmerror.CMError('Action %s not found, error: %s' %
                                      (action, str(attrerror)))

        except cmerror.CMError as exp:
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        except Exception as exp:  # pylint: disable=broad-except
            rpc.rep_status = CMHTTPErrors.get_internal_error_status()
            rpc.rep_status += ','
            rpc.rep_status += str(exp)
        finally:
            logging.info('Replying with rpc=%s', str(rpc))
            response_headers = [('Content-type', 'application/json')]
            start_response(rpc.rep_status, response_headers)
            yield rpc.rep_body
Ejemplo n.º 26
0
 def list_snapshots(self, rpc):
     logging.error('list_snapshots not implemented')
     rpc.rep_status = CMHTTPErrors.get_resource_not_found_status()
     raise cmerror.CMError('Not implemented')