|
| 1 | +# UpCloud-python-api |
| 2 | +Python client for [UpCloud's API](https://www.upcloud.com/documentation/api/). |
| 3 | + |
| 4 | +## Features |
| 5 | +* OOP based management of Servers, Storages and IP-addresses with full CRUD. |
| 6 | +* Clear way to define your infrastructure, emphasis on clear and easy syntax |
| 7 | +* Access all the data of the objects ( e.g. ssh credentials ) |
| 8 | +* Scale horizontally by creating / destroying servers |
| 9 | +* Scale vertically by changing the RAM, CPU, storage specs of any server |
| 10 | + |
| 11 | +**TODO:** |
| 12 | +* Cloning of storages |
| 13 | +* Full management of special storage types: |
| 14 | + * CDROMs, custom OS templates |
| 15 | + * custom templates can already be cloned to a disk via UUID |
| 16 | +* Full management of Firewall rules (per server on/off supported at the moment) |
| 17 | + * defining a server's L2 firewall's rules through the API client |
| 18 | +* Full management of backups (instant and scheduled) |
| 19 | +* IPv6 support |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +Note that operations are not instant, for example a server is not fully shut down when the API responds. |
| 26 | +You must take this into account in your automations. |
| 27 | + |
| 28 | +### Defining and creating Servers |
| 29 | + |
| 30 | +```python |
| 31 | +import upcloud |
| 32 | +import upcloud.Server |
| 33 | +import upcloud.Storage |
| 34 | +import upcloud.ZONE |
| 35 | + |
| 36 | +manager = upcloud.CloudManager("api_user", "password") |
| 37 | +manager.authenticate() # test credentials |
| 38 | + |
| 39 | +cluster = { |
| 40 | + "web1": Server( core_number = 1, # CPU cores |
| 41 | + memory_amount = 512, # RAM in MB |
| 42 | + hostname = "web1.example.com", |
| 43 | + zone = ZONE.London, # Zone.Helsinki and Zone.Chicago available also |
| 44 | + storage_devices = [ |
| 45 | + # OS: Ubuntu 14.04 from template |
| 46 | + # default tier: maxIOPS, the 100k IOPS storage backend |
| 47 | + Storage(os = "Ubuntu 14.04", size=10), |
| 48 | + # secondary storage, hdd for reduced cost |
| 49 | + Storage(size=100, tier="hdd") |
| 50 | + ]), |
| 51 | + |
| 52 | + "web2": Server( core_number = 1, |
| 53 | + memory_amount = 512, |
| 54 | + hostname = "web2.example.com", |
| 55 | + zone = ZONE.London, |
| 56 | + storage_devices = [ |
| 57 | + Storage(os = "Ubuntu 14.04", size=10), |
| 58 | + Storage(size=100, tier="hdd"), |
| 59 | + ]), |
| 60 | + |
| 61 | + "db": Server( core_number = 2, |
| 62 | + memory_amount = 2048, |
| 63 | + hostname = "db.example.com", |
| 64 | + zone = ZONE.London, |
| 65 | + storage_devices = [ |
| 66 | + Storage(os = "Ubuntu 14.04", size=10), |
| 67 | + Storage(size=100), |
| 68 | + ]), |
| 69 | + |
| 70 | + "lb": Server( core_number = 2, |
| 71 | + memory_amount = 1024, |
| 72 | + hostname = "balancer.example.com", |
| 73 | + zone = ZONE.London, |
| 74 | + storage_devices = [ |
| 75 | + Storage(os = "Ubuntu 14.04", size=10) |
| 76 | + ]) |
| 77 | +} |
| 78 | + |
| 79 | +for server in cluster: |
| 80 | + manager.create_server( server ) # automatically populates the Server objects with data from API |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +### Stop / Start / Destroy Servers |
| 85 | +```python |
| 86 | + |
| 87 | +for server in cluster: |
| 88 | + server.shutdown() |
| 89 | + # OR: |
| 90 | + server.start() |
| 91 | + # OR: |
| 92 | + server.destroy() |
| 93 | + for storage in server.storage_devices: |
| 94 | + storage.destroy() |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +### Upgrade a Server |
| 99 | +```python |
| 100 | + |
| 101 | +server = cluster["web1"] |
| 102 | +server.shutdown() |
| 103 | +server.core_number = 4 |
| 104 | +server.memory_amount = 4096 |
| 105 | +server.save() |
| 106 | +server.start() |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +### GET resources: |
| 111 | +```python |
| 112 | + |
| 113 | +servers = manager.get_servers() |
| 114 | +server1 = manager.get_server(UUID) # e.g servers[0].uuid |
| 115 | +storages = manager.get_storages() |
| 116 | +storage1 = manager.get_storage(UUID) # e.g sever1.storage_devices[0].uuid |
| 117 | +ip_addrs = manager.get_IPs() |
| 118 | +ip_addr = manager.get_IP(address) # e.g server1.ip_addresses[0].address |
| 119 | + |
| 120 | +``` |
| 121 | + |
| 122 | +## Tests |
| 123 | + |
| 124 | +Tests located in `project_root/tests/` directory. Run with: |
| 125 | + |
| 126 | +```python |
| 127 | +py.test tests/ |
| 128 | +``` |
| 129 | + |
| 130 | +## Documentation |
| 131 | + |
| 132 | +* Server, Storage and IP_address attributes match UpCloud's API responses. |
| 133 | +* Source code is documented with Docstrings |
| 134 | +* Full API documentation coming soon... |
0 commit comments