Skip to content
Permalink
Browse files
Merge pull request #112 from xanzy/svh/f-with-domain
Add a `WithDomain` helper to set `domainid` fields
  • Loading branch information
Sander van Harmelen committed Apr 23, 2019
2 parents e82309d + bd15592 commit b6f53ed3282d22b6446422879587b0a78e1b0f11
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
@@ -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,
},
@@ -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)
@@ -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(" },")
@@ -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)")
@@ -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)
3 go.mod
@@ -0,0 +1,3 @@
module github.com/xanzy/go-cloudstack

go 1.12

0 comments on commit b6f53ed

Please sign in to comment.