This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from NetApp/trident
/
volume.go
109 lines (97 loc) · 3.83 KB
/
volume.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
// Copyright 2018 NetApp, Inc. All Rights Reserved.
package storage
import (
"bytes"
"encoding/gob"
"fmt"
"strings"
"github.com/netapp/trident/config"
"github.com/netapp/trident/utils"
)
type VolumeConfig struct {
Version string `json:"version"`
Name string `json:"name"`
InternalName string `json:"internalName"`
Size string `json:"size"`
Protocol config.Protocol `json:"protocol"`
SpaceReserve string `json:"spaceReserve"`
SecurityStyle string `json:"securityStyle"`
SnapshotPolicy string `json:"snapshotPolicy,omitempty"`
SnapshotReserve string `json:"snapshotReserve,omitempty"`
SnapshotDir string `json:"snapshotDirectory,omitempty"`
ExportPolicy string `json:"exportPolicy,omitempty"`
UnixPermissions string `json:"unixPermissions,omitempty"`
StorageClass string `json:"storageClass,omitempty"`
AccessMode config.AccessMode `json:"accessMode,omitempty"`
AccessInfo utils.VolumeAccessInfo `json:"accessInformation"`
BlockSize string `json:"blockSize"`
FileSystem string `json:"fileSystem"`
Encryption string `json:"encryption"`
CloneSourceVolume string `json:"cloneSourceVolume"`
CloneSourceVolumeInternal string `json:"cloneSourceVolumeInternal"`
CloneSourceSnapshot string `json:"cloneSourceSnapshot"`
SplitOnClone string `json:"splitOnClone"`
QoS string `json:"qos,omitempty"`
QoSType string `json:"type,omitempty"`
}
func (c *VolumeConfig) Validate() error {
if c.Name == "" || c.Size == "" {
return fmt.Errorf("the following fields for \"Volume\" are mandatory: name and size")
}
if !config.IsValidProtocol(c.Protocol) {
return fmt.Errorf("%v is an usupported protocol! Acceptable values: "+
"%s", c.Protocol,
strings.Join([]string(config.GetValidProtocolNames()), ", "),
)
}
return nil
}
func (c *VolumeConfig) ConstructClone() *VolumeConfig {
clone := &VolumeConfig{}
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
enc.Encode(c)
dec.Decode(clone)
return clone
}
type Volume struct {
Config *VolumeConfig
Backend string // Name of the storage backend
Pool string // Name of the pool on which this volume was first provisioned
Orphaned bool // An Orphaned volume isn't currently tracked by the storage backend
}
func NewVolume(conf *VolumeConfig, backend string, pool string, orphaned bool) *Volume {
return &Volume{
Config: conf,
Backend: backend,
Pool: pool,
Orphaned: orphaned,
}
}
type VolumeExternal struct {
Config *VolumeConfig
Backend string `json:"backend"`
Pool string `json:"pool"`
Orphaned bool `json:"orphaned"`
}
func (v *VolumeExternal) GetCHAPSecretName() string {
secretName := fmt.Sprintf("trident-chap-%v-%v", v.Backend, v.Config.AccessInfo.IscsiUsername)
secretName = strings.Replace(secretName, "_", "-", -1)
secretName = strings.Replace(secretName, ".", "-", -1)
secretName = strings.ToLower(secretName)
return secretName
}
func (v *Volume) ConstructExternal() *VolumeExternal {
return &VolumeExternal{
Config: v.Config,
Backend: v.Backend,
Pool: v.Pool,
Orphaned: v.Orphaned,
}
}
// VolumeExternalWrapper is used to return volumes and errors via channels between goroutines
type VolumeExternalWrapper struct {
Volume *VolumeExternal
Error error
}