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."""
27 from keystoneclient
.v3
import client
29 from osm_mon
.core
.auth
import AuthManager
31 __author__
= "Helena McGough"
33 log
= logging
.getLogger(__name__
)
37 """Common calls for Gnocchi/Aodh plugins."""
40 """Create the common instance."""
41 self
.auth_manager
= AuthManager()
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
,
50 password
=creds
.password
,
51 tenant_name
=creds
.tenant_name
)
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
,
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
)
76 def perform_request(url
, auth_token
,
77 req_type
=None, payload
=None, params
=None):
78 """Perform the POST/PUT/GET/DELETE request."""
80 headers
= {'X-Auth-Token': auth_token
,
81 'Content-type': 'application/json'}
82 # perform request and return its result
84 response
= requests
.put(
85 url
, data
=payload
, headers
=headers
,
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)
94 response
= requests
.post(
95 url
, data
=payload
, headers
=headers
,
98 # Raises exception if there was an error
100 response
.raise_for_status()
101 # pylint: disable=broad-except
103 # Log out the result of the request for debugging purpose
106 response
.status_code
, response
.text
)