• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Goconsul获取KV

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.简单实现

package main

import (
	"encoding/json"
	"fmt"
	consulapi "github.com/hashicorp/consul/api"
	"log"
	//_ "github.com/spf13/viper/remote"
)

var (
	cloudEntryURL =  "cloud_entry/openstack/"
	resourceURL   =  "cmp/resource/yacmp:cloudentry:type:openstack/iaas.machine/"
)

type cloudEntryValue struct {
	authInfo     map[string]string
	resourceInfo map[string]map[string]string
}

func translate(m map[string]interface{}) map[string]string {
	ret := make(map[string]string, len(m))
	for k, v := range m {
		ret[k] = fmt.Sprint(v)
	}
	return ret
}

func listResources(address string) (cloudEntry, cloudResource consulapi.KVPairs) {
	config := consulapi.DefaultConfig()
	config.Address = address
	client, err := consulapi.NewClient(config)
	if err != nil {
		log.Fatal("consul client error : ", err)
	}
	cloudEntry, _, _ = client.KV().List(cloudEntryURL, nil)
	cloudResource, _, _ = client.KV().List(resourceURL, nil)
	return
}

func getResourceCollection(address string) map[string]*cloudEntryValue {
	resourceCollection := make(map[string]*cloudEntryValue)
	cloudEntry, cloudResource := listResources(address)
	for _, value := range cloudEntry {
		var cloudEntryValue = new(cloudEntryValue)
		var entryInfos = make(map[string]string)
		if value.Value == nil {
			continue
		}
		err := json.Unmarshal(value.Value, &entryInfos)
		if err != nil {
			log.Fatal("unmarshal json from consul error : ", err)
		}
		if _, ok := resourceCollection[entryInfos["cloud_entry_id"]]; ok {
			continue
		}
		fmt.Println(resourceCollection,"resourceCollection")
		cloudEntryValue.authInfo = entryInfos
		cloudEntryValue.resourceInfo = make(map[string]map[string]string)
		resourceCollection[entryInfos["cloud_entry_id"]] = cloudEntryValue
	}
	for _, value := range cloudResource {
		var resources map[string]interface{}
		if value.Value == nil {
			continue
		}
		err := json.Unmarshal(value.Value, &resources)
		if err != nil {
			log.Fatal("unmarshal json from consul error : ", err)
		}
		if _, ok := resourceCollection[resources["cloud_entry_id"].(string)]; ok {
			if cmpInfos, ok := resources["resources"]; ok {
				cmpInfo := cmpInfos.([]interface{})[0].(map[string]interface{})
				if instanceInfo, ok := cmpInfo["cmp_info"]; ok {
					key := instanceInfo.(map[string]interface{})["external_name"].(string)
					value := translate(instanceInfo.(map[string]interface{}))
					cloudEntry := resourceCollection[resources["cloud_entry_id"].(string)]
					cloudEntry.resourceInfo[key] = value
				}
			}
		}
	}
	return resourceCollection
}


func main(){
	consulAddress := "http://47.98.136.126:8500"
	res:=getResourceCollection(consulAddress)
	for k,v:=range res{
		fmt.Println(k,v)
		fmt.Println("id",v.authInfo["cloud_entry_id"])
		for k,v:=range v.resourceInfo{
			fmt.Println("k",k)
			fmt.Println("v",v["zone"])
		}
	}


}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言核心36讲(Go语言进阶技术三)--学习笔记发布时间:2022-07-10
下一篇:
《Go语言实战》摘录:6.2 并发 - goroutine发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap