Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRUD support for availability zone #1049

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client struct {
*ReplicationMgr
*FileShareMgr
*HostMgr
*AvailabilityZoneMgr

cfg *Config
}
Expand Down Expand Up @@ -100,6 +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),
AvailabilityZoneMgr: NewAvailabilityZoneMgr(r, c.Endpoint, t),
}, nil
}

Expand Down
64 changes: 64 additions & 0 deletions client/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func NewFakeClient(config *Config) *Client {
Receiver: NewFakeHostReceiver(),
Endpoint: config.Endpoint,
},
AvailabilityZoneMgr: &AvailabilityZoneMgr{
Receiver: NewFakeZoneReceiver(),
Endpoint: config.Endpoint,
},
}
})
return fakeClient
Expand Down Expand Up @@ -488,3 +492,63 @@ func (*fakeHostReceiver) Recv(

return nil
}

func NewFakeZoneReceiver() Receiver {
return &fakeZoneReceiver{}
}

type fakeZoneReceiver struct{}

func (*fakeZoneReceiver) Recv(
string,
method string,
in interface{},
out interface{},
) error {
switch strings.ToUpper(method) {
case "POST":
switch out.(type) {
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteAvailabilityZone), out); err != nil {
return err
}
break
default:
return errors.New("output format not supported")
}
break
case "GET":
switch out.(type) {
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteAvailabilityZone), out); err != nil {
return err
}
break
case *[]*model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteAvailabilityZones), out); err != nil {
return err
}
break
default:
return errors.New("output format not supported")
}
break
case "PUT":
switch out.(type) {
case *model.AvailabilityZoneSpec:
if err := json.Unmarshal([]byte(ByteAvailabilityZone), out); err != nil {
return err
}
break
default:
return errors.New("output format not supported")
}
break
case "DELETE":
break
default:
return errors.New("inputed method format not supported")
}

return nil
}
117 changes: 117 additions & 0 deletions client/zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"strings"

"github.com/opensds/opensds/pkg/model"
"github.com/opensds/opensds/pkg/utils/urls"
)

// 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 AvailabilityZoneBuilder *model.AvailabilityZoneSpec

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

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

// 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)}, "/")

if err := p.Recv(url, "POST", body, &res); err != nil {
return nil, err
}

return &res, nil
}

// 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)}, "/")

if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

return &res, nil
}

// 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)}, "/")

if err := p.Recv(url, "PUT", body, &res); err != nil {
return nil, err
}

return &res, nil
}

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

url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId)}, "/")

param, err := processListParam(args)
if err != nil {
return nil, err
}

if param != "" {
url += "?" + param
}
if err := p.Recv(url, "GET", nil, &res); err != nil {
return nil, err
}

return res, nil
}

// DeleteAvailabilityZone
func (p *AvailabilityZoneMgr) DeleteAvailabilityZone(zoneID string) error {
url := strings.Join([]string{
p.Endpoint,
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")

return p.Recv(url, "DELETE", nil, nil)
}
129 changes: 129 additions & 0 deletions client/zone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"reflect"
"testing"

"github.com/opensds/opensds/pkg/model"
)

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

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.CreateAvailabilityZone(&model.AvailabilityZoneSpec{})
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(zn, expected) {
t.Errorf("Expected %v, got %v", expected, zn)
return
}
}

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

zn, err := fzn.GetAvailabilityZone(znID)
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(zn, expected) {
t.Errorf("Expected %v, got %v", expected, zn)
return
}
}

func TestListAvailabilityZone(t *testing.T) {
expected := []*model.AvailabilityZoneSpec{
{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
Name: "default",
Description: "default zone",
},
{
BaseModel: &model.BaseModel{
Id: "2f9c0a04-66ef-11e7-ade2-43158893e017",
},
Name: "test",
Description: "test zone",
},
}

zns, err := fzn.ListAvailabilityZones()
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(zns, expected) {
t.Errorf("Expected %v, got %v", expected[1], zns[1])
return
}
}

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.UpdateAvailabilityZone("1106b972-66ef-11e7-b172-db03f3689c9c", &model.AvailabilityZoneSpec{})
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(zn, expected) {
t.Errorf("Expected %v, got %v", expected, zn)
return
}
}

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

if err := fzn.DeleteAvailabilityZone(znID); err != nil {
t.Error(err)
return
}
}
4 changes: 4 additions & 0 deletions examples/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"volume_group:update": "rule:admin_or_owner",
"volume_group:delete": "rule:admin_or_owner",
"availability_zone:list":"",
"availability_zone:get":"",
"availability_zone:create":"admin_api",
"availability_zone:delete":"admin_api",
"availability_zone:update":"admin_api",
"metrics:get": "rule:admin_or_owner",
"metrics:collect": "rule:admin_or_owner",
"metrics:uploadconf": "rule:admin_api",
Expand Down
1 change: 1 addition & 0 deletions osdsctl/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
rootCommand.AddCommand(profileCommand)
rootCommand.AddCommand(fileShareCommand)
rootCommand.AddCommand(hostCommand)
rootCommand.AddCommand(zoneCommand)
flags := rootCommand.PersistentFlags()
flags.BoolVar(&Debug, "debug", false, "shows debugging output.")
}
Expand Down
Loading