Skip to content

Commit

Permalink
Clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-v committed Nov 16, 2019
1 parent 25beec6 commit aaedd1f
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 328 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Client struct {
*ReplicationMgr
*FileShareMgr
*HostMgr
*ZoneMgr
*AvailabilityZoneMgr

cfg *Config
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewClient(c *Config) (*Client, error) {
ReplicationMgr: NewReplicationMgr(r, c.Endpoint, t),
FileShareMgr: NewFileShareMgr(r, c.Endpoint, t),
HostMgr: NewHostMgr(r, c.Endpoint, t),
ZoneMgr: NewZoneMgr(r, c.Endpoint, t),
AvailabilityZoneMgr: NewAvailabilityZoneMgr(r, c.Endpoint, t),
}, nil
}

Expand Down
15 changes: 7 additions & 8 deletions client/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ func NewFakeClient(config *Config) *Client {
Receiver: NewFakeFileShareReceiver(),
Endpoint: config.Endpoint,
},
<<<<<<< 211553282aff0b5c8d3f9d7faec703fbb9d34cdb
HostMgr: &HostMgr{
Receiver: NewFakeHostReceiver(),
=======
ZoneMgr: &ZoneMgr{
Endpoint: config.Endpoint,
},
AvailabilityZoneMgr: &AvailabilityZoneMgr{
Receiver: NewFakeZoneReceiver(),
>>>>>>> Add testcases for zone client
Endpoint: config.Endpoint,
},
}
Expand Down Expand Up @@ -509,7 +508,7 @@ func (*fakeZoneReceiver) Recv(
switch strings.ToUpper(method) {
case "POST":
switch out.(type) {
case *model.ZoneSpec:
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteZone), out); err != nil {
return err
}
Expand All @@ -520,12 +519,12 @@ func (*fakeZoneReceiver) Recv(
break
case "GET":
switch out.(type) {
case *model.ZoneSpec:
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteZone), out); err != nil {
return err
}
break
case *[]*model.ZoneSpec:
case *[]*model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteZones), out); err != nil {
return err
}
Expand All @@ -536,7 +535,7 @@ func (*fakeZoneReceiver) Recv(
break
case "PUT":
switch out.(type) {
case *model.ZoneSpec:
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteZone), out); err != nil {
return err
}
Expand Down
44 changes: 22 additions & 22 deletions client/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ import (
"github.com/opensds/opensds/pkg/utils/urls"
)

// ZoneBuilder contains request body of handling a zone request.
// Currently it's assigned as the pointer of ZoneSpec struct, but it
// AvailabilityZoneBuilder contains request body of handling a zone request.
// Currently it's assigned as the pointer of AvailabilityZoneSpec struct, but it
// could be discussed if it's better to define an interface.
type ZoneBuilder *model.ZoneSpec
type AvailabilityZoneBuilder *model.AvailabilityZoneSpec

