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