Ejemplo n.º 1
0
// @Title List
// @Description find networks by condition
// @Param	body		body 	models.CommonRequestBody	true		"the networks you want to get"
// @Success 200 {object} models.IPtable
// @Failure 400 invalid request message
// @router /list [post]
func (self *NetworkController) 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(403)
		self.ServeJSON()
		return
	}

	if body.All {
		list := models.GetAllIPtable()
		self.Data["json"] = list
		self.ServeJSON()
		return
	}

	list := make([]interface{}, len(body.ID))
	for i, cid := range body.ID {
		if strings.TrimSpace(cid) == "" {
			continue
		}
		if err, c := models.GetIPtable(cid); err == nil {
			list[i] = c
		} else {
			list[i] = models.IPtable{
				ID:    cid,
				Error: ErrNotFound.Error(),
			}
		}
	}
	self.Data["json"] = list
	self.ServeJSON()
}
Ejemplo n.º 2
0
// @Title Get
// @Description find network by networkID
// @Param	netwrokID		path 	string	true		"The networkID you want to get"
// @Success 200 {object} models.IPtable
// @Failure 403 :networkID is empty
// @router /:id [get]
func (self *NetworkController) Get() {
	id := self.GetString(":id")
	if strings.TrimSpace(id) != "" {
		err, iptable := models.GetIPtable(id)

		if err != nil {
			self.Data["json"] = models.NewInternalError(403, err)
			self.Ctx.Output.SetStatus(403)
		} else {
			self.Data["json"] = iptable
		}
	} else {
		self.Data["json"] = models.NewInternalError(403,
			fmt.Errorf("%s iptable_id is empty", id))
		self.Ctx.Output.SetStatus(403)
	}
	self.ServeJSON()
}