Ejemplo n.º 1
0
func QueryHostGroups() (map[int][]int, error) {
	m := make(map[int][]int)

	groupMap, err := db.GetHostGroupList()
	if err != nil {
		log.Println("[ERROR] get hostgroup list error! %v", err)
		return m, err
	}

	//遍历group map,从enc中分别取出group 与 host的map
	for gid, gname := range groupMap {
		cmd := "GetHostGroup " + gname

		output, err := ExecCommand(cmd)
		if err != nil {
			return m, nil
		}

		for hidString, _ := range output {
			hid, _ := strconv.Atoi(hidString)
			if _, exists := m[hid]; exists {
				m[hid] = append(m[hid], gid)
			} else {
				m[hid] = []int{gid}
			}
		}
	}

	return m, nil
}
Ejemplo n.º 2
0
// 一个机器ID对应了多个模板ID
func QueryHostTemplateIds() (map[int][]int, error) {
	ret := make(map[int][]int)
	//获取group 与 template的对应关系
	groupTempMap, err := db.QueryGroupTemplates()
	if err != nil {
		return ret, nil
	}

	//获取group map
	groupMap, err := db.GetHostGroupList()
	if err != nil {
		return ret, err
	}

	//hostTempMap := make(map[int]map[int]string)
	for gid, gname := range groupMap {
		cmd := "GetHostGroup " + gname
		output, err := ExecCommand(cmd)
		if err != nil {
			continue
		}

		for hidString, _ := range output {
			hid, _ := strconv.Atoi(hidString)
			if _, exists := ret[hid]; !exists {
				ret[hid] = make([]int, 0)
			}
			ret[hid] = append(ret[hid], groupTempMap[gid]...)
		}
	}

	return ret, nil
}