// NewZoneMgr
func NewZoneMgr(r Receiver, edp string, tenantId string) *ZoneMgr {
return &ZoneMgr{
// NewAvailabilityZoneMgr
func NewAvailabilityZoneMgr(r Receiver, edp string, tenantId string) *AvailabilityZoneMgr {
return &AvailabilityZoneMgr{
Receiver: r,
Endpoint: edp,
TenantId: tenantId,
}
}

// ZoneMgr
type ZoneMgr struct {
// AvailabilityZoneMgr
type AvailabilityZoneMgr struct {
Receiver
Endpoint string
TenantId string
}

// CreateZone
func (p *ZoneMgr) CreateZone(body ZoneBuilder) (*model.ZoneSpec, error) {
var res model.ZoneSpec
// CreateAvailabilityZone
func (p *AvailabilityZoneMgr) CreateAvailabilityZone(body AvailabilityZoneBuilder) (*model.AvailabilityZoneSpec, error) {
var res model.AvailabilityZoneSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId)}, "/")
Expand All @@ -56,9 +56,9 @@ func (p *ZoneMgr) CreateZone(body ZoneBuilder) (*model.ZoneSpec, error) {
return &res, nil
}

// GetZone
func (p *ZoneMgr) GetZone(zoneID string) (*model.ZoneSpec, error) {
var res model.ZoneSpec
// GetAvailabilityZone
func (p *AvailabilityZoneMgr) GetAvailabilityZone(zoneID string) (*model.AvailabilityZoneSpec, error) {
var res model.AvailabilityZoneSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
Expand All @@ -70,9 +70,9 @@ func (p *ZoneMgr) GetZone(zoneID string) (*model.ZoneSpec, error) {
return &res, nil
}

// UpdateZone ...
func (p *ZoneMgr) UpdateZone(zoneID string, body ZoneBuilder) (*model.ZoneSpec, error) {
var res model.ZoneSpec
// UpdateAvailabilityZone ...
func (p *AvailabilityZoneMgr) UpdateAvailabilityZone(zoneID string, body AvailabilityZoneBuilder) (*model.AvailabilityZoneSpec, error) {
var res model.AvailabilityZoneSpec
url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
Expand All @@ -84,9 +84,9 @@ func (p *ZoneMgr) UpdateZone(zoneID string, body ZoneBuilder) (*model.ZoneSpec,
return &res, nil
}

// ListZones
func (p *ZoneMgr) ListZones(args ...interface{}) ([]*model.ZoneSpec, error) {
var res []*model.ZoneSpec
// ListAvailabilityZones
func (p *AvailabilityZoneMgr) ListAvailabilityZones(args ...interface{}) ([]*model.AvailabilityZoneSpec, error) {
var res []*model.AvailabilityZoneSpec

url := strings.Join([]string{
p.Endpoint,
Expand All @@ -107,8 +107,8 @@ func (p *ZoneMgr) ListZones(args ...interface{}) ([]*model.ZoneSpec, error) {
return res, nil
}

// DeleteZone
func (p *ZoneMgr) DeleteZone(zoneID string) error {
// DeleteAvailabilityZone
func (p *AvailabilityZoneMgr) DeleteAvailabilityZone(zoneID string) error {
url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
Expand Down
30 changes: 15 additions & 15 deletions client/zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import (
"github.com/opensds/opensds/pkg/model"
)

var fzn = &ZoneMgr{
var fzn = &AvailabilityZoneMgr{
Receiver: NewFakeZoneReceiver(),
}

func TestCreateZone(t *testing.T) {
expected := &model.ZoneSpec{
func TestCreateAvailabilityZone(t *testing.T) {
expected := &model.AvailabilityZoneSpec{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
Name: "default",
Description: "default zone",
}

zn, err := fzn.CreateZone(&model.ZoneSpec{})
zn, err := fzn.CreateAvailabilityZone(&model.AvailabilityZoneSpec{})
if err != nil {
t.Error(err)
return
Expand All @@ -46,17 +46,17 @@ func TestCreateZone(t *testing.T) {
}
}

func TestGetZone(t *testing.T) {
func TestGetAvailabilityZone(t *testing.T) {
var znID = "1106b972-66ef-11e7-b172-db03f3689c9c"
expected := &model.ZoneSpec{
expected := &model.AvailabilityZoneSpec{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
Name: "default",
Description: "default zone",
}

zn, err := fzn.GetZone(znID)
zn, err := fzn.GetAvailabilityZone(znID)
if err != nil {
t.Error(err)
return
Expand All @@ -68,8 +68,8 @@ func TestGetZone(t *testing.T) {
}
}

func TestListZones(t *testing.T) {
expected := []*model.ZoneSpec{
func TestListAvailabilityZone(t *testing.T) {
expected := []*model.AvailabilityZoneSpec{
{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
Expand All @@ -86,7 +86,7 @@ func TestListZones(t *testing.T) {
},
}

zns, err := fzn.ListZones()
zns, err := fzn.ListAvailabilityZones()
if err != nil {
t.Error(err)
return
Expand All @@ -98,16 +98,16 @@ func TestListZones(t *testing.T) {
}
}

func TestUpdateZone(t *testing.T) {
expected := &model.ZoneSpec{
func TestUpdateAvailabilityZone(t *testing.T) {
expected := &model.AvailabilityZoneSpec{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
Name: "default",
Description: "default zone",
}

zn, err := fzn.UpdateZone("1106b972-66ef-11e7-b172-db03f3689c9c", &model.ZoneSpec{})
zn, err := fzn.UpdateAvailabilityZone("1106b972-66ef-11e7-b172-db03f3689c9c", &model.AvailabilityZoneSpec{})
if err != nil {
t.Error(err)
return
Expand All @@ -119,10 +119,10 @@ func TestUpdateZone(t *testing.T) {
}
}

func TestDeleteZone(t *testing.T) {
func TestDeleteAvailabilityZone(t *testing.T) {
var znID = "1106b972-66ef-11e7-b172-db03f3689c9c"

if err := fzn.DeleteZone(znID); err != nil {
if err := fzn.DeleteAvailabilityZone(znID); err != nil {
t.Error(err)
return
}
Expand Down
14 changes: 7 additions & 7 deletions osdsctl/cli/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ var zoneFormatters = FormatterList{}

func zoneCreateAction(cmd *cobra.Command, args []string) {
ArgsNumCheck(cmd, args, 1)
az := &model.ZoneSpec{}
az := &model.AvailabilityZoneSpec{}
if err := json.Unmarshal([]byte(args[0]), az); err != nil {
Errorln(err)
cmd.Usage()
os.Exit(1)
}

resp, err := client.CreateZone(az)
resp, err := client.CreateAvailabilityZone(az)
if err != nil {
Fatalln(HttpErrStrip(err))
}
Expand All @@ -114,7 +114,7 @@ func zoneCreateAction(cmd *cobra.Command, args []string) {

func zoneShowAction(cmd *cobra.Command, args []string) {
ArgsNumCheck(cmd, args, 1)
resp, err := client.GetZone(args[0])
resp, err := client.GetAvailabilityZone(args[0])
if err != nil {
Fatalln(HttpErrStrip(err))
}
Expand All @@ -128,7 +128,7 @@ func zoneListAction(cmd *cobra.Command, args []string) {
"sortKey": zoneSortKey, "Id": zoneId,
"Name": zoneName, "Description": zoneDescription}

resp, err := client.ListZones(opts)
resp, err := client.ListAvailabilityZones(opts)
if err != nil {
Fatalln(HttpErrStrip(err))
}
Expand All @@ -138,23 +138,23 @@ func zoneListAction(cmd *cobra.Command, args []string) {

func zoneDeleteAction(cmd *cobra.Command, args []string) {
ArgsNumCheck(cmd, args, 1)
err := client.DeleteZone(args[0])
err := client.DeleteAvailabilityZone(args[0])
if err != nil {
Fatalln(HttpErrStrip(err))
}
}

func zoneUpdateAction(cmd *cobra.Command, args []string) {
ArgsNumCheck(cmd, args, 2)
az := &model.ZoneSpec{}
az := &model.AvailabilityZoneSpec{}

if err := json.Unmarshal([]byte(args[1]), az); err != nil {
Errorln(err)
cmd.Usage()
os.Exit(1)
}

resp, err := client.UpdateZone(args[0], az)
resp, err := client.UpdateAvailabilityZone(args[0], az)
if err != nil {
Fatalln(HttpErrStrip(err))
}
Expand Down
18 changes: 0 additions & 18 deletions pkg/api/controllers/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,9 @@ import (
func init() {
var poolPortal PoolPortal
beego.Router("/v1beta/pools", &poolPortal, "get:ListPools")
// beego.Router("/v1beta/availabilityZones", &poolPortal, "get:ListAvailabilityZones")
beego.Router("/v1beta/pools/:poolId", &poolPortal, "get:GetPool")
}

// func TestListAvailabilityZones(t *testing.T) {

// t.Run("Should return 200 if everything works well", func(t *testing.T) {
// mockClient := new(dbtest.Client)
// mockClient.On("ListAvailabilityZones", c.NewAdminContext()).Return(SampleAvailabilityZones, nil)
// db.C = mockClient

// r, _ := http.NewRequest("GET", "/v1beta/availabilityZones", nil)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
// var output []string
// json.Unmarshal(w.Body.Bytes(), &output)
// assertTestResult(t, w.Code, 200)
// assertTestResult(t, output, SampleAvailabilityZones)
// })
// }

func TestListPools(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down
Loading

0 comments on commit aaedd1f

Please sign in to comment.