Skip to content

Commit

Permalink
Fix some minor styling issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-wangxu committed Apr 3, 2018
1 parent 60c657b commit 1906382
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
31 changes: 17 additions & 14 deletions client/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
"github.com/sirupsen/logrus"
)

var log *logrus.Logger = logrus.New()
var log = logrus.New()

// VolumeFormat defines the `volume` output format
var VolumeFormat = `Volume Information:
Multipath: %s
Single Paths:
Expand All @@ -40,6 +41,7 @@ Multipath ID: %s
WWN: %s
`

// HostInfoFormat defines the `info` command output
var HostInfoFormat = `
Host Name(FQDN): %s
iSCSI Qualified Name(IQN): %s
Expand All @@ -51,7 +53,7 @@ Connected iSCSI Sessions:
%s
`

// Enable the console log for client
// InitLog enables the console log for client
func InitLog(debug bool) error {
if debug {
log = logrus.New()
Expand Down Expand Up @@ -84,17 +86,16 @@ func HandleConnect(args ...string) error {
var err error
if len(args) <= 0 {
log.Error("Target IP or wwn is required.")
err = fmt.Errorf("Target IP or wwn is required.")
err = fmt.Errorf("target IP or wwn is required")
} else if len(args) == 1 {
// User only supply the LUN ID, so did a wildcard scan for all connected targets
err = fmt.Errorf("Currently [lun id] is not supported.")
log.WithError(err).Error("Unsupported parameters.")
log.Error("%s", args)
err = fmt.Errorf("currently [lun id] is not supported")
log.WithError(err).Error("Unsupported parameters. {%s}", args)
} else {
target := args[0]
// Make sure the last param is LUN ID.
if _, err = ValidateLunId(args[len(args)-1:]); err == nil {
if IsIpLike(target) {
if _, err = ValidateLunID(args[len(args)-1:]); err == nil {
if IsIPLike(target) {
return HandleISCSIConnect(args...)
}
if IsFcLike(target) {
Expand All @@ -114,10 +115,10 @@ func HandleDisconnect(args ...string) error {
func HandleExtend(args ...string) error {
var err error
if len(args) <= 0 {
err = fmt.Errorf("Need device name or Target IP with LUN ID.")
err = fmt.Errorf("need device name or Target IP with LUN ID")
} else if len(args) == 1 {
// User only supplies the local device name
err = fmt.Errorf("Currently device name is not supported.")
err = fmt.Errorf("currently device name is not supported")
} else {
// User specify TargetIP with LUN ID
err = HandleISCSIExtend(args...)
Expand All @@ -126,6 +127,7 @@ func HandleExtend(args ...string) error {

}

// HandleInfo displays the host information
func HandleInfo(args ...string) error {
hostInfo, err := connector.GetHostInfo()
if err != nil {
Expand Down Expand Up @@ -154,13 +156,14 @@ func BeautifyHostInfo(info connector.HostInfo) {
fmt.Printf(HostInfoFormat, info.Hostname, info.Initiator, sWwns, sTargetWwns, sIscsiTargets)
}

func ValidateLunId(lunIDs []string) ([]int, error) {
// ValidateLunID validates the LunIDs as integer
func ValidateLunID(lunIDs []string) ([]int, error) {
var err error
re, _ := regexp.Compile("\\d+")
var ret []int
for _, lun := range lunIDs {
if re.MatchString(lun) == false {
err = fmt.Errorf("%s does not look like a LUN ID.", lun)
err = fmt.Errorf("%s does not look like a LUN ID", lun)
break
}
i, _ := strconv.Atoi(lun)
Expand All @@ -181,8 +184,8 @@ func IsLunLike(data string) bool {
return true
}

// IsIpLike tests if *data* is a ipv4 address.
func IsIpLike(data string) bool {
// IsIPLike tests if *data* is a ipv4 address.
func IsIPLike(data string) bool {
// IPv4 match
if m, _ := regexp.MatchString("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", data); !m {
return false
Expand Down
10 changes: 5 additions & 5 deletions client/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func TestHandleInfoFailed(t *testing.T) {
assert.Error(t, err)
}
func TestValidateLunId_True(t *testing.T) {
lunids, err := ValidateLunId([]string{"12", "113"})
lunids, err := ValidateLunID([]string{"12", "113"})
assert.Nil(t, err)
assert.Len(t, lunids, 2)
}

func TestValidateLunId_False(t *testing.T) {
lunids, err := ValidateLunId([]string{"a", "b"})
lunids, err := ValidateLunID([]string{"a", "b"})
assert.Error(t, err)
assert.Len(t, lunids, 0)
}
Expand All @@ -67,15 +67,15 @@ func TestIsLunLike_True(t *testing.T) {
}

func TestIsIpLike_True(t *testing.T) {
r := IsIpLike("192.168.1.29")
r := IsIPLike("192.168.1.29")
assert.True(t, r)
}

func TestIsIpLike_False(t *testing.T) {
r := IsIpLike("192.168.1.")
r := IsIPLike("192.168.1.")
assert.False(t, r)

r = IsIpLike("129")
r = IsIPLike("129")
assert.False(t, r)
}

Expand Down
13 changes: 8 additions & 5 deletions client/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,34 @@ package client

import (
"fmt"
"github.com/peter-wangxu/goock/connector"
"strconv"
"github.com/peter-wangxu/goock/connector"
)

var fcConnector = connector.NewFibreChannelConnector()

// SetFcConnector sets the connector for FC connection
func SetFcConnector(fc connector.FibreChannelInterface) {
fcConnector = fc
}

func Convert2ConnectionProperty(wwns []string, lunId string) connector.ConnectionProperty {
// Convert2ConnectionProperty converts wwn and lunid pair into ConnnectionProperty
func Convert2ConnectionProperty(wwns []string, lunID string) connector.ConnectionProperty {
var property connector.ConnectionProperty
property.TargetWwns = wwns
property.TargetLun, _ = strconv.Atoi(lunId)
property.TargetLun, _ = strconv.Atoi(lunID)
property.StorageProtocol = connector.FcProtocol

return property
}

// HandleFCConnect handles the connection of FC target
func HandleFCConnect(args ...string) error {
var err error
if len(args) == 1 {
// User only supply the LUN ID, so did a wildcard scan for all connected targets
err = fmt.Errorf("Currently [lun id] is not supported.")
err = fmt.Errorf("currently [LUN ID] is not supported")
log.WithError(err).Error("Unsupported parameters.")
log.Error("%s", args)
} else {
targets := args[:len(args)-1]

Expand All @@ -58,6 +60,7 @@ func HandleFCConnect(args ...string) error {
return err
}

// HandleFCExtend handle the request to extend the FC devices.
func HandleFCExtend(args ...string) error {
return nil
}
23 changes: 15 additions & 8 deletions client/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (

var iscsiConnector = connector.NewISCSIConnector()

// SetISCSIConnector sets the iSCSI connector
// This will help when doing mock testing
func SetISCSIConnector(iscsi connector.ISCSIInterface) {
iscsiConnector = iscsi
}

// Session2ConnectionProperty converts a session to an ConnectionProperty
func Session2ConnectionProperty(sessions []model.ISCSISession, lun int) connector.ConnectionProperty {
conn := connector.ConnectionProperty{}
var portals []string
Expand All @@ -45,10 +48,11 @@ func Session2ConnectionProperty(sessions []model.ISCSISession, lun int) connecto
return conn
}

// HandleISCSIConnect connects the iSCSI target via iscsiadm
func HandleISCSIConnect(args ...string) error {
var err error
if len(args) == 1 {
err = fmt.Errorf("Currently Target IP is not supported.")
err = fmt.Errorf("currently Target IP is not supported")
log.Error("Target IP and LUN ID(s) is required.")
//log.Info("LUN ID is not specified, will query all LUNs on target IP: %s", args[0])
//targetIP := args[0]
Expand All @@ -62,7 +66,7 @@ func HandleISCSIConnect(args ...string) error {
} else {
log.Debugf("Trying to validate the target IP : %s, LUN ID: %s", args[0], args[1:])
var lunIDs []int
lunIDs, err = ValidateLunId(args[1:])
lunIDs, err = ValidateLunID(args[1:])
if err == nil {
targetIP := args[0]
sessions := iscsiConnector.DiscoverPortal(targetIP)
Expand All @@ -76,20 +80,20 @@ func HandleISCSIConnect(args ...string) error {
return err
}

// HandleISCSIDisconnect disconnects the volume devices from local host
// Accessible format likes follows:
// /dev/sdb
// sdb
// <Target IP> <LUN ID>

func HandleISCSIDisconnect(args ...string) error {
var err error
if len(args) == 1 {
// TODO Support the device name removal
err = fmt.Errorf("Currently device name is not supported.")
err = fmt.Errorf("currently device name is not supported")
} else if len(args) >= 2 {

targetIP := args[0]
lunIDs, err := ValidateLunId(args[1:])
lunIDs, err := ValidateLunID(args[1:])
if err == nil {
sessions := iscsiConnector.DiscoverPortal(targetIP)
for _, lun := range lunIDs {
Expand All @@ -105,11 +109,12 @@ func HandleISCSIDisconnect(args ...string) error {
return err
}

// HandleISCSIExtend extends the iscsi block device
func HandleISCSIExtend(args ...string) error {
targetIp := args[0]
lunIDs, err := ValidateLunId(args[1:])
targetIP := args[0]
lunIDs, err := ValidateLunID(args[1:])

sessions := iscsiConnector.DiscoverPortal(targetIp)
sessions := iscsiConnector.DiscoverPortal(targetIP)
if err == nil {
for _, lun := range lunIDs {
property := Session2ConnectionProperty(sessions, lun)
Expand All @@ -119,12 +124,14 @@ func HandleISCSIExtend(args ...string) error {
return err
}

// FetchVolumeInfo fetches the volume information via iscsiConnector.
func FetchVolumeInfo(sessions []model.ISCSISession, lun int) (connector.VolumeInfo, error) {
connectionProperty := Session2ConnectionProperty(sessions, lun)
return iscsiConnector.ConnectVolume(connectionProperty)

}

// BeautifyVolumeInfo output the volume information to stdout.
func BeautifyVolumeInfo(info connector.VolumeInfo) {
beautifiedPaths := ""
for _, path := range info.Paths {
Expand Down
2 changes: 1 addition & 1 deletion client/iscsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (fake *FakeISCSIConnector) GetHostInfo() (connector.HostInfo, error) {

func (fake *FakeISCSIConnector) ConnectVolume(connectionProperty connector.ConnectionProperty) (connector.VolumeInfo, error) {
if len(connectionProperty.TargetPortals) > 0 && connectionProperty.TargetPortals[0] == "10.244.244.244" {
return connector.VolumeInfo{}, fmt.Errorf("Failed to connect volume.")
return connector.VolumeInfo{}, fmt.Errorf("failed to connect volume")
}
return connector.VolumeInfo{}, nil
}
Expand Down
20 changes: 13 additions & 7 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@ import (
)

const (
// CommandName sets the cli entry
CommandName = "goock"
// Version is the string of cli version
// Generated by http://patorjk.com/software/taag/#p=display&f=Slant&t=Goock
// need to be updated along with version update.
Version = `
______ __ ____ ___ ____
/ ____/___ ____ _____/ /__ / __ \ < // __ \
/ / __/ __ \/ __ \/ ___/ //_/ / / / / / // / / /
/ /_/ / /_/ / /_/ / /__/ ,< / /_/ / / // /_/ /
\____/\____/\____/\___/_/|_| \____(_)_(_)____/
v0.1.0
______ __ ____ ___ ___
/ ____/___ ____ _____/ /__ / __ \ < /|__ \
/ / __/ __ \/ __ \/ ___/ //_/ / / / / / / __/ /
/ /_/ / /_/ / /_/ / /__/ ,< / /_/ / / / / __/
\____/\____/\____/\___/_/|_| \____(_)_(_)____/
v0.1.2
`
// Usage specifies the simple usage
Usage = `A easy-to-use block device management tool.`
)

// App is the super struct of urfave app
type App struct {
*cli.App
}

// NewApp initiates the CLI entry.
func NewApp() *App {
app := cli.NewApp()
app.Version = Version
Expand Down

0 comments on commit 1906382

Please sign in to comment.