Example #1
0
// print list of cloud files containers to stdout
func GetList(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)
	if err != nil {
		fmt.Println(err)
	}

	// set rax region
	serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		fmt.Println(err)
	}

	// _, err := containers.Get(serviceClient, "{containerName}").ExtractMetadata()

	err3 := containers.List(serviceClient, nil).EachPage(func(page pagination.Page) (bool, error) {
		containerList, err4 := containers.ExtractNames(page)
		// https://github.com/rackspace/gophercloud/blob/master/openstack/blockstorage/v1/volumes/results.go
		for _, v := range containerList {
			fmt.Println(v)
		}
		if err4 != nil {
			fmt.Println(err4)
		}
		return true, nil
	})
	if err3 != nil {
		fmt.Println(err3)
	}
}
func TestContainers(t *testing.T) {
	c, err := createClient(t, false)
	th.AssertNoErr(t, err)

	t.Logf("Containers Info available to the currently issued token:")
	count := 0
	err = raxContainers.List(c, &osContainers.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		containers, err := raxContainers.ExtractInfo(page)
		th.AssertNoErr(t, err)

		for i, container := range containers {
			t.Logf("[%02d]      name=[%s]", i, container.Name)
			t.Logf("            count=[%d]", container.Count)
			t.Logf("            bytes=[%d]", container.Bytes)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
	if count == 0 {
		t.Errorf("No containers listed for your current token.")
	}

	t.Logf("Container Names available to the currently issued token:")
	count = 0
	err = raxContainers.List(c, &osContainers.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		names, err := raxContainers.ExtractNames(page)
		th.AssertNoErr(t, err)

		for i, name := range names {
			t.Logf("[%02d] %s", i, name)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
	if count == 0 {
		t.Errorf("No containers listed for your current token.")
	}

	createres := raxContainers.Create(c, "gophercloud-test", nil)
	th.AssertNoErr(t, createres.Err)
	defer func() {
		res := raxContainers.Delete(c, "gophercloud-test")
		th.AssertNoErr(t, res.Err)
	}()

	updateres := raxContainers.Update(c, "gophercloud-test", raxContainers.UpdateOpts{Metadata: map[string]string{"white": "mountains"}})
	th.AssertNoErr(t, updateres.Err)
	t.Logf("Headers from Update Account request: %+v\n", updateres.Header)
	defer func() {
		res := raxContainers.Update(c, "gophercloud-test", raxContainers.UpdateOpts{Metadata: map[string]string{"white": ""}})
		th.AssertNoErr(t, res.Err)
		metadata, err := raxContainers.Get(c, "gophercloud-test").ExtractMetadata()
		th.AssertNoErr(t, err)
		t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
		th.CheckEquals(t, metadata["White"], "")
	}()

	getres := raxContainers.Get(c, "gophercloud-test")
	t.Logf("Headers from Get Account request (after update): %+v\n", getres.Header)
	metadata, err := getres.ExtractMetadata()
	th.AssertNoErr(t, err)
	t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
	th.CheckEquals(t, metadata["White"], "mountains")
}