Example #1
0
// delete a cloud files container
func Delete(c *cli.Context) {
	// assign vars from cli args
	user := c.String("user")
	key := c.String("key")
	region := c.String("region")
	containerName := c.Args().First()
	if c.String("name") != "" {
		containerName = c.String("name")
	}

	// 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)
	}

	err2 := containers.Delete(serviceClient, containerName)
	//err2 := containers.Create(serviceClient, containerName, nil)
	fmt.Println(err2)
	fmt.Printf("%v Deleted.", containerName)
}
func TestCDNObjects(t *testing.T) {
	raxClient, err := createClient(t, false)
	th.AssertNoErr(t, err)

	createContResult := raxContainers.Create(raxClient, "gophercloud-test", nil)
	th.AssertNoErr(t, createContResult.Err)
	t.Logf("Headers from Create Container request: %+v\n", createContResult.Header)
	defer func() {
		deleteResult := raxContainers.Delete(raxClient, "gophercloud-test")
		th.AssertNoErr(t, deleteResult.Err)
	}()

	header, err := raxObjects.Create(raxClient, "gophercloud-test", "test-object", bytes.NewBufferString("gophercloud cdn test"), nil).ExtractHeader()
	th.AssertNoErr(t, err)
	t.Logf("Headers from Create Object request: %+v\n", header)
	defer func() {
		deleteResult := raxObjects.Delete(raxClient, "gophercloud-test", "test-object", nil)
		th.AssertNoErr(t, deleteResult.Err)
	}()

	raxCDNClient, err := createClient(t, true)
	th.AssertNoErr(t, err)

	enableResult := raxCDNContainers.Enable(raxCDNClient, "gophercloud-test", raxCDNContainers.EnableOpts{CDNEnabled: true, TTL: 900})
	th.AssertNoErr(t, enableResult.Err)
	t.Logf("Headers from Enable CDN Container request: %+v\n", enableResult.Header)

	deleteResult := raxCDNObjects.Delete(raxCDNClient, "gophercloud-test", "test-object", nil)
	th.AssertNoErr(t, deleteResult.Err)
	t.Logf("Headers from Delete CDN Object request: %+v\n", deleteResult.Err)
}
Example #3
0
File: delete.go Project: flazz/rack
func (command *commandDelete) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsDelete)
	containerName := params.container
	if command.Ctx.CLIContext.IsSet("purge") {
		allPages, err := objects.List(command.Ctx.ServiceClient, containerName, nil).AllPages()
		if err != nil {
			resource.Err = err
			return
		}
		objectNames, err := objects.ExtractNames(allPages)
		if err != nil {
			resource.Err = err
			return
		}
		wg := &sync.WaitGroup{}
		for _, objectName := range objectNames {
			wg.Add(1)
			go func(objectName string) {
				defer wg.Done()
				rawResponse := objects.Delete(command.Ctx.ServiceClient, containerName, objectName, nil)
				if rawResponse.Err != nil {
					resource.Err = rawResponse.Err
					return
				}
			}(objectName)
		}
		wg.Wait()
		numTimesChecked := 0
		for {
			allPages, err := objects.List(command.Ctx.ServiceClient, containerName, nil).AllPages()
			if err != nil {
				resource.Err = err
				return
			}
			objectNames, err := objects.ExtractNames(allPages)
			if err != nil {
				resource.Err = err
				return
			}
			if len(objectNames) == 0 {
				break
			}
			numTimesChecked++
			if numTimesChecked == 60 {
				resource.Err = fmt.Errorf("Purging objects from container [%s] timed out. There are still %d object left.\n", containerName, len(objectNames))
			}
			time.Sleep(5 * time.Second)
		}
	}
	rawResponse := containers.Delete(command.Ctx.ServiceClient, containerName)
	if rawResponse.Err != nil {
		resource.Err = rawResponse.Err
		return
	}
	resource.Result = fmt.Sprintf("Successfully deleted container [%s]\n", containerName)
}
Example #4
0
func TestCDNContainers(t *testing.T) {
	raxClient, err := createClient(t, false)
	th.AssertNoErr(t, err)

	createres := raxContainers.Create(raxClient, "gophercloud-test", nil)
	th.AssertNoErr(t, createres.Err)
	t.Logf("Headers from Create Container request: %+v\n", createres.Header)
	defer func() {
		res := raxContainers.Delete(raxClient, "gophercloud-test")
		th.AssertNoErr(t, res.Err)
	}()

	raxCDNClient, err := createClient(t, true)
	th.AssertNoErr(t, err)
	enableRes := raxCDNContainers.Enable(raxCDNClient, "gophercloud-test", raxCDNContainers.EnableOpts{CDNEnabled: true, TTL: 900})
	t.Logf("Header map from Enable CDN Container request: %+v\n", enableRes.Header)
	enableHeader, err := enableRes.Extract()
	th.AssertNoErr(t, err)
	t.Logf("Headers from Enable CDN Container request: %+v\n", enableHeader)

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

		names, err := raxCDNContainers.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 CDN containers listed for your current token.")
	}

	updateOpts := raxCDNContainers.UpdateOpts{XCDNEnabled: raxCDNContainers.Disabled, XLogRetention: raxCDNContainers.Enabled}
	updateHeader, err := raxCDNContainers.Update(raxCDNClient, "gophercloud-test", updateOpts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Headers from Update CDN Container request: %+v\n", updateHeader)

	getRes := raxCDNContainers.Get(raxCDNClient, "gophercloud-test")
	getHeader, err := getRes.Extract()
	th.AssertNoErr(t, err)
	t.Logf("Headers from Get CDN Container request (after update): %+v\n", getHeader)
	metadata, err := getRes.ExtractMetadata()
	t.Logf("Metadata from Get CDN Container request (after update): %+v\n", metadata)
}
Example #5
0
func (s *Swift) DeleteContainer() error {
	var err error

	objch, n := s.List()
N:
	for {
		select {
		case obj := <-objch:
			if err = s.Delete(obj.Name); err != nil {
				return err
			}
		case <-n:
			break N
		}
	}

	result := containers.Delete(s.client, s.containerName)
	return result.Err
}
Example #6
0
func TestObjects(t *testing.T) {
	c, err := createClient(t, false)
	th.AssertNoErr(t, err)

	res := raxContainers.Create(c, "gophercloud-test", nil)
	th.AssertNoErr(t, res.Err)

	defer func() {
		t.Logf("Deleting container...")
		res := raxContainers.Delete(c, "gophercloud-test")
		th.AssertNoErr(t, res.Err)
	}()

	content := bytes.NewBufferString("Lewis Carroll")
	options := &osObjects.CreateOpts{ContentType: "text/plain"}
	createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options)
	th.AssertNoErr(t, createres.Err)

	defer func() {
		t.Logf("Deleting object o1...")
		res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
		th.AssertNoErr(t, res.Err)
	}()

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

		objects, err := raxObjects.ExtractInfo(page)
		th.AssertNoErr(t, err)

		for i, object := range objects {
			t.Logf("[%02d]      name=[%s]", i, object.Name)
			t.Logf("            content-type=[%s]", object.ContentType)
			t.Logf("            bytes=[%d]", object.Bytes)
			t.Logf("            last-modified=[%s]", object.LastModified)
			t.Logf("            hash=[%s]", object.Hash)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
	if count == 0 {
		t.Errorf("No objects listed for your current token.")
	}
	t.Logf("Container Names available to the currently issued token:")
	count = 0
	err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		names, err := raxObjects.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 objects listed for your current token.")
	}

	copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"})
	th.AssertNoErr(t, copyres.Err)
	defer func() {
		t.Logf("Deleting object o2...")
		res := raxObjects.Delete(c, "gophercloud-test", "o2", nil)
		th.AssertNoErr(t, res.Err)
	}()

	o1Content, err := raxObjects.Download(c, "gophercloud-test", "o1", nil).ExtractContent()
	th.AssertNoErr(t, err)
	o2Content, err := raxObjects.Download(c, "gophercloud-test", "o2", nil).ExtractContent()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, string(o2Content), string(o1Content))

	updateres := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.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 := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}})
		th.AssertNoErr(t, res.Err)
		metadata, err := raxObjects.Get(c, "gophercloud-test", "o2", nil).ExtractMetadata()
		th.AssertNoErr(t, err)
		t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
		th.CheckEquals(t, "", metadata["White"])
	}()

	getres := raxObjects.Get(c, "gophercloud-test", "o2", nil)
	th.AssertNoErr(t, getres.Err)
	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, "mountains", metadata["White"])

	createTempURLOpts := osObjects.CreateTempURLOpts{
		Method: osObjects.GET,
		TTL:    600,
	}
	tempURL, err := raxObjects.CreateTempURL(c, "gophercloud-test", "o1", createTempURLOpts)
	th.AssertNoErr(t, err)
	t.Logf("TempURL for object (%s): %s", "o1", tempURL)
}
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")
}