Fixes bugs for integration with policy module
[osm/MON.git] / osm_mon / plugins / OpenStack / common.py
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Common methods for the OpenStack plugins."""
23
24 import logging
25
26 import requests
27 from keystoneclient.v3 import client
28
29 from osm_mon.core.auth import AuthManager
30
31 __author__ = "Helena McGough"
32
33 log = logging.getLogger(__name__)
34
35
36 class Common(object):
37 """Common calls for Gnocchi/Aodh plugins."""
38
39 def __init__(self):
40 """Create the common instance."""
41 self.auth_manager = AuthManager()
42
43 @staticmethod
44 def get_auth_token(vim_uuid):
45 """Authenticate and/or renew the authentication token."""
46 auth_manager = AuthManager()
47 creds = auth_manager.get_credentials(vim_uuid)
48 ks = client.Client(auth_url=creds.url,
49 username=creds.user,
50 password=creds.password,
51 tenant_name=creds.tenant_name)
52 return ks.auth_token
53
54 @staticmethod
55 def get_endpoint(service_type, vim_uuid):
56 """Get the endpoint for Gnocchi/Aodh."""
57 auth_manager = AuthManager()
58 creds = auth_manager.get_credentials(vim_uuid)
59 ks = client.Client(auth_url=creds.url,
60 username=creds.user,
61 password=creds.password,
62 tenant_name=creds.tenant_name)
63 return ks.service_catalog.url_for(
64 service_type=service_type,
65 endpoint_type='publicURL',
66 region_name='RegionOne')
67
68 @staticmethod
69 def perform_request(url, auth_token,
70 req_type=None, payload=None, params=None):
71 """Perform the POST/PUT/GET/DELETE request."""
72 # request headers
73 headers = {'X-Auth-Token': auth_token,
74 'Content-type': 'application/json'}
75 # perform request and return its result
76 if req_type == "put":
77 response = requests.put(
78 url, data=payload, headers=headers,
79 timeout=10)
80 elif req_type == "get":
81 response = requests.get(
82 url, params=params, headers=headers, timeout=10)
83 elif req_type == "delete":
84 response = requests.delete(
85 url, headers=headers, timeout=10)
86 else:
87 response = requests.post(
88 url, data=payload, headers=headers,
89 timeout=10)
90
91 # Raises exception if there was an error
92 try:
93 response.raise_for_status()
94 # pylint: disable=broad-except
95 except Exception:
96 # Log out the result of the request for debugging purpose
97 log.debug(
98 'Result: %s, %s',
99 response.status_code, response.text)
100 return response