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+ }
0 commit comments