Skip to content

Commit

Permalink
feat: add host cloud region and zone attr
Browse files Browse the repository at this point in the history
  • Loading branch information
wcy00000000000000 committed May 13, 2024
1 parent 1873716 commit 7c8b452
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resources/language/default/property.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
"host_property_bk_cloud_inst_id": "云主机实例ID",
"host_property_bk_cloud_host_status": "云主机状态",
"host_property_bk_cloud_vendor": "云厂商",
"host_property_bk_cloud_region": "云地域(Region)",
"host_property_bk_cloud_zone": "云可用区(Zone)",
"process_property_bk_biz_id": "业务ID",
"process_property_bk_process_name": "进程别名",
"process_property_description": "备注",
Expand Down
2 changes: 2 additions & 0 deletions resources/language/en/property.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
"host_property_bk_cloud_inst_id": "Cloud Instance ID",
"host_property_bk_cloud_host_status": "Cloud Host status",
"host_property_bk_cloud_vendor": "Cloud vendor",
"host_property_bk_cloud_region": "Cloud Region",
"host_property_bk_cloud_zone": "Cloud Zone",
"process_property_bk_biz_id": "Business ID",
"process_property_bk_process_name": "Alias",
"process_property_description": "Description",
Expand Down
5 changes: 5 additions & 0 deletions src/common/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ const (

// ObjectIDField the object id field, it is an int type field and is used to associate with the model
ObjectIDField = "object_id"

// BKCloudRegionField is the cloud region field
BKCloudRegionField = "bk_cloud_region"
// BKCloudZoneField is the cloud zone field
BKCloudZoneField = "bk_cloud_zone"
)

