github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/common/storagecommon/filesystems.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storagecommon
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/apiserver/params"
    11  	"github.com/juju/juju/environs/config"
    12  	"github.com/juju/juju/state"
    13  	"github.com/juju/juju/storage/poolmanager"
    14  )
    15  
    16  // FilesystemParams returns the parameters for creating or destroying the
    17  // given filesystem.
    18  func FilesystemParams(
    19  	f state.Filesystem,
    20  	storageInstance state.StorageInstance,
    21  	environConfig *config.Config,
    22  	poolManager poolmanager.PoolManager,
    23  ) (params.FilesystemParams, error) {
    24  
    25  	var pool string
    26  	var size uint64
    27  	if stateFilesystemParams, ok := f.Params(); ok {
    28  		pool = stateFilesystemParams.Pool
    29  		size = stateFilesystemParams.Size
    30  	} else {
    31  		filesystemInfo, err := f.Info()
    32  		if err != nil {
    33  			return params.FilesystemParams{}, errors.Trace(err)
    34  		}
    35  		pool = filesystemInfo.Pool
    36  		size = filesystemInfo.Size
    37  	}
    38  
    39  	filesystemTags, err := storageTags(storageInstance, environConfig)
    40  	if err != nil {
    41  		return params.FilesystemParams{}, errors.Annotate(err, "computing storage tags")
    42  	}
    43  
    44  	providerType, cfg, err := StoragePoolConfig(pool, poolManager)
    45  	if err != nil {
    46  		return params.FilesystemParams{}, errors.Trace(err)
    47  	}
    48  	result := params.FilesystemParams{
    49  		f.Tag().String(),
    50  		"", // volume tag
    51  		size,
    52  		string(providerType),
    53  		cfg.Attrs(),
    54  		filesystemTags,
    55  		nil, // attachment params set by the caller
    56  	}
    57  
    58  	volumeTag, err := f.Volume()
    59  	if err == nil {
    60  		result.VolumeTag = volumeTag.String()
    61  	} else if err != state.ErrNoBackingVolume {
    62  		return params.FilesystemParams{}, errors.Trace(err)
    63  	}
    64  
    65  	return result, nil
    66  }
    67  
    68  // FilesystemsToState converts a slice of params.Filesystem to a mapping
    69  // of filesystem tags to state.FilesystemInfo.
    70  func FilesystemsToState(in []params.Filesystem) (map[names.FilesystemTag]state.FilesystemInfo, error) {
    71  	m := make(map[names.FilesystemTag]state.FilesystemInfo)
    72  	for _, v := range in {
    73  		tag, filesystemInfo, err := FilesystemToState(v)
    74  		if err != nil {
    75  			return nil, errors.Trace(err)
    76  		}
    77  		m[tag] = filesystemInfo
    78  	}
    79  	return m, nil
    80  }
    81  
    82  // FilesystemToState converts a params.Filesystem to state.FilesystemInfo
    83  // and names.FilesystemTag.
    84  func FilesystemToState(v params.Filesystem) (names.FilesystemTag, state.FilesystemInfo, error) {
    85  	filesystemTag, err := names.ParseFilesystemTag(v.FilesystemTag)
    86  	if err != nil {
    87  		return names.FilesystemTag{}, state.FilesystemInfo{}, errors.Trace(err)
    88  	}
    89  	return filesystemTag, state.FilesystemInfo{
    90  		v.Info.Size,
    91  		"", // pool is set by state
    92  		v.Info.FilesystemId,
    93  	}, nil
    94  }
    95  
    96  // FilesystemFromState converts a state.Filesystem to params.Filesystem.
    97  func FilesystemFromState(f state.Filesystem) (params.Filesystem, error) {
    98  	info, err := f.Info()
    99  	if err != nil {
   100  		return params.Filesystem{}, errors.Trace(err)
   101  	}
   102  	result := params.Filesystem{
   103  		f.FilesystemTag().String(),
   104  		"",
   105  		FilesystemInfoFromState(info),
   106  	}
   107  	volumeTag, err := f.Volume()
   108  	if err == nil {
   109  		result.VolumeTag = volumeTag.String()
   110  	} else if err != state.ErrNoBackingVolume {
   111  		return params.Filesystem{}, errors.Trace(err)
   112  	}
   113  	return result, nil
   114  }
   115  
   116  // FilesystemInfoFromState converts a state.FilesystemInfo to params.FilesystemInfo.
   117  func FilesystemInfoFromState(info state.FilesystemInfo) params.FilesystemInfo {
   118  	return params.FilesystemInfo{
   119  		info.FilesystemId,
   120  		info.Size,
   121  	}
   122  }
   123  
   124  // FilesystemAttachmentToState converts a storage.FilesystemAttachment
   125  // to a state.FilesystemAttachmentInfo.
   126  func FilesystemAttachmentToState(in params.FilesystemAttachment) (names.MachineTag, names.FilesystemTag, state.FilesystemAttachmentInfo, error) {
   127  	machineTag, err := names.ParseMachineTag(in.MachineTag)
   128  	if err != nil {
   129  		return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err
   130  	}
   131  	filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
   132  	if err != nil {
   133  		return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err
   134  	}
   135  	info := state.FilesystemAttachmentInfo{
   136  		in.Info.MountPoint,
   137  		in.Info.ReadOnly,
   138  	}
   139  	return machineTag, filesystemTag, info, nil
   140  }
   141  
   142  // FilesystemAttachmentFromState converts a state.FilesystemAttachment to params.FilesystemAttachment.
   143  func FilesystemAttachmentFromState(v state.FilesystemAttachment) (params.FilesystemAttachment, error) {
   144  	info, err := v.Info()
   145  	if err != nil {
   146  		return params.FilesystemAttachment{}, errors.Trace(err)
   147  	}
   148  	return params.FilesystemAttachment{
   149  		v.Filesystem().String(),
   150  		v.Machine().String(),
   151  		FilesystemAttachmentInfoFromState(info),
   152  	}, nil
   153  }
   154  
   155  // FilesystemAttachmentInfoFromState converts a state.FilesystemAttachmentInfo
   156  // to params.FilesystemAttachmentInfo.
   157  func FilesystemAttachmentInfoFromState(info state.FilesystemAttachmentInfo) params.FilesystemAttachmentInfo {
   158  	return params.FilesystemAttachmentInfo{
   159  		info.MountPoint,
   160  		info.ReadOnly,
   161  	}
   162  }
   163  
   164  // ParseFilesystemAttachmentIds parses the strings, returning machine storage IDs.
   165  func ParseFilesystemAttachmentIds(stringIds []string) ([]params.MachineStorageId, error) {
   166  	ids := make([]params.MachineStorageId, len(stringIds))
   167  	for i, s := range stringIds {
   168  		m, f, err := state.ParseFilesystemAttachmentId(s)
   169  		if err != nil {
   170  			return nil, err
   171  		}
   172  		ids[i] = params.MachineStorageId{
   173  			MachineTag:    m.String(),
   174  			AttachmentTag: f.String(),
   175  		}
   176  	}
   177  	return ids, nil
   178  }