-
Notifications
You must be signed in to change notification settings - Fork 292
CP-47357: Add a Go JSON-RPC client file #5615
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
CP-47357: Add a Go JSON-RPC client file #5615
Conversation
21c0782
to
69fc086
Compare
Is this the one needed for testing in the CI? |
Yes, this is the JSON HTTP RPC client for Go SDK, we will add component test in following PRs in CI. Besides we also test the SDK functionalities in XenRT with https://github.com/xenserver/xenserver-samples/tree/master/go. |
Would you please comment the test for this code here, either manual(snapshot) or XenRT(job) is ok, thanks (To indicate the current version of code passed the test). |
For Golang lint checking. root@3eea0d3e0e5d:/app/go/goSDK# golangci-lint run --config=/app/.golangci.yml
WARN The linter 'gomnd' is deprecated (since v1.58.0) due to: The linter has been renamed. Replaced by mnd.
WARN The linter 'execinquery' is deprecated (since v1.58.0) due to: The repository of the linter has been archived by the owner.
0 issues. XenRT job base on 874f9a6 with this change: |
} | ||
|
||
func convertUnhandledJSONData(jsonBytes []byte) []byte { | ||
jsonString := string(jsonBytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting the bytes
to string
and then back to bytes
may not be clear here.
What I'm concerning is the encoding in the converting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When converting byte slices ([]byte) to strings in Go, encoding is not directly involved. Strings in Go are read-only slices of bytes, we can directly convert a []byte (byte slice) to a string. we can also use:
s := fmt.Sprintf("%s", b)
|
||
request.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
request.Header.Set("Accept", "application/json") | ||
request.Header.Set("User-Agent", "XenAPI/" + APIVersionLatest.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like XenAPI Go SDK/24.17.0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, ming. Here I just follow the code in XC https://code.citrite.net/projects/XSIF/repos/xencenter/browse/XenModel/XenAPI/Session.cs#47.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, ming. Here I just follow the code in XC https://code.citrite.net/users/xueqingz/repos/xencenter/browse/XenModel/XenAPI/Session.cs#47.
Access is denied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, update link.
headers: make(map[string]string), | ||
} | ||
|
||
if strings.HasPrefix(opts.URL, "https://") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "https://pkg.go.dev/net/url#Parse" could be used to parse a raw URL.
caCertPool := x509.NewCertPool() | ||
certs := []tls.Certificate{} | ||
if opts.SecureOpts != nil { | ||
skipVerify = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this should be false
iif when the CA
is presented. Otherwise, the CA
is not required.
So it should be moved into if opts.SecureOpts.ServerCert != "" {
certs = []tls.Certificate{cert} | ||
} | ||
} | ||
tlsConfig := &tls.Config{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the tls.Config
, the CipherSuites
needs to be set explicitly for the sake of security.
It would be better to set the TLS version explicitly as well.
if err != nil { | ||
log.Fatal(err) | ||
} | ||
caCertPool.AppendCertsFromPEM(caCert) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return needs to be cared. An invalid cert can't be ignored.
func paramsParse(params ...interface{}) interface{} { | ||
var finalParams interface{} | ||
finalParams = params | ||
if len(params) == 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could some comments be added to explain why this is required when there is only one parameter
?
And why is a finalParams
required here, as it gets a copy of a slice, which means it actually refers to the same memory location of params
? This is because I understand the params
is a slice rather than an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The paramParse is to handle different types of parameter support way in method. eg.
Examples:
Examples:
1.Nil: sendCall(ctx, "method_name") -> {"method": "method_name"}
2.Single value (simple type): sendCall(ctx, "method_name", 1) -> {"method": "method_name", "params": [1]}
3.Single value (slice/array): sendCall(ctx, "method_name", []int{1, 2, 3}) -> {"method": "method_name", "params": [1, 2, 3]}
4.Single value (struct): sendCall(ctx, "method_name", &Person{Name: "Alex", Age: 35}) -> {"method": "method_name", "params": {"name": "Alex", "age": 35}}
5.Multiple values: sendCall(ctx, "method_name", 1, 2, 3) -> {"method": "method_name", "params": [1, 2, 3]}
For case 3,4 ,we need to set finalParams = params[0].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll add some comments on code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion about details , we think it's actually not needed, we only need to support 1, 2,5. remove this function.
Signed-off-by: xueqingz <xueqing.zhang@citrix.com>
Signed-off-by: xueqingz <xueqing.zhang@citrix.com>
69fc086
to
890ae8d
Compare
checking golang lint on new commit:
Test for review changes:
Try run xenserver-samples test manually, no special error found. |
pytype_reporter extracted 50 problem reports from pytype output. You can check the results of the job here |
No description provided.