From e77106787905b3187fcbdbc24253b7b0792fd559 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 14 Sep 2018 17:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20billing=5Ftype=20=E5=92=8C?= =?UTF-8?q?=20expire=5Fat=20=E5=AD=97=E6=AE=B5=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=AE=A1=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudprovider/resources.go | 7 ++++ pkg/compute/models/billingresource.go | 21 ++++++++++++ pkg/compute/models/disks.go | 10 ++++++ pkg/compute/models/guests.go | 10 ++++++ pkg/util/aliyun/disk.go | 18 +++++++++- pkg/util/aliyun/image.go | 15 +++++++- pkg/util/aliyun/instance.go | 23 +++++++++++++ pkg/util/esxi/host.go | 2 +- pkg/util/esxi/virtualmachine.go | 8 +++++ pkg/util/seclib2/seclib.go | 49 +++++++++++++++++++++------ pkg/util/seclib2/seclib_test.go | 12 +++++++ 11 files changed, 161 insertions(+), 14 deletions(-) create mode 100644 pkg/compute/models/billingresource.go create mode 100644 pkg/util/seclib2/seclib_test.go diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 323aaec730..ae0d7625d7 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -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 diff --git a/pkg/compute/models/billingresource.go b/pkg/compute/models/billingresource.go new file mode 100644 index 0000000000..3bb4303bfb --- /dev/null +++ b/pkg/compute/models/billingresource.go @@ -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 + } +} diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 7c4649f67b..13774ab504 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -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") } diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 723f40b45a..84657999c7 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -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") } diff --git a/pkg/util/aliyun/disk.go b/pkg/util/aliyun/disk.go index 111e36bfd2..cd688b4944 100644 --- a/pkg/util/aliyun/disk.go +++ b/pkg/util/aliyun/disk.go @@ -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 +} \ No newline at end of file diff --git a/pkg/util/aliyun/image.go b/pkg/util/aliyun/image.go index 2955cb6bfe..aa4bc732d7 100644 --- a/pkg/util/aliyun/image.go +++ b/pkg/util/aliyun/image.go @@ -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 { diff --git a/pkg/util/aliyun/instance.go b/pkg/util/aliyun/instance.go index 64afa92839..33c7047281 100644 --- a/pkg/util/aliyun/instance.go +++ b/pkg/util/aliyun/instance.go @@ -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 } \ No newline at end of file diff --git a/pkg/util/esxi/host.go b/pkg/util/esxi/host.go index 8b03a3814f..fa098fa474 100644 --- a/pkg/util/esxi/host.go +++ b/pkg/util/esxi/host.go @@ -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 -} +} \ No newline at end of file diff --git a/pkg/util/esxi/virtualmachine.go b/pkg/util/esxi/virtualmachine.go index 8996884109..680a28c654 100644 --- a/pkg/util/esxi/virtualmachine.go +++ b/pkg/util/esxi/virtualmachine.go @@ -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{} +} \ No newline at end of file diff --git a/pkg/util/seclib2/seclib.go b/pkg/util/seclib2/seclib.go index d3fd831df3..af4609363b 100644 --- a/pkg/util/seclib2/seclib.go +++ b/pkg/util/seclib2/seclib.go @@ -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 +} diff --git a/pkg/util/seclib2/seclib_test.go b/pkg/util/seclib2/seclib_test.go new file mode 100644 index 0000000000..c869234eba --- /dev/null +++ b/pkg/util/seclib2/seclib_test.go @@ -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)) +}