<commit_before># Copyright 2015 F5 Networks Inc.
#
# 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 f5.bigip import BigIP
from heat.common import exception
from heat.common.i18n import _
from heat.engine import properties
from heat.engine import resource


class F5SysiAppService(resource.Resource):
    '''Manages creation of an iApp resource on the BigIP device.'''

    PROPERTIES = (
        NAME,
        BIGIP_SERVER,
        TEMPLATE_NAME
    ) = (
        'name',
        'bigip_server',
        'template_name'
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('Name of the template.'),
            required=True
        ),
        BIGIP_SERVER: properties.Schema(
            properties.Schema.STRING,
            _('IP address of BigIP device.'),
            required=True
        ),
        TEMPLATE_NAME: properties.Schema(
            properties.Schema.STRING,
            _('Template to use when creating the service.'),
            required=True
        )
    }

    def bigip_connect(self):
        refid = self.properties[self.BIGIP_SERVER]
        bigip_resource = self.stack.resource_by_refid(refid)

        try:
            self.bigip = BigIP(
                bigip_resource.properties['ip'],
                bigip_resource.properties['username'],
                bigip_resource.properties['password']
            )
        except Exception as ex:
            raise Exception('Failed to initialize BigIP object: {}'.format(ex))

    def handle_create(self):
        '''Creates the iApp Service from an iApp template.

        :raises: ResourceFailure # TODO Change to proper exception
        '''

        template_dict = {
            'name': self.properties[self.NAME],
            'template': '/Common/{}'.format(
                self.properties[self.TEMPLATE_NAME]
            )
        }
        try:
            self.bigip.iapp.create_service(
                name=self.properties[self.NAME],
                service=template_dict
            )
        except Exception as ex:
            raise exception.ResourceFailure(ex, None, action='CREATE')

    def handle_delete(self):
        '''Deletes the iApp Service

        :raises: Resource Failure # TODO Change to proper exception
        '''

        try:
            self.bigip.iapp.delete_service(
                name=self.properties[self.NAME]
            )
        except Exception as ex:
            raise exception.ResourceFailure(ex, None, action='DELETE')


def resource_mapping():
    return {'F5::Sys::iAppService': F5SysiAppService}<commit_msg>Fixes: OPENSTACK-128<commit_after># Copyright 2015 F5 Networks Inc.
#
# 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 f5.bigip import BigIP
from heat.common import exception
from heat.common.i18n import _
from heat.engine import properties
from heat.engine import resource


class F5SysiAppService(resource.Resource):
    '''Manages creation of an iApp resource on the BigIP device.'''

    PROPERTIES = (
        NAME,
        BIGIP_SERVER,
        TEMPLATE_NAME
    ) = (
        'name',
        'bigip_server',
        'template_name'
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('Name of the template.'),
            required=True
        ),
        BIGIP_SERVER: properties.Schema(
            properties.Schema.STRING,
            _('IP address of BigIP device.'),
            required=True
        ),
        TEMPLATE_NAME: properties.Schema(
            properties.Schema.STRING,
            _('Template to use when creating the service.'),
            required=True
        )
    }

    def bigip_connect(self):
        refid = self.properties[self.BIGIP_SERVER]
        bigip_resource = self.stack.resource_by_refid(refid)

        try:
            self.bigip = BigIP(
                bigip_resource.properties['ip'],
                bigip_resource.properties['username'],
                bigip_resource.properties['password']
            )
        except Exception as ex:
            raise Exception('Failed to initialize BigIP object: {}'.format(ex))

    def handle_create(self):
        '''Creates the iApp Service from an iApp template.

        :raises: ResourceFailure # TODO Change to proper exception
        '''

        self.bigip_connect()

        template_dict = {
            'name': self.properties[self.NAME],
            'template': '/Common/{}'.format(
                self.properties[self.TEMPLATE_NAME]
            )
        }
        try:
            self.bigip.iapp.create_service(
                name=self.properties[self.NAME],
                service=template_dict
            )
        except Exception as ex:
            raise exception.ResourceFailure(ex, None, action='CREATE')

    def handle_delete(self):
        '''Deletes the iApp Service

        :raises: Resource Failure # TODO Change to proper exception
        '''

        self.bigip_connect()

        try:
            self.bigip.iapp.delete_service(
                name=self.properties[self.NAME]
            )
        except Exception as ex:
            raise exception.ResourceFailure(ex, None, action='DELETE')


def resource_mapping():
    return {'F5::Sys::iAppService': F5SysiAppService}
{'fraction_non_alphanumeric': 0.08852093615666295, 'fraction_numerical': 0.005253940455341506, 'mean_word_length': 2.1457185778668, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7543782', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1628, 'n_words': 480}