// WithStorageService returns a new context with the StorageService as the // value and attempts to assign the service's associated InstanceID and // LocalDevices (by way of the service's StorageDriver) to the context as well. func WithStorageService( parent context.Context, service types.StorageService) types.Context { var ( driver = service.Driver() driverName = strings.ToLower(driver.Name()) ) parent = newContext(parent, DriverKey, driver, nil, nil) // set the service's InstanceID if present if iidm, ok := parent.Value(AllInstanceIDsKey).(types.InstanceIDMap); ok { if iid, ok := iidm[driverName]; ok { parent = newContext(parent, InstanceIDKey, iid, nil, nil) } } // set the service's LocalDevices if present if ldm, ok := parent.Value(AllLocalDevicesKey).(types.LocalDevicesMap); ok { if ld, ok := ldm[driverName]; ok { parent = newContext(parent, LocalDevicesKey, ld, nil, nil) } } return newContext(parent, ServiceKey, service, nil, nil) }
func toServiceInfo( ctx types.Context, service types.StorageService, store types.Store) (*types.ServiceInfo, error) { d := service.Driver() var instance *types.Instance if store.GetBool("instance") { if _, ok := context.InstanceID(ctx); !ok { return nil, utils.NewMissingInstanceIDError(service.Name()) } var err error instance, err = d.InstanceInspect(ctx, store) if err != nil { return nil, err } } st, err := d.Type(ctx) if err != nil { return nil, err } nd, err := d.NextDeviceInfo(ctx) if err != nil { return nil, err } return &types.ServiceInfo{ Name: service.Name(), Instance: instance, Driver: &types.DriverInfo{ Name: d.Name(), Type: st, NextDevice: nd, }, }, nil }
func getFilteredVolumes( ctx types.Context, req *http.Request, store types.Store, storSvc types.StorageService, opts *types.VolumesOpts, filter *types.Filter) (types.VolumeMap, error) { var ( filterOp types.FilterOperator filterLeft string filterRight string objMap = types.VolumeMap{} ) iid, iidOK := context.InstanceID(ctx) if opts.Attachments.RequiresInstanceID() && !iidOK { return nil, utils.NewMissingInstanceIDError(storSvc.Name()) } ctx.WithField("attachments", opts.Attachments).Debug("querying volumes") objs, err := storSvc.Driver().Volumes(ctx, opts) if err != nil { return nil, err } if filter != nil { filterOp = filter.Op filterLeft = strings.ToLower(filter.Left) filterRight = strings.ToLower(filter.Right) } for _, obj := range objs { lf := log.Fields{ "attachments": opts.Attachments, "volumeID": obj.ID, "volumeName": obj.Name, } if filterOp == types.FilterEqualityMatch && filterLeft == "name" { ctx.WithFields(lf).Debug("checking name filter") if !strings.EqualFold(obj.Name, filterRight) { ctx.WithFields(lf).Debug("omitted volume due to name filter") continue } } if !handleVolAttachments(ctx, lf, iid, obj, opts.Attachments) { continue } if OnVolume != nil { ctx.WithFields(lf).Debug("invoking OnVolume handler") ok, err := OnVolume(ctx, req, store, obj) if err != nil { return nil, err } if !ok { continue } } objMap[obj.ID] = obj } return objMap, nil }