Пример #1
0
func listToMap(list []yaml.Node) map[string]yaml.Node {
	toMap := make(map[string]yaml.Node)

	for _, val := range list {
		name, ok := yaml.FindString(val, "name")
		if !ok {
			return nil
		}

		asMap, ok := val.Value().(map[string]yaml.Node)
		if !ok {
			return nil
		}

		newMap := make(map[string]yaml.Node)
		for key, val := range asMap {
			if key != "name" {
				newMap[key] = val
			}
		}

		toMap[name] = yaml.SubstituteNode(newMap, val)
	}

	return toMap
}
Пример #2
0
func stepName(index int, value yaml.Node) string {
	name, ok := yaml.FindString(value, "name")
	if ok {
		return name
	}

	return fmt.Sprintf("[%d]", index)
}
Пример #3
0
func stepName(index int, value yaml.Node, keyName string) string {
	if keyName == "" {
		keyName = "name"
	}
	name, ok := yaml.FindString(value, keyName)
	if ok {
		return keyName + ":" + name
	}

	return fmt.Sprintf("[%d]", index)
}
Пример #4
0
func findByNameOrIndex(node yaml.Node, others []yaml.Node, index int) (string, yaml.Node, bool) {
	name, ok := yaml.FindString(node, "name")
	if !ok {
		return findByIndex(others, index)
	}

	key, node, found := findByName(name, others)
	if !found {
		return findByIndex(others, index)
	}

	return key, node, true
}
Пример #5
0
func findByName(name string, nodes []yaml.Node) (string, yaml.Node, bool) {
	for _, node := range nodes {
		otherName, ok := yaml.FindString(node, "name")
		if !ok {
			continue
		}

		if otherName == name {
			return name, node, true
		}
	}

	return "", nil, false
}
Пример #6
0
func jobMap(jobs []yaml.Node) map[string]yaml.Node {
	byName := make(map[string]yaml.Node)

	for index, job := range jobs {
		attrs, ok := job.Value().(map[string]yaml.Node)
		attrs["index"] = yaml.NewNode(index, job.SourceName())

		name, ok := yaml.FindString(job, "name")
		if !ok {
			panic("job without string name")
		}

		byName[name] = yaml.NewNode(attrs, job.SourceName())
	}

	return byName
}
Пример #7
0
func newEntries(a []yaml.Node, b []yaml.Node) []yaml.Node {
	added := []yaml.Node{}

	for _, val := range a {
		name, ok := yaml.FindString(val, "name")
		if ok {
			_, found := yaml.Find(yaml.NewNode(b, "some map"), name) // TODO
			if found {
				continue
			}
		}

		added = append(added, val)
	}

	return added
}
Пример #8
0
func (e AutoExpr) Evaluate(binding Binding) (yaml.Node, EvaluationInfo, bool) {
	info := DefaultInfo()

	if len(e.Path) == 3 && e.Path[0] == "resource_pools" && e.Path[2] == "size" {
		jobs, info, found := refJobs.Evaluate(binding)
		if !found {
			info.Issue = "no jobs found"
			return nil, info, false
		}

		if !isResolved(jobs) {
			return node(e), info, true
		}
		jobsList, ok := jobs.Value().([]yaml.Node)
		if !ok {
			info.Issue = "jobs must be a list"
			return nil, info, false
		}

		var size int64

		for _, job := range jobsList {
			poolName, ok := yaml.FindString(job, "resource_pool")
			if !ok {
				continue
			}

			if poolName != yaml.PathComponent(e.Path[1]) {
				continue
			}

			instances, ok := yaml.FindInt(job, "instances")
			if !ok {
				return nil, info, false
			}

			size += instances
		}

		return node(size), info, true
	}

	info.Issue = "auto only allowed for size entry in resource pools"
	return nil, info, false
}
Пример #9
0
func (e AutoExpr) Evaluate(binding Binding) (yaml.Node, bool) {
	if len(e.Path) == 3 && e.Path[0] == "resource_pools" && e.Path[2] == "size" {
		jobs, found := binding.FindFromRoot([]string{"jobs"})
		if !found {
			return nil, false
		}

		jobsList, ok := jobs.Value().([]yaml.Node)
		if !ok {
			return nil, false
		}

		var size int64

		for _, job := range jobsList {
			poolName, ok := yaml.FindString(job, "resource_pool")
			if !ok {
				continue
			}

			if poolName != e.Path[1] {
				continue
			}

			instances, ok := yaml.FindInt(job, "instances")
			if !ok {
				return nil, false
			}

			size += instances
		}

		return node(size), true
	}

	return nil, false
}