From 87d7d84db2b53d3df55254a0b4e4cdfb3ba8156e Mon Sep 17 00:00:00 2001 From: Ludwik Grodzki Date: Fri, 30 Aug 2019 15:47:53 +0100 Subject: [PATCH] InstanceGroups refactor and fn for extracting zones --- plugin/authorizer_client_gcp.go | 33 +++++++++++++++------------ plugin/authorizer_client_gcp_test.go | 34 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 plugin/authorizer_client_gcp_test.go diff --git a/plugin/authorizer_client_gcp.go b/plugin/authorizer_client_gcp.go index d4ea0acf..eb70fdd5 100644 --- a/plugin/authorizer_client_gcp.go +++ b/plugin/authorizer_client_gcp.go @@ -19,6 +19,23 @@ 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) @@ -26,21 +43,7 @@ func (c *gcpClient) InstanceGroups(ctx context.Context, project string, boundIns 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 } diff --git a/plugin/authorizer_client_gcp_test.go b/plugin/authorizer_client_gcp_test.go new file mode 100644 index 00000000..bc934f15 --- /dev/null +++ b/plugin/authorizer_client_gcp_test.go @@ -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) + + + 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") + } +} \ No newline at end of file