github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/common/filesystems.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     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  		params.FilesystemInfo{
   106  			info.FilesystemId,
   107  			info.Size,
   108  		},
   109  	}
   110  	volumeTag, err := f.Volume()
   111  	if err == nil {
   112  		result.VolumeTag = volumeTag.String()
   113  	} else if err != state.ErrNoBackingVolume {
   114  		return params.Filesystem{}, errors.Trace(err)
   115  	}
   116  	return result, nil
   117  }
   118  
   119  // FilesystemAttachmentToState converts a storage.FilesystemAttachment
   120  // to a state.FilesystemAttachmentInfo.
   121  func FilesystemAttachmentToState(in params.FilesystemAttachment) (names.MachineTag, names.FilesystemTag, state.FilesystemAttachmentInfo, error) {
   122  	machineTag, err := names.ParseMachineTag(in.MachineTag)
   123  	if err != nil {
   124  		return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err
   125  	}
   126  	filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
   127  	if err != nil {
   128  		return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err
   129  	}
   130  	info := state.FilesystemAttachmentInfo{
   131  		in.Info.MountPoint,
   132  		in.Info.ReadOnly,
   133  	}
   134  	return machineTag, filesystemTag, info, nil
   135  }
   136  
   137  // FilesystemAttachmentFromState converts a state.FilesystemAttachment to params.FilesystemAttachment.
   138  func FilesystemAttachmentFromState(v state.FilesystemAttachment) (params.FilesystemAttachment, error) {
   139  	info, err := v.Info()
   140  	if err != nil {
   141  		return params.FilesystemAttachment{}, errors.Trace(err)
   142  	}
   143  	return params.FilesystemAttachment{
   144  		v.Filesystem().String(),
   145  		v.Machine().String(),
   146  		params.FilesystemAttachmentInfo{
   147  			info.MountPoint,
   148  			info.ReadOnly,
   149  		},
   150  	}, nil
   151  }
   152  
   153  // ParseFilesystemAttachmentIds parses the strings, returning machine storage IDs.
   154  func ParseFilesystemAttachmentIds(stringIds []string) ([]params.MachineStorageId, error) {
   155  	ids := make([]params.MachineStorageId, len(stringIds))
   156  	for i, s := range stringIds {
   157  		m, f, err := state.ParseFilesystemAttachmentId(s)
   158  		if err != nil {
   159  			return nil, err
   160  		}
   161  		ids[i] = params.MachineStorageId{
   162  			MachineTag:    m.String(),
   163  			AttachmentTag: f.String(),
   164  		}
   165  	}
   166  	return ids, nil
   167  }