Skip to content
This repository was archived by the owner on May 5, 2020. It is now read-only.

Repository files navigation

⚠️⚠️⚠️ Development moved to https://github.com/cloudscale-ch/python-cloudscale/⚠️⚠️⚠️


licensepython versionsstatuspypi versionPyPI - Downloadscodecov

Cloudscale

A cloudscale.ch API client for Python3 and your command line.

Install / Update

pip3 install -U cloudscale --user
export PATH=$PATH:$HOME/.local/bin
cloudscale-cli version

Command Line Interface

Autocompletion

zsh:

eval"$(_CLOUDSCALE_CLI_COMPLETE=source_zsh cloudscale-cli)"

bash:

eval"$(_CLOUDSCALE_CLI_COMPLETE=source cloudscale-cli)"

Authentication

Evironment variable

Using the ENV CLOUDSCALE_API_TOKEN variable:

export CLOUDSCALE_API_TOKEN=<your token>
cloudscale-cli flavor list

Command line argument

Passing the --api-token parameter:

cloudscale-cli server --api-token <your_token> create ...

Config file

Creating an ini file .cloudscale.ini (leading dot) in your $HOME or a cloudscale.ini (without leading dot) in the CWD with the following schema:

[default]api_token = <token>

The default profile taken if available is default. The profile can be chosen by passing --profile or CLOUDSCALE_PROFILE ENV variable.

export CLOUDSCALE_PROFILE=staging
[production]api_token = <token>
[staging]api_token = <token>

Passing the command line option will overwrite the ENV var as one would expect:

cloudscale-cli server --profile production list

Help

See all options:

 $ cloudscale-cli
Usage: cloudscale-cli [OPTIONS] COMMAND [ARGS]...
Options:
-h, --help Show this message and exit.
Commands:
flavor
floating-ip
image
network
objects-user
region
server
server-group
subnet
version
volume

Usage Examples

Create a server

cloudscale-cli server create --flavor flex-2 --name my-server --image centos-7 --ssh-key "$(cat ~/.ssh/id_rsa.pub)"

List all servers

cloudscale-cli server list

List servers having the tag project with value gemini

cloudscale-cli server list --filter-tag project=gemini

List servers having a tag project

cloudscale-cli server list --filter-tag project

Update servers tags (but keep all existing)

cloudscale-cli server update <uuid> --tag project=apollo --tag stage=prod

Update server tags, remove a specific tag key

cloudscale-cli server update <uuid> --tag project=apollo --tag stage=prod --clear-tag status

Update server tags, remove other tags

cloudscale-cli server update <uuid> --tag project=apollo --tag stage=prod --clear-all-tags

Stop a server

cloudscale-cli server stop <uuid>

Start a server

cloudscale-cli server start <uuid>

Usage in Python

List the slug of all flavors

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
cloudscale=Cloudscale(api_token=api_token)
flavors=cloudscale.flavor.get_all()
forflavorinflavors:
print(flavor['slug'])

Print the server names of running servers

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
cloudscale=Cloudscale(api_token=api_token)
servers=cloudscale.server.get_all()
forserverinservers:
ifserver['status'] =="running":
print(server['name'])

Print the server names of all servers having a specifc tag project

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
cloudscale=Cloudscale(api_token=api_token)
servers=cloudscale.server.get_all(filter_tag='project')
forserverinservers:
print(server['name'])

Print the server names of all servers having a specifc tag project with value apollo

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
cloudscale=Cloudscale(api_token=api_token)
servers=cloudscale.server.get_all(filter_tag='project=apollo')
forserverinservers:
print(server['name'])

Get resource by UUID

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
try:
cloudscale=Cloudscale(api_token=api_token)
server_group=cloudscale.server_group.get_by_uuid(uuid="5a1e5b28-d354-47a8-bfb2-01b048c20204")
print(server_group['name'])
exceptCloudscaleApiExceptionase:
print(e)

Error handling

importosfromcloudscaleimportCloudscale, CloudscaleApiExceptionapi_token=os.getenv('CLOUDSCALE_API_TOKEN')
try:
cloudscale=Cloudscale(api_token=api_token)
server=cloudscale.server.get_by_uuid(uuid="does-not-exist")
print(server['name'])
exceptCloudscaleApiExceptionase:
# Prints "API Response Error (404): Not found."print(e)
# Prints "404"print(e.status_code)
# Prints raw API responseprint(e.response)

Development

Run tests with coverage

tox -e coverage

About

A cloudscale.ch API client for Python3 and your command line.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages