forked from dell/goiscsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoiscsi_utils.go
147 lines (136 loc) · 4.27 KB
/
goiscsi_utils.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
/*
*
* Copyright © 2020-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 goiscsi
import (
"fmt"
"strings"
)
type sessionParser struct{}
func (sp *sessionParser) Parse(data []byte) []ISCSISession {
str := string(data)
lines := strings.Split(str, "\n")
var result []ISCSISession
var curSession *ISCSISession
for _, line := range lines {
line = strings.TrimSpace(line)
switch {
case strings.HasPrefix(line, "Target:"):
session := ISCSISession{}
session.Target = strings.Fields(line)[1]
if curSession != nil {
result = append(result, *curSession)
}
curSession = &session
case curSession == nil:
case strings.HasPrefix(line, "Current Portal:"):
curSession.Portal = strings.Split(sessionFieldValue(line), ",")[0]
case strings.HasPrefix(line, "Iface Transport:"):
curSession.IfaceTransport = ISCSITransportName(sessionFieldValue(line))
case strings.HasPrefix(line, "Iface Initiatorname:"):
curSession.IfaceInitiatorname = sessionFieldValue(line)
case strings.HasPrefix(line, "Iface IPaddress:"):
curSession.IfaceIPaddress = sessionFieldValue(line)
case strings.HasPrefix(line, "SID:"):
curSession.SID = sessionFieldValue(line)
case strings.HasPrefix(line, "iSCSI Connection State:"):
curSession.ISCSIConnectionState = ISCSIConnectionState(sessionFieldValue(line))
case strings.HasPrefix(line, "iSCSI Session State:"):
curSession.ISCSISessionState = ISCSISessionState(sessionFieldValue(line))
case strings.HasPrefix(line, "username:"):
curSession.Username = sessionFieldValue(line)
case strings.HasPrefix(line, "password:"):
curSession.Password = sessionFieldValue(line)
case strings.HasPrefix(line, "username_in:"):
curSession.UsernameIn = sessionFieldValue(line)
case strings.HasPrefix(line, "password_in:"):
curSession.PasswordIn = sessionFieldValue(line)
}
}
if curSession != nil {
result = append(result, *curSession)
}
return result
}
func sessionFieldValue(s string) string {
_, value := fieldKeyValue(s, ":")
return value
}
func nodeFieldKeyValue(s string) (string, string) {
return fieldKeyValue(s, "=")
}
func fieldKeyValue(s string, sep string) (string, string) {
var key, value string
splitted := strings.SplitN(s, sep, 2)
if len(splitted) > 0 {
key = strings.Trim(strings.TrimSpace(splitted[0]), sep)
}
if len(splitted) > 1 {
value = replaceEmpty(strings.TrimSpace(splitted[1]))
}
return key, value
}
func replaceEmpty(s string) string {
if s == "<empty>" {
return ""
}
return s
}
type nodeParser struct{}
func (np *nodeParser) Parse(data []byte) []ISCSINode {
str := string(data)
lines := strings.Split(str, "\n")
var result []ISCSINode
var curNode *ISCSINode
for _, line := range lines {
line = strings.TrimSpace(line)
switch {
case strings.HasPrefix(line, "# BEGIN RECORD"):
if curNode != nil {
result = append(result, *curNode)
}
curNode = &ISCSINode{Fields: make(map[string]string)}
case strings.HasPrefix(line, "# END RECORD"):
if curNode != nil {
result = append(result, *curNode)
}
curNode = nil
case curNode == nil:
case strings.HasPrefix(line, "node.name ="):
key, value := nodeFieldKeyValue(line)
curNode.Target = value
curNode.Fields[key] = value
case strings.HasPrefix(line, "node.conn[0].address ="):
key, value := nodeFieldKeyValue(line)
curNode.Portal = value
curNode.Fields[key] = value
case strings.HasPrefix(line, "node.conn[0].port ="):
key, value := nodeFieldKeyValue(line)
if curNode.Portal != "" {
curNode.Portal = fmt.Sprintf("%s:%s", curNode.Portal, value)
}
curNode.Fields[key] = value
default:
key, value := nodeFieldKeyValue(line)
curNode.Fields[key] = value
}
}
if curNode != nil {
result = append(result, *curNode)
}
return result
}