Skip to content

Commit

Permalink
Fix some conflict errors
Browse files Browse the repository at this point in the history
Signed-off-by: leonwanghui <wanghui71leon@gmail.com>
  • Loading branch information
leonwanghui committed Mar 18, 2019
2 parents ad93f69 + 3a29829 commit 398148f
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 100 deletions.
1 change: 1 addition & 0 deletions contrib/drivers/huawei/fusionstorage/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ const (
VolumeNotExistErrorCode = "32150005"
CmdBin = "fsc_cli"
MaxRetryNode = 3
DefaultConfPath = "/etc/opensds/driver/fusionstorage.yaml"
)
109 changes: 64 additions & 45 deletions contrib/drivers/huawei/fusionstorage/dsware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@ package fusionstorage
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"

log "github.com/golang/glog"
. "github.com/opensds/opensds/contrib/drivers/utils/config"
. "github.com/opensds/opensds/pkg/model"
pb "github.com/opensds/opensds/pkg/model/proto"
"github.com/opensds/opensds/pkg/utils/config"
"github.com/satori/go.uuid"
)

type Driver struct {
cli *Cli
cli *FsCli
conf *Config
}

type AuthOptions struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Url string `yaml:"url"`
FmIp string `yaml:"fmIp,omitempty"`
FsaIp []string `yaml:"fsaIp,flow"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Url string `yaml:"url"`
FmIp string `yaml:"fmIp,omitempty"`
FsaIp []string `yaml:"fsaIp,flow"`
PwdEncrypter string `yaml:"PwdEncrypter,omitempty"`
EnableEncrypted bool `yaml:"EnableEncrypted,omitempty"`
}

type Config struct {
Expand All @@ -51,39 +53,28 @@ func (d *Driver) Setup() error {

d.conf = conf

path := "./testdata/fusionstorage.yaml"
path := config.CONF.OsdsDock.Backends.HuaweiFusionStorage.ConfigPath
if path == "" {
path = ""
path = DefaultConfPath
}

Parse(conf, path)

client, err := newRestCommon(conf.Username, conf.Password, conf.Url, conf.FmIp, conf.FsaIp)
client, err := newRestCommon(conf)
if err != nil {
return err
}

err = client.login()
if err != nil {
fmt.Printf("Get new client failed, %v", err)
return err
}

_, err = exec.LookPath(CmdBin)
if err != nil {
if err == exec.ErrNotFound {
return fmt.Errorf("%q executable not found in $PATH", CmdBin)
}
log.Errorf("Get new client failed, %v", err)
return err
}

err = client.StartServer()
if err != nil {
log.Errorf("Get new client failed, %v", err)
return err
}

d.cli = client

log.Info("Get new client success")
return nil
}

Expand Down Expand Up @@ -133,6 +124,7 @@ func (d *Driver) ListPools() ([]*StoragePoolSpec, error) {
var pols []*StoragePoolSpec
pools, err := d.cli.queryPoolInfo()
if err != nil {
log.Errorf("List pools failed: %v", err)
return nil, err
}

Expand All @@ -149,7 +141,7 @@ func (d *Driver) ListPools() ([]*StoragePoolSpec, error) {
// Make sure uuid is unique
Id: uuid.NewV5(uuid.NamespaceOID, name).String(),
},
Name: poolId,
Name: "fusionstorage_pool" + poolId,
TotalCapacity: p.TotalCapacity >> UnitGiShiftBit,
FreeCapacity: (p.TotalCapacity - p.UsedCapacity) >> UnitGiShiftBit,
StorageType: c.Pool[poolId].StorageType,
Expand All @@ -161,18 +153,17 @@ func (d *Driver) ListPools() ([]*StoragePoolSpec, error) {
}
pols = append(pols, pol)
}

log.Info("List pools success")
return pols, nil
}

func (d *Driver) InitializeConnection(opt *pb.CreateVolumeAttachmentOpts) (*ConnectionInfo, error) {
lunId := opt.GetMetadata()[LunId]
if lunId == "" {
return nil, fmt.Errorf("Lun id is empty.")
}

connectorType := opt.GetMetadata()[ConnectorType]
if connectorType == "" || connectorType != FusionstorageIscsi {
return nil, fmt.Errorf("Connector type is empty or mismatch.")
msg := "Lun id is empty"
log.Error(msg)
return nil, fmt.Errorf(msg)
}

hostInfo := opt.GetHostInfo()
Expand All @@ -181,62 +172,74 @@ func (d *Driver) InitializeConnection(opt *pb.CreateVolumeAttachmentOpts) (*Conn
hostName := hostInfo.GetHost()

if initiator == "" || hostName == "" {
return nil, fmt.Errorf("Host name or initiator is empty.")
msg := "Host name or initiator is empty."
log.Error(msg)
return nil, fmt.Errorf(msg)
}

// Create port if not exist.
err := d.cli.queryPortInfo(initiator)
if err != nil {
if err.Error() == InitiatorNotExistErrorCode {
if strings.Contains(err.Error(), InitiatorNotExistErrorCode) {
err := d.cli.createPort(initiator)
if err != nil {
log.Errorf("Create port failed: %v", err)
return nil, err
}
} else {
log.Errorf("Query port info failed: %v", err)
return nil, err
}
}

// Create host if not exist.
isFind, err := d.cli.queryHostInfo(hostName)
if err != nil {
return nil, fmt.Errorf("Query host failed, host name =%s, error: %v", hostName, err)
log.Errorf("Query host info failed: %v", err)
return nil, err
}

if !isFind {
err = d.cli.createHost(hostInfo)
if err != nil {
return nil, fmt.Errorf("Create host failed, host name =%s, error: %v", hostName, err)
log.Errorf("Create host failed: %v", err)
return nil, err
}
}

// Add port to host if port not add to the host
hostPortMap, err := d.cli.queryHostByPort(initiator)
if err != nil {
log.Errorf("Query host by port failed: %v", err)
return nil, err
}

h, ok := hostPortMap.PortHostMap[initiator]
if ok && h[0] != hostName {
return nil, fmt.Errorf("Initiator is already added to another host, host name =%s", h[0])
msg := fmt.Sprintf("Initiator is already added to another host, host name =%s", h[0])
log.Error(msg)
return nil, fmt.Errorf(msg)
}

if !ok {
err = d.cli.addPortToHost(hostName, initiator)
if err != nil {
return nil, fmt.Errorf("Add port to host failed, error %v", err)
log.Errorf("Add port to host failed: %v", err)
return nil, err
}
}

// Map volume to host
err = d.cli.addLunsToHost(hostName, lunId)
if err != nil {
return nil, fmt.Errorf("Add luns to host failed, error %v", err)
log.Errorf("Add luns to host failed: %v", err)
return nil, err
}

// Get target lun id
hostLunList, err := d.cli.queryHostLunInfo(hostName)
if err != nil {
log.Errorf("Query host lun info failed: %v", err)
return nil, err
}

Expand All @@ -250,6 +253,7 @@ func (d *Driver) InitializeConnection(opt *pb.CreateVolumeAttachmentOpts) (*Conn
// Get target iscsi portal info
targetPortalInfo, err := d.cli.queryIscsiPortal(initiator)
if err != nil {
log.Errorf("Query iscsi portal failed: %v", err)
return nil, err
}

Expand All @@ -262,7 +266,7 @@ func (d *Driver) InitializeConnection(opt *pb.CreateVolumeAttachmentOpts) (*Conn
"volume_id": opt.GetVolumeId(),
"description": "huawei",
"host_name": hostName,
"target_lun": targetLunId,
"targetLun": targetLunId,
"connect_type": FusionstorageIscsi,
"host": hostName,
"initiator": initiator,
Expand All @@ -271,36 +275,47 @@ func (d *Driver) InitializeConnection(opt *pb.CreateVolumeAttachmentOpts) (*Conn
},
}

log.Infof("Initialize connection success: %v", connInfo)
return connInfo, nil
}

func (d *Driver) TerminateConnection(opt *pb.DeleteVolumeAttachmentOpts) error {
lunId := opt.GetMetadata()[LunId]
if lunId == "" {
return fmt.Errorf("Lun id is empty.")
msg := "Lun id is empty."
log.Error(msg)
return fmt.Errorf(msg)
}

hostInfo := opt.GetHostInfo()

initiator := hostInfo.GetInitiator()
hostName := hostInfo.GetHost()

if initiator == "" || hostName == "" {
return fmt.Errorf("Host name or initiator is empty.")
msg := "Host name or initiator is empty."
log.Error(msg)
return fmt.Errorf(msg)
}

// Make sure that host is exist.
hostIsFind, err := d.cli.queryHostInfo(hostName)
if err != nil {
return fmt.Errorf("Query host failed, host name =%s, error: %v", hostName, err)
msg := fmt.Sprintf("Query host failed, host name =%s, error: %v", hostName, err)
log.Error(msg)
return fmt.Errorf(msg)
}

if !hostIsFind {
return fmt.Errorf("Host can not be found, host name =%s", hostName)
msg := fmt.Sprintf("Host can not be found, host name =%s", hostName)
log.Error(msg)
return fmt.Errorf(msg)
}

// Check whether the volume attach to the host
hostLunList, err := d.cli.queryHostLunInfo(hostName)
if err != nil {
log.Errorf("Query host lun info failed, %v", err)
return err
}

Expand All @@ -313,19 +328,22 @@ func (d *Driver) TerminateConnection(opt *pb.DeleteVolumeAttachmentOpts) error {
}

if !lunIsFind {
return fmt.Errorf("The lun %s is not attach to the host %s", lunId, hostName)
msg := fmt.Sprintf("The lun %s is not attach to the host %s", lunId, hostName)
log.Error(msg)
return fmt.Errorf(msg)
}

// Remove lun from host
err = d.cli.deleteLunFromHost(hostName, lunId)
if err != nil {
log.Errorf("Delete lun from host failed, %v", err)
return err
}

// Remove initiator and host if there is no lun belong to the host
fmt.Println(len(hostLunList.LunList), hostLunList.LunList)
hostLunList, err = d.cli.queryHostLunInfo(hostName)
if err != nil {
log.Errorf("Query host lun info failed, %v", err)
return err
}

Expand All @@ -335,6 +353,7 @@ func (d *Driver) TerminateConnection(opt *pb.DeleteVolumeAttachmentOpts) error {
d.cli.deletePort(initiator)
}

log.Info("Terminate Connection success.")
return nil
}

Expand Down
Loading

0 comments on commit 398148f

Please sign in to comment.