09c911dc02f69c6fbf2707a37af286417183e3c0
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 """Common methods for the OpenStack plugins."""
28 from keystoneauth1
import session
29 from keystoneauth1
.identity
import v3
30 from keystoneclient
.v3
import client
32 from osm_mon
.core
.auth
import AuthManager
33 from osm_mon
.core
.settings
import Config
35 __author__
= "Helena McGough"
37 log
= logging
.getLogger(__name__
)
38 cfg
= Config
.instance()
42 """Common calls for Gnocchi/Aodh plugins."""
45 """Create the common instance."""
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
,
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']
63 def get_endpoint(service_type
, vim_uuid
, verify_ssl
=True):
65 Gets the public endpoint for an OpenStack service in the configured region (default: RegionOne).
66 :param service_type: Service type name (eg. metric or alarming)
67 :param vim_uuid: VIM UUID generated by OSM
68 :param verify_ssl: If False, disables SSL validation. Useful when using self signed certs.
69 :return: Endpoint url string.
71 :raises ValueError If it can't find services, or if it can find services but no endpoint for specified region.
73 auth_manager
= AuthManager()
74 creds
= auth_manager
.get_credentials(vim_uuid
)
75 auth
= v3
.Password(auth_url
=creds
.url
,
77 password
=creds
.password
,
78 project_name
=creds
.tenant_name
,
79 project_domain_id
='default',
80 user_domain_id
='default')
81 sess
= session
.Session(auth
=auth
, verify
=verify_ssl
)
82 ks
= client
.Client(session
=sess
, interface
='public')
83 services
= ks
.services
.list(type=service_type
)
85 raise ValueError("No services found for {}. Is the corresponding service enabled?".format(service_type
))
87 endpoints
= ks
.endpoints
.list(service
)
88 endpoint_type
= 'publicURL'
89 region_name
= 'RegionOne'
90 if creds
.config
is not None:
92 config
= json
.loads(creds
.config
)
94 config
= yaml
.safe_load(creds
.config
)
95 if 'endpoint_type' in config
:
96 endpoint_type
= config
['endpoint_type']
97 if 'region_name' in config
:
98 region_name
= config
['region_name']
99 for endpoint
in endpoints
:
100 if endpoint
.interface
in endpoint_type
and endpoint
.region
== region_name
:
102 raise ValueError("No endpoints found for service {} in region {}".format(service_type
, region_name
))
105 def perform_request(url
, auth_token
,
106 req_type
=None, payload
=None, params
=None, verify_ssl
=True):
107 """Perform the POST/PUT/GET/DELETE request."""
109 timeout
= cfg
.REQUEST_TIMEOUT
112 headers
= {'X-Auth-Token': auth_token
,
113 'Content-type': 'application/json'}
114 # perform request and return its result
115 if req_type
== "put":
116 response
= requests
.put(
117 url
, data
=payload
, headers
=headers
,
118 timeout
=timeout
, verify
=verify_ssl
)
119 elif req_type
== "get":
120 response
= requests
.get(
121 url
, params
=params
, headers
=headers
, timeout
=timeout
, verify
=verify_ssl
)
122 elif req_type
== "delete":
123 response
= requests
.delete(
124 url
, headers
=headers
, timeout
=timeout
, verify
=verify_ssl
)
126 response
= requests
.post(
127 url
, data
=payload
, headers
=headers
,
128 timeout
=timeout
, verify
=verify_ssl
)
130 # Raises exception if there was an error
132 response
.raise_for_status()
133 # pylint: disable=broad-except
135 # Log out the result of the request
138 response
.status_code
, response
.text
)