Example #1
0
// List returns all available volume mappings.
func (d *driver) List(
	ctx types.Context,
	opts types.Store) ([]types.VolumeMapping, error) {

	client := context.MustClient(ctx)
	vols, err := client.Storage().Volumes(
		ctx,
		&types.VolumesOpts{
			Attachments: opts.GetAttachments(),
			Opts:        opts,
		},
	)
	if err != nil {
		return nil, err
	}

	serviceName, serviceNameOK := context.ServiceName(ctx)
	if !serviceNameOK {
		return nil, goof.New("service name is missing")
	}

	volMaps := []types.VolumeMapping{}
	for _, v := range vols {
		vs := make(map[string]interface{})
		vs["name"] = v.Name
		vs["size"] = v.Size
		vs["iops"] = v.IOPS
		vs["type"] = v.Type
		vs["availabilityZone"] = v.AvailabilityZone
		vs["fields"] = v.Fields
		vs["service"] = serviceName
		vs["server"] = serviceName
		volMaps = append(volMaps, &volumeMapping{
			Name:             v.Name,
			VolumeMountPoint: v.MountPoint(),
			VolumeStatus:     vs,
		})
	}

	return volMaps, nil
}
Example #2
0
func (r *router) volumesForService(
	ctx types.Context,
	w http.ResponseWriter,
	req *http.Request,
	store types.Store) error {

	filter, err := parseFilter(store)
	if err != nil {
		return err
	}
	if filter != nil {
		store.Set("filter", filter)
	}

	service := context.MustService(ctx)

	opts := &types.VolumesOpts{
		Attachments: store.GetAttachments(),
		Opts:        store,
	}

	run := func(
		ctx types.Context,
		svc types.StorageService) (interface{}, error) {

		return getFilteredVolumes(ctx, req, store, svc, opts, filter)
	}

	return httputils.WriteTask(
		ctx,
		r.config,
		w,
		store,
		service.TaskExecute(ctx, run, schema.VolumeMapSchema),
		http.StatusOK)
}
Example #3
0
func (r *router) volumeInspect(
	ctx types.Context,
	w http.ResponseWriter,
	req *http.Request,
	store types.Store) error {

	attachments := store.GetAttachments()

	service := context.MustService(ctx)
	iid, iidOK := context.InstanceID(ctx)
	if !iidOK && attachments.RequiresInstanceID() {
		return utils.NewMissingInstanceIDError(service.Name())
	}

	opts := &types.VolumeInspectOpts{
		Attachments: attachments,
		Opts:        store,
	}

	var run types.StorageTaskRunFunc
	if store.IsSet("byName") {
		run = func(
			ctx types.Context,
			svc types.StorageService) (interface{}, error) {

			vols, err := svc.Driver().Volumes(
				ctx,
				&types.VolumesOpts{
					Attachments: attachments,
					Opts:        store,
				})

			if err != nil {
				return nil, err
			}

			volID := store.GetString("volumeID")
			for _, v := range vols {
				if strings.EqualFold(v.Name, volID) {
					if !handleVolAttachments(ctx, nil, iid, v, attachments) {
						return nil, utils.NewNotFoundError(volID)
					}
					if OnVolume != nil {
						ok, err := OnVolume(ctx, req, store, v)
						if err != nil {
							return nil, err
						}
						if !ok {
							return nil, utils.NewNotFoundError(volID)
						}
					}

					return v, nil
				}
			}

			return nil, utils.NewNotFoundError(volID)
		}

	} else {

		run = func(
			ctx types.Context,
			svc types.StorageService) (interface{}, error) {

			v, err := svc.Driver().VolumeInspect(
				ctx, store.GetString("volumeID"), opts)

			if err != nil {
				return nil, err
			}

			if !handleVolAttachments(ctx, nil, iid, v, attachments) {
				return nil, utils.NewNotFoundError(v.ID)
			}

			if OnVolume != nil {
				ok, err := OnVolume(ctx, req, store, v)
				if err != nil {
					return nil, err
				}
				if !ok {
					return nil, utils.NewNotFoundError(v.ID)
				}
			}

			return v, nil
		}
	}

	return httputils.WriteTask(
		ctx,
		r.config,
		w,
		store,
		service.TaskExecute(ctx, run, schema.VolumeSchema),
		http.StatusOK)
}
Example #4
0
func (r *router) volumes(
	ctx types.Context,
	w http.ResponseWriter,
	req *http.Request,
	store types.Store) error {

	filter, err := parseFilter(store)
	if err != nil {
		return err
	}
	if filter != nil {
		store.Set("filter", filter)
	}

	var (
		tasks   = map[string]*types.Task{}
		taskIDs []int
		opts    = &types.VolumesOpts{
			Attachments: store.GetAttachments(),
			Opts:        store,
		}
		reply = types.ServiceVolumeMap{}
	)

	for service := range services.StorageServices(ctx) {

		run := func(
			ctx types.Context,
			svc types.StorageService) (interface{}, error) {

			ctx = context.WithStorageService(ctx, svc)

			var err error
			if ctx, err = context.WithStorageSession(ctx); err != nil {
				return nil, err
			}

			return getFilteredVolumes(ctx, req, store, svc, opts, filter)
		}

		task := service.TaskExecute(ctx, run, schema.VolumeMapSchema)
		taskIDs = append(taskIDs, task.ID)
		tasks[service.Name()] = task
	}

	run := func(ctx types.Context) (interface{}, error) {

		services.TaskWaitAll(ctx, taskIDs...)

		for k, v := range tasks {
			if v.Error != nil {
				return nil, utils.NewBatchProcessErr(reply, v.Error)
			}

			objMap, ok := v.Result.(types.VolumeMap)
			if !ok {
				return nil, utils.NewBatchProcessErr(
					reply, goof.New("error casting to types.VolumeMap"))
			}
			reply[k] = objMap
		}

		return reply, nil
	}

	return httputils.WriteTask(
		ctx,
		r.config,
		w,
		store,
		services.TaskExecute(ctx, run, schema.ServiceVolumeMapSchema),
		http.StatusOK)
}