Skip to content

Commit

Permalink
add check existed volume by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
wnxn committed May 29, 2018
1 parent c0790c9 commit 49a1380
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/container-storage-interface/spec"
version = "v0.2.0"
26 changes: 26 additions & 0 deletions cmd/block/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"flag"
"os"
)

func init(){
flag.Set("logtostderr", "true")
}

var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
driverName = flag.String("drivername", "csi-qingcloud-performance", "name of the driver")
nodeID = flag.String("nodeid", "", "node id")
)

func main() {
flag.Parse()
handle()
os.Exit(0)
}

func handle(){

}
77 changes: 77 additions & 0 deletions pkg/block/volume_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package block

import (
// qcclient "github.com/yunify/qingcloud-sdk-go/client"
qcconfig "github.com/yunify/qingcloud-sdk-go/config"
qcservice "github.com/yunify/qingcloud-sdk-go/service"
"github.com/golang/glog"
)

const (
VolumeTypePerformance = 0
VolumeTypeHighPerformance = 3 // Only support BJ2
VolumeTypeCapacity = 1 // BJ1, AS1: 1; BJ2, GD1: 2
)

type persistentVolume struct{
VolName string `json:"volName"`
VolID string `json:"volID"`
VolType string `json:"volType"`
VolSize int64 `json:"volSize"`
}

type volumeManager struct {
volumeService *qcservice.VolumeService
persistentVolume *persistentVolume
}

func newVolumeManager(config *qcconfig.Config)(*volumeManager, error){
qcService, err := qcservice.Init(config)
if err != nil{
return nil,err
}
// create volume service
volumeService, err := qcService.Volume(config.Zone)
if err != nil {
return nil, err
}

qc := volumeManager{
volumeService: volumeService,
persistentVolume: &persistentVolume{},
}
glog.Infof("newVolumeManager init finish, zone: %v", config.Zone)
return &qc, nil
}

// check existence volume by volume ID
// Return: true: volume exist, false: volume not exist
func (vm *volumeManager)IsVolumeIdExist()(bool,error){
input := qcservice.DescribeVolumesInput{}
id := vm.persistentVolume.VolID
input.Volumes = append(input.Volumes, &id)
// consult volume info
output, err := vm.volumeService.DescribeVolumes(&input)
if err != nil{
return false, err
}
// IaaS response return code not equal to 0
if *output.RetCode != 0{
glog.Errorf("call DescribeVolumes() return %d, volume ID: %s, volume.zone: %s",
*output.RetCode, id, vm.volumeService.Properties.Zone)
}
// IaaS response total count more than 1
if *output.TotalCount == 0 {

}
if *output.TotalCount > 1 {
glog.Errorf("has duplicated volume ID %s in zone %s",
id, vm.volumeService.Properties.Zone)
}
if *output.TotalCount == 1 &&
*output.VolumeSet[0].VolumeID == id{
return true, nil
}else{
return false, nil
}
}
52 changes: 52 additions & 0 deletions pkg/block/volume_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package block

import(
qcconfig "github.com/yunify/qingcloud-sdk-go/config"
"testing"
"github.com/golang/glog"
)

func createConfig()(config *qcconfig.Config, err error){
accessKeyId := ""
accessKeySecret := ""
config,err = qcconfig.New(accessKeyId,accessKeySecret)
config.Zone = "sh1a"
if err != nil{
return nil, err
}else{
return config,nil
}
}

func TestVolumeIdExist(t *testing.T){
// create volume manager
config, err := createConfig()
if err != nil{
glog.Error(err)
}
vm, err := newVolumeManager(config)
if err != nil{
glog.Error(err)
}

testcase := []struct{
id string
ret bool
}{
{"vol-57sm6cas", true},
{"vol-aseereww", false},
}
for _, v:=range testcase{
vm.persistentVolume.VolID = v.id
flag, err := vm.IsVolumeIdExist()
if err != nil{
t.Errorf("test in %s: error: %v", v.id, err)
}
if flag != v.ret{
t.Errorf("testcase failed in %s, expected %t, actually %t",
v.id, v.ret, flag)
}
}


}

0 comments on commit 49a1380

Please sign in to comment.