feat(region): Add Guest Template & Service Catalog

Modify the structure of ServerCreateOptions so that
GuestTemplateCreateOptions can reuse most field.
This commit is contained in:
郑雨
2019-12-09 11:24:01 +08:00
parent 45302ab49f
commit 06eccd18d8
13 changed files with 1087 additions and 7 deletions
+168
View File
@@ -0,0 +1,168 @@
// Copyright 2019 Yunion
//
// 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 shell
import (
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
func init() {
type GuestTemplateListOptions struct {
options.BaseListOptions
}
R(&GuestTemplateListOptions{}, "server-template-list", "List server template", func(s *mcclient.ClientSession,
opts *GuestTemplateListOptions) error {
params, err := options.ListStructToParams(opts)
if err != nil {
return err
}
result, err := modules.GuestTemplate.List(s, params)
if err != nil {
return err
}
printList(result, modules.GuestTemplate.GetColumns(s))
return nil
})
type GuestTemplateCreateOptions struct {
options.ServerCreateOptionalOptions
NAME string `help:"Name of server template" json:"-"`
}
R(&GuestTemplateCreateOptions{}, "server-template-create", "Create a server template",
func(s *mcclient.ClientSession,
opts *GuestTemplateCreateOptions) error {
params, err := opts.OptionalParams()
if err != nil {
return err
}
if options.BoolV(opts.DryRun) {
fmt.Println("no support operator")
return nil
}
dict := jsonutils.NewDict()
if opts.GenerateName {
dict.Add(jsonutils.NewString(opts.NAME), "generate_name")
} else {
dict.Add(jsonutils.NewString(opts.NAME), "name")
}
dict.Add(params.JSON(params), "content")
tem, err := modules.GuestTemplate.Create(s, dict)
if err != nil {
return err
}
printObject(tem)
return nil
})
type GuestTemplateUpdateOptions struct {
options.ServerCreateOptionalOptions
ID string `help:"ID of server template"`
name string `help:"name of server template"`
}
R(&GuestTemplateUpdateOptions{}, "server-template-update", "Update a server template",
func(s *mcclient.ClientSession, opts *GuestTemplateUpdateOptions) error {
params, err := opts.OptionalParams()
if err != nil {
return err
}
if options.BoolV(opts.DryRun) {
fmt.Println("no support operator")
return nil
}
dict := jsonutils.NewDict()
if len(opts.name) != 0 {
dict.Add(jsonutils.NewString(opts.name), "name")
}
dict.Add(params.JSON(params), "content")
tem, err := modules.GuestTemplate.Update(s, opts.ID, dict)
if err != nil {
return err
}
printObject(tem)
return nil
})
type GuestTemplateOptions struct {
ID string `help:"ID or Name of server template"`
}
R(&GuestTemplateOptions{}, "server-template-show", "Show a server template",
func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
tem, err := modules.GuestTemplate.Get(s, opts.ID, jsonutils.JSONNull)
if err != nil {
return err
}
printObject(tem)
return nil
})
R(&GuestTemplateOptions{}, "server-tempalte-delete", "Delete a server template",
func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
tem, err := modules.GuestTemplate.Delete(s, opts.ID, jsonutils.JSONNull)
if err != nil {
return err
}
printObject(tem)
return nil
},
)
R(&GuestTemplateOptions{}, "server-template-private", "Private server template",
func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
tem, err := modules.GuestTemplate.PerformAction(s, opts.ID, "private", jsonutils.JSONNull)
if err != nil {
return err
}
printObject(tem)
return nil
},
)
type GuestTemplatePublicOptions struct {
ID string `help:"ID or Name of server template"`
PublicScope string `help:"public scope"`
}
R(&GuestTemplatePublicOptions{}, "server-template-public", "Public server template",
func(s *mcclient.ClientSession, opts *GuestTemplatePublicOptions) error {
dict := jsonutils.NewDict()
if len(opts.PublicScope) != 0 {
dict.Add(jsonutils.NewString(opts.PublicScope), "public_scope")
}
tem, err := modules.GuestTemplate.PerformAction(s, opts.ID, "public", dict)
if err != nil {
return err
}
printObject(tem)
return nil
},
)
}
+162
View File
@@ -0,0 +1,162 @@
// Copyright 2019 Yunion
//
// 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 shell
import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
func init() {
type ServiceCatalogListOpions struct {
options.BaseListOptions
}
R(&ServiceCatalogListOpions{}, "service-catalog-list", "List service catalog", func(s *mcclient.ClientSession,
opts *ServiceCatalogListOpions) error {
params, err := options.ListStructToParams(opts)
if err != nil {
return err
}
ret, err := modules.ServiceCatalog.List(s, params)
if err != nil {
return err
}
printList(ret, modules.ServiceCatalog.GetColumns(s))
return nil
})
type ServiceCatalogCreateOptions struct {
NAME string `help:"Name of service catalog"`
GuestTemplate string `help:"guest template of service catalog"`
IconUrl string `help:"icon url of service catalog"`
GenerateName bool `help:"whether to generate name"`
}
R(&ServiceCatalogCreateOptions{}, "service-catalog-create", "Create a service catalog",
func(s *mcclient.ClientSession, opts *ServiceCatalogCreateOptions) error {
params := jsonutils.NewDict()
if opts.GenerateName {
params.Add(jsonutils.NewString(opts.NAME), "generate_name")
} else {
params.Add(jsonutils.NewString(opts.NAME), "name")
}
if len(opts.GuestTemplate) != 0 {
params.Add(jsonutils.NewString(opts.GuestTemplate), "guest_template")
}
if len(opts.IconUrl) != 0 {
params.Add(jsonutils.NewString(opts.IconUrl), "icon_url")
}
serviceCatalog, err := modules.ServiceCatalog.Create(s, params)
if err != nil {
return err
}
printObject(serviceCatalog)
return nil
},
)
type ServiceCatalogOptions struct {
ID string `help:"ID or name of service catalog"`
}
R(&ServiceCatalogOptions{}, "service-catalog-show", "show a service catalog", func(s *mcclient.ClientSession,
opts *ServiceCatalogOptions) error {
sc, err := modules.ServiceCatalog.Get(s, opts.ID, jsonutils.JSONNull)
if err != nil {
return err
}
printObject(sc)
return nil
})
R(&ServiceCatalogOptions{}, "service-catalog-delete", "delete a service catalog", func(s *mcclient.ClientSession,
opts *ServiceCatalogOptions) error {
sc, err := modules.ServiceCatalog.Delete(s, opts.ID, jsonutils.JSONNull)
if err != nil {
return err
}
printObject(sc)
return nil
})
type ServiceCatalogUpdateOptions struct {
ServiceCatalogOptions
Name string `help:"Name of service catalog"`
GuestTemplate string `help:"guest template of service catalog"`
IconUrl string `help:"icon url of service catalog"`
}
R(&ServiceCatalogUpdateOptions{}, "service-catalog-update", "update a service catalog",
func(s *mcclient.ClientSession, opts *ServiceCatalogUpdateOptions) error {
params := jsonutils.NewDict()
if len(opts.Name) > 0 {
params.Add(jsonutils.NewString(opts.Name), "name")
}
if len(opts.GuestTemplate) > 0 {
params.Add(jsonutils.NewString(opts.GuestTemplate), "guest_template")
}
if len(opts.IconUrl) > 0 {
params.Add(jsonutils.NewString(opts.IconUrl), "icon_url")
}
sc, err := modules.ServiceCatalog.Update(s, opts.ID, params)
if err != nil {
return err
}
printObject(sc)
return nil
},
)
type ServiceCatalogDeployOptions struct {
ServiceCatalogOptions
Name string `help:"Name of guest"`
GenerateName bool `help:"whether to generate name for guest"`
Count int `help:"count of guest"`
ProjectID string `help:"project id of guest"`
}
R(&ServiceCatalogDeployOptions{}, "service-catalog-deploy", "deploy", func(s *mcclient.ClientSession,
opts *ServiceCatalogDeployOptions) error {
params := jsonutils.NewDict()
if opts.GenerateName {
params.Add(jsonutils.NewString(opts.Name), "generate_name")
} else {
params.Add(jsonutils.NewString(opts.Name), "name")
}
if opts.Count != 0 {
params.Add(jsonutils.NewInt(int64(opts.Count)), "count")
}
if len(opts.ProjectID) > 0 {
params.Add(jsonutils.NewString(opts.ProjectID), "project_id")
}
sc, err := modules.ServiceCatalog.PerformAction(s, opts.ID, "deploy", params)
if err != nil {
return err
}
printObject(sc)
return nil
})
}