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