示例#1
0
文件: main.go 项目: yangzhares/core
// Main entry point for the IPAM microservice
func main() {
	createSchema := flag.Bool("createSchema", false, "Create schema")
	overwriteSchema := flag.Bool("overwriteSchema", false, "Overwrite schema")
	rootUrl := flag.String("rootUrl", "", "Root service URL")
	flag.Parse()

	if *createSchema || *overwriteSchema {
		err := ipam.CreateSchema(*rootUrl, *overwriteSchema)
		if err != nil {
			panic(err)
		}
		fmt.Println("Schema created.")
		return
	}

	channel, _, err := ipam.Run(*rootUrl)
	if err != nil {
		panic(err)
	}
	for {
		msg := <-channel
		fmt.Println(msg)
	}
}
示例#2
0
// Test the integration with root and topology service
func (s *MySuite) TestIntegration(c *check.C) {
	myLog(c, "Entering TestIntegration()")

	dir, _ := os.Getwd()
	myLog(c, "In", dir)

	// 1. Start topology service
	myLog(c, "STARTING TOPOLOGY SERVICE")
	channelTop, topoAddr, err := topology.Run(s.rootUrl)
	if err != nil {
		c.Error(err)
	}
	msg := <-channelTop
	myLog(c, "Topology service said:", msg)

	// 2. Add some hosts to topology service and test.
	topoAddr = "http://" + topoAddr
	client, err := common.NewRestClient(topoAddr, common.DefaultRestTimeout)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Calling ", topoAddr)
	topIndex := &common.IndexResponse{}
	err = client.Get("/", &topIndex)
	if err != nil {
		c.Error(err)
	}

	c.Assert(topIndex.ServiceName, check.Equals, "topology")
	hostsRelUrl := topIndex.Links.FindByRel("host-list")
	hostsUrl := topoAddr + hostsRelUrl
	myLog(c, "Host list URL: ", hostsUrl)

	// Get list of hosts - should be empty for now.
	var hostList []common.HostMessage
	client.Get(hostsRelUrl, &hostList)
	myLog(c, "Host list: ", hostList)
	c.Assert(len(hostList), check.Equals, 0)
	newHostReq := common.HostMessage{Ip: "10.10.10.10", RomanaIp: "10.64.0.0/16", AgentPort: 9999, Name: "HOST1000"}

	host1 := common.HostMessage{}
	client.Post(hostsRelUrl, newHostReq, &host1)
	myLog(c, "Response: ", host1)
	c.Assert(host1.Ip, check.Equals, "10.10.10.10")
	c.Assert(host1.Id, check.Equals, "1")
	//
	newHostReq = common.HostMessage{Ip: "10.10.10.11", RomanaIp: "10.65.0.0/16", AgentPort: 9999, Name: "HOST2000"}
	host2 := common.HostMessage{}
	client.Post(hostsRelUrl, newHostReq, &host2)
	myLog(c, "Response: ", host2)

	c.Assert(host2.Ip, check.Equals, "10.10.10.11")
	c.Assert(host2.Id, check.Equals, "2")
	var hostList2 []common.HostMessage
	client.Get(hostsRelUrl, &hostList2)
	myLog(c, "Host list: ", hostList2)

	c.Assert(len(hostList2), check.Equals, 2)

	// 3. Start tenant service
	myLog(c, "STARTING TENANT SERVICE")
	channelTen, tenantAddr, err := tenant.Run(s.rootUrl)
	if err != nil {
		c.Error(err)
	}
	tenantAddr = "http://" + tenantAddr
	msg = <-channelTen
	myLog(c, "Tenant service said:", msg)
	client, err = common.NewRestClient(tenantAddr, common.DefaultRestTimeout)
	if err != nil {
		c.Error(err)
	}

	// 4. Add a tenant and a segment
	err = client.NewUrl(tenantAddr)
	if err != nil {
		c.Error(err)
	}

	// Add first tenant
	tIn := tenant.Tenant{Name: "t1"}
	tOut := tenant.Tenant{}
	err = client.Post("/tenants", tIn, &tOut)
	if err != nil {
		c.Error(err)
	}
	t1Id := tOut.Id
	myLog(c, "Tenant", tOut)

	// Add second tenant
	tIn = tenant.Tenant{Name: "t2"}
	tOut = tenant.Tenant{}
	err = client.Post("/tenants", tIn, &tOut)
	if err != nil {
		c.Error(err)
	}
	t2Id := tOut.Id
	myLog(c, "Tenant", tOut)

	// Find first tenant
	tOut2 := tenant.Tenant{}
	tenant1Path := fmt.Sprintf("/tenants/%d", t1Id)
	err = client.Get(tenant1Path, &tOut2)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Found", tOut2)

	// Add 2 segments to tenant 1
	sIn := tenant.Segment{Name: "s1", TenantId: t1Id}
	sOut := tenant.Segment{}
	err = client.Post(tenant1Path+"/segments", sIn, &sOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Added segment s1 to t1: ", sOut)
	sIn = tenant.Segment{Name: "s2", TenantId: t1Id}
	sOut = tenant.Segment{}
	err = client.Post(tenant1Path+"/segments", sIn, &sOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Added segment s2 to t1: ", sOut)

	// Add 2 segments to tenant 2
	tenant2Path := fmt.Sprintf("/tenants/%d", t2Id)

	sIn = tenant.Segment{Name: "s1", TenantId: t2Id}
	sOut = tenant.Segment{}
	err = client.Post(tenant2Path+"/segments", sIn, &sOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Added segment s1 to t2: ", sOut)

	sIn = tenant.Segment{Name: "s2", TenantId: t2Id}
	sOut = tenant.Segment{}
	err = client.Post(tenant2Path+"/segments", sIn, &sOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Added segment s2 to t2: ", sOut)

	// 4. Start IPAM service
	myLog(c, "STARTING IPAM SERVICE")
	channelIpam, ipamAddr, err := ipam.Run(s.rootUrl)
	if err != nil {
		c.Error(err)
	}
	ipamAddr = fmt.Sprintf("http://%s", ipamAddr)
	msg = <-channelIpam
	myLog(c, "IPAM service said: ", msg)
	client, err = common.NewRestClient(ipamAddr, common.DefaultRestTimeout)
	if err != nil {
		c.Error(err)
	}

	// Get first IP
	vmIn := ipam.Vm{Name: "vm1", TenantId: fmt.Sprintf("%d", tOut.Id), SegmentId: fmt.Sprintf("%d", sOut.Id), HostId: host2.Id}
	vmOut := ipam.Vm{}
	err = client.Post("/vms", vmIn, &vmOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Received:", vmOut)
	myLog(c, "IP:", vmOut.Ip)

	// Get second IP
	vmIn = ipam.Vm{Name: "vm2", TenantId: fmt.Sprintf("%d", tOut.Id), SegmentId: fmt.Sprintf("%d", sOut.Id), HostId: host2.Id}
	vmOut = ipam.Vm{}
	err = client.Post("/vms", vmIn, &vmOut)
	if err != nil {
		c.Error(err)
	}
	myLog(c, "Received:", vmOut)
	myLog(c, "IP:", vmOut.Ip)

	// Try legacy request
	vmOut = ipam.Vm{}
	legacyUrl := "/allocateIpByName?tenantName=t1&segmentName=s1&hostName=HOST2000&instanceName=bla"
	myLog(c, "Calling legacy URL", legacyUrl)

	err = client.Get(legacyUrl, &vmOut)

	if err != nil {
		myLog(c, "Error %s\n", err)
		c.Error(err)
	}
	myLog(c, "Legacy received:", vmOut)
	myLog(c, "Legacy IP:", vmOut.Ip)

	// 5. Start Agent service
	myLog(c, "STARTING Agent SERVICE")
	channelAgent, _, err := agent.Run(s.rootUrl)
	if err != nil {
		c.Error(err)
	}
	msg = <-channelAgent
	myLog(c, "Agent service said:", msg)

}