Ejemplo n.º 1
0
    def _check_validate_options(self, data):
        """
        Check option in foreign data wrapper Data and This will call
        validated options function to set flag for option valid or not in sql.
        :param data: Data.
        """
        is_valid_added_options = is_valid_changed_options = False

        if 'fdwoptions' in data and data['fdwoptions'] is not None and\
                'added' in data['fdwoptions']:
            is_valid_added_options, data['fdwoptions']['added'] = \
                validate_options(
                    data['fdwoptions']['added'],
                    'fdwoption',
                    'fdwvalue')

        if 'fdwoptions' in data and data['fdwoptions'] is not None and\
                'changed' in data['fdwoptions']:
            is_valid_changed_options, data['fdwoptions']['changed'] = \
                validate_options(
                    data['fdwoptions']['changed'],
                    'fdwoption',
                    'fdwvalue')

        return is_valid_added_options, is_valid_changed_options
Ejemplo n.º 2
0
    def _validate_fsvr_options(data, old_data):
        """
        Validate options for foreign server.
        :param data: Data.
        :param old_data: old data for get old args.
        :return: return is valid ad and change option flag.
        """
        required_args = [
            'name'
        ]
        for arg in required_args:
            if arg not in data:
                data[arg] = old_data[arg]

        is_valid_added_options = is_valid_changed_options = False
        if 'fsrvoptions' in data and data['fsrvoptions'] is not None and\
                'added' in data['fsrvoptions']:
            is_valid_added_options, data['fsrvoptions']['added'] = \
                validate_options(
                    data['fsrvoptions']['added'],
                    'fsrvoption',
                    'fsrvvalue')

        if 'fsrvoptions' in data and data['fsrvoptions'] is not None and\
                'changed' in data['fsrvoptions']:
            is_valid_changed_options, data['fsrvoptions']['changed'] = \
                validate_options(
                    data['fsrvoptions']['changed'],
                    'fsrvoption',
                    'fsrvvalue')

        return is_valid_added_options, is_valid_changed_options
Ejemplo n.º 3
0
    def create(self, gid, sid, did):
        """
        This function will create the foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
        """
        required_args = ['name']

        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({}).").format(
                            arg))

        try:
            if 'fdwacl' in data:
                data['fdwacl'] = parse_priv_to_db(data['fdwacl'], ['U'])

            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue')

            sql = render_template("/".join(
                [self.template_path, self._CREATE_SQL]),
                                  data=data,
                                  conn=self.conn,
                                  is_valid_options=is_valid_options)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  fname=data['name'],
                                  conn=self.conn)

            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(node=self.blueprint.generate_browser_node(
                    row['fdwoid'],
                    did,
                    row['name'],
                    icon='icon-foreign_data_wrapper'))

        except Exception as e:
            return internal_server_error(errormsg=str(e))
Ejemplo n.º 4
0
    def get_update_sql(self, data, old_data, required_args, fdw_data):
        """
        Get update SQL for user mapping.
        :param data:
        :param old_data:
        :param required_args:
        :param fdw_data:
        :return: SQL and display name for user mapping.
        """
        for arg in required_args:
            if arg not in data:
                data[arg] = old_data[arg]

        # Allow user to set the blank value in fdwvalue
        # field in option model
        is_valid_added_options = is_valid_changed_options = False
        if 'umoptions' in data and data['umoptions'] is not None and \
                'added' in data['umoptions']:
            is_valid_added_options, data['umoptions']['added'] = \
                validate_options(
                    data['umoptions']['added'],
                    'umoption',
                    'umvalue')
        if 'umoptions' in data and data['umoptions'] is not None and \
                'changed' in data['umoptions']:
            is_valid_changed_options, data['umoptions']['changed'] = \
                validate_options(
                    data['umoptions']['changed'],
                    'umoption',
                    'umvalue')

        sql = render_template(
            "/".join([self.template_path, self._UPDATE_SQL]),
            data=data,
            o_data=old_data,
            is_valid_added_options=is_valid_added_options,
            is_valid_changed_options=is_valid_changed_options,
            fdwdata=fdw_data,
            conn=self.conn
        )
        return sql, data['name'] if 'name' in data else old_data['name']
Ejemplo n.º 5
0
    def _get_create_sql(self, data):
        """
        Get sql for create foreign data wrapper.
        :param data: Data.
        :return: Create sql statement for foreign data wrapper.
        """
        for key in ['fdwacl']:
            if key in data and data[key] is not None:
                data[key] = parse_priv_to_db(data[key], ['U'])

        # Allow user to set the blank value in
        # fdwvalue field in option model
        is_valid_options = False
        if 'fdwoptions' in data:
            is_valid_options, data['fdwoptions'] = validate_options(
                data['fdwoptions'], 'fdwoption', 'fdwvalue')

        sql = render_template("/".join([self.template_path, self._CREATE_SQL]),
                              data=data,
                              conn=self.conn,
                              is_valid_options=is_valid_options)
        return sql
Ejemplo n.º 6
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected
                  foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = ['name']

        if fid is not None:
            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  fid=fid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the foreign"
                            " data wrapper information."))

            if res['rows'][0]['fdwoptions'] is not None:
                res['rows'][0]['fdwoptions'] = tokenize_options(
                    res['rows'][0]['fdwoptions'], 'fdwoption', 'fdwvalue')

            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'], ['U'])
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'], ['U'])
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'], ['U'])

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                is_valid_added_options, data['fdwoptions']['added'] = \
                    validate_options(
                        data['fdwoptions']['added'],
                        'fdwoption',
                        'fdwvalue')
            if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                is_valid_changed_options, data['fdwoptions']['changed'] = \
                    validate_options(
                        data['fdwoptions']['changed'],
                        'fdwoption',
                        'fdwvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])

            # Allow user to set the blank value in
            # fdwvalue field in option model
            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue')

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn,
                                  is_valid_options=is_valid_options)

        return sql, data['name']
Ejemplo n.º 7
0
    def get_sql(self, gid, sid, data, did, fid, fsid, umid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected user mapping node
            fid: foreign data wrapper ID
            fsid: foreign server ID
            umid: User mapping ID
        """

        required_args = ['name']

        if umid is not None:
            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  umid=umid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the user mapping information."))

            if res['rows'][0]['umoptions'] is not None:
                res['rows'][0]['umoptions'] = tokenize_options(
                    res['rows'][0]['umoptions'], 'umoption', 'umvalue')
            old_data = res['rows'][0]

            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  fserid=fsid,
                                  conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]

            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'umoptions' in data and 'added' in data['umoptions']:
                is_valid_added_options, data['umoptions']['added'] =\
                    validate_options(
                        data['umoptions']['added'],
                        'umoption',
                        'umvalue')
            if 'umoptions' in data and 'changed' in data['umoptions']:
                is_valid_changed_options, data['umoptions']['changed'] =\
                    validate_options(
                        data['umoptions']['changed'],
                        'umoption',
                        'umvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                fdwdata=fdw_data,
                conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  fserid=fsid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            fdw_data = res['rows'][0]

            is_valid_options = False
            if 'umoptions' in data:
                is_valid_options, data['umoptions'] = validate_options(
                    data['umoptions'], 'umoption', 'umvalue')

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
Ejemplo n.º 8
0
    def create(self, gid, sid, did, fid, fsid):
        """
        This function will create the user mapping node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: Foreign data wrapper ID
            fsid: Foreign server ID
        """

        required_args = ['name']

        data = request.form if request.form else json.loads(request.data,
                                                            encoding='utf-8')
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg))

        try:
            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  fserid=fsid,
                                  conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)

            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]

            is_valid_options = False
            if 'umoptions' in data:
                is_valid_options, data['umoptions'] = validate_options(
                    data['umoptions'], 'umoption', 'umvalue')

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join(
                [self.template_path, 'properties.sql']),
                                  fsid=fsid,
                                  data=data,
                                  conn=self.conn)
            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(node=self.blueprint.generate_browser_node(
                    row['um_oid'], fsid, row['name'],
                    icon='icon-user_mapping'))

        except Exception as e:
            return internal_server_error(errormsg=str(e))
Ejemplo n.º 9
0
    def get_sql(self, gid, sid, data, did, fid, fsid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected foreign server node
            fid: foreign data wrapper ID
            fsid: foreign server ID
        """
        if fsid is not None:
            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fsid=fsid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            elif len(res['rows']) == 0:
                return gone(self.not_found_error_msg())
            elif res['rows'][0]['fsrvoptions'] is not None:
                res['rows'][0]['fsrvoptions'] = tokenize_options(
                    res['rows'][0]['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            ForeignServerView._get_fsvr_acl(data)

            old_data = res['rows'][0]
            is_valid_added_options, \
                is_valid_changed_options = \
                ForeignServerView._validate_fsvr_options(data, old_data)

            sql = render_template(
                "/".join([self.template_path, self._UPDATE_SQL]),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn
            )
            return sql.strip('\n'), \
                data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fdwid=fid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            fdw_data = res['rows'][0]

            ForeignServerView._parse_priv(data)

            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            self._CREATE_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
Ejemplo n.º 10
0
    def create(self, gid, sid, did, fid):
        """
        This function will create the foreign server node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            fid: foreign data wrapper ID
        """

        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter ({})."
                    ).format(arg)
                )
        try:
            if 'fsrvacl' in data:
                data['fsrvacl'] = parse_priv_to_db(data['fsrvacl'], ['U'])

            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fdwid=fid, conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)
            if len(res1['rows']) == 0:
                return gone(
                    gettext("The specified foreign server could not be found.")
                )
            fdw_data = res1['rows'][0]

            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            self._CREATE_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  conn=self.conn)
            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            return jsonify(
                node=self.blueprint.generate_browser_node(
                    r_set['rows'][0]['oid'],
                    fid,
                    r_set['rows'][0]['name'],
                    icon="icon-foreign_server"
                )
            )
        except Exception as e:
            return internal_server_error(errormsg=str(e))
Ejemplo n.º 11
0
    def get_sql(self, gid, sid, data, did, fid, fsid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected foreign server node
            fid: foreign data wrapper ID
            fsid: foreign server ID
        """

        required_args = [
            'name'
        ]

        if fsid is not None:
            sql = render_template("/".join([self.template_path, 'properties.sql']),
                                  fsid=fsid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if res['rows'][0]['fsrvoptions'] is not None:
                res['rows'][0]['fsrvoptions'] = tokenize_options(
                    res['rows'][0]['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            for key in ['fsrvacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(data[key]['added'], ['U'])
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(data[key]['changed'], ['U'])
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(data[key]['deleted'], ['U'])

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            is_valid_added_options = is_valid_changed_options = False
            if 'fsrvoptions' in data and 'added' in data['fsrvoptions']:
                is_valid_added_options, data['fsrvoptions']['added'] = validate_options(
                    data['fsrvoptions']['added'], 'fsrvoption', 'fsrvvalue'
                )
            if 'fsrvoptions' in data and 'changed' in data['fsrvoptions']:
                is_valid_changed_options, data['fsrvoptions']['changed'] = validate_options(
                    data['fsrvoptions']['changed'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path, 'update.sql']),
                                  data=data, o_data=old_data,
                                  is_valid_added_options=is_valid_added_options,
                                  is_valid_changed_options=is_valid_changed_options,
                                  conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join([self.template_path, 'properties.sql']),
                                  fdwid=fid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            fdw_data = res['rows'][0]

            for key in ['fsrvacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])


            is_valid_options = False
            if 'fsrvoptions' in data:
                is_valid_options, data['fsrvoptions'] = validate_options(
                    data['fsrvoptions'], 'fsrvoption', 'fsrvvalue'
                )

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
Ejemplo n.º 12
0
    def get_sql(self, **kwargs):
        """
        This function will generate sql from model data.

        Args:
            kwargs: Server Group ID
        """
        fsid = kwargs.get('fsid')
        data = kwargs.get('data')
        umid = kwargs.get('umid', None)

        required_args = [
            'name'
        ]

        if umid is not None:
            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  umid=umid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(self.not_found_error_msg())

            if res['rows'][0]['umoptions'] is not None:
                res['rows'][0]['umoptions'] = tokenize_options(
                    res['rows'][0]['umoptions'],
                    'umoption', 'umvalue'
                )
            old_data = res['rows'][0]

            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fserid=fsid, conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]
            # Get SQL for update.
            sql, name = self.get_update_sql(data, old_data, required_args,
                                            fdw_data)
            return sql, name
        else:
            sql = render_template("/".join([self.template_path,
                                            self._PROPERTIES_SQL]),
                                  fserid=fsid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            fdw_data = res['rows'][0]

            is_valid_options = False
            if 'umoptions' in data:
                is_valid_options, data['umoptions'] = validate_options(
                    data['umoptions'], 'umoption', 'umvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            self._CREATE_SQL]),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
Ejemplo n.º 13
0
    def get_sql(self, **kwargs):
        """
        This function will generate sql from model data.

        Args:
            kwargs: Server Group ID
        """
        fsid = kwargs.get('fsid')
        data = kwargs.get('data')
        umid = kwargs.get('umid', None)

        required_args = ['name']

        if umid is not None:
            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  umid=umid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the user mapping information."))

            if res['rows'][0]['umoptions'] is not None:
                res['rows'][0]['umoptions'] = tokenize_options(
                    res['rows'][0]['umoptions'], 'umoption', 'umvalue')
            old_data = res['rows'][0]

            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  fserid=fsid,
                                  conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]

            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'umoptions' in data and 'added' in data['umoptions']:
                is_valid_added_options, data['umoptions']['added'] =\
                    validate_options(
                        data['umoptions']['added'],
                        'umoption',
                        'umvalue')
            if 'umoptions' in data and 'changed' in data['umoptions']:
                is_valid_changed_options, data['umoptions']['changed'] =\
                    validate_options(
                        data['umoptions']['changed'],
                        'umoption',
                        'umvalue')

            sql = render_template(
                "/".join([self.template_path, self._UPDATE_SQL]),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                fdwdata=fdw_data,
                conn=self.conn)
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join(
                [self.template_path, self._PROPERTIES_SQL]),
                                  fserid=fsid,
                                  conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            fdw_data = res['rows'][0]

            is_valid_options = False
            if 'umoptions' in data:
                is_valid_options, data['umoptions'] = validate_options(
                    data['umoptions'], 'umoption', 'umvalue')

            sql = render_template("/".join(
                [self.template_path, self._CREATE_SQL]),
                                  data=data,
                                  fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']
Ejemplo n.º 14
0
    def get_sql(self, gid, sid, data, did, fid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected
                  foreign data wrapper node
            fid: foreign data wrapper ID
        """
        required_args = [
            'name'
        ]

        if fid is not None:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fid=fid,
                                  conn=self.conn
                                  )
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the foreign"
                            " data wrapper information.")
                )

            if res['rows'][0]['fdwoptions'] is not None:
                res['rows'][0]['fdwoptions'] = tokenize_options(
                    res['rows'][0]['fdwoptions'],
                    'fdwoption', 'fdwvalue'
                )

            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    if 'added' in data[key]:
                        data[key]['added'] = parse_priv_to_db(
                            data[key]['added'],
                            ['U']
                        )
                    if 'changed' in data[key]:
                        data[key]['changed'] = parse_priv_to_db(
                            data[key]['changed'],
                            ['U']
                        )
                    if 'deleted' in data[key]:
                        data[key]['deleted'] = parse_priv_to_db(
                            data[key]['deleted'],
                            ['U']
                        )

            old_data = res['rows'][0]
            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'fdwoptions' in data and 'added' in data['fdwoptions']:
                is_valid_added_options, data['fdwoptions']['added'] = \
                    validate_options(
                        data['fdwoptions']['added'],
                        'fdwoption',
                        'fdwvalue')
            if 'fdwoptions' in data and 'changed' in data['fdwoptions']:
                is_valid_changed_options, data['fdwoptions']['changed'] = \
                    validate_options(
                        data['fdwoptions']['changed'],
                        'fdwoption',
                        'fdwvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                conn=self.conn
            )
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            for key in ['fdwacl']:
                if key in data and data[key] is not None:
                    data[key] = parse_priv_to_db(data[key], ['U'])

            # Allow user to set the blank value in
            # fdwvalue field in option model
            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue'
                )

            sql = render_template("/".join([self.template_path,
                                            'create.sql']),
                                  data=data, conn=self.conn,
                                  is_valid_options=is_valid_options
                                  )

        return sql, data['name']
Ejemplo n.º 15
0
    def create(self, gid, sid, did):
        """
        This function will create the foreign data wrapper node.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
        """
        required_args = [
            'name'
        ]

        data = request.form if request.form else json.loads(
            request.data, encoding='utf-8'
        )
        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=410,
                    success=0,
                    errormsg=gettext(
                        "Could not find the required parameter (%s)." % arg
                    )
                )

        try:
            if 'fdwacl' in data:
                data['fdwacl'] = parse_priv_to_db(data['fdwacl'], ['U'])

            is_valid_options = False
            if 'fdwoptions' in data:
                is_valid_options, data['fdwoptions'] = validate_options(
                    data['fdwoptions'], 'fdwoption', 'fdwvalue'
                )

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn,
                                  is_valid_options=is_valid_options
                                  )
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)

            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fname=data['name'],
                                  conn=self.conn
                                  )

            status, r_set = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=r_set)

            for row in r_set['rows']:
                return jsonify(
                    node=self.blueprint.generate_browser_node(
                        row['fdwoid'],
                        did,
                        row['name'],
                        icon='icon-foreign_data_wrapper'
                    )
                )

        except Exception as e:
            return internal_server_error(errormsg=str(e))
Ejemplo n.º 16
0
    def get_sql(self, gid, sid, data, did, fid, fsid, umid=None):
        """
        This function will generate sql from model data.

        Args:
            gid: Server Group ID
            sid: Server ID
            did: Database ID
            data: Contains the data of the selected user mapping node
            fid: foreign data wrapper ID
            fsid: foreign server ID
            umid: User mapping ID
        """

        required_args = [
            'name'
        ]

        if umid is not None:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  umid=umid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            if len(res['rows']) == 0:
                return gone(
                    gettext("Could not find the user mapping information.")
                )

            if res['rows'][0]['umoptions'] is not None:
                res['rows'][0]['umoptions'] = tokenize_options(
                    res['rows'][0]['umoptions'],
                    'umoption', 'umvalue'
                )
            old_data = res['rows'][0]

            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fserid=fsid, conn=self.conn)
            status, res1 = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res1)

            fdw_data = res1['rows'][0]

            for arg in required_args:
                if arg not in data:
                    data[arg] = old_data[arg]

            # Allow user to set the blank value in fdwvalue
            # field in option model
            is_valid_added_options = is_valid_changed_options = False
            if 'umoptions' in data and 'added' in data['umoptions']:
                is_valid_added_options, data['umoptions']['added'] =\
                    validate_options(
                        data['umoptions']['added'],
                        'umoption',
                        'umvalue')
            if 'umoptions' in data and 'changed' in data['umoptions']:
                is_valid_changed_options, data['umoptions']['changed'] =\
                    validate_options(
                        data['umoptions']['changed'],
                        'umoption',
                        'umvalue')

            sql = render_template(
                "/".join([self.template_path, 'update.sql']),
                data=data,
                o_data=old_data,
                is_valid_added_options=is_valid_added_options,
                is_valid_changed_options=is_valid_changed_options,
                fdwdata=fdw_data,
                conn=self.conn
            )
            return sql, data['name'] if 'name' in data else old_data['name']
        else:
            sql = render_template("/".join([self.template_path,
                                            'properties.sql']),
                                  fserid=fsid, conn=self.conn)
            status, res = self.conn.execute_dict(sql)
            if not status:
                return internal_server_error(errormsg=res)
            fdw_data = res['rows'][0]

            is_valid_options = False
            if 'umoptions' in data:
                is_valid_options, data['umoptions'] = validate_options(
                    data['umoptions'], 'umoption', 'umvalue'
                )

            sql = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data, fdwdata=fdw_data,
                                  is_valid_options=is_valid_options,
                                  conn=self.conn)
            sql += "\n"
        return sql, data['name']