This guide helps you migrate from the Xen Orchestra Go SDK v1 to v2. The v2 SDK introduces several breaking changes but provides a more consistent and easier-to-use API.
| Feature | v1 | v2 |
|---|---|---|
| API Protocol | JSON-RPC | REST API |
| ID Type | String | UUID |
| Client Interface | Flat structure | Method chaining |
| Error Handling | Custom error types | Standard Go errors with context |
| Context Support | Limited | All operations support context |
| Typing | Runtime type checking | Compile-time type checking |
| Initialization | GetConfigFromEnv() function |
config.New() function |
config := client.GetConfigFromEnv()
xoClient, err := client.NewClient(config)
if err != nil {
// Handle error
}cfg, err := config.New()
if err != nil {
// Handle error
}
client, err := v2.New(cfg)
if err != nil {
// Handle error
}vm, err := xoClient.GetVm(client.Vm{Id: "12345678-1234-1234-1234-123456789012"})
if err != nil {
// Handle error
}vmID, err := uuid.FromString("12345678-1234-1234-1234-123456789012")
if err != nil {
// Handle error
}
vm, err := client.VM().GetByID(ctx, vmID)
if err != nil {
// Handle error
}vms, err := xoClient.GetVms(client.Vm{})
if err != nil {
// Handle error
}vms, err := client.VM().GetAll(ctx, 0, "")
if err != nil {
// Handle error
}vm, err := xoClient.CreateVm(client.Vm{
NameLabel: "test-vm",
NameDescription: "Test VM",
Template: "template-id",
}, 5*time.Minute)
if err != nil {
// Handle error
}templateID, _ := uuid.FromString("template-id")
poolID, _ := uuid.FromString("pool-id")
newVM, err := client.VM().Create(ctx, poolID, &payloads.CreateVMParams{
NameLabel: "test-vm",
NameDescription: "Test VM",
Template: templateID,
})
if err != nil {
// Handle error
}err := xoClient.StartVm("vm-id")
if err != nil {
// Handle error
}
err = xoClient.HaltVm("vm-id")
if err != nil {
// Handle error
}vmID, _ := uuid.FromString("vm-id")
err := client.VM().Start(ctx, vmID)
if err != nil {
// Handle error
}
err = client.VM().CleanShutdown(ctx, vmID)
if err != nil {
// Handle error
}The v2 SDK uses the gofrs/uuid package for type-safe UUID handling, and XO uses the version 4 UUIDs:
import "github.com/gofrs/uuid"
// Creating a UUID from string
id, err := uuid.FromString("12345678-1234-1234-1234-123456789012")
if err != nil {
// Handle error
}
// Using a constant UUID (must be valid)
id := uuid.Must(uuid.FromString("12345678-1234-1234-1234-123456789012"))
// Generating a new UUID
id, err := uuid.NewV4()
if err != nil {
// Handle error
}All v2 operations accept a context, allowing timeout and cancellation:
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Use context in API calls
vm, err := client.VM().GetByID(ctx, vmID)Here's a complete example showing how to migrate a simple use case:
package main
import (
"fmt"
"github.com/vatesfr/xenorchestra-go-sdk/client"
)
func main() {
config := client.GetConfigFromEnv()
xoClient, err := client.NewClient(config)
if err != nil {
panic(err)
}
vms, err := xoClient.GetVms(client.Vm{})
if err != nil {
panic(err)
}
for _, vm := range vms {
fmt.Printf("VM: %s (ID: %s)\n", vm.NameLabel, vm.Id)
}
}package main
import (
"context"
"fmt"
"time"
"github.com/vatesfr/xenorchestra-go-sdk/pkg/config"
v2 "github.com/vatesfr/xenorchestra-go-sdk/v2"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg, err := config.New()
if err != nil {
panic(err)
}
client, err := v2.New(cfg)
if err != nil {
panic(err)
}
vms, err := client.VM().GetAll(ctx, 0, "")
if err != nil {
panic(err)
}
for _, vm := range vms {
fmt.Printf("VM: %s (ID: %s)\n", vm.NameLabel, vm.ID)
}
}