Updated logging for both OpenStack plugins
[osm/MON.git] / 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 log = logging.getLogger(__name__)
26
27 from keystoneclient.v3 import client
28
29 from plugins.OpenStack.settings import Config
30
31 import requests
32
33 __author__ = "Helena McGough"
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_token = None
42 self._endpoint = None
43 self._ks = None
44
45 def _authenticate(self):
46 """Authenticate and/or renew the authentication token."""
47 if self._auth_token is not None:
48 return self._auth_token
49
50 try:
51 cfg = Config.instance()
52 self._ks = client.Client(auth_url=cfg.OS_AUTH_URL,
53 username=cfg.OS_USERNAME,
54 password=cfg.OS_PASSWORD,
55 tenant_name=cfg.OS_TENANT_NAME)
56 self._auth_token = self._ks.auth_token
57 except Exception as exc:
58
59 log.warn("Authentication failed: %s", exc)
60
61 self._auth_token = None
62
63 return self._auth_token
64
65 def get_endpoint(self, service_type):
66 """Get the endpoint for Gnocchi/Aodh."""
67 try:
68 return self._ks.service_catalog.url_for(
69 service_type=service_type,
70 endpoint_type='internalURL',
71 region_name='RegionOne')
72 except Exception as exc:
73 log.warning("Failed to retreive endpoint for service due to: %s",
74 exc)
75 return None
76
77 @classmethod
78 def _perform_request(cls, url, auth_token,
79 req_type=None, payload=None, params=None):
80 """Perform the POST/PUT/GET/DELETE request."""
81 # request headers
82 headers = {'X-Auth-Token': auth_token,
83 'Content-type': 'application/json'}
84 # perform request and return its result
85 response = None
86 try:
87 if req_type == "put":
88 response = requests.put(
89 url, data=payload, headers=headers,
90 timeout=1)
91 elif req_type == "post":
92 response = requests.post(
93 url, data=payload, headers=headers,
94 timeout=1)
95 elif req_type == "get":
96 response = requests.get(
97 url, params=params, headers=headers, timeout=1)
98 elif req_type == "delete":
99 response = requests.delete(
100 url, headers=headers, timeout=1)
101 else:
102 log.warn("Invalid request type")
103
104 except Exception as e:
105 log.warn("Exception thrown on request", e)
106 if response is not None:
107 log.warn("Request resulted in %s code and %s response",
108 response.status_code, response.text)
109
110 return response