Created
September 30, 2020 08:13
-
-
Save erijpkema/56bd5f0da1da53505fd8d1468bac9124 to your computer and use it in GitHub Desktop.
api_demo_create_instance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Uses keystoneauth and the nova client api | |
to create an instance. | |
requires python3-openstackclient | |
""" | |
from os import environ as env | |
from novaclient import client as novaclient | |
from keystoneauth1 import loading | |
from keystoneauth1 import session | |
loader = loading.get_plugin_loader('password') | |
auth = loader.load_from_options( | |
auth_url=env['OS_AUTH_URL'], | |
username=env['OS_USERNAME'], | |
password=env['OS_PASSWORD'], | |
project_name=env['OS_PROJECT_NAME'], | |
user_domain_name=env['OS_USER_DOMAIN_NAME'], | |
project_domain_name=env['OS_PROJECT_DOMAIN_NAME']) | |
sess = session.Session(auth=auth) | |
nova = novaclient.Client(version='2', session=sess) | |
flavor = nova.flavors.find(name='m1.tiny') | |
# Note: I'm using the nova api to request network and image information | |
# instead of their own name. | |
network = nova.neutron.find_network('internal') | |
image = nova.glance.find_image('cirros') | |
if __name__ == '__main__': | |
server = nova.servers.create( | |
name='demo_instance', | |
image=image.id, | |
flavor=flavor.id, | |
nics=[{ | |
'net-id': network.id | |
}], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment