Go 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 Go. 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:
Then install the following dependencies:
go get github.com/stretchr/testify/assert
go get golang.org/x/net/context
2. Installation
To install the Hyperstack SDKs, run the command below. Replace the [latest-version] placeholder with the latest version of the SDK. You can find the lastest version here: hyperstack-sdk-go/tags.
The latest version of the repository is the one with the most recent commit date. If you run go get github.com/NexGenCloud/hyperstack-sdk-go
, it will fetch the version with the highest alphabetical version tag, which may not correspond to the most recent version.
go get github.com/NexGenCloud/hyperstack-sdk-go@[latest-version]
3. Usage
After installing the SDK, you can start using it in your Go 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. For the examples below, set this key as an environment variable: HYPERSTACK_API_KEY
. To get your API key see instructions here.
The following code snippet demonstrates how to authenticate with the Hyperstack API:
// Create the configurations
configuration := openapiclient.NewConfiguration()
configuration.DefaultHeader = map[string]string{
"api_key": os.Getenv("HYPERSTACK_API_KEY"),
}
// Create a new API client
apiClient := openapiclient.NewAPIClient(configuration)
// rest of code
3.2 Simple example
You can use the following Go script to list all the virtual machines in your Hyperstack account:
package main
import (
"context"
"encoding/json"
"fmt"
"os"
openapiclient "github.com/NexGenCloud/hyperstack-sdk-go"
)
func main() {
// Create the configurations
configuration := openapiclient.NewConfiguration()
configuration.DefaultHeader = map[string]string{
"api_key": os.Getenv("HYPERSTACK_API_KEY"),
}
// Create a new API client
apiClient := openapiclient.NewAPIClient(configuration)
// Call `ListVirtualMachines` operation
resp, r, err := apiClient.VirtualMachineAPI.ListVirtualMachines(context.Background()).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `VirtualMachineAPI.ListVirtualMachines``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// Get the response as JSON
respJSON, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshalling response to JSON: %v\n", err)
return
}
fmt.Printf("Response from VirtualMachineAPI: %s\n", respJSON)
}
3.3 Advanced example
The following example demonstrates how to create a virtual machine with specific configurations:
package main
import (
"context"
"encoding/json"
"fmt"
"os"
openapiclient "github.com/NexGenCloud/hyperstack-sdk-go"
)
func main() {
// Create the configurations
configuration := openapiclient.NewConfiguration()
configuration.DefaultHeader = map[string]string{
"api_key": os.Getenv("HYPERSTACK_API_KEY"),
}
// Create a new API client
apiClient := openapiclient.NewAPIClient(configuration)
// Payload for virtual machine creation
payload := openapiclient.CreateInstancesPayload{
Name: "my-vm-2",
EnvironmentName: "default-CANADA-1",
KeyName: "canada-key-prod-040624",
ImageName: openapiclient.PtrString("Ubuntu Server 22.04 LTS (Jammy Jellyfish)"),
FlavorName: "n1-cpu-medium",
Count: 1,
AssignFloatingIp: openapiclient.PtrBool(true),
Labels: []string{"sdk-example"},
SecurityRules: []openapiclient.CreateSecurityRulePayload{
{
Direction: "ingress",
Ethertype: "IPv4",
Protocol: "tcp",
RemoteIpPrefix: "0.0.0.0/0",
PortRangeMin: openapiclient.PtrInt32(22),
PortRangeMax: openapiclient.PtrInt32(22),
},
{
Direction: "ingress",
Ethertype: "IPv6",
Protocol: "tcp",
RemoteIpPrefix: "::/0",
PortRangeMin: openapiclient.PtrInt32(22),
PortRangeMax: openapiclient.PtrInt32(22),
},
},
}
// Call the `CreateVirtualMachine` operation
resp, r, err := apiClient.VirtualMachineAPI.CreateVirtualMachines(context.Background()).Payload(payload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `VirtualMachineAPI.CreateVirtualMachine`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return
}
// Get the response as JSON
respJSON, err := json.MarshalIndent(resp, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshalling response to JSON: %v\n", err)
return
}
fmt.Printf("Response from VirtualMachineAPI: %s\n", respJSON)
}
4. Other resources
To explore more advanced usage and other examples, refer to our Go SDK GitHub repository.