diff --git a/doc/source/users/guides/compute.rst b/doc/source/users/guides/compute.rst index 697aedb4c..cc06fa28c 100644 --- a/doc/source/users/guides/compute.rst +++ b/doc/source/users/guides/compute.rst @@ -52,6 +52,18 @@ A **network** provides connectivity to servers. Full example: `network resource list`_ +Create Key Pair +--------------- + +A **key pair** is the public key and private key of +`public–key cryptography`_. They are used to encrypt and decrypt login +information when connecting to your server. + +.. literalinclude:: ../examples/compute/create.py + :pyobject: create_keypair + +Full example: `compute resource create`_ + Create Server ------------- @@ -60,8 +72,8 @@ creation. You can discover the names and IDs of these attributes by listing them as above and then using the find methods to get the appropriate resources. -Ideally you'll also create a server using a public/private keypair so you can -login to that server with the private key. +Ideally you'll also create a server using a keypair so you can login to that +server with the private key. Servers take time to boot so we call ``wait_for_server`` to wait for it to become active. @@ -74,3 +86,4 @@ Full example: `compute resource create`_ .. _compute resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/compute/list.py .. _network resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/list.py .. _compute resource create: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/compute/create.py +.. _public–key cryptography: https://en.wikipedia.org/wiki/Public-key_cryptography \ No newline at end of file diff --git a/examples/compute/create.py b/examples/compute/create.py index 5fced0b06..847eba7ba 100644 --- a/examples/compute/create.py +++ b/examples/compute/create.py @@ -25,24 +25,35 @@ For a full guide see TODO(etoews):link to docs on developer.openstack.org """ -def create_server(conn): - print("Create Server:") +def create_keypair(conn): + keypair = conn.compute.find_keypair(KEYPAIR_NAME) - image = conn.compute.find_image(IMAGE_NAME) - flavor = conn.compute.find_flavor(FLAVOR_NAME) - network = conn.network.find_network(NETWORK_NAME) + if not keypair: + print("Create Key Pair:") - if not conn.compute.find_keypair(KEYPAIR_NAME): keypair = conn.compute.create_keypair(name=KEYPAIR_NAME) + print(keypair) + with open(PRIVATE_KEYPAIR_FILE, 'w') as f: f.write("%s" % keypair.private_key) os.chmod(PRIVATE_KEYPAIR_FILE, 0o400) + return keypair + + +def create_server(conn): + print("Create Server:") + + image = conn.compute.find_image(IMAGE_NAME) + flavor = conn.compute.find_flavor(FLAVOR_NAME) + network = conn.network.find_network(NETWORK_NAME) + keypair = create_keypair(conn) + server = conn.compute.create_server( name='openstacksdk-example', image=image, flavor=flavor, - networks=[{"uuid": network.id}], key_name=KEYPAIR_NAME) + networks=[{"uuid": network.id}], key_name=keypair.name) server = conn.compute.wait_for_server(server)