-
Notifications
You must be signed in to change notification settings - Fork 2
WIP - Feature/v2 initial modules rest client #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b0381d9
move existing example into v1 folder and introducing v2 for the new i…
iButcat 352d2ea
follow go structure with pkg, adding payloads, config, services with …
iButcat 05f6c85
introduce testing & uuid.
iButcat beea62c
add internal for shared component across version that are not necessa…
iButcat e5c7ab8
add v2 with generic client and xo client for the rest api.
iButcat d41d2c1
add go-version when using goenv.
iButcat abd9216
remove go.mod & go.sum, we can compile and run from the root director…
iButcat 71d3f0a
remove value of fallback and instead return an error when the necessa…
iButcat 81e848e
config returns an error if the necessary env aren't available.
iButcat dfd7c0e
docs for the v2 including example about how to add new service logic,…
iButcat 7814ab3
Remove docs and add them in docs directory instead.
iButcat 49edad2
remove comments and add them in docs instead.
iButcat 7867df9
Add more context for the project, also add the docs directory, how to…
iButcat e26aa04
fix tests for new added method. Comments out Create, fix later since …
iButcat c7e3a6f
Add makefile tests, run examples and other good to have for generatin…
iButcat fb8a842
Add package for go env var, easier for development purpose since it l…
iButcat 48240fb
add path builder instead of concatenate. Can be used in service to bu…
iButcat 44f5164
Add const for rest v0 path and import it in client.
iButcat 361dafc
uses the new path builder.
iButcat 84139e4
fix bad markdown formatting.
iButcat 9d7e3ea
Fix documentation from Cyrille suggestion.
iButcat a7a091e
upgrade go version following the upgrade of the go.mod to fix the lin…
iButcat d6d6fcf
fix linter and docs.
iButcat e923222
tidy to add the dependency in direct instead of undirect.
iButcat 5112df8
fix linter issue, also remove hardcoded vm name in the task polling, …
iButcat 6998431
fix 404 version not found for pipeline.
iButcat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Xen Orchestra Go SDK v2 | ||
|
||
## Overview | ||
|
||
The Xen Orchestra Go SDK v2 provides a modern, type-safe interface to interact with the Xen Orchestra API. This version moves from the JSON-RPC API used in v1 to a REST API implementation, offering significant improvements in usability, maintainability, and type safety. | ||
|
||
## Key Features | ||
|
||
- **REST API Support**: Uses Xen Orchestra's modern REST API | ||
- **Type Safety**: Leverages Go generics for type-safe API interactions | ||
- **Method Chaining**: Improved API ergonomics with `client.VM().Create()` pattern | ||
- **Context Support**: All operations support context for better timeout and cancellation handling | ||
- **UUID Support**: Native UUID type support instead of string IDs | ||
- **Structured Logging**: Built-in logging with configurable verbosity levels | ||
- **Interface-Based Design**: Clean interfaces for better testability and mocking | ||
- **Go Mocking**: Mocking is supported for all interfaces by adding the `//go:generate mockgen` tag in the interface file | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/vatesfr/xenorchestra-go-sdk/v2 | ||
``` | ||
|
||
## Quick Start | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/vatesfr/xenorchestra-go-sdk/pkg/config" | ||
v2 "github.com/vatesfr/xenorchestra-go-sdk/v2" | ||
) | ||
|
||
func main() { | ||
// Create a context with timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) | ||
defer cancel() | ||
|
||
// Initialize configuration | ||
cfg, err := config.New() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Create client | ||
client, err := v2.New(cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// List all VMs | ||
vms, err := client.VM().List(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Display VMs | ||
fmt.Printf("Found %d VMs\n", len(vms)) | ||
for i, vm := range vms { | ||
fmt.Printf("%d. %s (ID: %s, Power: %s)\n", i+1, vm.NameLabel, vm.ID, vm.PowerState) | ||
} | ||
} | ||
``` | ||
|
||
## Environment Variables | ||
|
||
The SDK uses the following environment variables for configuration: | ||
|
||
- `XOA_URL`: URL of the Xen Orchestra server | ||
- `XOA_USER`: Username for authentication | ||
- `XOA_PASSWORD`: Password for authentication | ||
- `XOA_INSECURE`: Set to "true" to skip TLS certificate verification | ||
- `XOA_DEVELOPMENT`: Set to "true" to enable development mode with additional logging | ||
- `XOA_RETRY_MODE`: Retry strategy ("none" or "backoff") | ||
- `XOA_RETRY_MAX_TIME`: Maximum time to wait between retries (default: 5 minutes) | ||
|
||
## Next Steps | ||
|
||
- [Architecture Guide](02-architecture.md) - Learn about the design patterns used in the SDK | ||
- [Migration Guide](03-migration-guide.md) - Migrate from v1 to v2 | ||
- [Service Implementation Guide](04-service-implementation.md) - Learn how to add new services |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
# Architecture Guide | ||
|
||
The v2 SDK uses a clean, interface-based architecture that makes it easy to use, test, and extend. This document explains the key architectural components. | ||
|
||
## Component Overview | ||
|
||
|
||
The SDK consists of these main components: | ||
|
||
``` | ||
v2/ | ||
├── client/ # HTTP client implementation | ||
├── xo.go # Main client entry point | ||
pkg/ | ||
├── config/ # Configuration handling | ||
├── payloads/ # API data structures | ||
├── services/ # Service implementations | ||
├── library/ # Service interfaces | ||
├── vm/ # VM service implementation | ||
└── ... # Other service implementations | ||
internal/ | ||
└── common/ # Shared utilities | ||
├── core/ # Core types and constants | ||
└── logger/ # Logging implementation | ||
``` | ||
|
||
|
||
## Key Design Patterns | ||
|
||
### Interface-Based Design | ||
|
||
The SDK uses interfaces to define service contracts in the `library` package. These interfaces define the operations available for each resource type. | ||
|
||
Example from `library/vm.go`: | ||
|
||
```go | ||
type VM interface { | ||
GetByID(ctx context.Context, id uuid.UUID) (*payloads.VM, error) | ||
List(ctx context.Context) ([]*payloads.VM, error) | ||
Create(ctx context.Context, vm *payloads.VM) (*payloads.VM, error) | ||
// ... other methods | ||
} | ||
``` | ||
|
||
This approach allows for: | ||
- Clear API documentation | ||
- Easy mocking for tests | ||
- Swappable implementations | ||
|
||
### Service Registry Pattern | ||
|
||
The `XOClient` struct acts as a registry for service implementations, providing a coherent API for accessing different services: | ||
|
||
```go | ||
// In v2/xo.go | ||
type XOClient struct { | ||
vmService library.VM | ||
// other services will be added here | ||
} | ||
|
||
func (c *XOClient) VM() library.VM { | ||
return c.vmService | ||
} | ||
``` | ||
|
||
This allows for method chaining like `client.VM().Create(...)`. | ||
|
||
### Generic HTTP Methods | ||
|
||
The client package uses Go generics to provide type-safe HTTP methods: | ||
|
||
```go | ||
// In client/client.go | ||
func TypedGet[P any, R any](ctx context.Context, c *Client, endpoint string, params P, result *R) error { | ||
// implementation | ||
} | ||
``` | ||
|
||
This makes service implementations cleaner and more type-safe. | ||
|
||
### Payload Structs | ||
|
||
API data structures are defined in the `payloads` package, separate from service logic: | ||
|
||
```go | ||
// In pkg/payloads/vm.go | ||
type VM struct { | ||
ID uuid.UUID `json:"id,omitempty"` | ||
NameLabel string `json:"name_label"` | ||
NameDescription string `json:"name_description"` | ||
// ... other fields | ||
} | ||
``` | ||
|
||
## Client Initialization Flow | ||
|
||
1. Configuration is loaded from environment variables or provided directly | ||
2. The HTTP client is created with the appropriate authentication | ||
3. Service implementations are initialized | ||
4. The main client registers all services | ||
5. The client is ready to use | ||
|
||
## Async Operations | ||
|
||
Some API operations (like VM creation) are asynchronous. The SDK handles this by: | ||
|
||
1. Making the initial request that returns a task URL | ||
2. Polling the task status until completion | ||
3. Retrieving the final result when the task succeeds | ||
|
||
This is encapsulated in service methods for a clean API. | ||
|
||
## Error Handling | ||
|
||
Errors are propagated from the HTTP client to the service methods and finally to the caller. Detailed error information is available to help diagnose issues. | ||
|
||
## Logging | ||
|
||
The SDK uses a structured logger based on zap for efficient and informative logging. Log levels are automatically adjusted based on whether the SDK is in development or production mode. | ||
iButcat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
## Key Design Patterns | ||
|
||
### Interface-Based Design | ||
|
||
The SDK uses interfaces to define service contracts in the `library` package. These interfaces define the operations available for each resource type. | ||
|
||
Example from `library/vm.go`: | ||
|
||
```go | ||
type VM interface { | ||
GetByID(ctx context.Context, id uuid.UUID) (*payloads.VM, error) | ||
List(ctx context.Context) ([]*payloads.VM, error) | ||
Create(ctx context.Context, vm *payloads.VM) (*payloads.VM, error) | ||
// ... other methods | ||
} | ||
``` | ||
|
||
This approach allows for: | ||
- Clear API documentation | ||
- Easy mocking for tests | ||
- Swappable implementations | ||
|
||
### Service Registry Pattern | ||
|
||
The `XOClient` struct acts as a registry for service implementations, providing a coherent API for accessing different services: | ||
|
||
```go | ||
// In v2/xo.go | ||
type XOClient struct { | ||
vmService library.VM | ||
// other services will be added here | ||
} | ||
|
||
func (c *XOClient) VM() library.VM { | ||
return c.vmService | ||
} | ||
``` | ||
|
||
This allows for method chaining like `client.VM().Create(...)`. | ||
|
||
### Generic HTTP Methods | ||
|
||
The client package uses Go generics to provide type-safe HTTP methods: | ||
|
||
```go | ||
// In client/client.go | ||
func TypedGet[P any, R any](ctx context.Context, c *Client, endpoint string, params P, result *R) error { | ||
// implementation | ||
} | ||
``` | ||
|
||
This makes service implementations cleaner and more type-safe. | ||
|
||
### Payload Structs | ||
|
||
API data structures are defined in the `payloads` package, separate from service logic: | ||
|
||
```go | ||
// In pkg/payloads/vm.go | ||
type VM struct { | ||
ID uuid.UUID `json:"id,omitempty"` | ||
NameLabel string `json:"name_label"` | ||
NameDescription string `json:"name_description"` | ||
// ... other fields | ||
} | ||
``` | ||
|
||
## Client Initialization Flow | ||
|
||
1. Configuration is loaded from environment variables or provided directly | ||
2. The HTTP client is created with the appropriate authentication | ||
3. Service implementations are initialized | ||
4. The main client registers all services | ||
5. The client is ready to use | ||
|
||
## Async Operations | ||
|
||
Some API operations (like VM creation) are asynchronous. The SDK handles this by: | ||
|
||
1. Making the initial request that returns a task URL | ||
2. Polling the task status until completion | ||
3. Retrieving the final result when the task succeeds | ||
|
||
This is encapsulated in service methods for a clean API. | ||
|
||
## Error Handling | ||
|
||
Errors are propagated from the HTTP client to the service methods and finally to the caller. Detailed error information is available to help diagnose issues. | ||
|
||
## Logging | ||
|
||
The SDK uses a structured logger based on zap for efficient and informative logging. Log levels are automatically adjusted based on whether the SDK is in development or production mode. | ||
iButcat marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.