提交 d4766f31 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

support instance use another instance

上级 56a1dc62
title: 私有IP
desc:
author: zentao
version: 1.0
field: privateIP
instances:
- instance: all
fields:
- field: part
from: system.ip.v1.yaml
use: privateA, privateB, privateC
prefix: ""
postfix: "/24"
......@@ -23,7 +23,20 @@ instances:
range: 16-31
postfix: .
- field: part3
range: 0-255
range: 0-254
postfix: .
- field: part4
range: 0-255
range: 1-254
- instance: privateA
note: A类IP地址
prefix: 10.
fields:
- field: part2
range: 0-254
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
\ No newline at end of file
......@@ -19,3 +19,9 @@ fields:
- field: field_loop # 引用default.xml里的自循环的字段
prefix: "[" # 复写前缀
postfix: "]" # 特殊字符加引号,否则无法解析
- field: field_nested_inst # 引用的instance使用多层嵌套的use定义
from: system.ip.private.yaml # 引用data/system/ip/v1.yaml
use: all # 使用该文件中定义的privateC和privateB两个实例。
prefix: "[" # 复写前缀
postfix: "]" # 特殊字符加引号,否则无法解析
......@@ -79,6 +79,10 @@ func GenerateForField(field *model.DefField, total int, withFix bool) []string {
} else if field.From != "" { // refer to res
if field.Use != "" { // refer to instance
if field.Use == "privateIP" {
fmt.Println("")
}
groupValues := vari.Res[field.From]
groups := strings.Split(field.Use, ",")
for _, group := range groups {
......
......@@ -147,23 +147,59 @@ func getResForInstances(insts model.ResInsts) map[string][]string {
groupedValue := map[string][]string{}
for _, inst := range insts.Instances {
group := inst.Instance
for _, instField := range inst.Fields { // prepare referred parent instances if needed
if instField.Use != "" { // refer to another instance
parent := getRootInstant(instField)
groupedValueParent := map[string][]string{}
for _, child := range parent.Instances {
field := convertInstantToField(parent, child)
// gen values
group := child.Instance
groupedValueParent[group] = GenerateForField(&field, constant.Total, false)
}
vari.Res[instField.From] = groupedValueParent
}
}
// convert instant field to standard field
tempField := model.DefField{}
tempField.Field = insts.Field
field := convertInstantToField(insts, inst)
child := model.DefField{}
child.Field = inst.Instance
// gen values
group := inst.Instance
groupedValue[group] = GenerateForField(&field, constant.Total, false)
}
copier.Copy(&child, inst)
return groupedValue
}
tempField.Fields = append(tempField.Fields, child)
func getRootInstant(inst model.DefField) (parentInsts model.ResInsts) {
resFile, _ := getResProp(inst.From)
groupedValue[group] = GenerateForField(&tempField, constant.Total, false)
yamlContent, err := ioutil.ReadFile(resFile)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", resFile))
return
}
return groupedValue
err = yaml.Unmarshal(yamlContent, &parentInsts)
if err == nil && parentInsts.Instances != nil && len(parentInsts.Instances) > 0 { // instances
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", resFile))
}
return
}
func convertInstantToField(insts model.ResInsts, inst model.ResInst) (field model.DefField) {
field.Field = insts.Field
child := model.DefField{}
child.Field = inst.Instance
copier.Copy(&child, inst)
field.Fields = append(field.Fields, child)
return field
}
func getResForRanges(ranges model.ResRanges) map[string][]string {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册