/
volumelistformatters.go
145 lines (124 loc) · 3.66 KB
/
volumelistformatters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storage
import (
"fmt"
"io"
"sort"
"strings"
"github.com/dustin/go-humanize"
"github.com/juju/juju/cmd/output"
)
// formatVolumeListTabular returns a tabular summary of volume instances.
func formatVolumeListTabular(writer io.Writer, infos map[string]VolumeInfo) error {
tw := output.TabWriter(writer)
print := func(values ...string) {
fmt.Fprintln(tw, strings.Join(values, "\t"))
}
haveMachines := false
volumeAttachmentInfos := make(volumeAttachmentInfos, 0, len(infos))
for volumeId, info := range infos {
volumeAttachmentInfo := volumeAttachmentInfo{
VolumeId: volumeId,
VolumeInfo: info,
}
if info.Attachments == nil {
volumeAttachmentInfos = append(volumeAttachmentInfos, volumeAttachmentInfo)
continue
}
// Each unit attachment must have a corresponding volume
// attachment. Enumerate each of the volume attachments,
// and locate the corresponding unit attachment if any.
// Each volume attachment has at most one corresponding
// unit attachment.
for machineId, machineInfo := range info.Attachments.Machines {
volumeAttachmentInfo := volumeAttachmentInfo
volumeAttachmentInfo.MachineId = machineId
volumeAttachmentInfo.VolumeAttachment = machineInfo
for unitId, unitInfo := range info.Attachments.Units {
if unitInfo.MachineId == machineId {
volumeAttachmentInfo.UnitId = unitId
volumeAttachmentInfo.UnitStorageAttachment = unitInfo
break
}
}
haveMachines = true
volumeAttachmentInfos = append(volumeAttachmentInfos, volumeAttachmentInfo)
}
for hostId, containerInfo := range info.Attachments.Containers {
volumeAttachmentInfo := volumeAttachmentInfo
volumeAttachmentInfo.VolumeAttachment = containerInfo
for unitId, unitInfo := range info.Attachments.Units {
if hostId == unitId {
volumeAttachmentInfo.UnitId = unitId
volumeAttachmentInfo.UnitStorageAttachment = unitInfo
break
}
}
volumeAttachmentInfos = append(volumeAttachmentInfos, volumeAttachmentInfo)
}
}
sort.Sort(volumeAttachmentInfos)
if haveMachines {
print("Machine", "Unit", "Storage ID", "Volume ID", "Provider ID", "Device", "Size", "State", "Message")
} else {
print("Unit", "Storage ID", "Volume ID", "Provider ID", "Size", "State", "Message")
}
for _, info := range volumeAttachmentInfos {
var size string
if info.Size > 0 {
size = humanize.IBytes(info.Size * humanize.MiByte)
}
if haveMachines {
print(
info.MachineId, info.UnitId, info.Storage,
info.VolumeId, info.ProviderVolumeId,
info.VolumeAttachment.DeviceName, size,
string(info.Status.Current), info.Status.Message,
)
} else {
print(
info.UnitId, info.Storage,
info.VolumeId, info.ProviderVolumeId, size,
string(info.Status.Current), info.Status.Message,
)
}
}
return tw.Flush()
}
type volumeAttachmentInfo struct {
VolumeId string
VolumeInfo
MachineId string
VolumeAttachment VolumeAttachment
UnitId string
UnitStorageAttachment UnitStorageAttachment
}
type volumeAttachmentInfos []volumeAttachmentInfo
func (v volumeAttachmentInfos) Len() int {
return len(v)
}
func (v volumeAttachmentInfos) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
func (v volumeAttachmentInfos) Less(i, j int) bool {
switch compareStrings(v[i].MachineId, v[j].MachineId) {
case -1:
return true
case 1:
return false
}
switch compareSlashSeparated(v[i].UnitId, v[j].UnitId) {
case -1:
return true
case 1:
return false
}
switch compareSlashSeparated(v[i].Storage, v[j].Storage) {
case -1:
return true
case 1:
return false
}
return v[i].VolumeId < v[j].VolumeId
}