Merge pull request #161 in YUNIONIO/onecloud from ~QIUJIAN/onecloud:feature/qj-elastic-ip to release/2.1.0

* commit '82bbb2054b9d72b542ea5d4d3aea931fe1103f26':
  fix log
  修正:1. 已经绑定的EIP禁止删除 2. 删除server时候清除public IP记录
  浮动IP支持
  temp commit
This commit is contained in:
邱剑
2018-09-05 17:02:45 +08:00
32 changed files with 2055 additions and 54 deletions
+16 -8
View File
@@ -9,7 +9,7 @@ import (
"strings"
"time"
prompt "github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt"
"yunion.io/x/log"
"yunion.io/x/pkg/util/version"
@@ -28,7 +28,7 @@ type BaseOptions struct {
Version bool `help:"Show version"`
Timeout int `default:"600" help:"Number of seconds to wait for a response"`
Insecure bool `default:"false" help:"Allow skip server cert verification if URL is https" short-token:"k"`
NoCachedToken bool `default:"false" help:"Force not use cached token"`
NoCachedToken bool `default:"$NO_CACHED_TOKEN|false" help:"Force not use cached token"`
OsUsername string `default:"$OS_USERNAME" help:"Username, defaults to env[OS_USERNAME]"`
OsPassword string `default:"$OS_PASSWORD" help:"Password, defaults to env[OS_PASSWORD]"`
// OsProjectId string `default:"$OS_PROJECT_ID" help:"Proejct ID, defaults to env[OS_PROJECT_ID]"`
@@ -126,8 +126,11 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
options.Insecure)
var cacheToken mcclient.TokenCredential
tokenCachePath := filepath.Join(os.TempDir(), "OS_AUTH_CACHE_TOKEN")
authUrlAlter := strings.Replace(options.OsAuthURL, "/", "", -1)
authUrlAlter = strings.Replace(authUrlAlter, ":", "", -1)
tokenCachePath := filepath.Join(os.TempDir(), fmt.Sprintf("OS_AUTH_CACHE_TOKEN-%s-%s-%s-%s", authUrlAlter, options.OsUsername, options.OsDomainName, options.OsProjectName))
cacheFile, err := os.Open(tokenCachePath)
if err == nil && cacheFile != nil && !options.NoCachedToken {
fileInfo, _ := cacheFile.Stat()
dur, err := time.ParseDuration("-24h")
@@ -138,13 +141,14 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
err := json.Unmarshal(bytesToken, token)
if err != nil {
fmt.Printf("Unmarshal token error:%s", err)
} else {
} else if token.IsValid() {
cacheToken = token
}
}
cacheFile.Close()
}
}
if cacheToken == nil {
token, err := client.Authenticate(options.OsUsername,
options.OsPassword,
@@ -158,12 +162,16 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
if err != nil {
fmt.Printf("Marshal token error:%s", err)
} else {
fo, _ := os.Create(tokenCachePath)
fo.Write(bytesCacheToken)
fo.Close()
fo, err := os.Create(tokenCachePath)
if err != nil {
fmt.Printf("Save token cache fail: %s", err)
} else {
fo.Write(bytesCacheToken)
fo.Close()
}
}
} else {
fmt.Printf("******** Use Token Cache At %s ********\n", tokenCachePath)
// fmt.Printf("******** Use Token Cache At %s ********\n", tokenCachePath)
}
session := client.NewSession(options.OsRegionName,
+186
View File
@@ -0,0 +1,186 @@
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 ElasticipListOptions struct {
Manager string `help:"Show servers imported from manager"`
Region string `help:"Show servers in cloudregion"`
options.BaseListOptions
}
R(&ElasticipListOptions{}, "eip-list", "List elastic IPs", func(s *mcclient.ClientSession, args *ElasticipListOptions) error {
var params *jsonutils.JSONDict
{
var err error
params, err = args.BaseListOptions.Params()
if err != nil {
return err
}
}
if len(args.Manager) > 0 {
params.Add(jsonutils.NewString(args.Manager), "manager")
}
if len(args.Region) > 0 {
params.Add(jsonutils.NewString(args.Region), "region")
}
results, err := modules.Elasticips.List(s, params)
if err != nil {
return err
}
printList(results, modules.Elasticips.GetColumns(s))
return nil
})
type EipCreateOptions struct {
MANAGER string `help:"cloud provider"`
REGION string `help:"cloud region in which EIP is allocated"`
NAME string `help:"name of the EIP"`
BW int `help:"Bandwidth in Mbps"`
ChargeType string `help:"bandwidth charge type, either traffic or bandwidth" choices:"traffic|bandwidth"`
}
R(&EipCreateOptions{}, "eip-create", "Create an EIP", func(s *mcclient.ClientSession, args *EipCreateOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString(args.MANAGER), "manager")
params.Add(jsonutils.NewString(args.REGION), "region")
params.Add(jsonutils.NewString(args.NAME), "name")
params.Add(jsonutils.NewInt(int64(args.BW)), "bandwidth")
if len(args.ChargeType) > 0 {
params.Add(jsonutils.NewString(args.ChargeType), "charge_type")
}
result, err := modules.Elasticips.Create(s, params)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipDeleteOptions struct {
ID string `help:"ID or name of EIP"`
}
R(&EipDeleteOptions{}, "eip-delete", "Delete an EIP", func(s *mcclient.ClientSession, args *EipDeleteOptions) error {
result, err := modules.Elasticips.Delete(s, args.ID, nil)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipUpdateOptions struct {
ID string `help:"ID or name of EIP"`
Name string `help:"New name of EIP"`
Desc string `help:"New description of EIP"`
}
R(&EipUpdateOptions{}, "eip-update", "Update EIP properties", func(s *mcclient.ClientSession, args *EipUpdateOptions) error {
params := jsonutils.NewDict()
if len(args.Name) > 0 {
params.Add(jsonutils.NewString(args.Name), "name")
}
if len(args.Desc) > 0 {
params.Add(jsonutils.NewString(args.Desc), "description")
}
result, err := modules.Elasticips.Update(s, args.ID, params)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipAssociateOptions struct {
ID string `help:"ID or name of EIP"`
INSTANCEID string `help:"ID of instance the eip associated with"`
InstanceType string `default:"server" help:"Instance type that the eip associated with, default is server" choices:"server"`
}
R(&EipAssociateOptions{}, "eip-associate", "Associate an EIP to an instance", func(s *mcclient.ClientSession, args *EipAssociateOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString(args.InstanceType), "instance_type")
params.Add(jsonutils.NewString(args.INSTANCEID), "instance_id")
result, err := modules.Elasticips.PerformAction(s, args.ID, "associate", params)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipSingleOptions struct {
ID string `help:"ID or name of EIP"`
}
R(&EipSingleOptions{}, "eip-dissociate", "Dissociate an EIP from an instance", func(s *mcclient.ClientSession, args *EipSingleOptions) error {
result, err := modules.Elasticips.PerformAction(s, args.ID, "dissociate", nil)
if err != nil {
return err
}
printObject(result)
return nil
})
R(&EipSingleOptions{}, "eip-sync", "Synchronize status of an EIP", func(s *mcclient.ClientSession, args *EipSingleOptions) error {
result, err := modules.Elasticips.PerformAction(s, args.ID, "sync", nil)
if err != nil {
return err
}
printObject(result)
return nil
})
type ServerCreateEipOptions struct {
ID string `help:"server ID or name"`
BW int `help:"EIP bandwidth in Mbps"`
ChargeType string `help:"bandwidth charge type, either traffic or bandwidth" choices:"traffic|bandwidth"`
}
R(&ServerCreateEipOptions{}, "server-create-eip", "allocate an EIP and associate EIP to server", func(s *mcclient.ClientSession, args *ServerCreateEipOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewInt(int64(args.BW)), "bandwidth")
if len(args.ChargeType) > 0 {
params.Add(jsonutils.NewString(args.ChargeType), "charge_type")
}
result, err := modules.Servers.PerformAction(s, args.ID, "create-eip", params)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipShowOptions struct {
ID string `help:"ID or name of EIP"`
}
R(&EipShowOptions{}, "eip-show", "show details of an EIP", func(s *mcclient.ClientSession, args *EipShowOptions) error {
result, err := modules.Servers.Get(s, args.ID, nil)
if err != nil {
return err
}
printObject(result)
return nil
})
type EipChangeBandwidthOptions struct {
ID string `help:"ID or name of the EIP"`
BW int `help:"new bandwidth of EIP"`
}
R(&EipChangeBandwidthOptions{}, "eip-change-bandwidth", "Change maximal bandwidth of EIP", func(s *mcclient.ClientSession, args *EipChangeBandwidthOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewInt(int64(args.BW)), "bandwidth")
result, err := modules.Elasticips.PerformAction(s, args.ID, "change-bandwidth", params)
if err != nil {
return err
}
printObject(result)
return nil
})
}