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."""
26 from keystoneclient
.v3
import client
28 from plugins
.OpenStack
.settings
import Config
32 __author__
= "Helena McGough"
36 """Common calls for Gnocchi/Aodh plugins."""
39 """Create the common instance."""
40 self
._auth
_token
= None
44 def _authenticate(self
, tenant_id
=None):
45 """Authenticate and/or renew the authentication token."""
46 if self
._auth
_token
is not None:
47 return self
._auth
_token
50 cfg
= Config
.instance()
51 self
._ks
= client
.Client(auth_url
=cfg
.OS_AUTH_URL
,
52 username
=cfg
.OS_USERNAME
,
53 password
=cfg
.OS_PASSWORD
,
54 tenant_name
=cfg
.OS_TENANT_NAME
)
55 self
._auth
_token
= self
._ks
.auth_token
56 except Exception as exc
:
58 log
.warn("Authentication failed: %s", exc
)
60 self
._auth
_token
= None
62 return self
._auth
_token
64 def get_endpoint(self
, service_type
):
65 """Get the endpoint for Gnocchi/Aodh."""
67 return self
._ks
.service_catalog
.url_for(
68 service_type
=service_type
,
69 endpoint_type
='internalURL',
70 region_name
='RegionOne')
71 except Exception as exc
:
72 log
.warning("Failed to retreive endpoint for service due to: %s",
77 def _perform_request(cls
, url
, auth_token
,
78 req_type
=None, payload
=None, params
=None):
79 """Perform the POST/PUT/GET/DELETE request."""
81 headers
= {'X-Auth-Token': auth_token
,
82 'Content-type': 'application/json'}
83 # perform request and return its result
87 response
= requests
.put(
88 url
, data
=payload
, headers
=headers
,
90 elif req_type
== "post":
91 response
= requests
.post(
92 url
, data
=payload
, headers
=headers
,
94 elif req_type
== "get":
95 response
= requests
.get(
96 url
, params
=params
, headers
=headers
, timeout
=1)
97 elif req_type
== "delete":
98 response
= requests
.delete(
99 url
, headers
=headers
, timeout
=1)
101 log
.warn("Invalid request type")
103 except Exception as e
:
104 log
.warn("Exception thrown on request", e
)