Fixes loading of vim config in OpenStack plugin
[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 import json
24 import logging
25
26 import requests
27 import yaml
28 from keystoneclient.v3 import client
29
30 from osm_mon.core.auth import AuthManager
31
32 __author__ = "Helena McGough"
33
34 log = logging.getLogger(__name__)
35
36
37 class Common(object):
38 """Common calls for Gnocchi/Aodh plugins."""
39
40 def __init__(self):
41 """Create the common instance."""
42 self.auth_manager = AuthManager()
43
44 @staticmethod
45 def get_auth_token(vim_uuid):
46 """Authenticate and/or renew the authentication token."""
47 auth_manager = AuthManager()
48 creds = auth_manager.get_credentials(vim_uuid)
49 ks = client.Client(auth_url=creds.url,
50 username=creds.user,
51 password=creds.password,
52 tenant_name=creds.tenant_name)
53 return ks.auth_token
54
55 @staticmethod
56 def get_endpoint(service_type, vim_uuid):
57 """Get the endpoint for Gnocchi/Aodh."""
58 auth_manager = AuthManager()
59 creds = auth_manager.get_credentials(vim_uuid)
60 ks = client.Client(auth_url=creds.url,
61 username=creds.user,
62 password=creds.password,
63 tenant_name=creds.tenant_name)
64 endpoint_type = 'publicURL'
65 region_name = 'RegionOne'
66 if creds.config is not None:
67 try:
68 config = json.loads(creds.config)
69 except ValueError:
70 config = yaml.safe_load(creds.config)
71 if 'endpoint_type' in config:
72 endpoint_type = config['endpoint_type']
73 if 'region_name' in config:
74 region_name = config['region_name']
75
76 return ks.service_catalog.url_for(
77 service_type=service_type,
78 endpoint_type=endpoint_type,
79 region_name=region_name)
80
81 @staticmethod
82 def perform_request(url, auth_token,
83 req_type=None, payload=None, params=None):
84 """Perform the POST/PUT/GET/DELETE request."""
85 # request headers
86 headers = {'X-Auth-Token': auth_token,
87 'Content-type': 'application/json'}
88 # perform request and return its result
89 if req_type == "put":
90 response = requests.put(
91 url, data=payload, headers=headers,
92 timeout=10)
93 elif req_type == "get":
94 response = requests.get(
95 url, params=params, headers=headers, timeout=10)
96 elif req_type == "delete":
97 response = requests.delete(
98 url, headers=headers, timeout=10)
99 else:
100 response = requests.post(
101 url, data=payload, headers=headers,
102 timeout=10)
103
104 # Raises exception if there was an error
105 try:
106 response.raise_for_status()
107 # pylint: disable=broad-except
108 except Exception:
109 # Log out the result of the request for debugging purpose
110 log.debug(
111 'Result: %s, %s',
112 response.status_code, response.text)
113 return response