github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 // FilesystemsToState converts a slice of params.Filesystem to a mapping 72 // of filesystem tags to state.FilesystemInfo. 73 func FilesystemsToState(in []params.Filesystem) (map[names.FilesystemTag]state.FilesystemInfo, error) { 74 m := make(map[names.FilesystemTag]state.FilesystemInfo) 75 for _, v := range in { 76 tag, filesystemInfo, err := FilesystemToState(v) 77 if err != nil { 78 return nil, errors.Trace(err) 79 } 80 m[tag] = filesystemInfo 81 } 82 return m, nil 83 } 84 85 // FilesystemToState converts a params.Filesystem to state.FilesystemInfo 86 // and names.FilesystemTag. 87 func FilesystemToState(v params.Filesystem) (names.FilesystemTag, state.FilesystemInfo, error) { 88 filesystemTag, err := names.ParseFilesystemTag(v.FilesystemTag) 89 if err != nil { 90 return names.FilesystemTag{}, state.FilesystemInfo{}, errors.Trace(err) 91 } 92 return filesystemTag, state.FilesystemInfo{ 93 v.Info.Size, 94 "", // pool is set by state 95 v.Info.FilesystemId, 96 }, nil 97 } 98 99 // FilesystemFromState converts a state.Filesystem to params.Filesystem. 100 func FilesystemFromState(f state.Filesystem) (params.Filesystem, error) { 101 info, err := f.Info() 102 if err != nil { 103 return params.Filesystem{}, errors.Trace(err) 104 } 105 result := params.Filesystem{ 106 f.FilesystemTag().String(), 107 "", 108 FilesystemInfoFromState(info), 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 // FilesystemInfoFromState converts a state.FilesystemInfo to params.FilesystemInfo. 120 func FilesystemInfoFromState(info state.FilesystemInfo) params.FilesystemInfo { 121 return params.FilesystemInfo{ 122 info.FilesystemId, 123 info.Size, 124 } 125 } 126 127 // FilesystemAttachmentToState converts a storage.FilesystemAttachment 128 // to a state.FilesystemAttachmentInfo. 129 func FilesystemAttachmentToState(in params.FilesystemAttachment) (names.MachineTag, names.FilesystemTag, state.FilesystemAttachmentInfo, error) { 130 machineTag, err := names.ParseMachineTag(in.MachineTag) 131 if err != nil { 132 return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err 133 } 134 filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag) 135 if err != nil { 136 return names.MachineTag{}, names.FilesystemTag{}, state.FilesystemAttachmentInfo{}, err 137 } 138 info := state.FilesystemAttachmentInfo{ 139 in.Info.MountPoint, 140 in.Info.ReadOnly, 141 } 142 return machineTag, filesystemTag, info, nil 143 } 144 145 // FilesystemAttachmentFromState converts a state.FilesystemAttachment to params.FilesystemAttachment. 146 func FilesystemAttachmentFromState(v state.FilesystemAttachment) (params.FilesystemAttachment, error) { 147 info, err := v.Info() 148 if err != nil { 149 return params.FilesystemAttachment{}, errors.Trace(err) 150 } 151 return params.FilesystemAttachment{ 152 v.Filesystem().String(), 153 v.Machine().String(), 154 FilesystemAttachmentInfoFromState(info), 155 }, nil 156 } 157 158 // FilesystemAttachmentInfoFromState converts a state.FilesystemAttachmentInfo 159 // to params.FilesystemAttachmentInfo. 160 func FilesystemAttachmentInfoFromState(info state.FilesystemAttachmentInfo) params.FilesystemAttachmentInfo { 161 return params.FilesystemAttachmentInfo{ 162 info.MountPoint, 163 info.ReadOnly, 164 } 165 } 166 167 // ParseFilesystemAttachmentIds parses the strings, returning machine storage IDs. 168 func ParseFilesystemAttachmentIds(stringIds []string) ([]params.MachineStorageId, error) { 169 ids := make([]params.MachineStorageId, len(stringIds)) 170 for i, s := range stringIds { 171 m, f, err := state.ParseFilesystemAttachmentId(s) 172 if err != nil { 173 return nil, err 174 } 175 ids[i] = params.MachineStorageId{ 176 MachineTag: m.String(), 177 AttachmentTag: f.String(), 178 } 179 } 180 return ids, nil 181 }