コード例 #1
0
ファイル: node.go プロジェクト: fugr/APIServer
// @Title Get
// @Description find node by nodeID
// @Param	nodeID		path 	string	true		"The nodeID you want to get"
// @Success 200 {object} models.ResponseNode
// @Failure 403 :nodeID is empty
// @router /:id [get]
func (self *NodeController) Get() {
	id := self.GetString(":id")
	if strings.TrimSpace(id) != "" {
		err, node := models.GetNode(id)

		if err != nil {
			self.Data["json"] = models.NewInternalError(403, err)
			self.Ctx.Output.SetStatus(403)
		} else {
			self.Data["json"] = models.ResponseNode{
				ID:        node.ID,
				IPAddress: node.IPAddress,
				NCPU:      node.NCPU,
				Mem_MB:    node.Mem_MB,
				Status:    node.Status,
				Runtime:   node.Runtime,
			}
		}
	} else {
		self.Data["json"] = models.NewInternalError(403,
			fmt.Errorf("%s node_id is empty", id))
		self.Ctx.Output.SetStatus(403)
	}
	self.ServeJSON()
}
コード例 #2
0
ファイル: node.go プロジェクト: fugr/APIServer
// @Title List
// @Description find node by node_ID
// @Param	body		body 	models.CommonRequestBody	true		"the nodes you want to get"
// @Success 200 {object} models.ResponseNode
// @Failure 400 invalid request message
// @router /list [post]
func (self *NodeController) List() {
	var body models.CommonRequestBody
	err := json.Unmarshal(self.Ctx.Input.CopyBody(beego.BConfig.MaxMemory), &body)
	if err != nil {
		self.Data["json"] = models.NewInternalError(400, err)
		self.Ctx.Output.SetStatus(400)
		self.ServeJSON()
		return
	}
	if body.All {
		list := make([]models.ResponseNode, 0, 100)
		nodes := models.GetAllNode()
		for _, node := range nodes {
			list = append(list, models.ResponseNode{
				ID:        node.ID,
				IPAddress: node.IPAddress,
				NCPU:      node.NCPU,
				Mem_MB:    node.Mem_MB,
				Status:    node.Status,
				Runtime:   node.Runtime,
			})
		}
		self.Data["json"] = list
		self.ServeJSON()
		return
	}

	list := make([]models.ResponseNode, len(body.ID))
	for i, id := range body.ID {
		if strings.TrimSpace(id) == "" {
			continue
		}
		if err, node := models.GetNode(id); err == nil {
			list[i] = models.ResponseNode{
				ID:        node.ID,
				IPAddress: node.IPAddress,
				NCPU:      node.NCPU,
				Mem_MB:    node.Mem_MB,
				Status:    node.Status,
				Runtime:   node.Runtime,
			}
		} else {
			list[i] = models.ResponseNode{
				ID:    id,
				Error: ErrNotFound.Error(),
			}
		}
	}
	self.Data["json"] = list
	self.ServeJSON()
}