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 keystoneauth1
import session
29 from keystoneauth1
.identity
import v3
30 from keystoneclient
.v3
import client
32 from osm_mon
.core
.auth
import AuthManager
33 from osm_mon
.core
.settings
import Config
35 __author__
= "Helena McGough"
37 log
= logging
.getLogger(__name__
)
38 cfg
= Config
.instance()
42 """Common calls for Gnocchi/Aodh plugins."""
45 """Create the common instance."""
48 def get_auth_token(vim_uuid
, verify_ssl
=True):
49 """Authenticate and/or renew the authentication token."""
50 auth_manager
= AuthManager()
51 creds
= auth_manager
.get_credentials(vim_uuid
)
52 sess
= session
.Session(verify
=verify_ssl
)
53 ks
= client
.Client(session
=sess
)
54 token_dict
= ks
.get_raw_token_from_identity_service(auth_url
=creds
.url
,
56 password
=creds
.password
,
57 project_name
=creds
.tenant_name
,
58 project_domain_id
='default',
59 user_domain_id
='default')
60 return token_dict
['auth_token']
63 def get_endpoint(service_type
, vim_uuid
, verify_ssl
=True):
64 """Get the endpoint for Gnocchi/Aodh."""
65 auth_manager
= AuthManager()
66 creds
= auth_manager
.get_credentials(vim_uuid
)
67 auth
= v3
.Password(auth_url
=creds
.url
,
69 password
=creds
.password
,
70 project_name
=creds
.tenant_name
,
71 project_domain_id
='default',
72 user_domain_id
='default')
73 sess
= session
.Session(auth
=auth
, verify
=verify_ssl
)
74 ks
= client
.Client(session
=sess
, interface
='public')
75 service
= ks
.services
.list(type=service_type
)[0]
76 endpoints
= ks
.endpoints
.list(service
)
77 endpoint_type
= 'publicURL'
78 region_name
= 'RegionOne'
79 if creds
.config
is not None:
81 config
= json
.loads(creds
.config
)
83 config
= yaml
.safe_load(creds
.config
)
84 if 'endpoint_type' in config
:
85 endpoint_type
= config
['endpoint_type']
86 if 'region_name' in config
:
87 region_name
= config
['region_name']
88 for endpoint
in endpoints
:
89 if endpoint
.interface
in endpoint_type
and endpoint
.region
== region_name
:
93 def perform_request(url
, auth_token
,
94 req_type
=None, payload
=None, params
=None, verify_ssl
=True):
95 """Perform the POST/PUT/GET/DELETE request."""
97 timeout
= cfg
.REQUEST_TIMEOUT
100 headers
= {'X-Auth-Token': auth_token
,
101 'Content-type': 'application/json'}
102 # perform request and return its result
103 if req_type
== "put":
104 response
= requests
.put(
105 url
, data
=payload
, headers
=headers
,
106 timeout
=timeout
, verify
=verify_ssl
)
107 elif req_type
== "get":
108 response
= requests
.get(
109 url
, params
=params
, headers
=headers
, timeout
=timeout
, verify
=verify_ssl
)
110 elif req_type
== "delete":
111 response
= requests
.delete(
112 url
, headers
=headers
, timeout
=timeout
, verify
=verify_ssl
)
114 response
= requests
.post(
115 url
, data
=payload
, headers
=headers
,
116 timeout
=timeout
, verify
=verify_ssl
)
118 # Raises exception if there was an error
120 response
.raise_for_status()
121 # pylint: disable=broad-except
123 # Log out the result of the request
126 response
.status_code
, response
.text
)