/
builder.go
199 lines (161 loc) · 4.84 KB
/
builder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2019 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package padd
import (
"fmt"
"os/exec"
"reflect"
"runtime"
"strings"
"github.com/openebs/cstor-operators/pkg/zcmd/bin"
"github.com/pkg/errors"
)
const (
// Operation defines type of zfs operation
Operation = "add"
)
//PoolExpansion defines structure for pool 'Expansion' operation
type PoolExpansion struct {
// list of vdev to add
VdevList []string
// property list
Property []string
// name of pool
Pool string
// Forcefully .. with -f
Forcefully bool
// command string
Command string
// DeviceType
DeviceType string
// Type of config.. like mirror
Type string
// checks is list of predicate function used for validating object
checks []PredicateFunc
// error
err error
// Executor is to execute the commands
Executor bin.Executor
}
// NewPoolExpansion returns new instance of object PoolExpansion
func NewPoolExpansion() *PoolExpansion {
return &PoolExpansion{}
}
// WithCheck add given check to checks list
func (p *PoolExpansion) WithCheck(check ...PredicateFunc) *PoolExpansion {
p.checks = append(p.checks, check...)
return p
}
// WithVdevList method fills the VdevList field of PoolExpansion object.
func (p *PoolExpansion) WithVdevList(vdevlist []string) *PoolExpansion {
p.VdevList = append(p.VdevList, vdevlist...)
return p
}
// WithProperty method fills the Property field of PoolExpansion object.
func (p *PoolExpansion) WithProperty(key, value string) *PoolExpansion {
p.Property = append(p.Property, fmt.Sprintf("%s=%s", key, value))
return p
}
// WithForcefully method fills the Forcefully field of PoolExpansion object.
func (p *PoolExpansion) WithForcefully(Forcefully bool) *PoolExpansion {
p.Forcefully = Forcefully
return p
}
// WithPool method fills the Pool field of PoolExpansion object.
func (p *PoolExpansion) WithPool(Pool string) *PoolExpansion {
p.Pool = Pool
return p
}
// WithDeviceType method fills the DeviceType field of PoolExpansion object.
func (p *PoolExpansion) WithDeviceType(DeviceType string) *PoolExpansion {
p.DeviceType = DeviceType
return p
}
// WithType method fills the Type field of PoolExpansion object.
func (p *PoolExpansion) WithType(Type string) *PoolExpansion {
p.Type = Type
return p
}
// WithCommand method fills the Command field of PoolExpansion object.
func (p *PoolExpansion) WithCommand(Command string) *PoolExpansion {
p.Command = Command
return p
}
// WithExecutor method fills the Executor field of PoolExpansion object.
func (p *PoolExpansion) WithExecutor(executor bin.Executor) *PoolExpansion {
p.Executor = executor
return p
}
// Validate is to validate generated PoolExpansion object by builder
func (p *PoolExpansion) Validate() *PoolExpansion {
for _, check := range p.checks {
if !check(p) {
p.err = errors.Wrapf(p.err, "validation failed {%v}", runtime.FuncForPC(reflect.ValueOf(check).Pointer()).Name())
}
}
return p
}
// Execute is to execute generated PoolCreate object
func (p *PoolExpansion) Execute() ([]byte, error) {
var ret []byte
var err error
p, err = p.Build()
if err != nil {
return nil, err
}
if IsExecutorSet()(p) {
ret, err = p.Executor.Execute(p.Command)
} else {
// execute command here
// #nosec
ret, err = exec.Command(bin.BASH, "-c", p.Command).CombinedOutput()
}
if err != nil {
return ret, errors.Errorf("error %s %s", err.Error(), string(ret))
}
return ret, nil
}
// Build returns the PoolExpansion object generated by builder
func (p *PoolExpansion) Build() (*PoolExpansion, error) {
var c strings.Builder
p = p.Validate()
p.appendCommand(&c, bin.ZPOOL)
p.appendCommand(&c, fmt.Sprintf(" %s ", Operation))
if IsForcefullySet()(p) {
p.appendCommand(&c, fmt.Sprintf(" -f "))
}
if IsPropertySet()(p) {
for _, v := range p.Property {
p.appendCommand(&c, fmt.Sprintf(" -o %s ", v))
}
}
p.appendCommand(&c, p.Pool)
if IsDeviceTypeSet()(p) {
p.appendCommand(&c, fmt.Sprintf(" %s ", p.DeviceType))
}
if IsTypeSet()(p) {
p.appendCommand(&c, fmt.Sprintf(" %s ", p.Type))
}
for _, v := range p.VdevList {
p.appendCommand(&c, fmt.Sprintf(" %s ", v))
}
p.Command = c.String()
return p, p.err
}
// appendCommand append string to given string builder
func (p *PoolExpansion) appendCommand(c *strings.Builder, cmd string) {
_, err := c.WriteString(cmd)
if err != nil {
p.err = errors.Wrapf(p.err, "Failed to append cmd{%s} : %s", cmd, err.Error())
}
}