Skip to content

Commit

Permalink
Merge pull request #5540 from duobei/private/fezhan/CP-48666
Browse files Browse the repository at this point in the history
Generate Golang codes for SDK
  • Loading branch information
duobei authored Apr 18, 2024
2 parents add5d2c + 1351856 commit 25df1a8
Show file tree
Hide file tree
Showing 24 changed files with 1,672 additions and 585 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,26 @@ sdk:
ocaml/sdk-gen/c/gen_c_binding.exe \
ocaml/sdk-gen/csharp/gen_csharp_binding.exe \
ocaml/sdk-gen/java/main.exe \
ocaml/sdk-gen/powershell/gen_powershell_binding.exe
ocaml/sdk-gen/powershell/gen_powershell_binding.exe \
ocaml/sdk-gen/go/gen_go_binding.exe
dune build --profile=$(PROFILE) -f\
@ocaml/sdk-gen/c/generate \
@ocaml/sdk-gen/csharp/generate \
@ocaml/sdk-gen/java/generate \
@ocaml/sdk-gen/powershell/generate
@ocaml/sdk-gen/powershell/generate \
@ocaml/sdk-gen/go/generate
rm -rf $(XAPISDK)
mkdir -p $(XAPISDK)/c
mkdir -p $(XAPISDK)/csharp
mkdir -p $(XAPISDK)/java
mkdir -p $(XAPISDK)/powershell
mkdir -p $(XAPISDK)/python
mkdir -p $(XAPISDK)/go
cp -r _build/default/ocaml/sdk-gen/c/autogen/* $(XAPISDK)/c
cp -r _build/default/ocaml/sdk-gen/csharp/autogen/* $(XAPISDK)/csharp
cp -r _build/default/ocaml/sdk-gen/java/autogen/* $(XAPISDK)/java
cp -r _build/default/ocaml/sdk-gen/powershell/autogen/* $(XAPISDK)/powershell
cp -r _build/default/ocaml/sdk-gen/go/autogen/* $(XAPISDK)/go
cp scripts/examples/python/XenAPI/XenAPI.py $(XAPISDK)/python
sh ocaml/sdk-gen/windows-line-endings.sh $(XAPISDK)/csharp
sh ocaml/sdk-gen/windows-line-endings.sh $(XAPISDK)/powershell
Expand Down
27 changes: 27 additions & 0 deletions ocaml/sdk-gen/common/CommonFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

open Printf
open Datamodel_types
open Datamodel_utils
open Dm_api

exception Unknown_wire_protocol

Expand Down Expand Up @@ -328,3 +330,28 @@ let json_releases =
, `Float (float_of_int (List.length unique_version_bumps))
)
]

let session_id =
{
param_type= Ref Datamodel_common._session
; param_name= "session_id"
; param_doc= "Reference to a valid session"
; param_release= Datamodel_common.rio_release
; param_default= None
}

let objects =
let api = Datamodel.all_api in
(* Add all implicit messages *)
let api = add_implicit_messages api in
(* Only include messages that are visible to a XenAPI client *)
let api = filter (fun _ -> true) (fun _ -> true) on_client_side api in
(* And only messages marked as not hidden from the docs, and non-internal fields *)
let api =
filter
(fun _ -> true)
(fun f -> not f.internal_only)
(fun m -> not m.msg_hide_from_docs)
api
in
objects_of_api api
8 changes: 8 additions & 0 deletions ocaml/sdk-gen/common/CommonFunctions.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open Datamodel_types

(** Exception for unknown wire protocol. *)
exception Unknown_wire_protocol

Expand Down Expand Up @@ -129,3 +131,9 @@ val render_file : string * string -> Mustache.Json.t -> string -> string -> unit

val json_releases : Mustache.Json.t
(** JSON structure representing release information. *)

val session_id : param
(** Param of session_id. *)

val objects : obj list
(** Objects of api that generate SDKs. *)
74 changes: 74 additions & 0 deletions ocaml/sdk-gen/go/README.md
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"
)
```
24 changes: 24 additions & 0 deletions ocaml/sdk-gen/go/autogen/dune
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 .)
)
)
3 changes: 3 additions & 0 deletions ocaml/sdk-gen/go/autogen/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go/xenapi

go 1.22.0
41 changes: 41 additions & 0 deletions ocaml/sdk-gen/go/dune
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)
)
)
77 changes: 77 additions & 0 deletions ocaml/sdk-gen/go/gen_go_binding.ml
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
12 changes: 12 additions & 0 deletions ocaml/sdk-gen/go/gen_go_binding.mli
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.
*)
Loading

0 comments on commit 25df1a8

Please sign in to comment.