c892a302e73575ba82d27b81dd9bf1c7e191f304
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."""
25 log
= logging
.getLogger(__name__
)
27 from keystoneclient
.v3
import client
29 from plugins
.OpenStack
.settings
import Config
33 __author__
= "Helena McGough"
37 """Common calls for Gnocchi/Aodh plugins."""
40 """Create the common instance."""
41 self
._auth
_token
= None
45 def _authenticate(self
):
46 """Authenticate and/or renew the authentication token."""
47 if self
._auth
_token
is not None:
48 return self
._auth
_token
51 cfg
= Config
.instance()
52 self
._ks
= client
.Client(auth_url
=cfg
.OS_AUTH_URL
,
53 username
=cfg
.OS_USERNAME
,
54 password
=cfg
.OS_PASSWORD
,
55 tenant_name
=cfg
.OS_TENANT_NAME
)
56 self
._auth
_token
= self
._ks
.auth_token
57 except Exception as exc
:
59 log
.warn("Authentication failed: %s", exc
)
61 self
._auth
_token
= None
63 return self
._auth
_token
65 def get_endpoint(self
, service_type
):
66 """Get the endpoint for Gnocchi/Aodh."""
68 return self
._ks
.service_catalog
.url_for(
69 service_type
=service_type
,
70 endpoint_type
='internalURL',
71 region_name
='RegionOne')
72 except Exception as exc
:
73 log
.warning("Failed to retreive endpoint for service due to: %s",
78 def _perform_request(cls
, url
, auth_token
,
79 req_type
=None, payload
=None, params
=None):
80 """Perform the POST/PUT/GET/DELETE request."""
82 headers
= {'X-Auth-Token': auth_token
,
83 'Content-type': 'application/json'}
84 # perform request and return its result
87 response
= requests
.put(
88 url
, data
=payload
, headers
=headers
,
90 elif req_type
== "get":
91 response
= requests
.get(
92 url
, params
=params
, headers
=headers
, timeout
=1)
93 elif req_type
== "delete":
94 response
= requests
.delete(
95 url
, headers
=headers
, timeout
=1)
97 response
= requests
.post(
98 url
, data
=payload
, headers
=headers
,
101 except Exception as e
:
102 log
.warn("Exception thrown on request", e
)
103 if response
is not None:
104 log
.warn("Request resulted in %s code and %s response",
105 response
.status_code
, response
.text
)