Skip to content
Permalink
Browse files
Merge pull request #124 from cezarsa/withzone
Add WithZone function to filter resources from a zone
  • Loading branch information
svanharmelen committed Nov 12, 2019
2 parents 923541c + 3abb05f commit 23b392287df62f59d4859a4b34a8fa0ad39c53d2
Showing 2 changed files with 56 additions and 0 deletions.
@@ -525,6 +525,34 @@ func WithVPCID(id string) OptionFunc {
}
}

// ZoneIDSetter is an interface that every type that can set a zone ID must implement
type ZoneIDSetter interface {
SetZoneid(string)
}

// WithZone takes either a zone name or ID and sets the `zoneid` parameter
func WithZone(zone string) OptionFunc {
return func(cs *CloudStackClient, p interface{}) error {
zs, ok := p.(ZoneIDSetter)

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

if !IsID(zone) {
id, _, err := cs.Zone.GetZoneID(zone)
if err != nil {
return err
}
zone = id
}

zs.SetZoneid(zone)

return nil
}
}

type APIDiscoveryService struct {
cs *CloudStackClient
}
@@ -600,6 +600,34 @@ func (as *allServices) GeneralCode() ([]byte, error) {
pn(" }")
pn("}")
pn("")
pn("// ZoneIDSetter is an interface that every type that can set a zone ID must implement")
pn("type ZoneIDSetter interface {")
pn(" SetZoneid(string)")
pn("}")
pn("")
pn("// WithZone takes either a zone name or ID and sets the `zoneid` parameter")
pn("func WithZone(zone string) OptionFunc {")
pn(" return func(cs *CloudStackClient, p interface{}) error {")
pn(" zs, ok := p.(ZoneIDSetter)")
pn("")
pn(" if !ok || zone == \"\" {")
pn(" return nil")
pn(" }")
pn("")
pn(" if !IsID(zone) {")
pn(" id, _, err := cs.Zone.GetZoneID(zone)")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn(" zone = id")
pn(" }")
pn("")
pn(" zs.SetZoneid(zone)")
pn("")
pn(" return nil")
pn(" }")
pn("}")
pn("")
for _, s := range as.services {
pn("type %s struct {", s.name)
pn(" cs *CloudStackClient")

0 comments on commit 23b3922

Please sign in to comment.