Merge pull request #112 from xanzy/svh/f-with-domain
Add a `WithDomain` helper to set `domainid` fields
diff --git a/cloudstack/cloudstack.go b/cloudstack/cloudstack.go
index 04bbac3..e3dcb4e 100644
--- a/cloudstack/cloudstack.go
+++ b/cloudstack/cloudstack.go
@@ -156,7 +156,7 @@
}).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 @@
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)
diff --git a/generate/generate.go b/generate/generate.go
index fa641b6..fcdbd3a 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -285,7 +285,7 @@
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 @@
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 @@
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)
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..ad96090
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module github.com/xanzy/go-cloudstack
+
+go 1.12