Skip to content

Instantly share code, notes, and snippets.

@OddCN
Last active April 27, 2018 15:11
Show Gist options
  • Save OddCN/3015a8c7da94d438665a1a3b6fe156b0 to your computer and use it in GitHub Desktop.
Save OddCN/3015a8c7da94d438665a1a3b6fe156b0 to your computer and use it in GitHub Desktop.
the 2nd way we can code
package ecs
import (
"errors"
"github.com/cloudlibz/gocloud/aliauth"
)
const errCommon = "ali-cloud create node miss required parameter: "
type CreateNode struct {
RegionID string //required
ImageID string //required
InstanceType string //required
SecurityGroupID string //required
ZoneID string //not necessary
InstanceName string //not necessary
Description string //not necessary
InternetChargeType string //not necessary
InternetMaxBandwidthIn int //not necessary
InternetMaxBandwidthOut int //not necessary
HostName string //not necessary
Password string //not necessary
IoOptimized string //not necessary
SystemDiskCategory string //not necessary
SystemDiskSize string //not necessary
SystemDiskName string //not necessary
SystemDiskDescription string //not necessary
}
type CreateNodeBuilder struct {
params *CreateNode
}
func NewCreateNodeBuilder() *CreateNodeBuilder {
return &CreateNodeBuilder{}
}
func (b *CreateNodeBuilder) RegionID(regionID string) *CreateNodeBuilder {
b.params.RegionID = regionID
return b
}
func (b *CreateNodeBuilder) ImageID(imageID string) *CreateNodeBuilder {
b.params.ImageID = imageID
return b
}
func (b *CreateNodeBuilder) InstanceType(instanceType string) *CreateNodeBuilder {
b.params.InstanceType = instanceType
return b
}
func (b *CreateNodeBuilder) SecurityGroupID(securityGroupID string) *CreateNodeBuilder {
b.params.SecurityGroupID = securityGroupID
return b
}
func (b *CreateNodeBuilder) ZoneID(zoneID string) *CreateNodeBuilder {
b.params.ZoneID = zoneID
return b
}
//func
//.
//.
//.
//func
func (b *CreateNodeBuilder) Build() (map[string]interface{}, error) {
if b.params.RegionID == "" {
return nil, errors.New(errCommon + "RegionID")
}
if b.params.ImageID == "" {
return nil, errors.New(errCommon + "ImageID")
}
if b.params.InstanceType == "" {
return nil, errors.New(errCommon + "InstanceType")
}
if b.params.SecurityGroupID == "" {
return nil, errors.New(errCommon + "SecurityGroupID")
}
params := make(map[string]interface{})
// Put all of options into params map[string]interface{}
params = aliauth.PutStructToMap(&b.params)
return params, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment