<commit_before># -*- encoding: utf-8 -*-
# Copyright (c) 2016 b<>com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from oslo_serialization import jsonutils
import uuid

from watcher_tempest_plugin.services.infra_optim import base


class InfraOptimClientJSON(base.BaseInfraOptimClient):
    """Base Tempest REST client for Watcher API v1."""

    URI_PREFIX = 'v1'

    def serialize(self, object_dict):
        """Serialize an Watcher object."""
        return jsonutils.dumps(object_dict)

    def deserialize(self, object_str):
        """Deserialize an Watcher object."""
        return jsonutils.loads(object_str.decode('utf-8'))

    # ### AUDIT TEMPLATES ### #

    @base.handle_errors
    def list_audit_templates(self, **kwargs):
        """List all existing audit templates."""
        return self._list_request('audit_templates', **kwargs)

    @base.handle_errors
    def list_audit_templates_detail(self, **kwargs):
        """Lists details of all existing audit templates."""
        return self._list_request('/audit_templates/detail', **kwargs)

    @base.handle_errors
    def show_audit_template(self, audit_template_uuid):
        """Gets a specific audit template.

        :param audit_template_uuid: Unique identifier of the audit template
        :return: Serialized audit template as a dictionary.
        """
        return self._show_request('audit_templates', audit_template_uuid)

    @base.handle_errors
    def create_audit_template(self, **kwargs):
        """Creates an audit template with the specified parameters.

        :param name: The name of the audit template.
        :param description: The description of the audit template.
        :param goal_uuid: The related Goal UUID associated.
        :param strategy_uuid: The related Strategy UUID associated.
        :param audit_scope: Scope the audit should apply to.
        :return: A tuple with the server response and the created audit
                 template.
        """

        parameters = {k: v for k, v in kwargs.items() if v is not None}
        # This name is unique to avoid the DB unique constraint on names
        unique_name = 'Tempest Audit Template %s' % uuid.uuid4()

        audit_template = {
            'name': parameters.get('name', unique_name),
            'description': parameters.get('description'),
            'goal': parameters.get('goal'),
            'strategy': parameters.get('strategy'),
            'scope': parameters.get('scope', []),
        }

        return self._create_request('audit_templates', audit_template)

    @base.handle_errors
    def delete_audit_template(self, audit_template_uuid):
        """Deletes an audit template having the specified UUID.

        :param audit_template_uuid: The unique identifier of the audit template
        :return: A tuple with the server response and the response body.
        """

        return self._delete_request('audit_templates', audit_template_uuid)

    @base.handle_errors
    def update_audit_template(self, audit_template_uuid, patch):<commit_msg>Use uuidutils instead of uuid.uuid4().<commit_after># -*- encoding: utf-8 -*-
# Copyright (c) 2016 b<>com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from oslo_serialization import jsonutils
from watcher.common import utils
from watcher_tempest_plugin.services.infra_optim import base


class InfraOptimClientJSON(base.BaseInfraOptimClient):
    """Base Tempest REST client for Watcher API v1."""

    URI_PREFIX = 'v1'

    def serialize(self, object_dict):
        """Serialize an Watcher object."""
        return jsonutils.dumps(object_dict)

    def deserialize(self, object_str):
        """Deserialize an Watcher object."""
        return jsonutils.loads(object_str.decode('utf-8'))

    # ### AUDIT TEMPLATES ### #

    @base.handle_errors
    def list_audit_templates(self, **kwargs):
        """List all existing audit templates."""
        return self._list_request('audit_templates', **kwargs)

    @base.handle_errors
    def list_audit_templates_detail(self, **kwargs):
        """Lists details of all existing audit templates."""
        return self._list_request('/audit_templates/detail', **kwargs)

    @base.handle_errors
    def show_audit_template(self, audit_template_uuid):
        """Gets a specific audit template.

        :param audit_template_uuid: Unique identifier of the audit template
        :return: Serialized audit template as a dictionary.
        """
        return self._show_request('audit_templates', audit_template_uuid)

    @base.handle_errors
    def create_audit_template(self, **kwargs):
        """Creates an audit template with the specified parameters.

        :param name: The name of the audit template.
        :param description: The description of the audit template.
        :param goal_uuid: The related Goal UUID associated.
        :param strategy_uuid: The related Strategy UUID associated.
        :param audit_scope: Scope the audit should apply to.
        :return: A tuple with the server response and the created audit
                 template.
        """

        parameters = {k: v for k, v in kwargs.items() if v is not None}
        # This name is unique to avoid the DB unique constraint on names
        unique_name = 'Tempest Audit Template %s' % utils.generate_uuid()

        audit_template = {
            'name': parameters.get('name', unique_name),
            'description': parameters.get('description'),
            'goal': parameters.get('goal'),
            'strategy': parameters.get('strategy'),
            'scope': parameters.get('scope', []),
        }

        return self._create_request('audit_templates', audit_template)

    @base.handle_errors
    def delete_audit_template(self, audit_template_uuid):
        """Deletes an audit template having the specified UUID.

        :param audit_template_uuid: The unique identifier of the audit template
        :return: A tuple with the server response and the response body.
        """

        return self._delete_request('audit_templates', audit_template_uuid)

    @base.handle_errors
    def update_audit_template(self, audit_template_uuid, patch):
{'fraction_non_alphanumeric': 0.0911509381125735, 'fraction_numerical': 0.0036404368524222907, 'mean_word_length': 3.2091926929876253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3434609', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1872, 'n_words': 661}