/
filesystemdetails.go
75 lines (65 loc) · 2.13 KB
/
filesystemdetails.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
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storagecommon
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/core/life"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
// FilesystemDetails returns the filesystem and its attachments as a params FilesystemDetails.
func FilesystemDetails(
sb DetailsBackend,
unitToMachine UnitAssignedMachineFunc,
f state.Filesystem,
attachments []state.FilesystemAttachment,
) (*params.FilesystemDetails, error) {
details := ¶ms.FilesystemDetails{
FilesystemTag: f.FilesystemTag().String(),
Life: life.Value(f.Life().String()),
}
if volumeTag, err := f.Volume(); err == nil {
details.VolumeTag = volumeTag.String()
}
if info, err := f.Info(); err == nil {
details.Info = FilesystemInfoFromState(info)
}
if len(attachments) > 0 {
details.MachineAttachments = make(map[string]params.FilesystemAttachmentDetails, len(attachments))
details.UnitAttachments = make(map[string]params.FilesystemAttachmentDetails, len(attachments))
for _, attachment := range attachments {
attDetails := params.FilesystemAttachmentDetails{
Life: life.Value(attachment.Life().String()),
}
if stateInfo, err := attachment.Info(); err == nil {
attDetails.FilesystemAttachmentInfo = FilesystemAttachmentInfoFromState(
stateInfo,
)
}
if attachment.Host().Kind() == names.MachineTagKind {
details.MachineAttachments[attachment.Host().String()] = attDetails
} else {
details.UnitAttachments[attachment.Host().String()] = attDetails
}
}
}
aStatus, err := f.Status()
if err != nil {
return nil, errors.Trace(err)
}
details.Status = common.EntityStatusFromState(aStatus)
if storageTag, err := f.Storage(); err == nil {
storageInstance, err := sb.StorageInstance(storageTag)
if err != nil {
return nil, errors.Trace(err)
}
storageDetails, err := StorageDetails(sb, unitToMachine, storageInstance)
if err != nil {
return nil, errors.Trace(err)
}
details.Storage = storageDetails
}
return details, nil
}