From 9622da66409485995f1dd388bca406d24a215661 Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Tue, 12 Sep 2017 11:30:55 -0400 Subject: dcaepolicy plugin and dcae.node.type * new plugin and node type file to be used by blueprints that use policy Change-Id: I79dc24f3bf6f8471457544f6bb8562cbcd448d00 Issue-Id: DCAEGEN2-62 Signed-off-by: Alex Shatov --- dcae-policy/LICENSE.txt | 32 ++++++++++++ dcae-policy/MANIFEST.in | 1 + dcae-policy/README.md | 31 +++++++++++ dcae-policy/dcaepolicy-node-type.yaml | 47 +++++++++++++++++ dcae-policy/dcaepolicyplugin/__init__.py | 22 ++++++++ dcae-policy/dcaepolicyplugin/discovery.py | 35 +++++++++++++ dcae-policy/dcaepolicyplugin/tasks.py | 86 +++++++++++++++++++++++++++++++ dcae-policy/requirements.txt | 1 + dcae-policy/setup.py | 38 ++++++++++++++ 9 files changed, 293 insertions(+) create mode 100644 dcae-policy/LICENSE.txt create mode 100644 dcae-policy/MANIFEST.in create mode 100644 dcae-policy/README.md create mode 100644 dcae-policy/dcaepolicy-node-type.yaml create mode 100644 dcae-policy/dcaepolicyplugin/__init__.py create mode 100644 dcae-policy/dcaepolicyplugin/discovery.py create mode 100644 dcae-policy/dcaepolicyplugin/tasks.py create mode 100644 dcae-policy/requirements.txt create mode 100644 dcae-policy/setup.py diff --git a/dcae-policy/LICENSE.txt b/dcae-policy/LICENSE.txt new file mode 100644 index 0000000..cb8008a --- /dev/null +++ b/dcae-policy/LICENSE.txt @@ -0,0 +1,32 @@ +============LICENSE_START======================================================= +org.onap.dcae +================================================================================ +Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +================================================================================ +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. +============LICENSE_END========================================================= + +ECOMP is a trademark and service mark of AT&T Intellectual Property. + + +Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +=================================================================== +Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +you may not use this documentation except in compliance with the License. +You may obtain a copy of the License at + https://creativecommons.org/licenses/by/4.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. diff --git a/dcae-policy/MANIFEST.in b/dcae-policy/MANIFEST.in new file mode 100644 index 0000000..f9bd145 --- /dev/null +++ b/dcae-policy/MANIFEST.in @@ -0,0 +1 @@ +include requirements.txt diff --git a/dcae-policy/README.md b/dcae-policy/README.md new file mode 100644 index 0000000..42fa53b --- /dev/null +++ b/dcae-policy/README.md @@ -0,0 +1,31 @@ +# dcae-policy plugin and node-type +- python-package dcaepolicyplugin to be used in cloudify plugins to retrieve the policy from policy-handler + +--- + +## dcaepolicy node type [dcaepolicy-node-type.yaml](./dcaepolicy-node-type.yaml) +- node type for dcae.nodes.policy + +--- + +## Usage + +import the dcaepolicy-node-type.yaml into your blueprint to use the dcae.nodes.type node + +```yaml +imports: + - https://YOUR_NEXUS_RAW_SERVER/type_files/dcaepolicy/1.0.0/node-type.yaml +``` + +provide the value for policy_id property + +```yaml +node_templates: +... + host_capacity_policy: + type: dcae.nodes.policy + properties: + policy_id: { get_input: host_capacity_policy_id } +``` + +Then the dcaepolicyplugin will bring the latest policy to the dcae.nodes.policy node during the install workflow of cloudify. diff --git a/dcae-policy/dcaepolicy-node-type.yaml b/dcae-policy/dcaepolicy-node-type.yaml new file mode 100644 index 0000000..515d6b9 --- /dev/null +++ b/dcae-policy/dcaepolicy-node-type.yaml @@ -0,0 +1,47 @@ +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +tosca_definitions_version: cloudify_dsl_1_3 + +imports: + - http://www.getcloudify.org/spec/cloudify/3.4/types.yaml + +plugins: + dcaepolicy: + executor: 'central_deployment_agent' + package_name: dcaepolicyplugin + package_version: 1.0.0 + +node_types: + dcae.nodes.policy: + derived_from: cloudify.nodes.Root + properties: + policy_id: + description: PK to policy in policy-engine + type: string + default: DCAE.Config_unknown-policy + policy_required: + description: whether to throw an exception when failed to get the policy + type: boolean + default: true + interfaces: + cloudify.interfaces.lifecycle: + create: + implementation: dcaepolicy.dcaepolicyplugin.policy_get diff --git a/dcae-policy/dcaepolicyplugin/__init__.py b/dcae-policy/dcaepolicyplugin/__init__.py new file mode 100644 index 0000000..d2946a6 --- /dev/null +++ b/dcae-policy/dcaepolicyplugin/__init__.py @@ -0,0 +1,22 @@ +""":policyplugin: gets the policy from policy-handler and stores it into runtime properties""" +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +from .tasks import policy_get diff --git a/dcae-policy/dcaepolicyplugin/discovery.py b/dcae-policy/dcaepolicyplugin/discovery.py new file mode 100644 index 0000000..8cdbde1 --- /dev/null +++ b/dcae-policy/dcaepolicyplugin/discovery.py @@ -0,0 +1,35 @@ +"""client to talk to consul on standard port 8500""" + +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +import requests + +# it is safe to assume that consul agent is at localhost:8500 along with cloudify manager +CONSUL_SERVICE_URL = "http://localhost:8500/v1/catalog/service/{0}" + +def discover_service_url(service_name): + """find the service record in consul""" + response = requests.get(CONSUL_SERVICE_URL.format(service_name)) + response.raise_for_status() + resp_json = response.json() + if resp_json: + service = resp_json[0] + return "http://{0}:{1}".format(service["ServiceAddress"], service["ServicePort"]) diff --git a/dcae-policy/dcaepolicyplugin/tasks.py b/dcae-policy/dcaepolicyplugin/tasks.py new file mode 100644 index 0000000..7b5ea1f --- /dev/null +++ b/dcae-policy/dcaepolicyplugin/tasks.py @@ -0,0 +1,86 @@ +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +# Lifecycle interface calls for DockerContainer + +import json +import uuid + +import requests + +from cloudify import ctx +from cloudify.decorators import operation +from cloudify.context import NODE_INSTANCE +from cloudify.exceptions import NonRecoverableError + +from .discovery import discover_service_url + +SERVICE_NAME_POLICY_HANDLER = "policy_handler" +X_ECOMP_REQUESTID = 'X-ECOMP-RequestID' +POLICY_ID = 'policy_id' +POLICY_REQUIRED = 'policy_required' +POLICY_BODY = 'policy_body' +DCAE_POLICY_TYPE = 'dcae.nodes.policy' + +POLICY_HANDLER_URL = discover_service_url(SERVICE_NAME_POLICY_HANDLER) + +def _get_latest_policy(policy_id): + """retrieve the latest policy for policy_id from policy-handler""" + ph_path = "{0}/policy_latest/{1}".format(POLICY_HANDLER_URL, policy_id) + headers = {X_ECOMP_REQUESTID: str(uuid.uuid4())} + + ctx.logger.info("getting latest policy from {0} headers={1}".format( \ + ph_path, json.dumps(headers))) + res = requests.get(ph_path, headers=headers) + res.raise_for_status() + + if res.status_code == requests.codes.ok: + return res.json() + return {} + +######################################################### +@operation +def policy_get(**kwargs): + """retrieve the latest policy_body for policy_id property and save it in runtime_properties""" + if ctx.type != NODE_INSTANCE or DCAE_POLICY_TYPE not in ctx.node.type_hierarchy: + error = "can only invoke policy_get on node of type {0}".format(DCAE_POLICY_TYPE) + ctx.logger.error(error) + raise NonRecoverableError(error) + + if POLICY_ID not in ctx.node.properties: + error = "no {0} found in ctx.node.properties".format(POLICY_ID) + ctx.logger.error(error) + raise NonRecoverableError(error) + + try: + policy_id = ctx.node.properties[POLICY_ID] + policy = _get_latest_policy(policy_id) + if not policy: + raise NonRecoverableError("policy not found for policy_id {0}".format(policy_id)) + + ctx.logger.info("found policy {0}".format(json.dumps(policy))) + if POLICY_BODY in policy: + ctx.instance.runtime_properties[POLICY_BODY] = policy[POLICY_BODY] + + except Exception as ex: + error = "failed to get policy: {0}".format(str(ex)) + ctx.logger.error(error) + if ctx.node.properties.get(POLICY_REQUIRED, True): + raise NonRecoverableError(error) diff --git a/dcae-policy/requirements.txt b/dcae-policy/requirements.txt new file mode 100644 index 0000000..220b585 --- /dev/null +++ b/dcae-policy/requirements.txt @@ -0,0 +1 @@ +requests>=2.11.0,<3.0.0 diff --git a/dcae-policy/setup.py b/dcae-policy/setup.py new file mode 100644 index 0000000..528e744 --- /dev/null +++ b/dcae-policy/setup.py @@ -0,0 +1,38 @@ +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +from setuptools import setup + +setup( + name='dcaepolicyplugin', + description='Cloudify plugin for dcae.nodes.policy node to retrieve the policy config', + version="1.0.0", + author='Alex Shatov', + packages=['dcaepolicyplugin'], + install_requires=[ + "requests>=2.11.0,<3.0.0" + ], + keywords='policy dcae controller cloudify plugin', + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'Programming Language :: Python :: 2.7' + ] +) -- cgit 1.2.3-korg