Skip to content

Commit 366f319

Browse files
authored
Merge pull request #35 from humblec/rel-alpha1
Do error checking and error wrapping in the connector functions.
2 parents 83b78f5 + 56f04a2 commit 366f319

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

iscsi/iscsi.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ func (c *Connector) Connect() (string, error) {
293293
c.MountTargetDevice = mountTargetDevice
294294
if err != nil {
295295
debug.Printf("Connect failed: %v", err)
296-
RemoveSCSIDevices(c.Devices...)
296+
err := RemoveSCSIDevices(c.Devices...)
297+
if err != nil {
298+
return "", err
299+
}
297300
c.MountTargetDevice = nil
298301
c.Devices = []Device{}
299302
return "", err
@@ -392,15 +395,21 @@ func (c *Connector) discoverTarget(targetIqn string, iFace string, portal string
392395
func Disconnect(targetIqn string, targets []string) {
393396
for _, target := range targets {
394397
targetPortal := strings.Split(target, ":")[0]
395-
Logout(targetIqn, targetPortal)
398+
err := Logout(targetIqn, targetPortal)
399+
if err != nil {
400+
return
401+
}
396402
}
397403

398404
deleted := map[string]bool{}
399405
if _, ok := deleted[targetIqn]; ok {
400406
return
401407
}
402408
deleted[targetIqn] = true
403-
DeleteDBEntry(targetIqn)
409+
err := DeleteDBEntry(targetIqn)
410+
if err != nil {
411+
return
412+
}
404413
}
405414

406415
// Disconnect performs a disconnect operation from an appliance.
@@ -669,7 +678,9 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {
669678
for _, device := range c.Devices {
670679
devicePaths = append(devicePaths, device.GetPath())
671680
}
672-
681+
if c.MountTargetDevice == nil {
682+
return nil, fmt.Errorf("mountTargetDevice in the connector is nil")
683+
}
673684
if devices, err := GetSCSIDevices([]string{c.MountTargetDevice.GetPath()}, false); err != nil {
674685
return nil, err
675686
} else {

iscsi/iscsiadm.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ func CreateDBEntry(tgtIQN, portal, iFace string, discoverySecrets, sessionSecret
6565

6666
if discoverySecrets.SecretsType == "chap" {
6767
debug.Printf("Setting CHAP Discovery...")
68-
createCHAPEntries(baseArgs, discoverySecrets, true)
68+
err := createCHAPEntries(baseArgs, discoverySecrets, true)
69+
if err != nil {
70+
return err
71+
}
6972
}
7073

7174
if sessionSecrets.SecretsType == "chap" {
7275
debug.Printf("Setting CHAP Session...")
73-
createCHAPEntries(baseArgs, sessionSecrets, false)
76+
err := createCHAPEntries(baseArgs, sessionSecrets, false)
77+
if err != nil {
78+
return err
79+
}
7480
}
7581

7682
return err

iscsi/multipath.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ package iscsi
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78
"os/exec"
89
"strings"
910
"time"
1011
)
1112

12-
type pathGroup struct {
13-
Paths []path `json:"paths"`
14-
}
15-
16-
type path struct {
17-
Device string `json:"dev"`
18-
}
19-
2013
// ExecWithTimeout execute a command with a timeout and returns an error if timeout is excedeed
2114
func ExecWithTimeout(command string, args []string, timeout time.Duration) ([]byte, error) {
2215
debug.Printf("Executing command '%v' with args: '%v'.\n", command, args)
@@ -35,13 +28,14 @@ func ExecWithTimeout(command string, args []string, timeout time.Duration) ([]by
3528
// We want to check the context error to see if the timeout was executed.
3629
// The error returned by cmd.Output() will be OS specific based on what
3730
// happens when a process is killed.
38-
if ctx.Err() == context.DeadlineExceeded {
31+
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
3932
debug.Printf("Command '%s' timeout reached.\n", command)
4033
return nil, ctx.Err()
4134
}
4235

4336
if err != nil {
44-
if ee, ok := err.(*exec.ExitError); ok {
37+
var ee *exec.ExitError
38+
if ok := errors.Is(err, ee); ok {
4539
debug.Printf("Non-zero exit code: %s\n", err)
4640
err = fmt.Errorf("%s", ee.Stderr)
4741
}

0 commit comments

Comments
 (0)