func (a *API) ListVolumes(filter params.VolumeFilter) (params.VolumeItemsResult, error) { if !filter.IsEmpty() { return params.VolumeItemsResult{Results: a.filterVolumes(filter)}, nil } volumes, err := a.listVolumeAttachments() if err != nil { return params.VolumeItemsResult{}, common.ServerError(err) } return params.VolumeItemsResult{Results: volumes}, nil }
func filterVolumes( st storageAccess, f params.VolumeFilter, ) ([]state.Volume, map[names.VolumeTag][]state.VolumeAttachment, error) { if f.IsEmpty() { // No filter was specified: get all volumes, and all attachments. volumes, err := st.AllVolumes() if err != nil { return nil, nil, errors.Trace(err) } volumeAttachments := make(map[names.VolumeTag][]state.VolumeAttachment) for _, v := range volumes { attachments, err := st.VolumeAttachments(v.VolumeTag()) if err != nil { return nil, nil, errors.Trace(err) } volumeAttachments[v.VolumeTag()] = attachments } return volumes, volumeAttachments, nil } volumesByTag := make(map[names.VolumeTag]state.Volume) volumeAttachments := make(map[names.VolumeTag][]state.VolumeAttachment) for _, machine := range f.Machines { machineTag, err := names.ParseMachineTag(machine) if err != nil { return nil, nil, errors.Trace(err) } attachments, err := st.MachineVolumeAttachments(machineTag) if err != nil { return nil, nil, errors.Trace(err) } for _, attachment := range attachments { volumeTag := attachment.Volume() volumesByTag[volumeTag] = nil volumeAttachments[volumeTag] = append(volumeAttachments[volumeTag], attachment) } } for volumeTag := range volumesByTag { volume, err := st.Volume(volumeTag) if err != nil { return nil, nil, errors.Trace(err) } volumesByTag[volumeTag] = volume } volumes := make([]state.Volume, 0, len(volumesByTag)) for _, volume := range volumesByTag { volumes = append(volumes, volume) } return volumes, volumeAttachments, nil }