Python SDK (alpha release)
Hyperstack provides SDKs for Python and Go, allowing developers to programmatically manage their cloud resources with ease. Similar to SDKs offered by other major cloud platforms, the Hyperstack SDKs enable you to integrate and automate interactions with cloud resources directly within your applications, covering services like virtual machines, Kubernetes clusters, and network configurations.
In this guide, you’ll find detailed instructions to use the Hyperstack SDK for Python. By following these steps, you can streamline cloud operations and management, bringing industry-standard practices directly into your codebase.
To view more examples and resources:
- Check out our public GitHub repository here.
The Hyperstack SDKs are currently in alpha, and some features may still be under development. Exercise caution when deploying production-level operations.
In this article
1. Prerequisites
Before you begin using any of the SDKs, ensure you have installed the programming language’s runtime environment on your system. You can download the required runtime from the official websites:
2. Installation
To install the Hyperstack SDKs, use one of the following commands:
- PyPI
- GitHub
- Setuptools
pip install hyperstack
pip install git+https://github.com/NexGenCloud/hyperstack-sdk-python.git
git clone https://github.com/NexGenCloud/hyperstack-sdk-python.git
cd hyperstack-sdk-python
python setup.py install --user
3. Usage
After installing the SDK, you can start using it in your Python scripts. Here’s an example of how you can import the SDK and list all the virtual machines in your Hyperstack account:
3.1 Authenticate
You can authenticate with the Hyperstack API using your API key.
To get your API key see instructions here
You can set the API key using the SDK configuration:
configuration = hyperstack.Configuration(
api_key={"apiKey": "[replace-with-your-hyperstack-api-key]"}
)
with hyperstack.ApiClient() as api_client:
# rest of code
Alternatively you can set the API key after initializing the Configuration
class.
configuration = hyperstack.Configuration()
configuration.api_key["apiKey"] = "[replace-with-your-hyperstack-api-key]"
with hyperstack.ApiClient() as api_client:
# rest of code
3.2 Simple example
After setting the API key, you can use the following Python script to list all the virtual machines in your Hyperstack account. For this examples, make sure to set your API key as the environment variable HYPERSTACK_API_KEY
.
import os
import json
import hyperstack
from hyperstack.rest import ApiException
# Define the configuration for the API client
configuration = hyperstack.Configuration(
api_key={"apiKey": os.environ["HYPERSTACK_API_KEY"]}
)
# Enter a context with an instance of the API client
with hyperstack.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = hyperstack.VirtualMachineApi(api_client)
try:
# List virtual machines
api_response = api_instance.list_virtual_machines()
instances = api_response.instances
instance_names = [instance.name for instance in instances]
# Print output
print("List of virtual machines:")
print(json.dumps(instance_names, indent=2))
except ApiException as e:
print(
f"An exception occurred while calling VirtualMachineApi.list_virtual_machines: {e}"
)
3.3 Advanced example
The following example demonstrates how to create a virtual machine with specific configurations. For this example, make sure to set your API key as the environment variable HYPERSTACK_API_KEY
.
import os
import hyperstack
configuration = hyperstack.Configuration()
configuration.api_key["apiKey"] = os.environ["HYPERSTACK_API_KEY"]
with hyperstack.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = hyperstack.VirtualMachineApi(api_client)
payload = payload = hyperstack.CreateInstancesPayload(
name="my-vm-2",
environment_name="default-CANADA-1",
key_name="canada-key-prod-040624",
image_name="Ubuntu Server 22.04 LTS (Jammy Jellyfish)",
flavor_name="n1-cpu-medium",
count=1,
assign_floating_ip=True,
labels=["sdk-example"],
# Allow SSH
security_rules=[
hyperstack.CreateSecurityRulePayload(
direction="ingress",
ethertype="IPv4",
protocol="tcp",
remote_ip_prefix="0.0.0.0/0",
port_range_min=22,
port_range_max=22,
),
hyperstack.CreateSecurityRulePayload(
direction="ingress",
ethertype="IPv6",
protocol="tcp",
remote_ip_prefix="::/0",
port_range_min=22,
port_range_max=22,
),
],
)
try:
# Create virtual machine
api_response = api_instance.create_virtual_machines(payload)
instance_details = api_response.instances[0]
id = instance_details.id
print(f"Created virtual machine with ID: {id}")
except Exception as e:
print(
f"An exception occurred while calling VirtualMachineApi.create_virtual_machine: {e}"
)
4. Other resources
To explore more advanced usage and other examples, refer to our Python SDK GitHub repository.