pip install python-selvpcclient
Note: when working with the API library, the URL and version of the API are indicated separately!
from selvpcclient.client import Client, setup_http_client SEL_TOKEN=YOUR_API_TOKEN_HERE SEL_URL="https://api.selectel.ru/vpc/resell" SEL_API_VERSION=2 http_client = setup_http_client(api_url=SEL_URL, api_version=SEL_API_VERSION, api_token=SEL_TOKEN) selvpc = Client(client=http_client)
project = selvpc.projects.create("Bonnie")
Note: Also from library version 1.1 you can get a “bare” json response from the API, for this you need to pass the return_raw key:
project_json = selvpc.projects.create("Clyde", return_raw=True)
project_json will contain the value:
{ "name": "Clyde", "id": "f3504dc929ee40e5a296143218bf435d", "url": "https://xxxx.selvpc.ru", "enabled": True }
"quotas": { "compute_cores": [ { "region": "ru-1", "zone": "ru-1a", "value": 10 } ], "compute_ram": [ { "region": "ru-1", "zone": "ru-1a", "value": 1024 } ] } } # via object project.update_quotas(quotas) # via quotas manager quotas = client.quotas.update(project.id, quotas=quotas)
"licenses": [{ "region": "ru-1", "quantity": 1, "type": "license_windows_2012_standard" }] } # via object project.add_license(license) # via licenses manager licenses = selvpc.licenses.add(project.id, licenses=licenses)
Note: by default, the library is already configured to work with version 2 of the API, SEL_API_VERSION / --api-version can be omitted.
Note: if information is passed through arguments and is present in environment variables, then arguments will be given priority.
export SEL_TOKEN="xxxxvGBYVXcQ3q86zQCGxgvk_42069" export SEL_URL="https://api.selectel.ru/vpc/resell" export SEL_API_VERSION=2 selvpc --url "https://api.selectel.ru/vpc/resell" --token "xxxxvGBYVXcQ3q86zQCGxgvk_42069" project list
selvpc project create -n awesome
selvpc project list +----------------------------------+----------+-------------------------+---------+ | id | name | url | enabled | +----------------------------------+----------+-------------------------+---------+ | f3504dc929ee40e5a296143218bf435d | awesome | https://xxxxx.selvpc.ru | True | +----------------------------------+----------+-------------------------+---------+
selvpc help Commands: ... capabilities show traffic Show available traffic values complete print bash completion command floatingip add Create new floatingip IP address floatingip delete Delete floatingip IP floatingip list List floatingip IP ...
selvpc project create -n "another-project" +---------+----------------------------------+ | Field | Value | +---------+----------------------------------+ | id | 96063b0c4a71443c8a842c647bdab316 | | name | "another-project" | | url | https://xxxxx.selvpc.ru | | enabled | True | +---------+----------------------------------+
selvpc user create --name "T-Rex" --password "c1017e8c8fd14b7e8057618a882240df" +---------+----------------------------------+ | Field | Value | +---------+----------------------------------+ | id | 052027b58a3f49e390c3849d6697e2f3 | | name | "T-Rex" | | enabled | True | +---------+----------------------------------+
selvpc role add -p 96063b0c4a71443c8a842c647bdab316 -u 052027b58a3f49e390c3849d6697e2f3 +------------+----------------------------------+ | Field | Value | +------------+----------------------------------+ | project_id | 96063b0c4a71443c8a842c647bdab316 | | user_id | 052027b58a3f49e390c3849d6697e2f3 | +------------+----------------------------------+
selvpc capabilities show resources +-------------------------------+-------------+----------+------------+ | name | quota_scope | quotable | unbillable | +-------------------------------+-------------+----------+------------+ | compute_cores | zone | True | True | | compute_ram | zone | True | True | | volume_gigabytes_fast | zone | True | False | +-------------------------------+-------------+----------+------------+
selvpc quota set 96063b0c4a71443c8a842c647bdab316 --region ru-1 --zone ru-1a --value 1 --resource compute_cores selvpc quota set 96063b0c4a71443c8a842c647bdab316 --region ru-1 --zone ru-1a --value 512 --resource compute_ram selvpc quota set 96063b0c4a71443c8a842c647bdab316 --region ru-1 --zone ru-1a --value 5 --resource volume_gigabytes_fast +----------------------------+--------+-------+-------+ | resource | region | zone | value | +----------------------------+--------+-------+-------+ | compute_cores | ru-1 | ru-1a | 1 | | compute_ram | ru-1 | ru-1a | 512 | | volume_gigabytes_fast | ru-1 | ru-1a | 5 | +----------------------------+--------+-------+-------+
export OS_AUTH_URL="https://api.selvpc.ru/identity/v3" export OS_IDENTITY_API_VERSION="3" export OS_VOLUME_API_VERSION="2" export OS_PROJECT_DOMAIN_NAME='xxxx' export OS_PROJECT_ID='96063b0c4a71443c8a842c647bdab316' export OS_TENANT_ID='96063b0c4a71443c8a842c647bdab316' export OS_REGION_NAME='ru-1' export OS_USER_DOMAIN_NAME='xxxx' export OS_USERNAME='T-Rex' export OS_PASSWORD='c1017e8c8fd14b7e8057618a882240df'
Note: In the original script, the variable “OS_PASSWORD” will not contain the password, as it is explicitly initialized for demonstration. Never shine your passwords - it is not safe!After downloading the script, it is necessary to execute it with the source command:
source RC.sh
openstack image list +--------------------------------------+---------------------------------+--------+ | ID | Name | Status | +--------------------------------------+---------------------------------+--------+ | 9feac917-f155-4013-b2fa-f5c8b72fd33c | Ubuntu 16.04 LTS 64-bit | active | +--------------------------------------+---------------------------------+--------+
openstack volume create ubuntu-volume --image "9feac917-f155-4013-b2fa-f5c8b72fd33c" --size 5 +---------------------+--------------------------------------+ | Field | Value | +---------------------+--------------------------------------+ | id | f79c0d35-f54c-4b6a-82ad-1764e425eec8 | +---------------------+--------------------------------------+
openstack flavor create --private --ram 512 --vcpus 1 my-flavor +----------------------------+--------------------------------------+ | Field | Value | +----------------------------+--------------------------------------+ | id | 006f7e84-f957-4764-a3a7-db8575c54ba7 | | name | my-flavor | | os-flavor-access:is_public | False | | ram | 512 | | vcpus | 1 | +----------------------------+--------------------------------------+
openstack server create --wait --volume "f79c0d35-f54c-4b6a-82ad-1764e425eec8" --flavor "006f7e84-f957-4764-a3a7-db8575c54ba7" "server-1"
Note: if you have multiple subnets in your project, then when creating a virtual machine, you must specify which subnet to use.
openstack subnet list +--------------------------------------+----------------+ | Network | Subnet | +--------------------------------------+----------------+ | a220c08e-a63f-48b8-aca5-563136ee9131 | 192.168.0.0/24 | | afc24500-65f0-4d28-ada0-773d92820850 | 192.168.0.0/24 | +--------------------------------------+----------------+ openstack server create --wait --volume "f79c0d35-f54c-4b6a-82ad-1764e425eec8" --flavor "006f7e84-f957-4764-a3a7-db8575c54ba7" --network a220c08e-a63f-48b8-aca5-563136ee9131 "server-1"
Note: the current version of the library at the time of this writing is 1.0.
Source: https://habr.com/ru/post/336066/
All Articles