示例#1
0
文件: list.go 项目: rackeric/erack
func List(c *cli.Context) {
	// assign vars from cli args
	user := c.String("user")
	key := c.String("key")
	region := c.String("region")

	// step 1, set up auth options
	ao := gophercloud.AuthOptions{
		Username: user,
		APIKey:   key,
	}
	// step 2, rax auth to get back provider instance
	//provider, err := rackspace.AuthenticatedClient(ao)
	provider, err := rackspace.AuthenticatedClient(ao)
	if err != nil {
		fmt.Println(err)
	}

	// set rax region
	serviceClient, err2 := rackspace.NewNetworkV2(provider, gophercloud.EndpointOpts{
		Name: "cloudNetworks",
		//Type: "network",
		Region: region,
	})
	if err2 != nil {
		fmt.Println(err2)
	}

	// Retrieve a pager (i.e. a paginated collection)
	networks_pager := networks.List(serviceClient)
	fmt.Println("networks_pager: ", networks_pager)

	// Define an anonymous function to be executed on each page's iteration
	err6 := networks_pager.EachPage(func(page pagination.Page) (bool, error) {
		networkList, err7 := networks.ExtractNetworks(page)
		if err7 != nil {
			fmt.Println(err7)
		}
		for _, n := range networkList {
			// "s" will be a servers.Server
			// https://github.com/rackspace/gophercloud/blob/master/openstack/compute/v2/servers/results.go
			fmt.Println(n)
		}
		return true, nil
	})
	if err6 != nil {
		fmt.Println(err6)
	}
}
示例#2
0
func TestNetworks(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	// Create a network
	n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created network: %+v\n", n)
	defer networks.Delete(client, n.ID)
	th.AssertEquals(t, n.Label, "sample_network")
	th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
	networkID := n.ID

	// List networks
	pager := networks.List(client)
	err = pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		networkList, err := networks.ExtractNetworks(page)
		th.AssertNoErr(t, err)

		for _, n := range networkList {
			t.Logf("Network: ID [%s] Label [%s] CIDR [%s]",
				n.ID, n.Label, n.CIDR)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)

	// Get a network
	if networkID == "" {
		t.Fatalf("In order to retrieve a network, the NetworkID must be set")
	}
	n, err = networks.Get(client, networkID).Extract()
	t.Logf("Retrieved Network: %+v\n", n)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
	th.AssertEquals(t, n.Label, "sample_network")
	th.AssertEquals(t, n.ID, networkID)
}