/
initiator.go
172 lines (141 loc) · 4.81 KB
/
initiator.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
package dorado
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
// Initiator is iSCSI initiator
type Initiator struct {
FAILOVERMODE string `json:"FAILOVERMODE"`
HEALTHSTATUS string `json:"HEALTHSTATUS"`
ID string `json:"ID"` // = IQN
ISFREE string `json:"ISFREE"`
MULTIPATHTYPE string `json:"MULTIPATHTYPE"`
OPERATIONSYSTEM string `json:"OPERATIONSYSTEM"`
PATHTYPE string `json:"PATHTYPE"`
RUNNINGSTATUS string `json:"RUNNINGSTATUS"`
SPECIALMODETYPE string `json:"SPECIALMODETYPE"`
TYPE int `json:"TYPE"`
USECHAP string `json:"USECHAP"`
PARENTID string `json:"PARENTID,omitempty"`
PARENTNAME string `json:"PARENTNAME,omitempty"`
PARENTTYPE int `json:"PARENTTYPE,omitempty"`
}
func encodeIqn(iqn string) string {
// must escape colon when using filter string.
return strings.ReplaceAll(iqn, `:`, `\:`)
}
// GetInitiators search initiators.
// you must use encodeIqn when to search iqn.
// ex: initiators, err := d.GetInitiators(ctx, NewSearchQueryId(encodeIqn(iqn)))
func (d *Device) GetInitiators(ctx context.Context, query *SearchQuery) ([]Initiator, error) {
spath := "/iscsi_initiator"
req, err := d.newRequest(ctx, "GET", spath, nil)
if err != nil {
return nil, fmt.Errorf(ErrCreateRequest+": %w", err)
}
req = AddSearchQuery(req, query)
var initiators []Initiator
if err = d.requestWithRetry(req, &initiators, DefaultHTTPRetryCount); err != nil {
return nil, fmt.Errorf(ErrRequestWithRetry+": %w", err)
}
if len(initiators) == 0 {
return nil, ErrInitiatorNotFound
}
return initiators, nil
}
// GetInitiator get initiator by id.
func (d *Device) GetInitiator(ctx context.Context, iqn string) (*Initiator, error) {
spath := fmt.Sprintf("/iscsi_initiator/%s", iqn)
req, err := d.newRequest(ctx, "GET", spath, nil)
if err != nil {
return nil, fmt.Errorf(ErrCreateRequest+": %w", err)
}
initiators := &Initiator{}
if err = d.requestWithRetry(req, initiators, DefaultHTTPRetryCount); err != nil {
return nil, fmt.Errorf(ErrRequestWithRetry+": %w", err)
}
return initiators, nil
}
// CreateInitiator create initiator.
func (d *Device) CreateInitiator(ctx context.Context, iqn string) (*Initiator, error) {
spath := "/iscsi_initiator"
param := struct {
USECHAP string `json:"USECHAP"`
TYPE string `json:"TYPE"`
ID string `json:"ID"`
}{
USECHAP: "false",
TYPE: strconv.Itoa(TypeInitiator),
ID: iqn,
}
jb, err := json.Marshal(param)
if err != nil {
return nil, fmt.Errorf(ErrCreatePostValue+": %w", err)
}
req, err := d.newRequest(ctx, "POST", spath, bytes.NewBuffer(jb))
if err != nil {
return nil, fmt.Errorf(ErrCreateRequest+": %w", err)
}
initiator := &Initiator{}
if err = d.requestWithRetry(req, initiator, DefaultHTTPRetryCount); err != nil {
return nil, fmt.Errorf(ErrRequestWithRetry+": %w", err)
}
return initiator, nil
}
// DeleteInitiator delete initiator.
func (d *Device) DeleteInitiator(ctx context.Context, iqn string) error {
spath := fmt.Sprintf("/iscsi_initiator/%s", iqn)
req, err := d.newRequest(ctx, "DELETE", spath, nil)
if err != nil {
return fmt.Errorf(ErrCreateRequest+": %w", err)
}
var i interface{} // this endpoint return N/A
if err = d.requestWithRetry(req, i, DefaultHTTPRetryCount); err != nil {
return fmt.Errorf(ErrRequestWithRetry+": %w", err)
}
return nil
}
// UpdateInitiatorParam is parameter for UpdateInitiator
type UpdateInitiatorParam struct {
USECHAP string `json:"USECHAP"`
PARENTTYPE string `json:"PARENTTYPE"`
TYPE string `json:"TYPE"`
ID string `json:"ID"`
PARENTID string `json:"PARENTID"`
}
// UpdateInitiator update initiator information.
func (d *Device) UpdateInitiator(ctx context.Context, iqn string, initiatorParam UpdateInitiatorParam) (*Initiator, error) {
spath := fmt.Sprintf("/iscsi_initiator/%s", iqn)
jb, err := json.Marshal(initiatorParam)
if err != nil {
return nil, fmt.Errorf(ErrCreatePostValue+": %w", err)
}
req, err := d.newRequest(ctx, "PUT", spath, bytes.NewBuffer(jb))
if err != nil {
return nil, fmt.Errorf(ErrCreateRequest+": %w", err)
}
initiator := &Initiator{}
if err = d.requestWithRetry(req, initiator, DefaultHTTPRetryCount); err != nil {
return nil, fmt.Errorf(ErrRequestWithRetry+": %w", err)
}
return initiator, nil
}
// GetInitiatorForce get initiator and create initiator if not exists.
func (d *Device) GetInitiatorForce(ctx context.Context, iqn string) (*Initiator, error) {
initiators, err := d.GetInitiators(ctx, NewSearchQueryID(encodeIqn(iqn)))
if err != nil {
if err == ErrInitiatorNotFound {
return d.CreateInitiator(ctx, iqn)
}
return nil, fmt.Errorf("failed to get initiators: %w", err)
}
if len(initiators) != 1 {
return nil, errors.New("fount multiple initiators in same iqn")
}
return &initiators[0], nil
}