Ejemplo n.º 1
0
Archivo: delete.go Proyecto: 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)
}
Ejemplo n.º 2
0
func (s *Swift) List() (objch chan objects.Object, n chan int) {
	objch = make(chan objects.Object)
	n = make(chan int)

	go func() {
		pager := objects.List(s.client, s.containerName, objects.ListOpts{
			Full: true,
		})

		i := 0
		pager.EachPage(func(page pagination.Page) (bool, error) {
			objlist, err := objects.ExtractInfo(page)
			if err != nil {
				log.Debugf("%v\n", err)
				return false, err
			}

			for _, obj := range objlist {
				objch <- obj
				i++
			}

			return true, nil
		})

		n <- i
	}()

	return objch, n
}
Ejemplo n.º 3
0
func TestObjects(t *testing.T) {
	// Create a provider client for executing the HTTP request.
	// See common.go for more information.
	client := newClient(t)

	// Make a slice of length numObjects to hold the random object names.
	oNames := make([]string, numObjects)
	for i := 0; i < len(oNames); i++ {
		oNames[i] = tools.RandomString("test-object-", 8)
	}

	// Create a container to hold the test objects.
	cName := tools.RandomString("test-container-", 8)
	header, err := containers.Create(client, cName, nil).ExtractHeader()
	th.AssertNoErr(t, err)
	t.Logf("Create object headers: %+v\n", header)

	// Defer deletion of the container until after testing.
	defer func() {
		res := containers.Delete(client, cName)
		th.AssertNoErr(t, res.Err)
	}()

	// Create a slice of buffers to hold the test object content.
	oContents := make([]*bytes.Buffer, numObjects)
	for i := 0; i < numObjects; i++ {
		oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
		res := objects.Create(client, cName, oNames[i], oContents[i], nil)
		th.AssertNoErr(t, res.Err)
	}
	// Delete the objects after testing.
	defer func() {
		for i := 0; i < numObjects; i++ {
			res := objects.Delete(client, cName, oNames[i], nil)
			th.AssertNoErr(t, res.Err)
		}
	}()

	ons := make([]string, 0, len(oNames))
	err = objects.List(client, cName, &objects.ListOpts{Full: false, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
		names, err := objects.ExtractNames(page)
		th.AssertNoErr(t, err)
		ons = append(ons, names...)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.AssertEquals(t, len(ons), len(oNames))

	ois := make([]objects.Object, 0, len(oNames))
	err = objects.List(client, cName, &objects.ListOpts{Full: true, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) {
		info, err := objects.ExtractInfo(page)
		th.AssertNoErr(t, err)

		ois = append(ois, info...)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.AssertEquals(t, len(ois), len(oNames))

	// Copy the contents of one object to another.
	copyres := objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]})
	th.AssertNoErr(t, copyres.Err)

	// Download one of the objects that was created above.
	o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent()
	th.AssertNoErr(t, err)

	// Download the another object that was create above.
	o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent()
	th.AssertNoErr(t, err)

	// Compare the two object's contents to test that the copy worked.
	th.AssertEquals(t, string(o2Content), string(o1Content))

	// Update an object's metadata.
	updateres := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata})
	th.AssertNoErr(t, updateres.Err)

	// Delete the object's metadata after testing.
	defer func() {
		tempMap := make(map[string]string)
		for k := range metadata {
			tempMap[k] = ""
		}
		res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
		th.AssertNoErr(t, res.Err)
	}()

	// Retrieve an object's metadata.
	om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata()
	th.AssertNoErr(t, err)
	for k := range metadata {
		if om[k] != metadata[strings.Title(k)] {
			t.Errorf("Expected custom metadata with key: %s", k)
			return
		}
	}
}
Ejemplo n.º 4
0
// List is a function that retrieves objects in the container as
// well as container metadata. It returns a pager which can be iterated with the
// EachPage function.
func List(c *gophercloud.ServiceClient, containerName string, opts os.ListOptsBuilder) pagination.Pager {
	return os.List(c, containerName, opts)
}