-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from cxl123156/br_release_sdk_v3
发布2.0.7版本
- Loading branch information
Showing
503 changed files
with
64,901 additions
and
9,870 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package collector | ||
|
||
import ( | ||
"time" | ||
|
||
cesmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ces/v1/model" | ||
|
||
"github.com/huaweicloud/cloudeye-exporter/logs" | ||
) | ||
|
||
var cfwServerInfo serversInfo | ||
|
||
type CFWInfo struct{} | ||
|
||
func (cfw CFWInfo) GetResourceInfo() (map[string]labelInfo, []cesmodel.MetricInfoList) { | ||
resourceInfos := map[string]labelInfo{} | ||
filterMetrics := make([]cesmodel.MetricInfoList, 0) | ||
cfwServerInfo.Lock() | ||
defer cfwServerInfo.Unlock() | ||
if cfwServerInfo.LabelInfo == nil || time.Now().Unix() > cfwServerInfo.TTL { | ||
cfwConfigMap := getMetricConfigMap("SYS.CFW") | ||
if cfwConfigMap == nil { | ||
logs.Logger.Warn("Metric config is nil.") | ||
return cfwServerInfo.LabelInfo, cfwServerInfo.FilterMetrics | ||
} | ||
|
||
if _, ok := cfwConfigMap["fw_instance_id"]; !ok { | ||
logs.Logger.Warn("Metric config is nil of SYS.CFW of fw_instance_id.") | ||
return cfwServerInfo.LabelInfo, cfwServerInfo.FilterMetrics | ||
} | ||
|
||
metricNames := cfwConfigMap["fw_instance_id"] | ||
if len(metricNames) == 0 { | ||
logs.Logger.Warn("Metric config is empty of SYS.CFW of fw_instance_id.") | ||
return cfwServerInfo.LabelInfo, cfwServerInfo.FilterMetrics | ||
} | ||
|
||
servers, err := getResourcesBaseInfoFromRMS("cfw", "cfw_instance") | ||
if err != nil { | ||
logs.Logger.Errorf("Get resource base info from RMS Server error:", err.Error()) | ||
return ecsInfo.LabelInfo, ecsInfo.FilterMetrics | ||
} | ||
|
||
for _, server := range servers { | ||
metrics := buildSingleDimensionMetrics(metricNames, "SYS.CFW", "fw_instance_id", server.ID) | ||
filterMetrics = append(filterMetrics, metrics...) | ||
info := labelInfo{ | ||
Name: []string{"name", "ep_id"}, | ||
Value: []string{server.Name, server.EpId}, | ||
} | ||
keys, values := getTags(server.Tags) | ||
info.Name = append(info.Name, keys...) | ||
info.Value = append(info.Value, values...) | ||
resourceInfos[GetResourceKeyFromMetricInfo(metrics[0])] = info | ||
} | ||
|
||
cfwServerInfo.LabelInfo = resourceInfos | ||
cfwServerInfo.FilterMetrics = filterMetrics | ||
cfwServerInfo.TTL = time.Now().Add(TTL).Unix() | ||
} | ||
return cfwServerInfo.LabelInfo, cfwServerInfo.FilterMetrics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package collector | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/rms/v1/model" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/huaweicloud/cloudeye-exporter/logs" | ||
) | ||
|
||
func TestCFWInfo_GetResourceInfo_configIsNil(t *testing.T) { | ||
patches := getPatches() | ||
defer patches.Reset() | ||
logs.InitLog() | ||
cfwInfoTest := CFWInfo{} | ||
labelInfos, filterMetrics := cfwInfoTest.GetResourceInfo() | ||
assert.Nil(t, labelInfos) | ||
assert.Nil(t, filterMetrics) | ||
} | ||
|
||
func TestCFWInfo_GetResourceInfo_dimConfigIsNotExists(t *testing.T) { | ||
metricConfigMap := map[string][]string{} | ||
patches := getPatches() | ||
defer patches.Reset() | ||
patches.ApplyFuncReturn(getMetricConfigMap, metricConfigMap) | ||
|
||
logs.InitLog() | ||
cfwInfoTest := CFWInfo{} | ||
labelInfos, filterMetrics := cfwInfoTest.GetResourceInfo() | ||
assert.Nil(t, labelInfos) | ||
assert.Nil(t, filterMetrics) | ||
} | ||
|
||
func TestCFWInfo_GetResourceInfo_dimConfigIsEmpty(t *testing.T) { | ||
patches := getPatches() | ||
defer patches.Reset() | ||
|
||
metricConfigMap := map[string][]string{ | ||
"fw_instance_id": nil, | ||
} | ||
patches.ApplyFuncReturn(getMetricConfigMap, metricConfigMap) | ||
logs.InitLog() | ||
cfwInfoTest := CFWInfo{} | ||
labelInfos, filterMetrics := cfwInfoTest.GetResourceInfo() | ||
assert.Nil(t, labelInfos) | ||
assert.Nil(t, filterMetrics) | ||
} | ||
|
||
func TestCFWInfo_GetResourceInfo_getResourcesFromRMSFailed(t *testing.T) { | ||
patches := getPatches() | ||
defer patches.Reset() | ||
|
||
metricConfigMap := map[string][]string{ | ||
"fw_instance_id": {"metric1", "metric2"}, | ||
} | ||
patches.ApplyFuncReturn(getMetricConfigMap, metricConfigMap) | ||
patches.ApplyFuncReturn(listResources, nil, errors.New("test err")) | ||
|
||
logs.InitLog() | ||
cfwInfoTest := CFWInfo{} | ||
labelInfos, filterMetrics := cfwInfoTest.GetResourceInfo() | ||
assert.Nil(t, labelInfos) | ||
assert.Nil(t, filterMetrics) | ||
} | ||
|
||
func TestCFWInfo_GetResourceInfo_success(t *testing.T) { | ||
patches := getPatches() | ||
defer patches.Reset() | ||
|
||
metricConfigMap := map[string][]string{ | ||
"fw_instance_id": {"metric1", "metric2"}, | ||
} | ||
patches.ApplyFuncReturn(getMetricConfigMap, metricConfigMap) | ||
patches.ApplyFuncReturn(listResources, resourceEntityInit(), nil) | ||
logs.InitLog() | ||
cfwInfoTest := CFWInfo{} | ||
// 两个指标,两个资源 | ||
labelInfos, filterMetrics := cfwInfoTest.GetResourceInfo() | ||
assert.NotNil(t, labelInfos) | ||
assert.Equal(t, 2, len(labelInfos)) | ||
assert.NotNil(t, filterMetrics) | ||
assert.Equal(t, 4, len(filterMetrics)) | ||
} | ||
|
||
func getPatches() *gomonkey.Patches { | ||
confLoader := &logs.ConfLoader{} | ||
patches := gomonkey.ApplyMethodFunc(*confLoader, "LoadFile", func(fPath string, cfg interface{}) error { | ||
cfgTmp, _ := cfg.(*map[string][]logs.Config) | ||
cfgPointer := make(map[string][]logs.Config) | ||
cfgPointer["business"] = []logs.Config{ | ||
{ | ||
Level: zapcore.InfoLevel, | ||
}, | ||
} | ||
*cfgTmp = cfgPointer | ||
return nil | ||
}) | ||
return patches | ||
} | ||
|
||
func resourceEntityInit() []model.ResourceEntity { | ||
id1 := "xxxx2" | ||
name1 := "test" | ||
epId1 := "1" | ||
epName1 := "测试企业1" | ||
checksum1 := "xxxb" | ||
create1 := "2023-12-23T07:58:14.000Z" | ||
update1 := "2023-12-23T07:58:14.000Z" | ||
provisioningState1 := "Succeeded" | ||
|
||
id2 := "xxxx3" | ||
name2 := "test2" | ||
epId2 := "2" | ||
epName2 := "测试企业2" | ||
checksum2 := "xxxc" | ||
create2 := "2023-12-23T07:58:14.000Z" | ||
update2 := "2023-12-25T07:58:14.000Z" | ||
provisioningState2 := "Succeeded" | ||
|
||
provider := "cfw" | ||
typestr := "cfw_instance" | ||
regionId := "cn-north-7" | ||
projectId := "xxxx0" | ||
projectName := "cn-north-7" | ||
tags := map[string]string{} | ||
properties := map[string]interface{}{ | ||
"domain_id": "xxxx001", | ||
"engine_type": 0, | ||
"enterprise_project_id": "1", | ||
"service_type": 0, | ||
"project_id": "xxxx002", | ||
"fw_instance_name": "test", | ||
"name": "1703318294687", | ||
"policy_count": 0, | ||
"fw_instance_id": "xxxx003", | ||
"status": "status", | ||
} | ||
|
||
response := []model.ResourceEntity{ | ||
{ | ||
Id: &id1, | ||
Name: &name1, | ||
Provider: &provider, | ||
Type: &typestr, | ||
RegionId: ®ionId, | ||
ProjectId: &projectId, | ||
ProjectName: &projectName, | ||
EpId: &epId1, | ||
EpName: &epName1, | ||
Checksum: &checksum1, | ||
Created: &create1, | ||
Updated: &update1, | ||
ProvisioningState: &provisioningState1, | ||
Tags: tags, | ||
Properties: properties, | ||
}, | ||
{ | ||
Id: &id2, | ||
Name: &name2, | ||
Provider: &provider, | ||
Type: &typestr, | ||
RegionId: ®ionId, | ||
ProjectId: &projectId, | ||
ProjectName: &projectName, | ||
EpId: &epId2, | ||
EpName: &epName2, | ||
Checksum: &checksum2, | ||
Created: &create2, | ||
Updated: &update2, | ||
ProvisioningState: &provisioningState2, | ||
Tags: tags, | ||
Properties: properties, | ||
}, | ||
} | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.