Esempio n. 1
0
func (this *HostController) Post() {
	var resp_message ResponseMessage
	var libvirt_host LibvirtHost
	var insert_id string

	err := this.ParseForm(&libvirt_host)
	if err != nil {
		err_msg := fmt.Sprintf("parseform failed: %s", err)
		utils.Println(err_msg)
		resp_message.Code = 1
		resp_message.Message = err_msg
		this.Ctx.Output.Status = 500
		goto end
	}

	err = this.Valid(libvirt_host)
	if err != nil {
		err_msg := fmt.Sprintf("validation failed: %s", err)
		utils.Println(err_msg)
		resp_message.Code = 2
		resp_message.Message = err_msg
		this.Ctx.Output.Status = 500
		goto end
	}

	libvirt_host.UUID = utils.MakeRandomID()
	insert_id = libvirt_host.UUID
	err = mongo.InsertOne("libvirt_host", libvirt_host)

	if resp_message.Code == 0 {
		resp_message.Code = 0
		resp_message.Message = insert_id
	}

end:
	this.Data["json"] = resp_message
	this.ServeJson()
}
Esempio n. 2
0
func (this *ConnectionManagerController) CreateConnection() {
	var resp_message ResponseMessage
	var found_protocol bool
	var decode_failed bool
	var decode_failed_reason string
	var insert_failed bool
	var insert_failed_reason string
	var insert_id string
	var valid_failed bool
	var valid_failed_reason string

	conn_type := this.Ctx.Input.Param(":conn_type")

	found_protocol = client.ValidProtocol(conn_type)

	if found_protocol {

		if conn_type == "vnc" {
			args, err := this.DecodeVNCArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode vnc args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save vnc args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if conn_type == "rdp" {
			args, err := this.DecodeRDPArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode rdp args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save rdp args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if conn_type == "ssh" {
			args, err := this.DecodeSSHArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode ssh args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save ssh args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if conn_type == "telnet" {
			args, err := this.DecodeTELNETArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode telnet args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save telnet args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if conn_type == "spice" {
			args, err := this.DecodeSPICEArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode spice args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save spice args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if conn_type == "libvirt" {
			args, err := this.DecodeLIBVIRTArgs()
			if err != nil {
				decode_failed = true
				decode_failed_reason = fmt.Sprintf("decode libvirt args failed: %s", err)
			} else {
				err = this.Valid(args)
				if err != nil {
					valid_failed = true
					valid_failed_reason = fmt.Sprintf("valid error: %s", err)
				} else {
					args.UUID = utils.MakeRandomID()
					insert_id = args.UUID
					err = mongo.InsertOne("connection", *args)
					if err != nil {
						insert_failed = true
						insert_failed_reason = fmt.Sprintf("save libvirt args failed: %s, args: %s", err, *args)
					}
				}
			}
		}

		if decode_failed {
			utils.Println(decode_failed_reason)
			resp_message.Code = 1
			resp_message.Message = decode_failed_reason
			this.Ctx.Output.Status = 500
		}

		if insert_failed {
			utils.Println(insert_failed_reason)
			resp_message.Code = 2
			resp_message.Message = insert_failed_reason
			this.Ctx.Output.Status = 500
		}

		if valid_failed {
			utils.Println(valid_failed_reason)
			resp_message.Code = 3
			resp_message.Message = valid_failed_reason
			this.Ctx.Output.Status = 500
		}

	} else {
		resp_message.Code = 4
		resp_message.Message = "wrong protocol type"
		this.Ctx.Output.Status = 400
	}

	if resp_message.Code == 0 {
		resp_message.Code = 0
		resp_message.Message = insert_id
	}

	this.Data["json"] = resp_message
	this.ServeJson()
}