[MON] Adds check for 'insecure' vim config param 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 keystoneauth1 import session
29 from keystoneauth1.identity import v3
30 from keystoneclient.v3 import client
31
32 from osm_mon.core.auth import AuthManager
33 from osm_mon.core.settings import Config
34
35 __author__ = "Helena McGough"
36
37 log = logging.getLogger(__name__)
38 cfg = Config.instance()
39
40
41 class Common(object):
42 """Common calls for Gnocchi/Aodh plugins."""
43
44 def __init__(self):
45 """Create the common instance."""
46
47 @staticmethod
48 def get_auth_token(vim_uuid, verify_ssl=True):
49 """Authenticate and/or renew the authentication token."""
50 auth_manager = AuthManager()
51 creds = auth_manager.get_credentials(vim_uuid)
52 sess = session.Session(verify=verify_ssl)
53 ks = client.Client(session=sess)
54 token_dict = ks.get_raw_token_from_identity_service(auth_url=creds.url,
55 username=creds.user,
56 password=creds.password,
57 project_name=creds.tenant_name,
58 project_domain_id='default',
59 user_domain_id='default')
60 return token_dict['auth_token']
61
62 @staticmethod
63 def get_endpoint(service_type, vim_uuid, verify_ssl=True):
64 """Get the endpoint for Gnocchi/Aodh."""
65 auth_manager = AuthManager()
66 creds = auth_manager.get_credentials(vim_uuid)
67 auth = v3.Password(auth_url=creds.url,
68 username=creds.user,
69 password=creds.password,
70 project_name=creds.tenant_name,
71 project_domain_id='default',
72 user_domain_id='default')
73 sess = session.Session(auth=auth, verify=verify_ssl)
74 ks = client.Client(session=sess, interface='public')
75 service = ks.services.list(type=service_type)[0]
76 endpoints = ks.endpoints.list(service)
77 endpoint_type = 'publicURL'
78 region_name = 'RegionOne'
79 if creds.config is not None:
80 try:
81 config = json.loads(creds.config)
82 except ValueError:
83 config = yaml.safe_load(creds.config)
84 if 'endpoint_type' in config:
85 endpoint_type = config['endpoint_type']
86 if 'region_name' in config:
87 region_name = config['region_name']
88 for endpoint in endpoints:
89 if endpoint.interface in endpoint_type and endpoint.region == region_name:
90 return endpoint.url
91
92 @staticmethod
93 def perform_request(url, auth_token,
94 req_type=None, payload=None, params=None, verify_ssl=True):
95 """Perform the POST/PUT/GET/DELETE request."""
96
97 timeout = cfg.REQUEST_TIMEOUT
98
99 # request headers
100 headers = {'X-Auth-Token': auth_token,
101 'Content-type': 'application/json'}
102 # perform request and return its result
103 if req_type == "put":
104 response = requests.put(
105 url, data=payload, headers=headers,
106 timeout=timeout, verify=verify_ssl)
107 elif req_type == "get":
108 response = requests.get(
109 url, params=params, headers=headers, timeout=timeout, verify=verify_ssl)
110 elif req_type == "delete":
111 response = requests.delete(
112 url, headers=headers, timeout=timeout, verify=verify_ssl)
113 else:
114 response = requests.post(
115 url, data=payload, headers=headers,
116 timeout=timeout, verify=verify_ssl)
117
118 # Raises exception if there was an error
119 try:
120 response.raise_for_status()
121 # pylint: disable=broad-except
122 except Exception:
123 # Log out the result of the request
124 log.warning(
125 'Result: %s, %s',
126 response.status_code, response.text)
127 return response