Merge "Get Metric_UUID directly from VIM Change-Id: I9b8c47b2fd7987f8834dc7c90e826d9d...
[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 endpoint_type = 'publicURL'
64 region_name = 'regionOne'
65 if creds.config is not None:
66 if 'endpoint_type' in creds.config:
67 endpoint_type = creds.config['endpoint_type']
68 if 'region_name' in creds.config:
69 region_name = creds.config['region_name']
70 return ks.service_catalog.url_for(
71 service_type=service_type,
72 endpoint_type=endpoint_type,
73 region_name=region_name)
74
75 @staticmethod
76 def perform_request(url, auth_token,
77 req_type=None, payload=None, params=None):
78 """Perform the POST/PUT/GET/DELETE request."""
79 # request headers
80 headers = {'X-Auth-Token': auth_token,
81 'Content-type': 'application/json'}
82 # perform request and return its result
83 if req_type == "put":
84 response = requests.put(
85 url, data=payload, headers=headers,
86 timeout=10)
87 elif req_type == "get":
88 response = requests.get(
89 url, params=params, headers=headers, timeout=10)
90 elif req_type == "delete":
91 response = requests.delete(
92 url, headers=headers, timeout=10)
93 else:
94 response = requests.post(
95 url, data=payload, headers=headers,
96 timeout=10)
97
98 # Raises exception if there was an error
99 try:
100 response.raise_for_status()
101 # pylint: disable=broad-except
102 except Exception:
103 # Log out the result of the request for debugging purpose
104 log.debug(
105 'Result: %s, %s',
106 response.status_code, response.text)
107 return response