Skip to content

Commit

Permalink
Add support for ListUsable() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerhance committed May 5, 2021
1 parent 2e28a39 commit 01c8cc0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
91 changes: 91 additions & 0 deletions pkg/cloud/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ type {{.WrapType}} interface {
{{- if .AggregatedList}}
AggregatedList(ctx context.Context, fl *filter.F) (map[string][]*{{.FQObjectType}}, error)
{{- end}}
{{- if .ListUsable}}
ListUsable(ctx context.Context, fl *filter.F) ([]*{{.FQListUsableObjectType}}, error)
{{- end}}
{{- with .Methods -}}
{{- range .}}
{{.InterfaceFunc}}
Expand Down Expand Up @@ -349,6 +352,9 @@ type {{.MockWrapType}} struct {
{{- if .AggregatedList}}
AggregatedListError *error
{{- end}}
{{- if .ListUsable}}
ListUsableError *error
{{- end}}
// xxxHook allow you to intercept the standard processing of the mock in
// order to add your own logic. Return (true, _, _) to prevent the normal
Expand Down Expand Up @@ -377,6 +383,9 @@ type {{.MockWrapType}} struct {
{{- if .AggregatedList}}
AggregatedListHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, map[string][]*{{.FQObjectType}}, error)
{{- end}}
{{- if .ListUsable}}
ListUsableHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQListUsableObjectType}}, error)
{{- end}}
{{- with .Methods -}}
{{- range .}}
Expand Down Expand Up @@ -618,6 +627,44 @@ func (m *{{.MockWrapType}}) AggregatedList(ctx context.Context, fl *filter.F) (m
}
{{- end}}
{{- if .ListUsable}}
// List all of the objects in the mock.
func (m *{{.MockWrapType}}) ListUsable(ctx context.Context, fl *filter.F) ([]*{{.FQListUsableObjectType}}, error) {
if m.ListUsableHook != nil {
if intercept, objs, err := m.ListUsableHook(ctx, fl, m); intercept {
klog.V(5).Infof("{{.MockWrapType}}.ListUsable(%v, %v) = [%v items], %v", ctx, fl, len(objs), err)
return objs, err
}
}
m.Lock.Lock()
defer m.Lock.Unlock()
if m.ListError != nil {
err := *m.ListError
klog.V(5).Infof("{{.MockWrapType}}.ListUsable(%v, %v) = nil, %v", ctx, fl, err)
return nil, *m.ListError
}
var objs []*{{.FQListUsableObjectType}}
for _, obj := range m.Objects {
if !fl.Match(obj.To{{.VersionTitle}}()) {
continue
}
{{.Version}}Obj := obj.To{{.VersionTitle}}()
dest := &{{.FQListUsableObjectType}}{}
// Convert to Usable type to avoid separate Usable struct
if err := copyViaJSON(dest, {{.Version}}Obj); err != nil {
klog.Errorf("Could not convert %T to *{{.FQListUsableObjectType}} via JSON: %v", {{.Version}}Obj, err)
}
objs = append(objs, dest)
}
klog.V(5).Infof("{{.MockWrapType}}.ListUsable(%v, %v) = [%v items], nil", ctx, fl, len(objs))
return objs, nil
}
{{- end}}
// Obj wraps the object for use in the mock.
func (m *{{.MockWrapType}}) Obj(o *{{.FQObjectType}}) *Mock{{.Service}}Obj {
return &Mock{{.Service}}Obj{o}
Expand Down Expand Up @@ -889,6 +936,50 @@ func (g *{{.GCEWrapType}}) AggregatedList(ctx context.Context, fl *filter.F) (ma
}
{{- end}}
{{- if .ListUsable}}
// List all Usable {{.Object}} objects.
func (g *{{.GCEWrapType}}) ListUsable(ctx context.Context, fl *filter.F) ([]*{{.FQListUsableObjectType}}, error) {
klog.V(5).Infof("{{.GCEWrapType}}.ListUsable(%v, %v) called", ctx, fl)
projectID := g.s.ProjectRouter.ProjectID(ctx, "{{.Version}}", "{{.Service}}")
rk := &RateLimitKey{
ProjectID: projectID,
Operation: "ListUsable",
Version: meta.Version("{{.Version}}"),
Service: "{{.Service}}",
}
if err := g.s.RateLimiter.Accept(ctx, rk); err != nil {
return nil, err
}
klog.V(5).Infof("{{.GCEWrapType}}.ListUsable(%v, %v): projectID = %v, rk = %+v", ctx, fl, projectID, rk)
call := g.s.{{.VersionTitle}}.{{.Service}}.ListUsable(projectID)
if fl != filter.None {
call.Filter(fl.String())
}
var all []*{{.FQListUsableObjectType}}
f := func(l *{{.ObjectListUsableType}}) error {
klog.V(5).Infof("{{.GCEWrapType}}.ListUsable(%v, ..., %v): page %+v", ctx, fl, l)
all = append(all, l.Items...)
return nil
}
if err := call.Pages(ctx, f); err != nil {
klog.V(4).Infof("{{.GCEWrapType}}.ListUsable(%v, ..., %v) = %v, %v", ctx, fl, nil, err)
return nil, err
}
if klog.V(4).Enabled() {
klog.V(4).Infof("{{.GCEWrapType}}.ListUsable(%v, ..., %v) = [%v items], %v", ctx, fl, len(all), nil)
} else if klog.V(5).Enabled() {
var asStr []string
for _, o := range all {
asStr = append(asStr, fmt.Sprintf("%+v", o))
}
klog.V(5).Infof("{{.GCEWrapType}}.ListUsable(%v, ..., %v) = %v, %v", ctx, fl, asStr, nil)
}
return all, nil
}
{{- end}}
{{- with .Methods -}}
{{- range .}}
// {{.Name}} is a method on {{.GCEWrapType}}.
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloud/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
CustomOps = 1 << iota
// AggregatedList will generated a method for AggregatedList().
AggregatedList = 1 << iota
// ListUsable will generate a method for ListUsable().
ListUsable = 1 << iota

// ReadOnly specifies that the given resource is read-only and should not
// have insert() or delete() methods generated for the wrapper.
Expand Down
14 changes: 14 additions & 0 deletions pkg/cloud/meta/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func (i *ServiceInfo) ObjectAggregatedListType() string {
return fmt.Sprintf("%v.%vAggregatedList", i.Version(), i.Object)
}

// ObjectListUsableType is the compute List type for the object (contains Items field).
func (i *ServiceInfo) ObjectListUsableType() string {
return fmt.Sprintf("%v.Usable%vAggregatedList", i.version, i.Service)
}

// FQObjectType is fully qualified name of the object (e.g. compute.Instance).
func (i *ServiceInfo) FQListUsableObjectType() string {
return fmt.Sprintf("%v.Usable%v", i.Version(), i.Object)
}

// MockWrapType is the name of the concrete mock for this type.
func (i *ServiceInfo) MockWrapType() string {
return "Mock" + i.WrapType()
Expand Down Expand Up @@ -221,6 +231,10 @@ func (i *ServiceInfo) AggregatedListField() string {
return i.aggregatedListField
}

func (i *ServiceInfo) ListUsable() bool {
return i.options&ListUsable != 0
}

// ServiceGroup is a grouping of the same service but at different API versions.
type ServiceGroup struct {
Alpha *ServiceInfo
Expand Down

0 comments on commit 01c8cc0

Please sign in to comment.