8000 Add a `WithDomain` helper to set `domainid` fields by svanharmelen · Pull Request #112 · xanzy/go-cloudstack · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jul 26, 2021. It is now read-only.

Add a WithDomain helper to set domainid fields #112

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion cloudstack/cloudstack.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func newClient(apiurl string, apikey string, secret string, async bool, verifyss
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyssl}, // If verifyssl is true, skipping the verify should be false and vice versa
TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyssl},
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Expand Down Expand Up @@ -419,6 +419,34 @@ func getRawValue(b json.RawMessage) (json.RawMessage, error) {
return nil, fmt.Errorf("Unable to extract the raw value from:\n\n%s\n\n", string(b))
}

// DomainIDSetter is an interface that every type that can set a domain ID must implement
type DomainIDSetter interface {
SetDomainid(string)
}

// WithDomain takes either a domain name or ID and sets the `domainid` parameter
func WithDomain(domain string) OptionFunc {
return func(cs *CloudStackClient, p interface{}) error {
ps, ok := p.(DomainIDSetter)

if !ok || domain == "" {
return nil
}

if !IsID(domain) {
id, _, err := cs.Domain.GetDomainID(domain)
if err != nil {
return err
}
domain = id
}

ps.SetDomainid(domain)

return nil
}
}

// ProjectIDSetter is an interface that every type that can set a project ID must implement
type ProjectIDSetter interface {
SetProjectid(string)
Expand Down
34 changes: 31 additions & 3 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (as *allServices) GeneralCode() ([]byte, error) {
pn(" }).DialContext,")
pn(" MaxIdleConns: 100,")
pn(" IdleConnTimeout: 90 * time.Second,")
pn(" TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyssl}, // If verifyssl is true, skipping the verify should be false and vice versa")
pn(" TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifyssl},")
pn(" TLSHandshakeTimeout: 10 * time.Second,")
pn(" ExpectContinueTimeout: 1 * time.Second,")
pn(" },")
Expand Down Expand Up @@ -482,6 +482,34 @@ func (as *allServices) GeneralCode() ([]byte, error) {
pn(" return nil, fmt.Errorf(\"Unable to extract the raw value from:\\n\\n%%s\\n\\n\", string(b))")
pn("}")
pn("")
pn("// DomainIDSetter is an interface that every type that can set a domain ID must implement")
pn("type DomainIDSetter interface {")
pn(" SetDomainid(string)")
pn("}")
pn("")
pn("// WithDomain takes either a domain name or ID and sets the `domainid` parameter")
pn("func WithDomain(domain string) OptionFunc {")
pn(" return func(cs *CloudStackClient, p interface{}) error {")
pn(" ps, ok := p.(DomainIDSetter)")
pn("")
pn(" if !ok || domain == \"\" {")
pn(" return nil")
pn(" }")
pn("")
pn(" if !IsID(domain) {")
pn(" id, _, err := cs.Domain.GetDomainID(domain)")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn(" domain = id")
pn(" }")
pn("")
pn(" ps.SetDomainid(domain)")
pn("")
pn(" return nil")
pn(" }")
pn("}")
pn("")
pn("// ProjectIDSetter is an interface that every type that can set a project ID must implement")
pn("type ProjectIDSetter interface {")
pn(" SetProjectid(string)")
Expand Down Expand Up @@ -1233,8 +1261,8 @@ func (s *service) generateResponseType(a *API) {
tn := capitalize(strings.TrimPrefix(a.Name, "configure") + "Response")
ln := capitalize(strings.TrimPrefix(a.Name, "list"))

// If this is a 'list' response, we need an seperate list struct. There seem to be other
// types of responses that also need a seperate list struct, so checking on exact matches
// If this is a 'list' response, we need an separate list struct. There seem to be other
// types of responses that also need a separate list struct, so checking on exact matches
// for those once.
if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" {
pn("type %s struct {", tn)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/xanzy/go-cloudstack

go 1.12
0