-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5540 from duobei/private/fezhan/CP-48666
Generate Golang codes for SDK
- Loading branch information
Showing
24 changed files
with
1,672 additions
and
585 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,74 @@ | ||
# XenServer SDK for Go | ||
|
||
Copyright (c) 2023-2024 Cloud Software Group, Inc. All Rights Reserved. | ||
|
||
XenServer SDK for Go is a complete SDK for XenServer, exposing the XenServer | ||
API as Go module. It is written in Go. | ||
|
||
XenServer SDK for Go includes a struct for every API class, and a method for each API | ||
call, so API documentation and examples written for other languages will apply | ||
equally well to Go. In particular, the SDK Guide and the Management API Guide | ||
are ideal for developers wishing to use XenServer SDK for Go. | ||
|
||
XenServer SDK for Go is free software. You can redistribute and modify it under the | ||
terms of the BSD 2-Clause license. See LICENSE.txt for details. | ||
|
||
## Reference | ||
|
||
For XenServer documentation see <https://docs.xenserver.com> | ||
|
||
The XenServer Management API Reference is available at | ||
<https://docs.xenserver.com/en-us/xenserver/8/developer/management-api> | ||
|
||
The XenServer Software Development Kit Guide is available at | ||
<https://docs.xenserver.com/en-us/xenserver/8/developer/sdk-guide> | ||
|
||
A number of examples to help you get started with the SDK is available at | ||
<https://github.com/xenserver/xenserver-samples> | ||
|
||
For community content, blogs, and downloads, visit | ||
<https://www.xenserver.com/blogs> and <https://www.citrix.com/community> | ||
|
||
To network with other developers using XenServer visit | ||
<https://discussions.citrix.com/forum/101-hypervisor-formerly-xenserver> | ||
|
||
## Prerequisites | ||
|
||
This library requires Go 1.22 or greater. | ||
|
||
## Folder Structure | ||
|
||
This archive contains the following folders that are relevant to Go developers: | ||
|
||
- `XenServerGo\src`: contains the Go source files can be used as the local module in a Go project. | ||
|
||
## Getting Started | ||
|
||
Extract the contents of this archive. | ||
|
||
A. To set up the local go module: | ||
|
||
1. Create a new folder in your Go project, eg. `XenServerGo` | ||
2. Copy all files in `XenServerGo\src` to the new folder | ||
|
||
B. To use the XenServer module for Go in your Go project: | ||
|
||
1. Add the following lines to your go.mod file: | ||
|
||
``` | ||
replace <project-package-name>/XenServerGo => ./XenServerGo | ||
``` | ||
|
||
2. Run the command: | ||
|
||
``` | ||
go mod tidy | ||
``` | ||
|
||
3. Use the XenServer module for Go in file as follows: | ||
|
||
``` | ||
import ( | ||
xenapi "<project-package-name>/XenServerGo" | ||
) | ||
``` |
This file contains 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,24 @@ | ||
(rule | ||
(targets LICENSE) | ||
(deps | ||
../../LICENSE | ||
) | ||
(action (copy %{deps} %{targets})) | ||
) | ||
|
||
(rule | ||
(targets README) | ||
(deps | ||
../README.md | ||
) | ||
(action (copy %{deps} %{targets})) | ||
) | ||
|
||
(alias | ||
(name generate) | ||
(deps | ||
LICENSE | ||
README | ||
(source_tree .) | ||
) | ||
) |
This file contains 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,3 @@ | ||
module go/xenapi | ||
|
||
go 1.22.0 |
This file contains 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,41 @@ | ||
(executable | ||
(modes exe) | ||
(name gen_go_binding) | ||
(modules gen_go_binding) | ||
(libraries | ||
CommonFunctions | ||
mustache | ||
xapi-datamodel | ||
xapi-stdext-unix | ||
gen_go_helper | ||
) | ||
) | ||
|
||
(library | ||
(name gen_go_helper) | ||
(modules gen_go_helper) | ||
(libraries | ||
CommonFunctions | ||
mustache | ||
xapi-datamodel | ||
) | ||
) | ||
|
||
(rule | ||
(alias generate) | ||
(deps | ||
(:x gen_go_binding.exe) | ||
(source_tree templates) | ||
) | ||
(action (run %{x} --destdir autogen)) | ||
) | ||
|
||
(test | ||
(name test_gen_go) | ||
(modules test_gen_go) | ||
(libraries alcotest xapi-test-utils gen_go_helper) | ||
(deps | ||
(source_tree test_data) | ||
(source_tree templates) | ||
) | ||
) |
This file contains 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,77 @@ | ||
(* Copyright (c) Cloud Software Group, Inc. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published | ||
by the Free Software Foundation; version 2.1 only. with the special | ||
exception on linking described in file LICENSE. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
*) | ||
|
||
open CommonFunctions | ||
open Gen_go_helper | ||
|
||
let render_enums enums destdir = | ||
let header = | ||
render_template "FileHeader.mustache" | ||
(`O [("modules", `Null)]) | ||
~newline:true () | ||
in | ||
let enums = render_template "Enum.mustache" enums () |> String.trim in | ||
let rendered = header ^ enums ^ "\n" in | ||
generate_file ~rendered ~destdir ~output_file:"enums.go" | ||
|
||
let render_api_messages_and_errors destdir = | ||
let obj = | ||
`O | ||
[ | ||
("api_errors", `A Json.api_errors) | ||
; ("api_messages", `A Json.api_messages) | ||
; ("modules", `Null) | ||
] | ||
in | ||
let header = render_template "FileHeader.mustache" obj ~newline:true () in | ||
let error_rendered = | ||
header ^ render_template "APIErrors.mustache" obj ~newline:true () | ||
in | ||
let messages_rendered = | ||
header ^ render_template "APIMessages.mustache" obj ~newline:true () | ||
in | ||
generate_file ~rendered:error_rendered ~destdir ~output_file:"api_errors.go" ; | ||
generate_file ~rendered:messages_rendered ~destdir | ||
~output_file:"api_messages.go" | ||
|
||
let main destdir = | ||
render_api_messages_and_errors destdir ; | ||
let enums = Json.all_enums objects in | ||
render_enums enums destdir ; | ||
let objects = Json.xenapi objects in | ||
List.iter | ||
(fun (name, obj) -> | ||
let header_rendered = | ||
render_template "FileHeader.mustache" obj ~newline:true () | ||
in | ||
let record_rendered = render_template "Record.mustache" obj () in | ||
let rendered = header_rendered ^ record_rendered in | ||
let output_file = name ^ ".go" in | ||
generate_file ~rendered ~destdir ~output_file | ||
) | ||
objects | ||
|
||
let _ = | ||
let destdir = ref "." in | ||
Arg.parse | ||
[ | ||
( "--destdir" | ||
, Arg.Set_string destdir | ||
, "the destination directory for the generated files" | ||
) | ||
] | ||
(fun x -> Printf.fprintf stderr "Ignoring unknown parameter: %s\n%!" x) | ||
"Generates Go SDK." ; | ||
let destdir = !destdir // "src" in | ||
Xapi_stdext_unix.Unixext.mkdir_rec destdir 0o755 ; | ||
main destdir |
This file contains 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,12 @@ | ||
(* Copyright (c) Cloud Software Group, Inc. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published | ||
by the Free Software Foundation; version 2.1 only. with the special | ||
exception on linking described in file LICENSE. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
*) |
Oops, something went wrong.