-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged all separated repository into one
- Loading branch information
Showing
27 changed files
with
4,716 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package itadm | ||
|
||
import "fmt" | ||
|
||
// Error structure | ||
type Error struct { | ||
Err error | ||
Debug string | ||
Stderr string | ||
} | ||
|
||
// returns the string representation of an Error. | ||
func (e Error) Error() string { | ||
return fmt.Sprintf("%s: %q => %s", e.Err, e.Debug, e.Stderr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package itadm | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// wrapper for itadm | ||
func cmdItadm(arg ...string) ([][]string, error) { | ||
c := command{Command: "itadm"} | ||
return c.Run(" ", arg...) | ||
} | ||
|
||
// ListTargetPortGroups - list itadm target port group (TPG) | ||
func ListTargetPortGroups(tpg string) ([]*TargetPortGroup, error) { | ||
args := []string{"list-tpg", "-v"} | ||
|
||
if tpg != "" { | ||
args = append(args, tpg) | ||
} | ||
|
||
out, err := cmdItadm(args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for len(out) == 0 { | ||
return nil, err | ||
} | ||
|
||
var tpgs []*TargetPortGroup | ||
var tpgTmp *TargetPortGroup | ||
|
||
for i := 0; i < len(out); i++ { | ||
if out[i][0] == "TARGET" { | ||
continue | ||
} | ||
if out[i][0] != "portals:" { | ||
count, err := strconv.ParseUint(out[i][1], 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tpgTmp = &TargetPortGroup{ | ||
TargetPortGroup: out[i][0], | ||
Count: count, | ||
} | ||
} | ||
if out[i][0] == "portals:" { | ||
tpgTmp.Portals = strings.Split(out[i][1], ",") | ||
tpgs = append(tpgs, tpgTmp) | ||
} | ||
} | ||
|
||
return tpgs, nil | ||
} | ||
|
||
// GetTargetPortGroup - get specified target port group. | ||
func GetTargetPortGroup(tpg string) (*TargetPortGroup, error) { | ||
tpgs, err := ListTargetPortGroups(tpg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tpgs[0], nil | ||
} | ||
|
||
// CreateTargetPortGroup - create target port group. | ||
func CreateTargetPortGroup(tpg string, ipaddrs []string) (*TargetPortGroup, error) { | ||
args := []string{"create-tpg", tpg} | ||
args = append(args, ipaddrs...) | ||
|
||
_, err := cmdItadm(args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return GetTargetPortGroup(tpg) | ||
} | ||
|
||
// Delete - delete target port group | ||
func (tpg *TargetPortGroup) Delete(force bool) error { | ||
args := []string{"delete-tpg"} | ||
|
||
if force { | ||
args = append(args, "-f") | ||
} | ||
|
||
args = append(args, tpg.TargetPortGroup) | ||
|
||
_, err := cmdItadm(args...) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// CreateTarget - Create Target. | ||
// TODO: add interface to chap authentication. | ||
func CreateTarget(iqn, alias, targetPortGroup string) (*Target, error) { | ||
args := []string{"create-target", "-a", "default"} | ||
|
||
if alias != "" { | ||
args = append(args, "-l", alias) | ||
} | ||
|
||
if targetPortGroup != "" { | ||
args = append(args, "-n", iqn) | ||
} | ||
|
||
if iqn != "" { | ||
args = append(args, "-t", targetPortGroup) | ||
} | ||
|
||
out, err := cmdItadm(args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
created_target := out[0][1] | ||
|
||
return GetTarget(created_target) | ||
} | ||
|
||
// ListTargets - list available targets | ||
func ListTargets(iqn string) ([]*Target, error) { | ||
args := []string{"list-target", "-v"} | ||
|
||
if iqn != "" { | ||
args = append(args, iqn) | ||
} | ||
|
||
out, err := cmdItadm(args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for len(out) == 0 { | ||
return nil, err | ||
} | ||
|
||
var targets []*Target | ||
var targetTmp *Target | ||
|
||
for i := 0; i < len(out); i++ { | ||
if out[i][0] == "TARGET" { | ||
continue | ||
} | ||
// check iqn. | ||
if out[i][0][0:4] == "iqn." { | ||
sessionCount, err := strconv.ParseUint(out[i][2], 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
targetTmp = &Target{ | ||
IQN: out[i][0], | ||
State: out[i][1], | ||
Sessions: sessionCount, | ||
} | ||
} | ||
if out[i][0] == "alias:" { | ||
targetTmp.Alias = out[i][1] | ||
} | ||
if out[i][0] == "auth:" { | ||
targetTmp.Auth = out[i][1] | ||
} | ||
if out[i][0] == "targetchapuser:" { | ||
targetTmp.TargetChapUser = out[i][1] | ||
} | ||
if out[i][0] == "targetchapsecret:" { | ||
targetTmp.TargetChapSecret = out[i][1] | ||
} | ||
if out[i][0] == "tpg-tags:" { | ||
targetTmp.TpgTags = out[i][1] | ||
targets = append(targets, targetTmp) | ||
} | ||
} | ||
|
||
return targets, nil | ||
} | ||
|
||
// GetTarget - Get specified target | ||
func GetTarget(iqn string) (*Target, error) { | ||
target, err := ListTargets(iqn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return target[0], nil | ||
} | ||
|
||
// Delete - delete Target | ||
func (target *Target) Delete(force bool) error { | ||
args := []string{"delete-target"} | ||
|
||
if force { | ||
args = append(args, "-f") | ||
} | ||
|
||
args = append(args, target.IQN) | ||
|
||
_, err := cmdItadm(args...) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package itadm_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/d-helios/itadm" | ||
"testing" | ||
) | ||
|
||
var tpgs = []string{ | ||
"tpg1", | ||
"tpg2", | ||
"tpg3", | ||
"tpg4", | ||
} | ||
|
||
var ipaddrs = [][]string{ | ||
[]string{"192.168.2.100"}, | ||
[]string{"192.168.2.101"}, | ||
[]string{"192.168.3.100", "192.168.4.100"}, | ||
[]string{"192.168.3.101", "192.168.4.101", "192.168.5.101", "192.168.6.101"}, | ||
} | ||
|
||
var targets = []string{ | ||
"iqn.1986-03.com.sun:9f0f1c727925", | ||
"iqn.1986-03.com.sun:9f0f1c727926", | ||
"iqn.1986-03.com.sun:9f0f1c727927", | ||
"iqn.1986-03.com.sun:9f0f1c727928", | ||
} | ||
|
||
func TestCreateTargetPortGroup(t *testing.T) { | ||
for index := range tpgs { | ||
tpg, err := itadm.CreateTargetPortGroup(tpgs[index], ipaddrs[index]) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
if tpg.TargetPortGroup != tpgs[index] { | ||
t.Fatalf(itadm.Error{ | ||
Err: errors.New( | ||
fmt.Sprintf("TargetPortGroup name is not "+ | ||
"equal to specified one. TGP: %q", tpg)), | ||
Debug: "", | ||
Stderr: "", | ||
}.Error()) | ||
} | ||
if tpg.Count != uint64(len(ipaddrs[index])) { | ||
t.Fatalf(itadm.Error{ | ||
Err: errors.New( | ||
fmt.Sprintf("TargetPortGroup count "+ | ||
"not equal to specified. TGP: %q", tpg)), | ||
Debug: "", | ||
Stderr: "", | ||
}.Error()) | ||
} | ||
} | ||
} | ||
|
||
func TestCreateTarget(t *testing.T) { | ||
for index := range targets { | ||
alias := targets[index][len(targets[index])-4 : len(targets[index])] | ||
tpg, err := itadm.GetTargetPortGroup(tpgs[index]) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
target, err := itadm.CreateTarget(targets[index], alias, tpg.TargetPortGroup) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
|
||
if target.IQN != targets[index] { | ||
t.Fatalf(itadm.Error{ | ||
Err: errors.New( | ||
fmt.Sprintf("Target IQN not match specified."+ | ||
".Target: %q", target)), | ||
Debug: "", | ||
Stderr: "", | ||
}.Error()) | ||
} | ||
} | ||
} | ||
|
||
func TestTarget_Delete(t *testing.T) { | ||
targets, err := itadm.ListTargets("") | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
|
||
for _, target := range targets { | ||
err := target.Delete(true) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
} | ||
} | ||
|
||
func TestTargetPortGroup_Delete(t *testing.T) { | ||
tpgs, err := itadm.ListTargetPortGroups("") | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
|
||
for _, tpg := range tpgs { | ||
err := tpg.Delete(true) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package itadm | ||
|
||
type TargetPortGroup struct { | ||
TargetPortGroup string `json:"tpg"` | ||
Count uint64 `json:"count"` | ||
Portals []string `json:"portals"` | ||
} | ||
|
||
type Target struct { | ||
IQN string `json:"iqn"` | ||
State string `json:"state"` | ||
Sessions uint64 `json:"sessions"` | ||
Alias string `json:"alias"` | ||
Auth string `json:"auth"` | ||
TargetChapUser string `json:"chapuser"` | ||
TargetChapSecret string `json:"chapsecret"` | ||
TpgTags string `json:"tpg-tags"` | ||
} |
Oops, something went wrong.