1 from __future__ import print_function
2 from __future__ import absolute_import
4 from novaclient import client as novaclient
5 from neutronclient.v2_0 import client as neutronclient
8 def get_credentials(service): # pragma: no cover
9 """Returns a creds dictionary filled with the following keys:
11 * password/api_key (depending on the service)
12 * tenant_name/project_id (depending on the service)
14 :param service: a string indicating the name of the service
15 requesting the credentials.
18 # Unfortunately, each of the OpenStack client will request slightly
19 # different entries in their credentials dict.
20 if service.lower() in ("nova", "cinder"):
25 tenant = "tenant_name"
27 # The most common way to pass these info to the script is to do it through
28 # environment variables.
30 "username": os.environ.get('OS_USERNAME', "admin"),
31 password: os.environ.get("OS_PASSWORD", 'admin'),
32 "auth_url": os.environ.get("OS_AUTH_URL"),
33 tenant: os.environ.get("OS_TENANT_NAME", "admin"),
35 cacert = os.environ.get("OS_CACERT")
36 if cacert is not None:
37 # each openstack client uses differnt kwargs for this
38 creds.update({"cacert": cacert,
40 "https_ca_cert": cacert,
41 "https_cacert": cacert,
43 creds.update({"insecure": "True", "https_insecure": "True"})
44 if not os.path.isfile(cacert):
45 print(("WARNING: The 'OS_CACERT' environment variable is " +
46 "set to %s but the file does not exist." % cacert))
50 def get_instances(nova_client): # pragma: no cover
52 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
54 except Exception as e:
55 print("Error [get_instances(nova_client)]:", e)
59 def get_SFs(nova_client): # pragma: no cover
61 instances = get_instances(nova_client)
63 for instance in instances:
64 if "sfc_test" not in instance.name:
67 except Exception as e:
68 print("Error [get_SFs(nova_client)]:", e)
72 def get_external_net_id(neutron_client): # pragma: no cover
73 for network in neutron_client.list_networks()['networks']:
74 if network['router:external']:
79 def create_floating_ips(neutron_client): # pragma: no cover
80 extnet_id = get_external_net_id(neutron_client)
82 props = {'floating_network_id': extnet_id}
85 ip_json = neutron_client.create_floatingip({'floatingip': props})
86 fip_addr = ip_json['floatingip']['floating_ip_address']
88 except Exception as e:
89 print("Error [create_floating_ip(neutron_client)]:", e)
94 def floatIPtoSFs(SFs, floatips): # pragma: no cover
98 SF.add_floating_ip(floatips[i])
101 except Exception as e:
102 print(("Error [add_floating_ip(nova_client, '%s', '%s')]:" %
103 (SF, floatips[i]), e))
107 def get_an_IP(): # pragma: no cover
109 creds_nova = get_credentials("nova")
110 nova_client = novaclient.Client(version='2', **creds_nova)
111 creds_neutron = get_credentials("neutron")
112 neutron_client = neutronclient.Client(**creds_neutron)
113 SFs = get_SFs(nova_client)
114 floatips = create_floating_ips(neutron_client)
115 floatIPtoSFs(SFs, floatips)
119 if __name__ == '__main__': # pragma: no cover