Skip to content

Commit f47bf25

Browse files
committed
Add CRUD support for availability zone
1 parent 0da2142 commit f47bf25

12 files changed

Lines changed: 733 additions & 23 deletions

File tree

client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Client struct {
4242
*VersionMgr
4343
*ReplicationMgr
4444
*FileShareMgr
45+
*ZoneMgr
4546

4647
cfg *Config
4748
}
@@ -98,6 +99,7 @@ func NewClient(c *Config) (*Client, error) {
9899
VersionMgr: NewVersionMgr(r, c.Endpoint, t),
99100
ReplicationMgr: NewReplicationMgr(r, c.Endpoint, t),
100101
FileShareMgr: NewFileShareMgr(r, c.Endpoint, t),
102+
ZoneMgr: NewZoneMgr(r, c.Endpoint, t),
101103
}, nil
102104
}
103105

client/zone.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2019 The OpenSDS Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package client
16+
17+
import (
18+
"strings"
19+
20+
"github.com/opensds/opensds/pkg/model"
21+
"github.com/opensds/opensds/pkg/utils/urls"
22+
)
23+
24+
// ZoneBuilder contains request body of handling a zone request.
25+
// Currently it's assigned as the pointer of ZoneSpec struct, but it
26+
// could be discussed if it's better to define an interface.
27+
type ZoneBuilder *model.ZoneSpec
28+
29+
// NewZoneMgr
30+
func NewZoneMgr(r Receiver, edp string, tenantId string) *ZoneMgr {
31+
return &ZoneMgr{
32+
Receiver: r,
33+
Endpoint: edp,
34+
TenantId: tenantId,
35+
}
36+
}
37+
38+
// ZoneMgr
39+
type ZoneMgr struct {
40+
Receiver
41+
Endpoint string
42+
TenantId string
43+
}
44+
45+
// CreateZone
46+
func (p *ZoneMgr) CreateZone(body ZoneBuilder) (*model.ZoneSpec, error) {
47+
var res model.ZoneSpec
48+
url := strings.Join([]string{
49+
p.Endpoint,
50+
urls.GenerateZoneURL(urls.Client, p.TenantId)}, "/")
51+
52+
if err := p.Recv(url, "POST", body, &res); err != nil {
53+
return nil, err
54+
}
55+
56+
return &res, nil
57+
}
58+
59+
// GetZone
60+
func (p *ZoneMgr) GetZone(zoneID string) (*model.ZoneSpec, error) {
61+
var res model.ZoneSpec
62+
url := strings.Join([]string{
63+
p.Endpoint,
64+
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
65+
66+
if err := p.Recv(url, "GET", nil, &res); err != nil {
67+
return nil, err
68+
}
69+
70+
return &res, nil
71+
}
72+
73+
// UpdateZone ...
74+
func (p *ZoneMgr) UpdateZone(zoneID string, body ZoneBuilder) (*model.ZoneSpec, error) {
75+
var res model.ZoneSpec
76+
url := strings.Join([]string{
77+
p.Endpoint,
78+
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
79+
80+
if err := p.Recv(url, "PUT", body, &res); err != nil {
81+
return nil, err
82+
}
83+
84+
return &res, nil
85+
}
86+
87+
// ListZones
88+
func (p *ZoneMgr) ListZones(args ...interface{}) ([]*model.ZoneSpec, error) {
89+
var res []*model.ZoneSpec
90+
91+
url := strings.Join([]string{
92+
p.Endpoint,
93+
urls.GenerateZoneURL(urls.Client, p.TenantId)}, "/")
94+
95+
param, err := processListParam(args)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
if param != "" {
101+
url += "?" + param
102+
}
103+
if err := p.Recv(url, "GET", nil, &res); err != nil {
104+
return nil, err
105+
}
106+
107+
return res, nil
108+
}
109+
110+
// DeleteZone
111+
func (p *ZoneMgr) DeleteZone(zoneID string) error {
112+
url := strings.Join([]string{
113+
p.Endpoint,
114+
urls.GenerateZoneURL(urls.Client, p.TenantId, zoneID)}, "/")
115+
116+
return p.Recv(url, "DELETE", nil, nil)
117+
}

osdsctl/cli/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func init() {
4949
rootCommand.AddCommand(poolCommand)
5050
rootCommand.AddCommand(profileCommand)
5151
rootCommand.AddCommand(fileShareCommand)
52+
rootCommand.AddCommand(zoneCommand)
5253
flags := rootCommand.PersistentFlags()
5354
flags.BoolVar(&Debug, "debug", false, "shows debugging output.")
5455
}

osdsctl/cli/zone.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2019 The OpenSDS Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*
16+
This module implements a entry into the OpenSDS service.
17+
*/
18+
19+
package cli
20+
21+
import (
22+
"encoding/json"
23+
"os"
24+
25+
"github.com/opensds/opensds/pkg/model"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var zoneCommand = &cobra.Command{
30+
Use: "zone",
31+
Short: "manage OpenSDS Availability Zone resources",
32+
Run: zoneAction,
33+
}
34+
35+
var zoneCreateCommand = &cobra.Command{
36+
Use: "create <availability zone info>",
37+
Short: "create a new availability zone resource",
38+
Run: zoneCreateAction,
39+
}
40+
41+
var zoneShowCommand = &cobra.Command{
42+
Use: "show <availability zone id>",
43+
Short: "show information of specified availability zone",
44+
Run: zoneShowAction,
45+
}
46+
47+
var zoneListCommand = &cobra.Command{
48+
Use: "list",
49+
Short: "get all availability zone resources",
50+
Run: zoneListAction,
51+
}
52+
53+
var zoneDeleteCommand = &cobra.Command{
54+
Use: "delete <availability zone id>",
55+
Short: "delete a specified availability zone resource",
56+
Run: zoneDeleteAction,
57+
}
58+
59+
var zoneUpdateCommand = &cobra.Command{
60+
Use: "update <availability zone id> <availability zone info>",
61+
Short: "update a specified zone resource",
62+
Run: zoneUpdateAction,
63+
}
64+
65+
var (
66+
zoneLimit string
67+
zoneOffset string
68+
zoneSortDir string
69+
zoneSortKey string
70+
zoneId string
71+
zoneName string
72+
zoneDescription string
73+
)
74+
75+
func init() {
76+
zoneListCommand.Flags().StringVarP(&zoneLimit, "limit", "", "50", "the number of ertries displayed per page")
77+
zoneListCommand.Flags().StringVarP(&zoneOffset, "offset", "", "0", "all requested data offsets")
78+
zoneListCommand.Flags().StringVarP(&zoneSortDir, "sortDir", "", "desc", "the sort direction of all requested data. supports asc or desc(default)")
79+
zoneListCommand.Flags().StringVarP(&zoneSortKey, "sortKey", "", "id", "the sort key of all requested data. supports id(default), name, description")
80+
zoneListCommand.Flags().StringVarP(&zoneId, "id", "", "", "list availability zone by id")
81+
zoneListCommand.Flags().StringVarP(&zoneName, "name", "", "", "list availability zone by name")
82+
zoneListCommand.Flags().StringVarP(&zoneDescription, "description", "", "", "list availability zone by description")
83+
84+
zoneCommand.AddCommand(zoneCreateCommand)
85+
zoneCommand.AddCommand(zoneShowCommand)
86+
zoneCommand.AddCommand(zoneListCommand)
87+
zoneCommand.AddCommand(zoneDeleteCommand)
88+
zoneCommand.AddCommand(zoneUpdateCommand)
89+
}
90+
91+
func zoneAction(cmd *cobra.Command, args []string) {
92+
cmd.Usage()
93+
os.Exit(1)
94+
}
95+
96+
var zoneFormatters = FormatterList{}
97+
98+
func zoneCreateAction(cmd *cobra.Command, args []string) {
99+
ArgsNumCheck(cmd, args, 1)
100+
az := &model.ZoneSpec{}
101+
if err := json.Unmarshal([]byte(args[0]), az); err != nil {
102+
Errorln(err)
103+
cmd.Usage()
104+
os.Exit(1)
105+
}
106+
107+
resp, err := client.CreateZone(az)
108+
if err != nil {
109+
Fatalln(HttpErrStrip(err))
110+
}
111+
keys := KeyList{"Id", "CreatedAt", "UpdatedAt", "Name", "Description"}
112+
PrintDict(resp, keys, zoneFormatters)
113+
}
114+
115+
func zoneShowAction(cmd *cobra.Command, args []string) {
116+
ArgsNumCheck(cmd, args, 1)
117+
resp, err := client.GetZone(args[0])
118+
if err != nil {
119+
Fatalln(HttpErrStrip(err))
120+
}
121+
keys := KeyList{"Id", "CreatedAt", "UpdatedAt", "Name", "Description"}
122+
PrintDict(resp, keys, zoneFormatters)
123+
}
124+
125+
func zoneListAction(cmd *cobra.Command, args []string) {
126+
ArgsNumCheck(cmd, args, 0)
127+
var opts = map[string]string{"limit": zoneLimit, "offset": zoneOffset, "sortDir": zoneSortDir,
128+
"sortKey": zoneSortKey, "Id": zoneId,
129+
"Name": zoneName, "Description": zoneDescription}
130+
131+
resp, err := client.ListZones(opts)
132+
if err != nil {
133+
Fatalln(HttpErrStrip(err))
134+
}
135+
keys := KeyList{"Id", "CreatedAt", "UpdatedAt", "Name", "Description"}
136+
PrintList(resp, keys, FormatterList{})
137+
}
138+
139+
func zoneDeleteAction(cmd *cobra.Command, args []string) {
140+
ArgsNumCheck(cmd, args, 1)
141+
err := client.DeleteZone(args[0])
142+
if err != nil {
143+
Fatalln(HttpErrStrip(err))
144+
}
145+
}
146+
147+
func zoneUpdateAction(cmd *cobra.Command, args []string) {
148+
ArgsNumCheck(cmd, args, 2)
149+
az := &model.ZoneSpec{}
150+
151+
if err := json.Unmarshal([]byte(args[1]), az); err != nil {
152+
Errorln(err)
153+
cmd.Usage()
154+
os.Exit(1)
155+
}
156+
157+
resp, err := client.UpdateZone(args[0], az)
158+
if err != nil {
159+
Fatalln(HttpErrStrip(err))
160+
}
161+
keys := KeyList{"Id", "UpdatedAt", "Name", "Description"}
162+
PrintDict(resp, keys, FormatterList{})
163+
}

pkg/api/controllers/pool.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ type PoolPortal struct {
3333
BasePortal
3434
}
3535

36-
func (p *PoolPortal) ListAvailabilityZones() {
37-
if !policy.Authorize(p.Ctx, "availability_zone:list") {
38-
return
39-
}
40-
azs, err := db.C.ListAvailabilityZones(c.GetContext(p.Ctx))
41-
if err != nil {
42-
errMsg := fmt.Sprintf("get AvailabilityZones for pools failed: %s", err.Error())
43-
p.ErrorHandle(model.ErrorInternalServer, errMsg)
44-
return
45-
}
46-
47-
body, err := json.Marshal(azs)
48-
if err != nil {
49-
errMsg := fmt.Sprintf("marshal AvailabilityZones failed: %s", err.Error())
50-
p.ErrorHandle(model.ErrorInternalServer, errMsg)
51-
return
52-
}
53-
54-
p.SuccessHandle(StatusOK, body)
55-
return
56-
}
57-
5836
func (p *PoolPortal) ListPools() {
5937
if !policy.Authorize(p.Ctx, "pool:list") {
6038
return

0 commit comments

Comments
 (0)