-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add MachineAudit support #104
Comments
Hi @cheapRoc, I am working on this and a Pull Request would be coming. But, before I open one, I have a question about a design of the client API concerning handling nested objects. As per: https://apidocs.joyent.com/cloudapi/#MachineAudit; the results include Therefore, we have these two possible options:
type Audit struct {
Action string `json:"action"`
Success string `json:"success"`
Time time.Time `json:"time"`
Caller map[string]string `json:"caller"`
}
(Note: the type AuditCaller struct {
Type string `json:"type"`
User string `json:"user,omitempty"`
IP string `json:"ip,omitempty"`
KeyID string `json:"keyId,omitempty"`
} type Audit struct {
Action string `json:"action"`
Success string `json:"success"`
Time time.Time `json:"time"`
Caller AuditCaller `json:"caller"`
} In the case of using a map as per (1), the fields which are not set in the response are not going to be present in the resulting map, for example:
But, in the case of using nested struct, the missing fields would be set to their respective zero-values (this is a side-effect of using nested structs when de-marshalling JSON), for example:
This happens regardless of the JSON attribute We don't seem to be using string pointers (like for example the AWS SDK for Go), albeit it would have a very similar result as the zero-value for a pointer in Go is
The one obvious side effect of using string pointers, would be more work for the user using the client, who would have to make provisions to check for Therefore, my question here would be - what would be the best way to handle nested objects in the response? Especially, since there are already different approaches to handling such things in the code. |
I'd like to hear @sean-'s input as well but I'd prefer nested structs and leveraging zero values. I'm "ok" with empty strings as default values only because they're generally expected by a majority of Go consumers. We have had consumer issues in the past with zero values but I believe those cases were due to using them where they weren't expected before. Does that make sense? Try to keep note and maybe open a new issue if we're doing things different across |
Also to add, this should be similar to how we handle machine CNS tags (here) |
@cheapRoc Yes, definitely nested structs over type AuditCaller struct {
Type string `json:"type"`
RawUser *string `json:"user,omitempty"`
RawIP *string `json:"ip,omitempty"`
RawKeyID *string `json:"keyId,omitempty"`
}
func (ac *AuditCaller) IP() (string, bool) {
if ac.RawIP == nil {
return "", false
}
return *ac.RawIP, true
} Doing that repeatedly at the call sites is irritating, at best. For instance, this forces the implementation onto the caller: if ac.IP == nil {
return foo
}
return fmt.Sprintf("something %s", *ac.IP) Because we're inclined to change and improve things in the future, I'd even go so far as to consider: // auditCaller is the struct that we decode into before foisting the values into AuditCaller
type auditCaller struct {
Type string `json:"type"`
User *string `json:"user,omitempty"`
IP *string `json:"ip,omitempty"`
KeyID *string `json:"keyId,omitempty"`
}
// AuditCaller represents the interface of
type AuditCaller struct {
raw auditCaller
}
// IP returns the IP of the caller. If the IP was not set the second argument will be false.
func (ac *AuditCaller) IP() (string, bool) {
if ac.raw.IP == nil {
return "", false
}
return *ac.raw.IP, true
} And again, if this weren't an exported API, I wouldn't care so much about adding the abstraction, but since it is, I'm erring on the side of decoupling the library (which we own and control) from the consuming applications (which we don't). We loose something when we round-trip an wtb |
@kwilczynski SGTM, let us know! |
Ref: #25
Support the
MachineAudit
endpoint in CloudAPI.The text was updated successfully, but these errors were encountered: