1 """Common methods for the Aodh Sender/Receiver."""
5 from keystoneclient
.v3
import client
7 from plugins
.OpenStack
.settings
import Config
11 # from keystoneauth1.identity.v3 import AuthMethod
12 # from keystoneclient.service_catalog import ServiceCatalog
16 """Common calls for Gnocchi/Aodh plugins."""
19 """Create the common instance."""
20 self
._auth
_token
= None
24 def _authenticate(self
, tenant_id
=None):
25 """Authenticate and/or renew the authentication token."""
26 if self
._auth
_token
is not None:
27 return self
._auth
_token
30 cfg
= Config
.instance()
31 self
._ks
= client
.Client(auth_url
=cfg
.OS_AUTH_URL
,
32 username
=cfg
.OS_USERNAME
,
33 password
=cfg
.OS_PASSWORD
,
34 tenant_name
=cfg
.OS_TENANT_NAME
)
35 self
._auth
_token
= self
._ks
.auth_token
36 except Exception as exc
:
38 log
.warn("Authentication failed with the following exception: %s",
40 self
._auth
_token
= None
42 return self
._auth
_token
44 def get_endpoint(self
, service_type
):
45 """Get the endpoint for Gnocchi/Aodh."""
47 return self
._ks
.service_catalog
.url_for(
48 service_type
=service_type
,
49 endpoint_type
='internalURL',
50 region_name
='RegionOne')
51 except Exception as exc
:
52 log
.warning("Failed to retreive endpoint for Aodh due to: %s",
57 def _perform_request(cls
, url
, auth_token
,
58 req_type
=None, payload
=None, params
=None):
59 """Perform the POST/PUT/GET/DELETE request."""
61 headers
= {'X-Auth-Token': auth_token
,
62 'Content-type': 'application/json'}
63 # perform request and return its result
67 response
= requests
.put(
68 url
, data
=payload
, headers
=headers
,
70 elif req_type
== "post":
71 response
= requests
.post(
72 url
, data
=payload
, headers
=headers
,
74 elif req_type
== "get":
75 response
= requests
.get(
76 url
, params
=params
, headers
=headers
, timeout
=1)
77 elif req_type
== "delete":
78 response
= requests
.delete(
79 url
, headers
=headers
, timeout
=1)
81 log
.warn("Invalid request type")
83 except Exception as e
:
84 log
.warn("Exception thrown on request", e
)