def remove_from_device(self):
        base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
            self.client.provider['server'],
            self.client.provider['server_port'])
        uri = build_service_uri(base_uri, self.want.partition, self.want.name)

        # Metadata needs to be zero'd before the service is removed because
        # otherwise, the API will error out saying that "configuration items"
        # currently exist.
        #
        # In other words, the REST API is not able to delete a service while
        # there is existing metadata
        payload = dict(metadata=[])
        resp = self.client.api.patch(uri, json=payload)
        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if 'code' in response and response['code'] == 400:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)

        resp = self.client.api.delete(uri)

        if resp.status == 200:
            return True
        raise F5ModuleError(resp.content)
    def exists(self):
        base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
            self.client.provider['server'],
            self.client.provider['server_port'])
        uri = build_service_uri(base_uri, self.want.partition, self.want.name)
        resp = self.client.api.get(uri)
        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if resp.status == 404 or 'code' in response and response['code'] == 404:
            return False
        if resp.status in [
                200, 201
        ] or 'code' in response and response['code'] in [200, 201]:
            return True

        errors = [401, 403, 409, 500, 501, 502, 503, 504]

        if resp.status in errors or 'code' in response and response[
                'code'] in errors:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)
    def update_on_device(self):
        params = self.changes.api_params()
        if params:
            params['execute-action'] = 'definition'
            base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
                self.client.provider['server'],
                self.client.provider['server_port'])
            uri = build_service_uri(base_uri, self.want.partition,
                                    self.want.name)
            resp = self.client.api.patch(uri, json=params)
            try:
                response = resp.json()
            except ValueError as ex:
                raise F5ModuleError(str(ex))

            if 'code' in response and response['code'] == 400:
                if 'message' in response:
                    raise F5ModuleError(response['message'])
                else:
                    raise F5ModuleError(resp.content)

        if self.changes.metadata:
            params = dict(metadata=self.changes.metadata)
            params.update({'execute-action': 'definition'})
            base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
                self.client.provider['server'],
                self.client.provider['server_port'])
            uri = build_service_uri(base_uri, self.want.partition,
                                    self.want.name)
            resp = self.client.api.patch(uri, json=params)

            try:
                response = resp.json()
            except ValueError as ex:
                raise F5ModuleError(str(ex))

            if 'code' in response and response['code'] == 400:
                if 'message' in response:
                    raise F5ModuleError(response['message'])
                else:
                    raise F5ModuleError(resp.content)
 def exists(self):
     base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
         self.client.provider['server'],
         self.client.provider['server_port'])
     uri = build_service_uri(base_uri, self.want.partition, self.want.name)
     resp = self.client.api.get(uri)
     try:
         response = resp.json()
     except ValueError:
         return False
     if resp.status == 404 or 'code' in response and response['code'] == 404:
         return False
     return True
Example #5
0
    def read_current_from_device(self):
        base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
            self.client.provider['server'],
            self.client.provider['server_port'])
        uri = build_service_uri(base_uri, self.want.partition, self.want.name)
        resp = self.client.api.get(uri)

        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if resp.status in [
                200, 201
        ] or 'code' in response and response['code'] in [200, 201]:
            return ApiParameters(params=response)
        raise F5ModuleError(resp.content)
Example #6
0
    def create_on_device(self):
        params = self.changes.api_params()
        params['name'] = self.want.name
        params['partition'] = self.want.partition
        uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
            self.client.provider['server'],
            self.client.provider['server_port']
        )
        resp = self.client.api.post(uri, json=params)

        try:
            response = resp.json()
        except ValueError as ex:
            raise F5ModuleError(str(ex))

        if 'code' in response and response['code'] in [400, 403]:
            if 'message' in response:
                raise F5ModuleError(response['message'])
            else:
                raise F5ModuleError(resp.content)

        if self.changes.metadata:
            payload = dict(metadata=self.changes.metadata)
            base_uri = "https://{0}:{1}/mgmt/tm/sys/application/service/".format(
                self.client.provider['server'],
                self.client.provider['server_port']
            )
            uri = build_service_uri(base_uri, self.want.partition, self.want.name)
            resp = self.client.api.patch(uri, json=payload)
            try:
                response = resp.json()
            except ValueError as ex:
                raise F5ModuleError(str(ex))

            if 'code' in response and response['code'] == 400:
                if 'message' in response:
                    raise F5ModuleError(response['message'])
                else:
                    raise F5ModuleError(resp.content)