cloud:recipe:coding
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cloud:recipe:coding [2020/05/14 08:50] – chudler | cloud:recipe:coding [2020/05/14 18:32] (current) – chudler | ||
---|---|---|---|
Line 2: | Line 2: | ||
This document gives a quick introduction to using the Openstack Python client to discover and consume compute resources. | This document gives a quick introduction to using the Openstack Python client to discover and consume compute resources. | ||
- | Before beginning, get an account as described in the [[ cloud:intro | Introduction ]] | + | Before beginning, get an account as described in the [[ cloud:intro | Introduction ]]. This is the right place to read about writing python scripts. A companion document shows how to use the [[ cloud:cli | Command Line ]]. |
======Installation====== | ======Installation====== | ||
Line 48: | Line 48: | ||
</ | </ | ||
+ | The main flaw here is that you are taking risks by storing your University credentials in a file. Instead, you can use the API or web interface to create an Application Credential. For example, | ||
+ | |||
+ | < | ||
+ | openstack application credential create --secret sekret --role member --expiration 2020-10-10: | ||
+ | </ | ||
=====Testing and Usage===== | =====Testing and Usage===== | ||
Ensure you can load the python module | Ensure you can load the python module | ||
Line 64: | Line 69: | ||
You have failed to provide a configuration file. This is different from a syntax error, but not so different from a permission problem, for example. | You have failed to provide a configuration file. This is different from a syntax error, but not so different from a permission problem, for example. | ||
+ | ======Demo Code====== | ||
+ | The following Python code shows how to create and destroy a server, and obtain information about Networks, Images, and Flavors. Consult the [[ https:// | ||
+ | |||
+ | For your learning, contrast this with the equivalent actions that are taken in the [[ cloud:cli | Command Line Usage Guide ]]. | ||
+ | |||
+ | <code Python> | ||
+ | |||
+ | import openstack | ||
+ | from openstack.config import loader | ||
+ | # to enable debug logging | ||
+ | # openstack.enable_logging(True) | ||
+ | config = loader.OpenStackConfig() | ||
+ | cloud = openstack.connect(cloud=' | ||
+ | flavor = cloud.get_flavor_by_ram(2048) | ||
+ | # print(flavor.name) | ||
+ | image = cloud.get_image(' | ||
+ | # pretty print | ||
+ | # cloud.pprint(image) | ||
+ | network = cloud.get_network(' | ||
+ | try: | ||
+ | server = cloud.create_server(' | ||
+ | finally: | ||
+ | cloud.delete_server(' | ||
+ | </ |
/var/lib/dokuwiki/data/attic/cloud/recipe/coding.1589464238.txt.gz · Last modified: 2020/05/14 08:50 by chudler