Skip to content

Commit 1b137a2

Browse files
authored
Merge pull request #11 from vatesfr/feature/v2-initial-modules-rest-client
2 parents a1f77ef + 6998431 commit 1b137a2

29 files changed

+2677
-98
lines changed

.github/workflows/lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ jobs:
2323
uses: actions/setup-go@v5
2424
with:
2525
# The versions of golangci-lint and setup-go here cross-depend and need to update together.
26-
go-version: '1.23'
26+
go-version: '1.24'
2727
# Either this action or golangci-lint needs to disable the cache
2828
cache: false
2929
- run: go mod tidy
3030
- name: golangci-lint
3131
uses: golangci/golangci-lint-action@v6
3232
with:
33-
version: v1.63.2
33+
version: v1.64.7

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ dist/
3939
.env
4040
.envrc
4141
.autoenv.zsh
42+
43+
# goenv version file
44+
.go-version

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ linters-settings:
2222
- structtag
2323
run:
2424
timeout: 20m
25+
go: '1.24'

Makefile

+75-51
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,92 @@
1-
SHELL := /bin/bash
1+
PROJECT_NAME := xenorchestra-go-sdk
2+
GO := go
3+
GOFLAGS :=
4+
MOCKGEN_VERSION := 1.6.0
25

3-
.DEFAULT_GOAL := all
4-
.PHONY: all
5-
all: ## build pipeline
6-
all: mod gen spell lint test
7-
8-
.PHONY: precommit
9-
precommit: ## validate the branch before commit
10-
precommit: all vuln
6+
# Colors
7+
BLUE := \033[0;34m
8+
GREEN := \033[0;32m
9+
YELLOW := \033[0;33m
10+
RED := \033[0;31m
11+
NC := \033[0m # No Color
1112

12-
.PHONY: ci
13-
ci: ## CI build pipeline
14-
ci: precommit diff
13+
.PHONY: all
14+
all: mod test lint
1515

1616
.PHONY: help
1717
help:
18-
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
19-
20-
.PHONY: clean
21-
clean: ## remove files created during build pipeline
22-
$(call print-target)
23-
rm -rf dist
24-
rm -f coverage.*
25-
rm -f '"$(shell go env GOCACHE)/../golangci-lint"'
26-
go clean -i -cache -testcache -modcache -fuzzcache -x
18+
@echo "$(BLUE)Makefile for $(PROJECT_NAME)$(NC)"
19+
@echo ""
20+
@echo "$(YELLOW)Usage:$(NC)"
21+
@echo " make $(GREEN)<target>$(NC)"
22+
@echo ""
23+
@echo "$(YELLOW)Targets:$(NC)"
24+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-15s$(NC) %s\n", $$1, $$2}'
2725

2826
.PHONY: mod
29-
mod: ## go mod tidy
30-
$(call print-target)
31-
go mod tidy
27+
mod:
28+
@echo "$(BLUE)Running go mod tidy...$(NC)"
29+
$(GO) mod tidy
30+
31+
.PHONY: test
32+
test:
33+
@echo "$(BLUE)Running tests...$(NC)"
34+
$(GO) test -race -covermode=atomic -coverprofile=coverage.out ./...
35+
$(GO) tool cover -html=coverage.out -o coverage.html
36+
@echo "$(GREEN)Coverage report generated at coverage.html$(NC)"
3237

33-
.PHONY: gen
34-
gen: ## go generate
35-
$(call print-target)
36-
go generate ./...
38+
.PHONY: test-v1
39+
test-v1:
40+
@echo "$(BLUE)Running v1 client tests...$(NC)"
41+
$(GO) test -race ./client/...
3742

38-
.PHONY: spell
39-
spell: ## misspell
40-
$(call print-target)
41-
misspell -error -locale=US -w **.md
43+
.PHONY: test-v2
44+
test-v2:
45+
@echo "$(BLUE)Running v2 client tests...$(NC)"
46+
$(GO) test -race ./v2/... ./pkg/... ./internal/...
4247

4348
.PHONY: lint
44-
lint: ## golangci-lint
45-
$(call print-target)
49+
lint:
50+
@echo "$(BLUE)Running linter...$(NC)"
4651
golangci-lint run
4752

48-
.PHONY: vuln
49-
vuln: ## govulncheck
50-
$(call print-target)
51-
govulncheck ./...
53+
.PHONY: install-mockgen
54+
install-mockgen:
55+
@echo "$(BLUE)Installing mockgen v$(MOCKGEN_VERSION)...$(NC)"
56+
$(GO) install github.com/golang/mock/mockgen@v$(MOCKGEN_VERSION)
5257

53-
.PHONY: test
54-
test: ## go test
55-
$(call print-target)
56-
go test -race -covermode=atomic -coverprofile=coverage.out -coverpkg=./... ./...
57-
go tool cover -html=coverage.out -o coverage.html
58+
.PHONY: mock
59+
mock: install-mockgen
60+
@echo "$(BLUE)Generating mocks...$(NC)"
61+
$(GO) generate ./...
62+
63+
.PHONY: run-example-v1
64+
run-example-v1:
65+
@echo "$(BLUE)Running v1 example...$(NC)"
66+
$(GO) run ./examples/v1/user_demo.go
5867

59-
.PHONY: diff
60-
diff: ## git diff
61-
$(call print-target)
62-
git diff --exit-code
63-
RES=$$(git status --porcelain) ; if [ -n "$$RES" ]; then echo $$RES && exit 1 ; fi
68+
.PHONY: run-example-v2
69+
run-example-v2:
70+
@echo "$(BLUE)Running v2 example...$(NC)"
71+
$(GO) run ./examples/v2/user_demo.go
6472

73+
.PHONY: run-examples
74+
run-examples: run-example-v1 run-example-v2
75+
@echo "$(GREEN)All examples executed successfully$(NC)"
76+
77+
.PHONY: clean
78+
clean:
79+
@echo "$(BLUE)Cleaning build artifacts...$(NC)"
80+
rm -f coverage.out coverage.html
81+
$(GO) clean -cache -testcache
6582

66-
define print-target
67-
@printf "Executing target: \033[36m$@\033[0m\n"
68-
endef
83+
.PHONY: vuln
84+
vuln:
85+
@echo "$(BLUE)Checking for vulnerabilities...$(NC)"
86+
govulncheck ./...
87+
88+
.PHONY: precommit
89+
precommit:
90+
@echo "$(BLUE)Running pre-commit checks...$(NC)"
91+
pre-commit run --all-files
92+
@echo "$(GREEN)Pre-commit checks passed!$(NC)"

README.md

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
11
# <p align="center">Golang client for XenOrchestra API</p>
22

3-
This is a Golang module for the [XenOrchestra](https://github.com/vatesfr/xen-orchestra) JSON-RPC API.
3+
This is a Golang module for the [XenOrchestra](https://github.com/vatesfr/xen-orchestra) API. It provides two client implementations:
44

5-
It is used in the [terraform-provider-xenorchestra](https://github.com/vatesfr/terraform-provider-xenorchestra) Terraform provider.
5+
- **v1**: Uses the JSON-RPC API (legacy)
6+
- **v2**: Uses the REST API (WIP, should be used in parallel with v1 for missing endpoints, until v2 is fully released)
67

78
## 📚 Documentation
89

9-
TODO
10+
### v1 Documentation
11+
12+
The v1 client uses the JSON-RPC API and is primarily used in the [terraform-provider-xenorchestra](https://github.com/vatesfr/terraform-provider-xenorchestra) Terraform provider.
13+
14+
### v2 Documentation
15+
16+
The v2 client uses the REST API and provides a more modern, type-safe interface. Comprehensive documentation is available in the `docs/v2` directory:
17+
18+
- [Overview](docs/v2/01-overview.md) - Introduction and key features
19+
- [Architecture](docs/v2/02-architecture.md) - Design patterns and components
20+
- [Migration Guide](docs/v2/03-migration-guide.md) - How to migrate from v1 to v2
21+
- [Service Implementation Guide](docs/v2/04-service-implementation.md) - How to add new services
1022

1123
## 🧑🏻‍💻 Usage
1224

1325
```shell
1426
go get github.com/vatesfr/xenorchestra-go-sdk
1527
```
1628

17-
See [examples](https://github.com/vatesfr/xenorchestra-go-sdk/tree/main/examples).
29+
### Examples
1830

31+
The SDK includes examples for both v1 and v2 clients:
32+
33+
- [v1 Examples](examples/v1) - Examples using the JSON-RPC API
34+
- [v2 Examples](examples/v2) - Examples using the REST API
1935

2036
## 🍰 Contributing
2137

2238
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
2339

40+
### Development
41+
42+
This project includes a Makefile to help with common development tasks:
43+
44+
```shell
45+
# Run tests
46+
make test
47+
48+
# Run specific client tests
49+
make test-v1
50+
make test-v2
51+
52+
# Run linter
53+
make lint
54+
55+
# Generate mocks
56+
make mock
57+
58+
# Run examples
59+
make example-v1
60+
make example-v2
61+
62+
# See all available commands
63+
make help
64+
```
65+
2466
## License
2567

2668
MIT License

docs/v2/01-overview.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Xen Orchestra Go SDK v2
2+
3+
## Overview
4+
5+
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.
6+
7+
## Key Features
8+
9+
- **REST API Support**: Uses Xen Orchestra's modern REST API
10+
- **Type Safety**: Leverages Go generics for type-safe API interactions
11+
- **Method Chaining**: Improved API ergonomics with `client.VM().Create()` pattern
12+
- **Context Support**: All operations support context for better timeout and cancellation handling
13+
- **UUID Support**: Native UUID type support instead of string IDs
14+
- **Structured Logging**: Built-in logging with configurable verbosity levels
15+
- **Interface-Based Design**: Clean interfaces for better testability and mocking
16+
- **Go Mocking**: Mocking is supported for all interfaces by adding the `//go:generate mockgen` tag in the interface file
17+
18+
## Installation
19+
20+
```bash
21+
go get github.com/vatesfr/xenorchestra-go-sdk/v2
22+
```
23+
24+
## Quick Start
25+
26+
```go
27+
package main
28+
29+
import (
30+
"context"
31+
"fmt"
32+
"time"
33+
34+
"github.com/vatesfr/xenorchestra-go-sdk/pkg/config"
35+
v2 "github.com/vatesfr/xenorchestra-go-sdk/v2"
36+
)
37+
38+
func main() {
39+
// Create a context with timeout
40+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
41+
defer cancel()
42+
43+
// Initialize configuration
44+
cfg, err := config.New()
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
// Create client
50+
client, err := v2.New(cfg)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
// List all VMs
56+
vms, err := client.VM().List(ctx)
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
// Display VMs
62+
fmt.Printf("Found %d VMs\n", len(vms))
63+
for i, vm := range vms {
64+
fmt.Printf("%d. %s (ID: %s, Power: %s)\n", i+1, vm.NameLabel, vm.ID, vm.PowerState)
65+
}
66+
}
67+
```
68+
69+
## Environment Variables
70+
71+
The SDK uses the following environment variables for configuration:
72+
73+
- `XOA_URL`: URL of the Xen Orchestra server
74+
- `XOA_USER`: Username for authentication
75+
- `XOA_PASSWORD`: Password for authentication
76+
- `XOA_INSECURE`: Set to "true" to skip TLS certificate verification
77+
- `XOA_DEVELOPMENT`: Set to "true" to enable development mode with additional logging
78+
- `XOA_RETRY_MODE`: Retry strategy ("none" or "backoff")
79+
- `XOA_RETRY_MAX_TIME`: Maximum time to wait between retries (default: 5 minutes)
80+
81+
## Next Steps
82+
83+
- [Architecture Guide](02-architecture.md) - Learn about the design patterns used in the SDK
84+
- [Migration Guide](03-migration-guide.md) - Migrate from v1 to v2
85+
- [Service Implementation Guide](04-service-implementation.md) - Learn how to add new services

0 commit comments

Comments
 (0)