mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
Merge pull request #689 in YUNIONIO/onecloud from ~QIUJIAN/onecloud:feature/qj-resouce-billing-info to release/2.4.0
* commit 'c28e07ab7cdb91987a702cf7dca10ef216613677': recode, add subaccountproject 标准化资源的计费字段信息
This commit is contained in:
@@ -1 +1,13 @@
|
||||
package appctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
var (
|
||||
Background context.Context
|
||||
)
|
||||
|
||||
func init() {
|
||||
Background = context.Background()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appctx"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
)
|
||||
|
||||
const (
|
||||
BILLING_TYPE_POSTPAID = "postpaid"
|
||||
@@ -19,3 +24,74 @@ func (self *SBillingResourceBase) GetChargeType() string {
|
||||
return BILLING_TYPE_POSTPAID
|
||||
}
|
||||
}
|
||||
|
||||
type SCloudBillingInfo struct {
|
||||
Provider string
|
||||
Account string
|
||||
AccountId string
|
||||
SubAccount string
|
||||
SubAccountId string
|
||||
SubAccountProject string
|
||||
SubAccountProjectId string
|
||||
Region string
|
||||
RegionId string
|
||||
RegionExtId string
|
||||
Zone string
|
||||
ZoneId string
|
||||
ZoneExtId string
|
||||
PriceKey string
|
||||
ChargeType string
|
||||
InternetChargeType string
|
||||
}
|
||||
|
||||
func MakeCloudBillingInfo(region *SCloudregion, zone *SZone, provider *SCloudprovider) SCloudBillingInfo {
|
||||
info := SCloudBillingInfo{}
|
||||
|
||||
if zone != nil {
|
||||
info.Zone = zone.GetName()
|
||||
info.ZoneId = zone.GetId()
|
||||
}
|
||||
|
||||
if region != nil {
|
||||
info.Region = region.GetName()
|
||||
info.RegionId = region.GetId()
|
||||
}
|
||||
|
||||
if provider != nil {
|
||||
info.SubAccount = provider.GetName()
|
||||
info.SubAccountId = provider.GetId()
|
||||
|
||||
if len(provider.ProjectId) > 0 {
|
||||
info.SubAccountProjectId = provider.ProjectId
|
||||
tc, err := db.TenantCacheManager.FetchTenantById(appctx.Background, provider.ProjectId)
|
||||
if err == nil {
|
||||
info.SubAccountProject = tc.GetName()
|
||||
}
|
||||
}
|
||||
|
||||
account := provider.GetCloudaccount()
|
||||
info.Account = account.GetName()
|
||||
info.AccountId = account.GetId()
|
||||
|
||||
driver, err := provider.GetDriver()
|
||||
|
||||
if err == nil {
|
||||
info.Provider = driver.GetId()
|
||||
|
||||
if region != nil {
|
||||
iregion, err := driver.GetIRegionById(region.ExternalId)
|
||||
if err == nil {
|
||||
info.RegionExtId = iregion.GetId()
|
||||
if zone != nil {
|
||||
izone, err := iregion.GetIZoneById(zone.ExternalId)
|
||||
if err == nil {
|
||||
info.ZoneExtId = izone.GetId()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ const (
|
||||
CLOUD_PROVIDER_START_SYNC = "start_sync"
|
||||
CLOUD_PROVIDER_SYNCING = "syncing"
|
||||
|
||||
CLOUD_PROVIDER_KVM = "KVM"
|
||||
CLOUD_PROVIDER_VMWARE = "VMware"
|
||||
CLOUD_PROVIDER_ALIYUN = "Aliyun"
|
||||
CLOUD_PROVIDER_QCLOUD = "Qcloud"
|
||||
|
||||
@@ -1298,12 +1298,6 @@ func (self *SDisk) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc.Add(jsonutils.NewString(storage.MediumType), "medium_type")
|
||||
}
|
||||
|
||||
if priceKey := self.GetMetadata("price_key", nil); len(priceKey) > 0 {
|
||||
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")
|
||||
}
|
||||
@@ -1320,6 +1314,21 @@ func (self *SDisk) GetShortDesc() *jsonutils.JSONDict {
|
||||
if len(tid) > 0 {
|
||||
desc.Add(jsonutils.NewString(tid), "template_id")
|
||||
}
|
||||
|
||||
var billingInfo SCloudBillingInfo
|
||||
|
||||
if storage != nil {
|
||||
billingInfo = storage.getCloudBillingInfo()
|
||||
}
|
||||
|
||||
if priceKey := self.GetMetadata("price_key", nil); len(priceKey) > 0 {
|
||||
billingInfo.PriceKey = priceKey
|
||||
}
|
||||
|
||||
billingInfo.ChargeType = self.GetChargeType()
|
||||
|
||||
desc.Update(jsonutils.Marshal(billingInfo))
|
||||
|
||||
return desc
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/tristate"
|
||||
@@ -146,17 +144,27 @@ func (self *SElasticip) GetRegion() *SCloudregion {
|
||||
|
||||
func (self *SElasticip) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc := self.SVirtualResourceBase.GetShortDesc()
|
||||
|
||||
desc.Add(jsonutils.NewString(self.ChargeType), "charge_type")
|
||||
|
||||
desc.Add(jsonutils.NewInt(int64(self.Bandwidth)), "bandwidth")
|
||||
desc.Add(jsonutils.NewString(self.Mode), "mode")
|
||||
region := self.GetRegion()
|
||||
if len(region.ExternalId) > 0 {
|
||||
regionInfo := strings.Split(region.ExternalId, "/")
|
||||
if len(regionInfo) == 2 {
|
||||
desc.Add(jsonutils.NewString(strings.ToLower(regionInfo[0])), "hypervisor")
|
||||
desc.Add(jsonutils.NewString(regionInfo[1]), "region")
|
||||
}
|
||||
}
|
||||
|
||||
// region := self.GetRegion()
|
||||
// if len(region.ExternalId) > 0 {
|
||||
// regionInfo := strings.Split(region.ExternalId, "/")
|
||||
// if len(regionInfo) == 2 {
|
||||
// desc.Add(jsonutils.NewString(strings.ToLower(regionInfo[0])), "hypervisor")
|
||||
// desc.Add(jsonutils.NewString(regionInfo[1]), "region")
|
||||
// }
|
||||
//}
|
||||
|
||||
billingInfo := self.getCloudBillingInfo()
|
||||
|
||||
billingInfo.InternetChargeType = self.ChargeType
|
||||
|
||||
desc.Update(jsonutils.Marshal(billingInfo))
|
||||
|
||||
return desc
|
||||
}
|
||||
|
||||
@@ -863,3 +871,9 @@ func (self *SElasticip) DoPendingDelete(ctx context.Context, userCred mcclient.T
|
||||
}
|
||||
self.Dissociate(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SElasticip) getCloudBillingInfo() SCloudBillingInfo {
|
||||
region := self.GetRegion()
|
||||
provider := self.GetCloudprovider()
|
||||
return MakeCloudBillingInfo(region, nil, provider)
|
||||
}
|
||||
|
||||
@@ -3003,20 +3003,16 @@ func (self *SGuest) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc.Add(jsonutils.NewString(self.OsType), "os_type")
|
||||
}
|
||||
|
||||
if priceKey := self.GetMetadata("price_key", nil); len(priceKey) > 0 {
|
||||
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")
|
||||
}
|
||||
|
||||
desc.Set("hypervisor", jsonutils.NewString(self.GetHypervisor()))
|
||||
|
||||
host := self.GetHost()
|
||||
|
||||
spec := self.GetSpec(false)
|
||||
if self.GetHypervisor() == HYPERVISOR_BAREMETAL {
|
||||
host := self.GetHost()
|
||||
if host != nil {
|
||||
hostSpec := host.GetSpec(false)
|
||||
hostSpecIdent := HostManager.GetSpecIdent(hostSpec)
|
||||
@@ -3026,6 +3022,21 @@ func (self *SGuest) GetShortDesc() *jsonutils.JSONDict {
|
||||
if spec != nil {
|
||||
desc.Update(spec)
|
||||
}
|
||||
|
||||
var billingInfo SCloudBillingInfo
|
||||
|
||||
if host != nil {
|
||||
billingInfo = host.getCloudBillingInfo()
|
||||
}
|
||||
|
||||
if priceKey := self.GetMetadata("price_key", nil); len(priceKey) > 0 {
|
||||
billingInfo.PriceKey = priceKey
|
||||
}
|
||||
|
||||
billingInfo.ChargeType = self.GetChargeType()
|
||||
|
||||
desc.Update(jsonutils.Marshal(billingInfo))
|
||||
|
||||
return desc
|
||||
}
|
||||
|
||||
|
||||
@@ -3356,3 +3356,18 @@ func (manager *SHostManager) GetHostByIp(hostIp string) (*SHost, error) {
|
||||
|
||||
return host.(*SHost), nil
|
||||
}
|
||||
|
||||
func (self *SHost) getCloudBillingInfo() SCloudBillingInfo {
|
||||
var region *SCloudregion
|
||||
zone := self.GetZone()
|
||||
if zone != nil {
|
||||
region = zone.GetRegion()
|
||||
}
|
||||
provider := self.GetCloudprovider()
|
||||
return MakeCloudBillingInfo(region, zone, provider)
|
||||
}
|
||||
|
||||
func (self *SHost) GetShortDesc() *jsonutils.JSONDict {
|
||||
info := self.getCloudBillingInfo()
|
||||
return jsonutils.Marshal(&info).(*jsonutils.JSONDict)
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ func (self *SSnapshot) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils.JSON
|
||||
func (self *SSnapshot) GetShortDesc() *jsonutils.JSONDict {
|
||||
res := self.SVirtualResourceBase.GetShortDesc()
|
||||
res.Add(jsonutils.NewInt(int64(self.Size)), "size")
|
||||
if cloudprovider := self.GetCloudprovider(); cloudprovider != nil {
|
||||
/*if cloudprovider := self.GetCloudprovider(); cloudprovider != nil {
|
||||
res.Add(jsonutils.NewString(cloudprovider.Provider), "hypervisor")
|
||||
}
|
||||
if len(self.CloudregionId) > 0 {
|
||||
@@ -188,7 +188,10 @@ func (self *SSnapshot) GetShortDesc() *jsonutils.JSONDict {
|
||||
if cloudRegion != nil {
|
||||
res.Add(jsonutils.NewString(cloudRegion.ExternalId), "region")
|
||||
}
|
||||
}
|
||||
}*/
|
||||
info := self.getCloudBillingInfo()
|
||||
res.Update(jsonutils.Marshal(&info))
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -603,3 +606,9 @@ func (self *SSnapshot) PerformPurge(ctx context.Context, userCred mcclient.Token
|
||||
err = self.RealDelete(ctx, userCred)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (self *SSnapshot) getCloudBillingInfo() SCloudBillingInfo {
|
||||
region := self.GetRegion()
|
||||
provider := self.GetCloudprovider()
|
||||
return MakeCloudBillingInfo(region, nil, provider)
|
||||
}
|
||||
|
||||
@@ -1127,3 +1127,18 @@ func (self *SStorage) ClearSchedDescCache() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SStorage) getCloudBillingInfo() SCloudBillingInfo {
|
||||
var region *SCloudregion
|
||||
zone := self.getZone()
|
||||
if zone != nil {
|
||||
region = zone.GetRegion()
|
||||
}
|
||||
provider := self.GetCloudprovider()
|
||||
return MakeCloudBillingInfo(region, zone, provider)
|
||||
}
|
||||
|
||||
func (self *SStorage) GetShortDesc() *jsonutils.JSONDict {
|
||||
info := self.getCloudBillingInfo()
|
||||
return jsonutils.Marshal(&info).(*jsonutils.JSONDict)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user