Skip to content

Commit 6997f95

Browse files
committed
updating to use keystone v3 api
1 parent f0d100a commit 6997f95

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

server/externalscripts/openstack-discovery

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ from sys import argv
66
from sys import stderr
77
import json
88
from datetime import datetime
9-
import re
9+
from urlparse import urlparse
1010

1111

1212
try:
13-
from keystoneclient.v2_0 import client as keystone_client
14-
from keystoneclient.auth.identity import v2
15-
from novaclient import client
13+
from keystoneclient.v3 import client as keystone_client
14+
from keystoneauth1 import loading
15+
from keystoneauth1 import session
16+
from novaclient import client as nova_client
1617
except ImportError:
1718
print 'ZBX_NOTSUPPORTED\x00 Failed to import openstack python modules'
1819
exit(1)
1920

2021
def get_host_port(url):
21-
m = re.match('htt.?://(.*):(\d+)',url)
22-
if m:
23-
return (m.group(1),m.group(2))
22+
parsed = urlparse(url)
23+
if parsed:
24+
return (parsed.hostname, parsed.port)
2425

2526
def print_json(data):
2627
print json.dumps({'data':data},sort_keys=True,indent=7,separators=(',',':'))
@@ -30,15 +31,26 @@ def discover_endpoints(auth_url, tenant_name, username, password):
3031

3132
services = []
3233
for e in conn.endpoints.list():
33-
host = get_host_port(e.publicurl)
34+
if e.interface != 'public':
35+
continue
36+
host = get_host_port(e.url)
3437
if e.enabled:
3538
service = conn.services.get(e.service_id)
36-
services.append({'{#OS_SERVICENAME}':service.name, '{#OS_SERVICEURL}':e.publicurl, '{#OS_SERVICEHOST}':host[0], '{#OS_SERVICEPORT}':host[1] })
39+
services.append({'{#OS_SERVICENAME}':service.name, '{#OS_SERVICEURL}':e.url, '{#OS_SERVICEHOST}':host[0], '{#OS_SERVICEPORT}':host[1] })
3740

3841
print_json(services)
3942

4043
def discover_hypervisors(auth_url, tenant_name, username, password):
41-
conn = nova = client.Client(2, username, password, tenant_name, auth_url)
44+
loader = loading.get_plugin_loader('password')
45+
print loader.get_options()
46+
auth = loader.load_from_options(auth_url=auth_url,
47+
username=username,
48+
password=password,
49+
project_name=tenant_name,
50+
user_domain_name='default',
51+
project_domain_name='default')
52+
sess = session.Session(auth=auth)
53+
conn = nova_client.Client(2, session=sess)
4254

4355
hypervisors = []
4456
for h in conn.hypervisors.list(detailed=True):
@@ -49,12 +61,12 @@ def discover_hypervisors(auth_url, tenant_name, username, password):
4961

5062
if __name__ == '__main__':
5163

52-
if (len(argv) != 6):
64+
if (len(argv) != 6):
5365
print """Usage: %s TYPE AUTH_URL TENANT USERNAME PASSWORD
5466
where
5567
5668
TYPE - one of the following: endpoints hypervisors
57-
AUTH_URL - URL to keystone
69+
AUTH_URL - URL to keystone
5870
TENANT - tenant name
5971
USERNAME - user name
6072
PASSWORD - password for user
@@ -70,6 +82,3 @@ if __name__ == '__main__':
7082
else:
7183
print 'ZBX_NOTSUPPORTED\x00 Unknown type: %s' % TYPE
7284
exit(1)
73-
74-
75-

0 commit comments

Comments
 (0)