Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions plugin/authorizer_client_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@ type gcpClient struct {
iamSvc *iam.Service
}

func genExtractZonesFn(igz map[string][]string, boundInstanceGroups []string) func(*compute.InstanceGroupAggregatedList) error {
return func(l *compute.InstanceGroupAggregatedList) error {
for k, v := range l.Items {
zone, err := zoneFromSelfLink(k)
if err != nil {
return err
}
for _, g := range v.InstanceGroups {
if strutil.StrListContains(boundInstanceGroups, g.Name) {
igz[zone] = append(igz[zone], g.Name)
}
}
}
return nil
}
}

func (c *gcpClient) InstanceGroups(ctx context.Context, project string, boundInstanceGroups []string) (map[string][]string, error) {
// map of zone names to a slice of instance group names in that zone.
igz := make(map[string][]string)

if err := c.computeSvc.InstanceGroups.
AggregatedList(project).
Fields("items/*/instanceGroups/name").
Pages(ctx, func(l *compute.InstanceGroupAggregatedList) error {
for k, v := range l.Items {
zone, err := zoneFromSelfLink(k)
if err != nil {
return err
}

for _, g := range v.InstanceGroups {
if strutil.StrListContains(boundInstanceGroups, g.Name) {
igz[zone] = append(igz[zone], g.Name)
}
}
}
return nil
}); err != nil {
Pages(ctx, genExtractZonesFn(igz, boundInstanceGroups)); err != nil {
return nil, err
}

Expand Down
34 changes: 34 additions & 0 deletions plugin/authorizer_client_gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gcpauth

import (
"testing"
"google.golang.org/api/compute/v1"
)

func TestInstanceGroups(t *testing.T) {

igz := make(map[string][]string)
boundInstanceGroups := []string{"foo-us1-baz-group"}

extractZonesFn := genExtractZonesFn(igz, boundInstanceGroups)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-newline

InstanceGroupAggregatedList := compute.InstanceGroupAggregatedList{
Items: map[string]compute.InstanceGroupsScopedList{
"zones/us-central1-c": compute.InstanceGroupsScopedList{
InstanceGroups: []*compute.InstanceGroup{&compute.InstanceGroup{Name: "foo-us1-bar-group"}, &compute.InstanceGroup{Name: "foo-us1-baz-group"}},
},
"regions/us-central1": compute.InstanceGroupsScopedList{},
},
}

err := extractZonesFn(&InstanceGroupAggregatedList)

if err != nil {
t.Fatal("error not expected")
}

if igz["us-central1-c"][0] != "foo-us1-baz-group" {
t.Fatal("expected value not found")
}
}