Skip to content

Commit

Permalink
Add WithZone function to filter resources from a zone
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Nov 12, 2019
1 parent 923541c commit 3abb05f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cloudstack/cloudstack.go
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions generate/generate.go
Expand Up @@ -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")
Expand Down

0 comments on commit 3abb05f

Please sign in to comment.