Beispiel #1
0
// CreateVolume creates a volume from parameters, including the policy to copy.
func CreateVolume(vr *VolumeRequest) (*Volume, error) {
	if vr.Name == "" {
		return nil, errored.Errorf("Volume name was empty").Combine(errors.InvalidVolume)
	}

	if vr.Policy == nil {
		return nil, errored.Errorf("Policy for volume %q was nil", vr.Name).Combine(errors.InvalidVolume)
	}

	var mount string

	if vr.Options != nil {
		mount = vr.Options["mount"]
		delete(vr.Options, "mount")
	}

	if err := merge.Opts(vr.Policy, vr.Options); err != nil {
		return nil, err
	}

	if vr.Policy.DriverOptions == nil {
		vr.Policy.DriverOptions = map[string]string{}
	}

	if err := vr.Policy.Validate(); err != nil {
		return nil, err
	}

	vc := &Volume{
		Backends:       vr.Policy.Backends,
		DriverOptions:  vr.Policy.DriverOptions,
		CreateOptions:  vr.Policy.CreateOptions,
		RuntimeOptions: vr.Policy.RuntimeOptions,
		Unlocked:       vr.Policy.Unlocked,
		PolicyName:     vr.Policy.Name,
		VolumeName:     vr.Name,
		MountSource:    mount,
	}

	if err := vc.Validate(); err != nil {
		return nil, err
	}

	if vc.CreateOptions.FileSystem == "" {
		vc.CreateOptions.FileSystem = DefaultFilesystem
	}

	return vc, nil
}
Beispiel #2
0
// CreateVolume sets the appropriate config metadata for a volume creation
// operation, and returns the Volume that was copied in.
func (c *Client) CreateVolume(rc *VolumeRequest) (*Volume, error) {
	resp, err := c.GetPolicy(rc.Policy)
	if err != nil {
		return nil, err
	}

	var mount string

	if rc.Options != nil {
		mount = rc.Options["mount"]
		delete(rc.Options, "mount")
	}

	if err := merge.Opts(resp, rc.Options); err != nil {
		return nil, err
	}

	if resp.DriverOptions == nil {
		resp.DriverOptions = map[string]string{}
	}

	if err := resp.Validate(); err != nil {
		return nil, err
	}

	vc := &Volume{
		Backends:       resp.Backends,
		DriverOptions:  resp.DriverOptions,
		CreateOptions:  resp.CreateOptions,
		RuntimeOptions: resp.RuntimeOptions,
		Unlocked:       resp.Unlocked,
		PolicyName:     rc.Policy,
		VolumeName:     rc.Name,
		MountSource:    mount,
	}

	if err := vc.Validate(); err != nil {
		return nil, err
	}

	if vc.CreateOptions.FileSystem == "" {
		vc.CreateOptions.FileSystem = defaultFilesystem
	}

	return vc, nil
}