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 keystoneclient
.v3
import client
30 from osm_mon
.core
.auth
import AuthManager
32 __author__
= "Helena McGough"
34 log
= logging
.getLogger(__name__
)
38 """Common calls for Gnocchi/Aodh plugins."""
41 """Create the common instance."""
42 self
.auth_manager
= AuthManager()
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
,
51 password
=creds
.password
,
52 tenant_name
=creds
.tenant_name
)
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
,
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:
68 config
= json
.loads(creds
.config
)
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']
76 return ks
.service_catalog
.url_for(
77 service_type
=service_type
,
78 endpoint_type
=endpoint_type
,
79 region_name
=region_name
)
82 def perform_request(url
, auth_token
,
83 req_type
=None, payload
=None, params
=None):
84 """Perform the POST/PUT/GET/DELETE request."""
86 headers
= {'X-Auth-Token': auth_token
,
87 'Content-type': 'application/json'}
88 # perform request and return its result
90 response
= requests
.put(
91 url
, data
=payload
, headers
=headers
,
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)
100 response
= requests
.post(
101 url
, data
=payload
, headers
=headers
,
104 # Raises exception if there was an error
106 response
.raise_for_status()
107 # pylint: disable=broad-except
109 # Log out the result of the request for debugging purpose
112 response
.status_code
, response
.text
)