Esempio n. 1
0
    def put(self, request):
        """
        Handler which updates app_list data entry in kvstore for the
        current user
        """
        # Check payload is valid
        # update kvstore entry
        app_list = validate_write_request(request)
        params = {KEY: DASHBOARD_APP_LIST, APP_NAMES: app_list}

        kvstore = KvStore(USER_META_COLLECTION_NAME,
                          request[SESSION][AUTHTOKEN],
                          owner=request[SESSION][USER])
        try:
            kvstore.update_item_by_key(DASHBOARD_APP_LIST, params)
            return {
                'payload':
                'Successfully updated kvstore entry with id={}'.format(
                    DASHBOARD_APP_LIST),
                'status':
                http.OK,
            }
        except Exception as e:
            raise Errors.SpacebridgeRestError(
                'Error: failed to update kvstore entry with id={}'.format(
                    DASHBOARD_APP_LIST), 400)
Esempio n. 2
0
def set_state_of_app(app_name, authtoken, system_authtoken, new_state):
    """
    Updates whether the specified app is enabled or not. This function:
        1. Validates the app_type for correctness
        2. If app is being disabled, delete all registered devices
        3. Attempts to retrieve the record of the app from the kvstore
        4. Updates or creates the new kvstore entry depending if one exists already
        5. Returns the state of the app_type

    :param app_name: Name of app in kvstore
    :param authtoken: User's authorization token
    :param system_authtoken: System authorization token
    :param new_state: Boolean signifying whether to enable the app
    :return: Success message
    """

    # Validates the app_type for correctness
    if app_name not in APP_TYPES:
        raise Errors.SpacebridgeRestError(
            'Error: Invalid app_type=%s' % app_name, 400)

    # If app is being disabled, delete all registered devices
    if not new_state:
        delete_all_devices_of_type(app_name, authtoken, system_authtoken)

    # Attempts to retrieve the record of the app from the kvstore
    kvstore = KvStore(constants.APPLICATION_TYPES_COLLECTION_NAME,
                      authtoken,
                      owner=NOBODY)
    r, app_record = kvstore.get_items_by_query({'application_name': app_name})
    app_record = json.loads(app_record)
    new_app = {'application_name': app_name, 'application_enabled': new_state}

    # Updates or creates the new kvstore entry depending if one exists already
    if app_record:
        kvstore.update_item_by_key(app_record[0]['_key'], new_app)
    else:
        kvstore.insert_single_item(new_app)

    result_string = 'Application app_type=%s is now new_state=%s' % (
        app_name, 'enabled' if new_state else 'disabled')

    LOGGER.info(result_string)

    # Returns the state of the app_type
    return {
        'payload': result_string,
        'status': 200,
    }