mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 14:19:49 +08:00
增加 billing_type 和 expire_at 字段,用于计费
This commit is contained in:
@@ -21,6 +21,11 @@ type ICloudResource interface {
|
||||
GetMetadata() *jsonutils.JSONDict
|
||||
}
|
||||
|
||||
type IBillingResource interface {
|
||||
GetBillingType() string
|
||||
GetExpiredAt() time.Time
|
||||
}
|
||||
|
||||
type ICloudRegion interface {
|
||||
ICloudResource
|
||||
|
||||
@@ -133,6 +138,7 @@ type ICloudHost interface {
|
||||
|
||||
type ICloudVM interface {
|
||||
ICloudResource
|
||||
IBillingResource
|
||||
|
||||
GetCreateTime() time.Time
|
||||
GetIHost() ICloudHost
|
||||
@@ -213,6 +219,7 @@ type ICloudSecurityGroup interface {
|
||||
|
||||
type ICloudDisk interface {
|
||||
ICloudResource
|
||||
IBillingResource
|
||||
|
||||
GetIStorge() ICloudStorage
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
BILLING_TYPE_POSTPAID = "postpaid"
|
||||
BILLING_TYPE_PREPAID = "prepaid"
|
||||
)
|
||||
|
||||
type SBillingResourceBase struct {
|
||||
BillingType string `width:"36" charset:"ascii" nullable:"true" default:"postpaid" list:"user" create:"required"`
|
||||
ExpiredAt time.Time `nullable:"true" list:"user" create:"optional"`
|
||||
}
|
||||
|
||||
func (self *SBillingResourceBase) GetChargeType() string {
|
||||
if len(self.BillingType) > 0 {
|
||||
return self.BillingType
|
||||
} else {
|
||||
return BILLING_TYPE_POSTPAID
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,8 @@ func init() {
|
||||
type SDisk struct {
|
||||
db.SSharableVirtualResourceBase
|
||||
|
||||
SBillingResourceBase
|
||||
|
||||
DiskFormat string `width:"32" charset:"ascii" nullable:"false" default:"qcow2" list:"user"` // Column(VARCHAR(32, charset='ascii'), nullable=False, default='qcow2')
|
||||
DiskSize int `nullable:"false" list:"user"` // Column(Integer, nullable=False) # in MB
|
||||
AccessPath string `width:"256" charset:"ascii" nullable:"true" get:"user"` // = Column(VARCHAR(256, charset='ascii'), nullable=True)
|
||||
@@ -612,6 +614,9 @@ func (self *SDisk) syncWithCloudDisk(ctx context.Context, userCred mcclient.Toke
|
||||
|
||||
self.IsEmulated = extDisk.IsEmulated()
|
||||
|
||||
self.BillingType = extDisk.GetBillingType()
|
||||
self.ExpiredAt = extDisk.GetExpiredAt()
|
||||
|
||||
self.ProjectId = userCred.GetProjectId()
|
||||
|
||||
return nil
|
||||
@@ -655,6 +660,9 @@ func (manager *SDiskManager) newFromCloudDisk(ctx context.Context, userCred mccl
|
||||
|
||||
disk.IsEmulated = extDisk.IsEmulated()
|
||||
|
||||
disk.BillingType = extDisk.GetBillingType()
|
||||
disk.ExpiredAt = extDisk.GetExpiredAt()
|
||||
|
||||
err := manager.TableSpec().Insert(&disk)
|
||||
if err != nil {
|
||||
log.Errorf("newFromCloudZone fail %s", err)
|
||||
@@ -1012,6 +1020,8 @@ func (self *SDisk) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc.Add(jsonutils.NewString(priceKey), "price_key")
|
||||
}
|
||||
|
||||
desc.Add(jsonutils.NewString(self.GetChargeType()), "charge_type")
|
||||
|
||||
if hypervisor := self.GetMetadata("hypervisor", nil); len(hypervisor) > 0 {
|
||||
desc.Add(jsonutils.NewString(hypervisor), "hypervisor")
|
||||
}
|
||||
|
||||
@@ -152,6 +152,8 @@ func init() {
|
||||
type SGuest struct {
|
||||
db.SVirtualResourceBase
|
||||
|
||||
SBillingResourceBase
|
||||
|
||||
VcpuCount int8 `nullable:"false" default:"1" list:"user" create:"optional"` // Column(TINYINT, nullable=False, default=1)
|
||||
VmemSize int `nullable:"false" list:"user" create:"required"` // Column(Integer, nullable=False)
|
||||
|
||||
@@ -1296,6 +1298,9 @@ func (self *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.Token
|
||||
|
||||
self.IsEmulated = extVM.IsEmulated()
|
||||
|
||||
self.BillingType = extVM.GetBillingType()
|
||||
self.ExpiredAt = extVM.GetExpiredAt()
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1343,6 +1348,9 @@ func (manager *SGuestManager) newCloudVM(ctx context.Context, userCred mcclient.
|
||||
|
||||
guest.IsEmulated = extVM.IsEmulated()
|
||||
|
||||
guest.BillingType = extVM.GetBillingType()
|
||||
guest.ExpiredAt = extVM.GetExpiredAt()
|
||||
|
||||
guest.HostId = host.Id
|
||||
guest.ProjectId = userCred.GetProjectId()
|
||||
|
||||
@@ -3284,6 +3292,8 @@ func (self *SGuest) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc.Add(jsonutils.NewString(priceKey), "price_key")
|
||||
}
|
||||
|
||||
desc.Add(jsonutils.NewString(self.GetChargeType()), "charge_type")
|
||||
|
||||
if len(self.ExternalId) > 0 {
|
||||
desc.Add(jsonutils.NewString(self.ExternalId), "externalId")
|
||||
}
|
||||
|
||||
+17
-1
@@ -31,7 +31,7 @@ type SDisk struct {
|
||||
Description string
|
||||
DetachedTime time.Time
|
||||
Device string
|
||||
DiskChargeType string
|
||||
DiskChargeType InstanceChargeType
|
||||
DiskId string
|
||||
DiskName string
|
||||
EnableAutoSnapshot bool
|
||||
@@ -377,3 +377,19 @@ func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (self *SDisk) GetBillingType() string {
|
||||
switch self.DiskChargeType {
|
||||
case PrePaidInstanceChargeType:
|
||||
return models.BILLING_TYPE_PREPAID
|
||||
case PostPaidInstanceChargeType:
|
||||
return models.BILLING_TYPE_POSTPAID
|
||||
default:
|
||||
return models.BILLING_TYPE_PREPAID
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SDisk) GetExpiredAt() time.Time {
|
||||
return self.ExpiredTime
|
||||
}
|
||||
@@ -57,7 +57,20 @@ type SImage struct {
|
||||
}
|
||||
|
||||
func (self *SImage) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
data := jsonutils.NewDict()
|
||||
if len(self.Architecture) > 0 {
|
||||
data.Add(jsonutils.NewString(self.Architecture), "os_arch")
|
||||
}
|
||||
if len(self.OSType) > 0 {
|
||||
data.Add(jsonutils.NewString(self.OSType), "os_name")
|
||||
}
|
||||
if len(self.Platform) > 0 {
|
||||
data.Add(jsonutils.NewString(self.Platform), "os_distribution")
|
||||
}
|
||||
if len(self.OSName) > 0 {
|
||||
data.Add(jsonutils.NewString(self.OSName), "os_version")
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (self *SImage) GetId() string {
|
||||
|
||||
@@ -156,6 +156,14 @@ func (self *SInstance) GetMetadata() *jsonutils.JSONDict {
|
||||
priceKey := fmt.Sprintf("%s::%s::%s::%s::%s", self.RegionId, self.InstanceType, self.InstanceNetworkType, self.OSType, optimized)
|
||||
data.Add(jsonutils.NewString(priceKey), "price_key")
|
||||
|
||||
if len(self.ImageId) > 0 {
|
||||
if image, err := self.host.zone.region.GetImage(self.ImageId); err != nil {
|
||||
log.Errorf("Failed to find image %s for instance %s", self.ImageId, self.GetName())
|
||||
} else if meta := image.GetMetadata(); meta != nil {
|
||||
data.Update(meta)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -742,4 +750,19 @@ func (self *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SInstance) GetBillingType() string {
|
||||
switch self.InstanceChargeType {
|
||||
case PrePaidInstanceChargeType:
|
||||
return models.BILLING_TYPE_PREPAID
|
||||
case PostPaidInstanceChargeType:
|
||||
return models.BILLING_TYPE_POSTPAID
|
||||
default:
|
||||
return models.BILLING_TYPE_PREPAID
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SInstance) GetExpiredAt() time.Time {
|
||||
return self.ExpiredTime
|
||||
}
|
||||
@@ -361,4 +361,4 @@ func (self *SHost) CreateVM(name string, imgId string, sysDiskSize int, cpu int,
|
||||
passwd string, storageType string, diskSizes []int, publicKey string, secGrpId string) (cloudprovider.ICloudVM, error) {
|
||||
log.Debugf("CreateVM")
|
||||
return nil, cloudprovider.ErrNotImplemented
|
||||
}
|
||||
}
|
||||
@@ -237,3 +237,11 @@ func (self *SVirtualMachine) acquireVmrcUrl() (jsonutils.JSONObject, error) {
|
||||
func (dc *SVirtualMachine) ChangeConfig(instanceId string, ncpu int, vmem int) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SVirtualMachine) GetBillingType() string {
|
||||
return models.BILLING_TYPE_POSTPAID
|
||||
}
|
||||
|
||||
func (self *SVirtualMachine) GetExpiredAt() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
+38
-11
@@ -10,38 +10,65 @@ import (
|
||||
const (
|
||||
DIGITS = "23456789"
|
||||
LETTERS = "abcdefghjkmnpqrstuvwxyz"
|
||||
UPPERS = "ABCDEFGHJKMNPRSTUVWXYZ"
|
||||
PUNC = "()~@#$%^&*-+={}[]:;<>,.?/"
|
||||
|
||||
ALL_DIGITS = "0123456789"
|
||||
ALL_LETTERS = "abcdefghijklmnopqrstuvwxyz"
|
||||
ALL_UPPERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
ALL_PUNC = PUNC
|
||||
)
|
||||
|
||||
var CHARS = fmt.Sprintf("%s%s%s%s", DIGITS, LETTERS, strings.ToUpper(LETTERS), PUNC)
|
||||
type PasswordStrength struct {
|
||||
Digits int
|
||||
Lowercases int
|
||||
Uppercases int
|
||||
Punctuats int
|
||||
}
|
||||
|
||||
var CHARS = fmt.Sprintf("%s%s%s%s", DIGITS, LETTERS, UPPERS, PUNC)
|
||||
|
||||
func RandomPassword2(width int) string {
|
||||
if width < 6 {
|
||||
width = 6
|
||||
}
|
||||
for {
|
||||
ps := PasswordStrength{}
|
||||
var buf bytes.Buffer
|
||||
digitsCnt := 0
|
||||
letterCnt := 0
|
||||
upperCnt := 0
|
||||
puncCnt := 0
|
||||
for i := 0; i < width; i += 1 {
|
||||
index := rand.Intn(len(CHARS))
|
||||
ch := CHARS[index]
|
||||
if strings.IndexByte(DIGITS, ch) >= 0 {
|
||||
digitsCnt += 1
|
||||
ps.Digits += 1
|
||||
} else if strings.IndexByte(LETTERS, ch) >= 0 {
|
||||
letterCnt += 1
|
||||
} else if strings.IndexByte(LETTERS, ch+32) >= 0 {
|
||||
upperCnt += 1
|
||||
ps.Lowercases += 1
|
||||
} else if strings.IndexByte(UPPERS, ch) >= 0 {
|
||||
ps.Uppercases += 1
|
||||
} else if strings.IndexByte(PUNC, ch) >= 0 {
|
||||
puncCnt += 1
|
||||
ps.Punctuats += 1
|
||||
}
|
||||
buf.WriteByte(ch)
|
||||
}
|
||||
if digitsCnt > 1 && letterCnt > 1 && upperCnt > 1 && puncCnt >= 1 && puncCnt <= 2 {
|
||||
if ps.Digits > 1 && ps.Lowercases > 1 && ps.Uppercases > 1 && ps.Punctuats >= 1 && ps.Punctuats <= 2 {
|
||||
return buf.String()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func AnalyzePasswordStrenth(passwd string) PasswordStrength {
|
||||
ps := PasswordStrength{}
|
||||
|
||||
for i := 0; i < len(passwd); i += 1 {
|
||||
if strings.IndexByte(ALL_DIGITS, passwd[i]) >= 0 {
|
||||
ps.Digits += 1
|
||||
} else if strings.IndexByte(ALL_LETTERS, passwd[i]) >= 0 {
|
||||
ps.Lowercases += 1
|
||||
} else if strings.IndexByte(ALL_UPPERS, passwd[i]) >= 0 {
|
||||
ps.Uppercases += 1
|
||||
} else if strings.IndexByte(ALL_PUNC, passwd[i]) >= 0 {
|
||||
ps.Punctuats += 1
|
||||
}
|
||||
}
|
||||
return ps
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package seclib2
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRandomPassword2(t *testing.T) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
t.Logf("%s", RandomPassword2(12))
|
||||
}
|
||||
Reference in New Issue
Block a user