Skip to content

Commit 75f4b8e

Browse files
committed
backward-compatibility
1 parent d192526 commit 75f4b8e

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

example/main.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,14 @@ func main() {
2626
iscsi.EnableDebugLogging(os.Stdout)
2727
}
2828

29-
var targets []iscsi.TargetInfo
30-
31-
for _, tgtp := range tgtps {
32-
parts := strings.Split(tgtp, ":")
33-
targets = append(targets, iscsi.TargetInfo{
34-
// Specify the target iqn we're dealing with
35-
Iqn: *iqn,
36-
Portal: parts[0],
37-
Port: parts[1],
38-
})
39-
}
40-
4129
// You can utilize the iscsiadm calls directly if you wish, but by creating a Connector
4230
// you can simplify interactions to simple calls like "Connect" and "Disconnect"
4331
c := &iscsi.Connector{
4432
// Our example uses chap
4533
AuthType: "chap",
4634
// List of targets must be >= 1 (>1 signals multipath/mpio)
47-
Targets: targets,
35+
TargetIqn: *iqn,
36+
TargetPortals: tgtps,
4837
// CHAP can be setup up for discovery as well as sessions, our example
4938
// device only uses CHAP security for sessions, for those that use Discovery
5039
// as well, we'd add a DiscoverySecrets entry the same way

iscsi/iscsi.go

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ type iscsiSession struct {
3939
Name string
4040
}
4141

42-
// TargetInfo contains connection information to connect to an iSCSI endpoint
43-
type TargetInfo struct {
44-
Iqn string `json:"iqn"`
45-
Portal string `json:"portal"`
46-
Port string `json:"port"`
47-
}
48-
4942
type deviceInfo struct {
5043
BlockDevices []Device
5144
}
@@ -69,13 +62,14 @@ type HCTL struct {
6962

7063
// Connector provides a struct to hold all of the needed parameters to make our iSCSI connection
7164
type Connector struct {
72-
VolumeName string `json:"volume_name"`
73-
Targets []TargetInfo `json:"targets"`
74-
Lun int32 `json:"lun"`
75-
AuthType string `json:"auth_type"`
76-
DiscoverySecrets Secrets `json:"discovery_secrets"`
77-
SessionSecrets Secrets `json:"session_secrets"`
78-
Interface string `json:"interface"`
65+
VolumeName string `json:"volume_name"`
66+
TargetIqn string `json:"target_iqn"`
67+
TargetPortals []string `json:"target_portal"`
68+
Lun int32 `json:"lun"`
69+
AuthType string `json:"auth_type"`
70+
DiscoverySecrets Secrets `json:"discovery_secrets"`
71+
SessionSecrets Secrets `json:"session_secrets"`
72+
Interface string `json:"interface"`
7973

8074
MountTargetDevice *Device `json:"mount_target_device"`
8175
Devices []Device `json:"devices"`
@@ -247,6 +241,11 @@ func getMultipathDevice(devices []Device) (*Device, error) {
247241
return multipathDevice, nil
248242
}
249243

244+
// Connect is for backward-compatiblity with c.Connect()
245+
func Connect(c Connector) (string, error) {
246+
return c.Connect()
247+
}
248+
250249
// Connect attempts to connect a volume to this node using the provided Connector info
251250
func (c *Connector) Connect() (string, error) {
252251
if c.RetryCount == 0 {
@@ -270,8 +269,8 @@ func (c *Connector) Connect() (string, error) {
270269

271270
var lastErr error
272271
var devicePaths []string
273-
for _, target := range c.Targets {
274-
devicePath, err := c.connectTarget(&target, iFace, iscsiTransport)
272+
for _, target := range c.TargetPortals {
273+
devicePath, err := c.connectTarget(c.TargetIqn, target, iFace, iscsiTransport)
275274
if err != nil {
276275
lastErr = err
277276
} else {
@@ -311,9 +310,15 @@ func (c *Connector) Connect() (string, error) {
311310
return c.MountTargetDevice.GetPath(), nil
312311
}
313312

314-
func (c *Connector) connectTarget(target *TargetInfo, iFace string, iscsiTransport string) (string, error) {
315-
debug.Printf("Process targetIqn: %s, portal: %s\n", target.Iqn, target.Portal)
316-
baseArgs := []string{"-m", "node", "-T", target.Iqn, "-p", target.Portal}
313+
func (c *Connector) connectTarget(targetIqn string, target string, iFace string, iscsiTransport string) (string, error) {
314+
debug.Printf("Process targetIqn: %s, portal: %s\n", targetIqn, target)
315+
targetParts := strings.Split(target, ":")
316+
targetPortal := targetParts[0]
317+
targetPort := defaultPort
318+
if len(targetParts) > 1 {
319+
targetPort = targetParts[1]
320+
}
321+
baseArgs := []string{"-m", "node", "-T", targetIqn, "-p", targetPortal}
317322
// Rescan sessions to discover newly mapped LUNs. Do not specify the interface when rescanning
318323
// to avoid establishing additional sessions to the same target.
319324
if _, err := iscsiCmd(append(baseArgs, []string{"-R"}...)...); err != nil {
@@ -329,18 +334,14 @@ func (c *Connector) connectTarget(target *TargetInfo, iFace string, iscsiTranspo
329334
}
330335

331336
// create our devicePath that we'll be looking for based on the transport being used
332-
port := defaultPort
333-
if target.Port != "" {
334-
port = target.Port
335-
}
336337
// portal with port
337-
portal := strings.Join([]string{target.Portal, port}, ":")
338-
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", portal, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
338+
portal := strings.Join([]string{targetPortal, targetPort}, ":")
339+
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", portal, "iscsi", targetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
339340
if iscsiTransport != "tcp" {
340-
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", portal, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
341+
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", portal, "iscsi", targetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
341342
}
342343

343-
exists, _ := sessionExists(portal, target.Iqn)
344+
exists, _ := sessionExists(portal, targetIqn)
344345
if exists {
345346
debug.Printf("Session already exists, checking if device path %q exists", devicePath)
346347
if err := waitForPathToExist(&devicePath, c.RetryCount, c.CheckInterval, iscsiTransport); err != nil {
@@ -349,12 +350,12 @@ func (c *Connector) connectTarget(target *TargetInfo, iFace string, iscsiTranspo
349350
return devicePath, nil
350351
}
351352

352-
if err := c.discoverTarget(target, iFace, portal); err != nil {
353+
if err := c.discoverTarget(targetIqn, iFace, portal); err != nil {
353354
return "", err
354355
}
355356

356357
// perform the login
357-
err := Login(target.Iqn, portal)
358+
err := Login(targetIqn, portal)
358359
if err != nil {
359360
debug.Printf("Failed to login: %v", err)
360361
return "", err
@@ -368,7 +369,7 @@ func (c *Connector) connectTarget(target *TargetInfo, iFace string, iscsiTranspo
368369
return devicePath, nil
369370
}
370371

371-
func (c *Connector) discoverTarget(target *TargetInfo, iFace string, portal string) error {
372+
func (c *Connector) discoverTarget(targetIqn string, iFace string, portal string) error {
372373
if c.DoDiscovery {
373374
// build discoverydb and discover iscsi target
374375
if err := Discoverydb(portal, iFace, c.DiscoverySecrets, c.DoCHAPDiscovery); err != nil {
@@ -379,7 +380,7 @@ func (c *Connector) discoverTarget(target *TargetInfo, iFace string, portal stri
379380

380381
if c.DoCHAPDiscovery {
381382
// Make sure we don't log the secrets
382-
err := CreateDBEntry(target.Iqn, portal, iFace, c.DiscoverySecrets, c.SessionSecrets)
383+
err := CreateDBEntry(targetIqn, portal, iFace, c.DiscoverySecrets, c.SessionSecrets)
383384
if err != nil {
384385
debug.Printf("Error creating db entry: %s\n", err.Error())
385386
return err
@@ -389,21 +390,25 @@ func (c *Connector) discoverTarget(target *TargetInfo, iFace string, portal stri
389390
return nil
390391
}
391392

392-
// Disconnect performs a disconnect operation from an appliance.
393-
// Be sure to disconnect all deivces properly before doing this as it can result in data loss.
394-
func (c *Connector) Disconnect() {
395-
for _, target := range c.Targets {
396-
Logout(target.Iqn, target.Portal)
393+
// Disconnect is for backward-compatibility with c.Disconnect()
394+
func Disconnect(targetIqn string, targets []string) {
395+
for _, target := range targets {
396+
targetPortal := strings.Split(target, ":")[0]
397+
Logout(targetIqn, targetPortal)
397398
}
398399

399400
deleted := map[string]bool{}
400-
for _, target := range c.Targets {
401-
if _, ok := deleted[target.Iqn]; ok {
402-
continue
403-
}
404-
deleted[target.Iqn] = true
405-
DeleteDBEntry(target.Iqn)
401+
if _, ok := deleted[targetIqn]; ok {
402+
return
406403
}
404+
deleted[targetIqn] = true
405+
DeleteDBEntry(targetIqn)
406+
}
407+
408+
// Disconnect performs a disconnect operation from an appliance.
409+
// Be sure to disconnect all deivces properly before doing this as it can result in data loss.
410+
func (c *Connector) Disconnect() {
411+
Disconnect(c.TargetIqn, c.TargetPortals)
407412
}
408413

409414
// DisconnectVolume removes a volume from a Linux host.
@@ -588,6 +593,11 @@ func RemoveSCSIDevices(devices ...Device) error {
588593
return nil
589594
}
590595

596+
// PersistConnector is for backward-compatibility with c.Persist()
597+
func PersistConnector(c *Connector, filePath string) error {
598+
return c.Persist(filePath)
599+
}
600+
591601
// Persist persists the Connector to the specified file (ie /var/lib/pfile/myConnector.json)
592602
func (c *Connector) Persist(filePath string) error {
593603
//file := path.Join("mnt", c.VolumeName+".json")

iscsi/iscsi_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,8 @@ func TestConnectorPersistance(t *testing.T) {
668668
}
669669
c := Connector{
670670
VolumeName: "fake volume name",
671-
Targets: []TargetInfo{},
671+
TargetIqn: "fake target iqn",
672+
TargetPortals: []string{},
672673
Lun: 42,
673674
AuthType: "fake auth type",
674675
DiscoverySecrets: secret,

0 commit comments

Comments
 (0)