Develop the flavor_manager.py program to perform operations related to cloud hosting type management.
The file has the following features.
- Creates a cloud host type based on the command line arguments and returns response.
- Query all cloud hosting types under admin account
- Query the type of cloud hosting given a specific name
- Support for deleting specified id cloud hosting types
The operation is not complicated and the program is relatively simple and is roughly divided into two main parts
I. Parsing incoming parameters and converting them into variables used by the program
Second, through openstacksdk, use the obtained variables to accomplish the appropriate behavior.
infrastructural
First of all build our program theme, definitely need argparse module, then we first add argparse module into our program.
import argparse parse = () args = parse.parse_args()
This won't do anything and won't satisfy the requirements of the topic, so I make a change, since we need to make the program have four functions, we must write four corresponding implementations, and parameter parsing is a must, so we need an operation like this, when we first parse out the required operation, then we make a feedback, and parse the continued parameters into the corresponding functions, so that the four functions will be separated, the The whole program will become very neat.
Creating a parsing object
Coming back to the title, the four functions are distinguished from each other using the position parameter, in that case our first parameter is set tooption
, which is used to identify the functions that need to be done by the program, after which it will be brought into the appropriate parsing and call the required program to complete the appropriate behavior.
That's the overall idea, in case you haven't been following along. So let me demonstrate the first step of parsing:
import argparse parse = () parse.add_argument('option') if __name__ == '__main__': args = parse.parse_args() print()
Predictably, after this step, a mandatory parameter is added into the program, the positional parameter we need, if you typecreate
Then the program'sJust set it to
create
Blah, blah, blah.getall ,get,delete. It's also just as well differentiated, and next we need a judgment where we read in different parameters in different judgments.
Parse positional parameters
It's better to take it one step at a time, let's start withcreate
As an example:
import argparse parse = () parse.add_argument('option') parse.add_argument('-n', '--name', type=str, metavar='name', help='New flavor name') parse.add_argument('-m', '--ram', type=int, metavar='size-mb', default=256, help='Memory size in MB (DEFAULT 256M)') parse.add_argument('-v', '--vcpus', type=int, metavar='vcpus', default=1, help='Number of vcpus (default 1)') parse.add_argument('-d', '--disk', type=int, metavar='size-gb', default=0, help='Disk size in GB (default 0G)') parse.add_argument('-id', '--id', type=str, metavar='id', default='auto', help='Unique flavor ID') if __name__ == '__main__': args = parse.parse_args() if == 'create': print("i will create new flavor")
As you can see, I've stripped out the create behavior, so I'm sure you know what I'm going to do next. Let's fill out the four functions and see.
Parsing Optional Parameters
import argparse parse = () parse.add_argument('option') parse.add_argument('-n', '--name', type=str, metavar='name', help='New flavor name') parse.add_argument('-m', '--ram', type=int, metavar='size-mb', default=256, help='Memory size in MB (DEFAULT 256M)') parse.add_argument('-v', '--vcpus', type=int, metavar='vcpus', default=1, help='Number of vcpus (default 1)') parse.add_argument('-d', '--disk', type=int, metavar='size-gb', default=0, help='Disk size in GB (default 0G)') parse.add_argument('-id', '--id', type=str, metavar='id', default='auto', help='Unique flavor ID') if __name__ == '__main__': args = parse.parse_args() if == 'create': print("i will create new flavor") if == 'getall': print("i will inquire all flavor") if == 'get': print("i will get flavor by id") if == 'delete': print("i will delete flavor by id")
Obviously, I haven't filled in the specific implementation yet, just changed the output to differentiate it and check that we're parsing all the parameters.
Fill in the main body of the program
Because I'm going to fill in the implementation next, which is one of the more important steps in the program.
- -n specifies the flavor name, data type is string
- -m specifies the memory size, data type is int, unit M
- -v specifies the number of virtual cpus, data type is int
- -d specifies the disk size, memory size type int, unit G
- -id specifies the id, type is string
We've already done the parsing of these parameters in the program, so let's fill in the create implementation first, and the program becomes something like the following.
import argparse import opentack parse = () parse.add_argument('option') parse.add_argument('-n', '--name', type=str, metavar='name', help='New flavor name') parse.add_argument('-m', '--ram', type=int, metavar='size-mb', default=256, help='Memory size in MB (DEFAULT 256M)') parse.add_argument('-v', '--vcpus', type=int, metavar='vcpus', default=1, help='Number of vcpus (default 1)') parse.add_argument('-d', '--disk', type=int, metavar='size-gb', default=0, help='Disk size in GB (default 0G)') parse.add_argument('-id', '--id', type=str, metavar='id', default='auto', help='Unique flavor ID') conn = ( auth_url='http://192.168.10.25:5000', username='admin', password='passwordadmin', project_name='admin', project_domain_name='Default', ) def create_flavor(name, ram, vcpus, disk, flavorid): response = conn.create_flavor(name, ram, vcpus, disk, flavorid=flavorid) return response if __name__ == '__main__': args = parse.parse_args() if == 'create': print(create_flavor(name=, ram=, vcpus=, disk=, flavorid=)) if == 'getall': print("i will inquire all flavor") if == 'get': print("i will get flavor by id") if == 'delete': print("i will delete flavor by id")
I hope you can still read it, in this step I imported the package openstack and I created a connection objectconn
So that I can operate openstack, I wrote a methodcreate_flavor
and specifies the parameters it can pass in, when option reads create, this method will be called with the appropriate parameters. The create_flaovr method is called by the connection object in the method to finalize the creation, and when it is done, this method returns the response and prints it to the terminal.
I've posted the results of the run below:
(venv) PS D:\Python> python .\ create -n flavor_small -m 1024 -v 1 -d 10 -id 100000
.(disk=10, OS-FLV-EXT-DATA:ephemeral=0, id=100000, os-flavor-access:is_public=True, name=flavor_small, ram=1024, rxtx_factor=1.0, swap=, vcpus=1, description=None, OS-FLV-DISABLED:disabled=False, extra_specs={}, location=Munch({'cloud': 'zed', 'region_name': '', 'zone': None, 'project': Munch({'id': '80074c3b4d09419e87ba0b8c05ce5164', 'name': 'admin', 'domain_id': None, 'domain_name': 'Default'})}))
Obviously, this is a correct output, and as such, I'm going to cut the crap and fill in all the functions quickly, and just create the remaining three methods based on the functions.
import argparse import openstack parse = () parse.add_argument('option') parse.add_argument('-n', '--name', type=str, metavar='name', help='New flavor name') parse.add_argument('-m', '--ram', type=int, metavar='size-mb', default=256, help='Memory size in MB (DEFAULT 256M)') parse.add_argument('-v', '--vcpus', type=int, metavar='vcpus', default=1, help='Number of vcpus (default 1)') parse.add_argument('-d', '--disk', type=int, metavar='size-gb', default=0, help='Disk size in GB (default 0G)') parse.add_argument('-id', '--id', type=str, metavar='id', default='auto', help='Unique flavor ID') conn = ( auth_url='http://192.168.10.25:5000', username='admin', password='passwordadmin', project_name='admin', project_domain_name='Default', ) def create_flavor(name, ram, vcpus, disk, flavorid): response = conn.create_flavor(name, ram, vcpus, disk, flavorid=flavorid) return response def getall_flavor(): response = conn.list_flavors() return response def get_flavor(id): response = conn.get_flavor_by_id(id) return response def delete_flavor(id): response = conn.delete_flavor(id) return response if __name__ == '__main__': args = parse.parse_args() if == 'create': print(create_flavor(, , , , )) if == 'getall': print(getall_flavor()) if == 'get': print(get_flavor()) if == 'delete': print(delete_flavor())
summarize
Here the entire command line script is largely completed, if you need other changes, just follow this idea to modify it. More use of the argparse module can refer to the official documentation of the analysis, very detailed, and with a very large number of cases, of course, it is too much trouble, I found a very well-written document recommended to you, I wrote my own is too bad.
The above is based on Python development cloud host type management script to share the details, more information about Python cloud host management script please pay attention to my other related articles!