func TestList(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/security-groups", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintf(w, ` { "security_groups": [ { "description": "default", "id": "85cc3048-abc3-43cc-89b3-377341426ac5", "name": "default", "security_group_rules": [], "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" } ] } `) }) count := 0 groups.List(fake.ServiceClient(), groups.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { count++ actual, err := groups.ExtractGroups(page) if err != nil { t.Errorf("Failed to extract secgroups: %v", err) return false, err } expected := []groups.SecGroup{ { Description: "default", ID: "85cc3048-abc3-43cc-89b3-377341426ac5", Name: "default", Rules: []rules.SecGroupRule{}, TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", }, } th.CheckDeepEquals(t, expected, actual) return true, nil }) if count != 1 { t.Errorf("Expected 1 page, got %d", count) } }
func TestSecurityGroupsList(t *testing.T) { client, err := clients.NewNetworkV2Client() if err != nil { t.Fatalf("Unable to create a network client: %v", err) } listOpts := groups.ListOpts{} allPages, err := groups.List(client, listOpts).AllPages() if err != nil { t.Fatalf("Unable to list groups: %v", err) } allGroups, err := groups.ExtractGroups(allPages) if err != nil { t.Fatalf("Unable to extract groups: %v", err) } for _, group := range allGroups { PrintSecurityGroup(t, &group) } }
func (os *OpenStack) ensureSecurityGroup(tenantID string) (string, error) { var securitygroup *groups.SecGroup opts := groups.ListOpts{ TenantID: tenantID, Name: securitygroupName, } pager := groups.List(os.network, opts) err := pager.EachPage(func(page pagination.Page) (bool, error) { sg, err := groups.ExtractGroups(page) if err != nil { glog.Errorf("Get openstack securitygroups error: %v", err) return false, err } if len(sg) > 0 { securitygroup = &sg[0] } return true, err }) if err != nil { return "", err } // If securitygroup doesn't exist, create a new one if securitygroup == nil { securitygroup, err = groups.Create(os.network, groups.CreateOpts{ Name: securitygroupName, TenantID: tenantID, }).Extract() if err != nil { return "", err } } var secGroupsRules int listopts := rules.ListOpts{ TenantID: tenantID, Direction: string(rules.DirIngress), SecGroupID: securitygroup.ID, } rulesPager := rules.List(os.network, listopts) err = rulesPager.EachPage(func(page pagination.Page) (bool, error) { r, err := rules.ExtractRules(page) if err != nil { glog.Errorf("Get openstack securitygroup rules error: %v", err) return false, err } secGroupsRules = len(r) return true, err }) if err != nil { return "", err } // create new rules if secGroupsRules == 0 { // create egress rule _, err = rules.Create(os.network, rules.CreateOpts{ TenantID: tenantID, SecGroupID: securitygroup.ID, Direction: rules.DirEgress, EtherType: rules.EtherType4, }).Extract() // create ingress rule _, err := rules.Create(os.network, rules.CreateOpts{ TenantID: tenantID, SecGroupID: securitygroup.ID, Direction: rules.DirIngress, EtherType: rules.EtherType4, }).Extract() if err != nil { return "", err } } return securitygroup.ID, nil }