// @Title create // @Description create users // @Param body body models.PostNode true "body for node content" // @Success 200 {object} models.ResponsePostNode // @Failure 400 invalid request message // @router / [post] func (self *NodeController) Post() { var body []models.PostNode 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 } resp := make([]models.ResponsePostNode, len(body)) for i := range body { err, node := models.CreateNode(body[i]) if err == nil { resp[i].ID = node.ID resp[i].TaskID = utils.Generate32UUID() resp[i].NodeID = body[i].NodeID } else { resp[i].ID = node.ID resp[i].Error = "CreateNode " + node.ID + " err" } } self.Data["json"] = resp self.ServeJSON() }
func CreateService(body PostService) (error, Service) { o := orm.NewOrm() service := Service{ ID: "service_" + utils.Generate32UUID(), Status: Enable, } _, err := o.Insert(&service) if err != nil { fmt.Println("CreateService err:" + err.Error()) } return err, service }
func CreateSAN(body SAN) (error, SAN) { o := orm.NewOrm() o.Begin() lc := LoginConfig{ ID: "LoginConfig_" + utils.Generate32UUID(), Name: body.Login.Name, Type: body.Login.Type, IPAddress: body.Login.IPAddress, UserName: body.Login.UserName, Password: body.Login.Password, } san := SAN{ ID: "san_" + utils.Generate32UUID(), Name: body.Name, Vender: body.Vender, Login: &lc, LUNRange: body.LUNRange, HLURange: body.HLURange, TotalMB: body.TotalMB, FreeMB: body.FreeMB, Error: body.Error, } _, err := o.Insert(&lc) if err == nil { _, err = o.Insert(&san) if err != nil { fmt.Println("CreateSAN err:" + err.Error()) o.Rollback() } } else { fmt.Println("CreateSAN LoginConfig err:" + err.Error()) o.Rollback() } if err == nil { o.Commit() } return err, san }
func CreateRaidGroup(body PostRaidGroup) (error, RaidGroup) { o := orm.NewOrm() rg := RaidGroup{ ID: "raidgroup_" + utils.Generate32UUID(), SANID: body.SANID, Number: body.Number, Status: Enable, } _, err := o.Insert(&rg) if err != nil { fmt.Println("CreateRaidGroup err:" + err.Error()) } return err, rg }
func CreateIPtable(body IPtable) (error, IPtable) { o := orm.NewOrm() iptable := IPtable{ ID: "network_" + utils.Generate32UUID(), Type: body.Type, IPAddress: body.IPAddress, Netmask: body.Netmask, Gateway: body.Gateway, Total: body.Total, Used: body.Used, Status: Enable, Error: body.Error, } _, err := o.Insert(&iptable) if err != nil { fmt.Println("CreateIPtable err:" + err.Error()) } return err, iptable }
func CreateNode(body PostNode) (error, Node) { o := orm.NewOrm() node := Node{ ID: "node_" + utils.Generate32UUID(), ClusterID: body.ClusterID, SoftwareID: body.SoftwareID, Status: Enable, Runtime: "running", MaxInstance: body.MaxInstance, IPAddress: "12.168.1.1", NCPU: 24, Mem_MB: 102400, } _, err := o.Insert(&node) if err != nil { fmt.Println("CreateNode err:" + err.Error()) } //TODO task return err, node }
func CreateCluster(body PostCluster) (error, ClusterInfo) { if strings.TrimSpace(body.StorageSystemID) == "" { err := "Create Cluster " + body.Name + " 'storage err:" fmt.Println(err) return errors.New(err), *new(ClusterInfo) } o := orm.NewOrm() o.Begin() storage := Storage{ Type: body.StorageType, ID: body.StorageSystemID, } clusterInfo := ClusterInfo{ ID: "cluster_" + utils.Generate32UUID(), Name: body.Name, Type: body.Type, Status: Enable, MaxNodeUsageLimit: body.MaxNodeUsageLimit, Storage: &storage, } _, err := o.Insert(&storage) if err == nil { _, err = o.Insert(&clusterInfo) if err != nil { fmt.Println("CreateCluster err:" + err.Error()) o.Rollback() } } else { fmt.Println("CreateCluster storage err:" + err.Error()) o.Rollback() } if err == nil { o.Commit() } return err, clusterInfo }
func TestAPIServer(t *testing.T) { objs := []string{"cluster", "network", "node", "service", "software", "storage/san", "storage/san/rg", "storage/nas"} //objs := []string{"cluster"} var req *httplib.BeegoHTTPRequest var b []byte for _, obj := range objs { if obj != "service" && obj != "software" { if strings.Contains(obj, "storage/san") == false { //GetTest req = httplib.Get("http://localhost:8080/v1.0/" + obj + "/" + "1001") actionTestTemplate(t, req, obj, "Get") if obj != "storage/nas" { //GetAllTest req = httplib.Get("http://localhost:8080/v1.0/" + obj) actionTestTemplate(t, req, obj, "GetAll") } } if obj != "storage/nas" { //ListTrueTest listBody := models.CommonRequestBody{ All: true, ID: []string{"1001", "9999"}, } req = httplib.Post("http://localhost:8080/v1.0/" + obj + "/list") b, _ = json.Marshal(listBody) req.Body(b) actionTestTemplate(t, req, obj, "ListTrue") //ListFalseTest listBody.All = false req = httplib.Post("http://localhost:8080/v1.0/" + obj + "/list") b, _ = json.Marshal(listBody) req.Body(b) actionTestTemplate(t, req, obj, "ListFalse") } } //PostTest req = httplib.Post("http://localhost:8080/v1.0/" + obj) switch obj { case "cluster": postBody := models.PostCluster{ Name: "ClusterPostName", Type: "PostType", StorageType: "StoragePostType", StorageSystemID: "storage_" + utils.Generate32UUID(), MaxNodeUsageLimit: 100, MaxNodeNum: 200, } b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "network": postBody := models.IPtable{ Type: "PostType", IPAddress: "0.0.0.2", Netmask: "255.255.255.2", //Gateway "" //miss property, Total: 10, Used: 2, //Status //Error } b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "node": postBodyList := make([]models.PostNode, 2) postBodyList[0] = models.PostNode{ NodeID: "node_9001", ClusterID: "cluster_1001", SoftwareID: "software_1001", MaxInstance: 11, } postBodyList[1] = models.PostNode{ NodeID: "node_9002", ClusterID: "cluster_1002", SoftwareID: "software_1002", MaxInstance: 12, } b, _ = json.Marshal(postBodyList) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "service": postBody := models.PostService{} b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "software": postBody := models.Software{ ID: "software_9999", Name: "software_name_9999", Version: "V999", Type: "999", StoreURL: "Z:\\", MD5: "", } b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "storage/san": san := new(models.SAN) san.ID = "san_1001" san.Name = "san_name_1001" san.Vender = "san_vender_1001" lc := new(models.LoginConfig) lc.Name = "lc_name_1001" lc.Type = "A" lc.IPAddress = "0.0.0.1" lc.UserName = "******" lc.Password = "******" san.Login = lc san.LUNRange = "a1" san.HLURange = "b1" san.TotalMB = 100 san.FreeMB = 50 san.Error = "NONE1" postBody := san b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "storage/rg": postBody := models.PostRaidGroup{ SANID: "PostRaidGroup_9999", Number: 111, } b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") case "storage/nas": postBody := models.NAS{ ID: "NAS_9999", ServerIPAddr: "0.0.0.9", ShareDir: "Z:\\", } b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "Post") } //PostRollbackTest if obj == "cluster" { postBody := models.PostCluster{ Name: "ClusterPostName", Type: "PostType", StorageType: "StoragePostType", StorageSystemID: "", MaxNodeUsageLimit: 100, MaxNodeNum: 200, } req = httplib.Post("http://localhost:8080/v1.0/" + obj) b, _ = json.Marshal(postBody) req.Body(b) actionTestTemplate(t, req, obj, "PostRollback") } //PutTest if obj == "node" { req = httplib.Put("http://localhost:8080/v1.0/" + obj + "/" + "1001") putBody := models.Node{ Status: models.Enable, MaxInstance: 99, } b, _ = json.Marshal(putBody) req.Body(b) actionTestTemplate(t, req, obj, "Put") } if obj != "software" && obj != "storage/san" && obj != "storage/nas" { //EnableTest req = httplib.Put("http://localhost:8080/v1.0/" + obj + "/" + "1001/enable") actionTestTemplate(t, req, obj, "Enable") //DisableTest req = httplib.Put("http://localhost:8080/v1.0/" + obj + "/" + "1001/disable") actionTestTemplate(t, req, obj, "Disable") } //DeleteTest if obj != "storage/nas" { req = httplib.Delete("http://localhost:8080/v1.0/" + obj + "/" + "1001") actionTestTemplate(t, req, obj, "Delete") } } }