const (
Expand Down
1 change: 1 addition & 0 deletions src/scene_server/admin_server/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ import (
_ "configcenter/src/scene_server/admin_server/upgrader/y3.13.202402281158"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.13.202403151855"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.13.202404221100"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.13.202405080955"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 THL A29 Limited,
* a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_13_202405080955

import (
"context"
"fmt"

"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
mCommon "configcenter/src/scene_server/admin_server/common"
"configcenter/src/scene_server/admin_server/upgrader"
"configcenter/src/storage/dal"
)

var attributes = []attribute{
{
PropertyID: common.BKCloudRegionField,
PropertyName: "云地域(Region)",
IsEditable: false,
IsRequired: false,
PropertyType: common.FieldTypeSingleChar,
Option: "",
},
{
PropertyID: common.BKCloudZoneField,
PropertyName: "云可用区(Zone)",
IsEditable: false,
IsRequired: false,
PropertyType: common.FieldTypeSingleChar,
Option: "",
},
}

func addAttribute(ctx context.Context, db dal.RDB, conf *upgrader.Config) error {
attrFilter := mapstr.MapStr{
common.BKObjIDField: common.BKInnerObjIDHost,
common.BKPropertyIDField: mapstr.MapStr{
common.BKDBIN: []string{common.BKCloudRegionField, common.BKCloudZoneField},
},
common.BkSupplierAccount: conf.OwnerID,
}

existAttrs := make([]attribute, 0)
err := db.Table(common.BKTableNameObjAttDes).Find(attrFilter).All(ctx, &existAttrs)
if err != nil {
blog.Errorf("get exist cloud region and zone attribute failed, err: %v, filter: %+v", err, attrFilter)
return err
}

existAttrMap := make(map[string]struct{})
for _, attr := range existAttrs {
if attr.Creator != conf.User {
blog.Errorf("exist attribute(%+v) is not created by %s", attr, conf.User)
return fmt.Errorf("exist attribute(%s) is not created by %s", attr.PropertyName, conf.User)
}
existAttrMap[attr.PropertyID] = struct{}{}
}

if len(existAttrMap) == len(attributes) {
return nil
}

attrIDs, err := db.NextSequences(ctx, common.BKTableNameObjAttDes, len(attributes)-len(existAttrMap))
if err != nil {
blog.Errorf("get new attribute ids for cloud region and zone failed, err: %v", err)
return err
}

attrIndexFilter := mapstr.MapStr{
common.BKObjIDField: common.BKInnerObjIDHost,
}
sort := common.BKPropertyIndexField + ":-1"
lastAttr := new(attribute)

if err = db.Table(common.BKTableNameObjAttDes).Find(attrIndexFilter).Sort(sort).One(ctx, lastAttr); err != nil {
blog.Errorf("get host attribute max property index id failed, err: %v", err)
return err
}

now := metadata.Now()

createAttrs := make([]attribute, 0)
createAttrIdx := 0
for _, attr := range attributes {
_, exists := existAttrMap[attr.PropertyID]
if exists {
continue
}

attr.ID = int64(attrIDs[createAttrIdx])
attr.OwnerID = conf.OwnerID
attr.ObjectID = common.BKInnerObjIDHost
attr.PropertyGroup = mCommon.BaseInfo
attr.PropertyIndex = lastAttr.PropertyIndex + 1 + int64(createAttrIdx)
attr.IsPre = true
attr.Creator = conf.User
attr.CreateTime = &now
attr.LastTime = &now
createAttrs = append(createAttrs, attr)
createAttrIdx++
}

if err = db.Table(common.BKTableNameObjAttDes).Insert(ctx, createAttrs); err != nil {
blog.Errorf("create attributes failed, err: %v, attrs: %+v", err, createAttrs)
return err
}
return nil
}

type attribute struct {
BizID int64 `field:"bk_biz_id" json:"bk_biz_id" bson:"bk_biz_id" mapstructure:"bk_biz_id"`
ID int64 `field:"id" json:"id" bson:"id" mapstructure:"id"`
OwnerID string `field:"bk_supplier_account" json:"bk_supplier_account" bson:"bk_supplier_account" mapstructure:"bk_supplier_account"`
ObjectID string `field:"bk_obj_id" json:"bk_obj_id" bson:"bk_obj_id" mapstructure:"bk_obj_id"`
PropertyID string `field:"bk_property_id" json:"bk_property_id" bson:"bk_property_id" mapstructure:"bk_property_id"`
PropertyName string `field:"bk_property_name" json:"bk_property_name" bson:"bk_property_name" mapstructure:"bk_property_name"`
PropertyGroup string `field:"bk_property_group" json:"bk_property_group" bson:"bk_property_group" mapstructure:"bk_property_group"`
PropertyGroupName string `field:"bk_property_group_name,ignoretomap" json:"bk_property_group_name" bson:"-" mapstructure:"bk_property_group_name"`
PropertyIndex int64 `field:"bk_property_index" json:"bk_property_index" bson:"bk_property_index" mapstructure:"bk_property_index"`
Unit string `field:"unit" json:"unit" bson:"unit" mapstructure:"unit"`
Placeholder string `field:"placeholder" json:"placeholder" bson:"placeholder" mapstructure:"placeholder"`
IsEditable bool `field:"editable" json:"editable" bson:"editable" mapstructure:"editable"`
IsPre bool `field:"ispre" json:"ispre" bson:"ispre" mapstructure:"ispre"`
IsRequired bool `field:"isrequired" json:"isrequired" bson:"isrequired" mapstructure:"isrequired"`
IsReadOnly bool `field:"isreadonly" json:"isreadonly" bson:"isreadonly" mapstructure:"isreadonly"`
IsOnly bool `field:"isonly" json:"isonly" bson:"isonly" mapstructure:"isonly"`
IsSystem bool `field:"bk_issystem" json:"bk_issystem" bson:"bk_issystem" mapstructure:"bk_issystem"`
IsAPI bool `field:"bk_isapi" json:"bk_isapi" bson:"bk_isapi" mapstructure:"bk_isapi"`
PropertyType string `field:"bk_property_type" json:"bk_property_type" bson:"bk_property_type" mapstructure:"bk_property_type"`
Option interface{} `field:"option" json:"option" bson:"option" mapstructure:"option"`
Default interface{} `field:"default" json:"default,omitempty" bson:"default" mapstructure:"default"`
IsMultiple *bool `field:"ismultiple" json:"ismultiple,omitempty" bson:"ismultiple" mapstructure:"ismultiple"`
Description string `field:"description" json:"description" bson:"description" mapstructure:"description"`
TemplateID int64 `field:"bk_template_id" json:"bk_template_id" bson:"bk_template_id" mapstructure:"bk_template_id"`
Creator string `field:"creator" json:"creator" bson:"creator" mapstructure:"creator"`
CreateTime *metadata.Time `json:"create_time" bson:"create_time" mapstructure:"create_time"`
LastTime *metadata.Time `json:"last_time" bson:"last_time" mapstructure:"last_time"`
}
42 changes: 42 additions & 0 deletions src/scene_server/admin_server/upgrader/y3.13.202405080955/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 THL A29 Limited,
* a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_13_202405080955

import (
"context"

"configcenter/src/common/blog"
"configcenter/src/scene_server/admin_server/upgrader"
"configcenter/src/storage/dal"
)

func init() {
upgrader.RegistUpgrader("y3.13.202405080955", upgrade)
}

func upgrade(ctx context.Context, db dal.RDB, conf *upgrader.Config) (err error) {
blog.Infof("start execute y3.13.202405080955")

if err = addAttribute(ctx, db, conf); err != nil {
blog.Errorf("upgrade y3.13.202405080955 add cloud region and zone attribute failed, err: %v", err)
return err
}

blog.Infof("upgrade y3.13.202405080955 add cloud region and zone attribute success")
return nil
}

0 comments on commit 7c8b452

Please sign in to comment.