Exemplo n.º 1
0
func GetVmDeviceLogByDeviceIdAndType(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		DeviceID uint
		Type     string
		Order    string
		MaxID    uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	if info.Order == "" {
		info.Order = "id DESC"
	}

	mods, err := repo.GetVmDeviceLogListByDeviceIDAndType(info.DeviceID, info.Type, info.Order, info.MaxID)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	type log struct {
		ID        uint
		DeviceID  uint
		Title     string
		Type      string
		Content   string
		CreatedAt utils.ISOTime
		UpdatedAt utils.ISOTime
	}

	var result []log
	for _, v := range mods {
		var row log
		row.ID = v.ID
		row.DeviceID = v.DeviceID
		row.Title = v.Title
		row.Type = v.Type
		row.Content = v.Content
		row.CreatedAt = utils.ISOTime(v.CreatedAt)
		row.UpdatedAt = utils.ISOTime(v.UpdatedAt)
		result = append(result, row)
	}

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": result})
}
Exemplo n.º 2
0
func GetUserById(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		ID uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	mod, err := repo.GetUserById(info.ID)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	type UserWithTime struct {
		ID          uint
		Username    string
		Password    string
		Name        string
		PhoneNumber string
		Permission  string
		Status      string
		Role        string
		CreatedAt   utils.ISOTime
		UpdatedAt   utils.ISOTime
	}

	var user UserWithTime
	user.ID = mod.ID
	user.Username = mod.Username
	user.Name = mod.Name
	user.PhoneNumber = mod.PhoneNumber
	user.Permission = mod.Permission
	user.Status = mod.Status
	user.Role = mod.Role
	user.CreatedAt = utils.ISOTime(mod.CreatedAt)
	user.UpdatedAt = utils.ISOTime(mod.UpdatedAt)

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": user})
}
Exemplo n.º 3
0
func CheckOnlineUpdate(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}

	lastestHardware, err := repo.GetLastestVersionHardware()
	type HardwareVersionWithDate struct {
		Version string
		Date    utils.ISOTime
	}
	var hardwareVersionWithDate HardwareVersionWithDate
	hardwareVersionWithDate.Version = lastestHardware.Version
	hardwareVersionWithDate.Date = utils.ISOTime(lastestHardware.CreatedAt)

	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	result := make(map[string]interface{})
	result["Version"] = lastestHardware.Version
	b, err := json.Marshal(result)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	body := bytes.NewBuffer([]byte(b))
	resp, err := http.Post("http://open.idcos.com/api/x86/check-online-update", "application/json;charset=utf-8", body)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "网络连接故障,在线更新配置库失败!", "CurrentVersion": hardwareVersionWithDate})
		return
	}
	reportResult, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	type content struct {
		Version string `json:"Version"`
		Date    string `json:"Date"`
	}
	type Response struct {
		Status  string    `json:"Status"`
		Message string    `json:"Message"`
		Content []content `json:"Content"`
	}
	var response Response
	errJson := json.Unmarshal(reportResult, &response)
	if errJson != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errJson.Error()})
		return
	}
	if response.Status == "success" {
		w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "", "Content": response.Content, "CurrentVersion": hardwareVersionWithDate})
	} else {
		w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作失败!"})
	}
}
Exemplo n.º 4
0
func GetScanDeviceByDeviceId(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误", "Content": nil})
		return
	}
	var info struct {
		DeviceID uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error(), "Content": nil})
		return
	}

	mod, err := repo.GetManufacturerByDeviceId(info.DeviceID)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error(), "Content": nil})
		return
	}

	type DeviceWithTime struct {
		ID          uint
		DeviceID    uint
		Company     string
		Product     string
		ModelName   string
		Sn          string
		Ip          string
		Mac         string
		Nic         string
		Cpu         string
		CpuSum      uint
		Memory      string
		MemorySum   uint
		Disk        string
		DiskSum     uint
		Motherboard string
		Raid        string
		Oob         string
		IsVm        string
		NicDevice   string
		CreatedAt   utils.ISOTime
		UpdatedAt   utils.ISOTime
	}

	var device DeviceWithTime
	device.ID = mod.ID
	device.DeviceID = mod.DeviceID
	device.Company = mod.Company
	device.Product = mod.Product
	device.ModelName = mod.ModelName
	device.Sn = mod.Sn
	device.Ip = mod.Ip
	device.Mac = mod.Mac
	device.Nic = mod.Nic
	device.Cpu = mod.Cpu
	device.CpuSum = mod.CpuSum
	device.Memory = mod.Memory
	device.MemorySum = mod.MemorySum
	device.Disk = mod.Disk
	device.DiskSum = mod.DiskSum
	device.Motherboard = mod.Motherboard
	device.Raid = strings.Replace(mod.Raid, "\n", "<br>", -1)
	device.Oob = mod.Oob
	device.IsVm = mod.IsVm
	device.NicDevice = strings.Replace(mod.NicDevice, "\n", "<br>", -1)

	device.CreatedAt = utils.ISOTime(mod.CreatedAt)
	device.UpdatedAt = utils.ISOTime(mod.UpdatedAt)

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": device})
}
Exemplo n.º 5
0
func GetDeviceList(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		Limit          uint
		Offset         uint
		Keyword        string
		OsID           int
		HardwareID     int
		SystemID       int
		Status         string
		BatchNumber    string
		StartUpdatedAt string
		EndUpdatedAt   string
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	info.Keyword = strings.TrimSpace(info.Keyword)
	info.Status = strings.TrimSpace(info.Status)
	info.BatchNumber = strings.TrimSpace(info.BatchNumber)

	var where string
	where = " where t1.id > 0 "
	if info.OsID > 0 {
		where += " and t1.os_id = " + strconv.Itoa(info.OsID)
	}
	if info.HardwareID > 0 {
		where += " and t1.hardware_id = " + strconv.Itoa(info.HardwareID)
	}
	if info.SystemID > 0 {
		where += " and t1.system_id = " + strconv.Itoa(info.SystemID)
	}
	if info.Status != "" {
		where += " and t1.status = '" + info.Status + "'"
	}
	if info.BatchNumber != "" {
		where += " and t1.batch_number = '" + info.BatchNumber + "'"
	}

	if info.StartUpdatedAt != "" {
		where += " and t1.updated_at >= '" + info.StartUpdatedAt + "'"
	}

	if info.EndUpdatedAt != "" {
		where += " and t1.updated_at <= '" + info.EndUpdatedAt + "'"
	}

	if info.Keyword != "" {
		where += " and ( "
		info.Keyword = strings.Replace(info.Keyword, "\n", ",", -1)
		info.Keyword = strings.Replace(info.Keyword, ";", ",", -1)
		list := strings.Split(info.Keyword, ",")
		for k, v := range list {
			var str string
			v = strings.TrimSpace(v)
			if k == 0 {
				str = ""
			} else {
				str = " or "
			}
			where += str + " t1.sn = '" + v + "' or t1.batch_number = '" + v + "' or t1.hostname = '" + v + "' or t1.ip = '" + v + "'"
		}
		where += " ) "
	}

	mods, err := repo.GetDeviceListWithPage(info.Limit, info.Offset, where)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	type DeviceWithTime struct {
		ID              uint
		BatchNumber     string
		Sn              string
		Hostname        string
		Ip              string
		NetworkID       uint
		OsID            uint
		HardwareID      uint
		SystemID        uint
		Location        string
		LocationID      uint
		AssetNumber     string
		Status          string
		InstallProgress float64
		InstallLog      string
		NetworkName     string
		OsName          string
		HardwareName    string
		SystemName      string
		LocationName    string
		IsSupportVm     string
		CreatedAt       utils.ISOTime
		UpdatedAt       utils.ISOTime
	}
	var rows []DeviceWithTime
	for _, v := range mods {
		var device DeviceWithTime
		device.ID = v.ID
		device.BatchNumber = v.BatchNumber
		device.Sn = v.Sn
		device.Hostname = v.Hostname
		device.Ip = v.Ip
		device.NetworkID = v.NetworkID
		device.OsID = v.OsID
		device.HardwareID = v.HardwareID
		device.SystemID = v.SystemID
		device.Location = v.Location
		device.LocationID = v.LocationID
		device.AssetNumber = v.AssetNumber
		device.Status = v.Status
		device.InstallProgress = v.InstallProgress
		device.InstallLog = v.InstallLog
		device.NetworkName = v.NetworkName
		device.OsName = v.OsName
		device.HardwareName = v.HardwareName
		device.SystemName = v.SystemName
		device.IsSupportVm = v.IsSupportVm
		/*
			device.LocationName, err = repo.FormatLocationNameById(v.LocationID, "", "-")
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}
		*/

		device.CreatedAt = utils.ISOTime(v.CreatedAt)
		device.UpdatedAt = utils.ISOTime(v.UpdatedAt)

		deviceLog, _ := repo.GetLastDeviceLogByDeviceID(v.ID)
		device.InstallLog = deviceLog.Title
		rows = append(rows, device)
	}

	result := make(map[string]interface{})
	result["list"] = rows

	//总条数
	count, err := repo.CountDevice(where)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	result["recordCount"] = count

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": result})
}
Exemplo n.º 6
0
func GetFullDeviceById(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		ID uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	mod, err := repo.GetFullDeviceById(info.ID)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	type DeviceWithTime struct {
		ID              uint
		BatchNumber     string
		Sn              string
		Hostname        string
		Ip              string
		NetworkID       uint
		OsID            uint
		HardwareID      uint
		SystemID        uint
		Location        string
		LocationID      uint
		AssetNumber     string
		Status          string
		InstallProgress float64
		InstallLog      string
		NetworkName     string
		OsName          string
		HardwareName    string
		SystemName      string
		LocationName    string
		IsSupportVm     string
		CreatedAt       utils.ISOTime
		UpdatedAt       utils.ISOTime
	}

	var device DeviceWithTime
	device.ID = mod.ID
	device.BatchNumber = mod.BatchNumber
	device.Sn = mod.Sn
	device.Hostname = mod.Hostname
	device.Ip = mod.Ip
	device.NetworkID = mod.NetworkID
	device.OsID = mod.OsID
	device.HardwareID = mod.HardwareID
	device.SystemID = mod.SystemID
	device.Location = mod.Location
	device.LocationID = mod.LocationID
	device.AssetNumber = mod.AssetNumber
	device.Status = mod.Status
	device.InstallProgress = mod.InstallProgress
	device.InstallLog = mod.InstallLog
	device.NetworkName = mod.NetworkName
	device.OsName = mod.OsName
	device.HardwareName = mod.HardwareName
	device.SystemName = mod.SystemName
	device.IsSupportVm = mod.IsSupportVm
	device.LocationName, err = repo.FormatLocationNameById(mod.LocationID, "", "-")
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	device.CreatedAt = utils.ISOTime(mod.CreatedAt)
	device.UpdatedAt = utils.ISOTime(mod.UpdatedAt)

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": device})
}
Exemplo n.º 7
0
func GetFullVmDeviceById(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		ID uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	mod, err := repo.GetFullVmDeviceById(info.ID)
	type VmDeviceFullWithTime struct {
		ID                    uint
		DeviceID              uint
		DeviceSn              string
		Hostname              string
		Mac                   string
		Ip                    string
		NetworkID             uint
		NetworkName           string
		OsID                  uint
		OsName                string
		CpuCoresNumber        uint
		CpuHotPlug            string
		CpuPassthrough        string
		CpuTopSockets         uint
		CpuTopCores           uint
		CpuTopThreads         uint
		CpuPinning            string
		MemoryCurrent         uint
		MemoryMax             uint
		MemoryKsm             string
		DiskType              string
		DiskSize              uint
		DiskBusType           string
		DiskCacheMode         string
		DiskIoMode            string
		NetworkType           string
		NetworkDeviceType     string
		DisplayType           string
		DisplayPassword       string
		DisplayUpdatePassword string
		Status                string
		CreatedAt             utils.ISOTime
		UpdatedAt             utils.ISOTime
	}
	var vm VmDeviceFullWithTime
	vm.ID = mod.ID
	vm.DeviceID = mod.DeviceID
	vm.DeviceSn = mod.DeviceSn
	vm.Hostname = mod.Hostname
	vm.Mac = mod.Mac
	vm.Ip = mod.Ip
	vm.NetworkID = mod.NetworkID
	vm.NetworkName = mod.NetworkName
	vm.OsID = mod.OsID
	vm.OsName = mod.OsName
	vm.CpuCoresNumber = mod.CpuCoresNumber
	vm.CpuHotPlug = mod.CpuHotPlug
	vm.CpuPassthrough = mod.CpuPassthrough
	vm.CpuTopSockets = mod.CpuTopSockets
	vm.CpuTopCores = mod.CpuTopCores
	vm.CpuTopThreads = mod.CpuTopThreads
	vm.CpuPinning = mod.CpuPinning
	vm.MemoryCurrent = mod.MemoryCurrent
	vm.MemoryMax = mod.MemoryMax
	vm.MemoryKsm = mod.MemoryKsm
	vm.DiskType = mod.DiskType
	vm.DiskSize = mod.DiskSize
	vm.DiskBusType = mod.DiskBusType
	vm.DiskCacheMode = mod.DiskCacheMode
	vm.DiskIoMode = mod.DiskIoMode
	vm.NetworkType = mod.NetworkType
	vm.NetworkDeviceType = mod.NetworkDeviceType
	vm.DisplayType = mod.DisplayType
	vm.DisplayPassword = mod.DisplayPassword
	vm.DisplayUpdatePassword = mod.DisplayUpdatePassword
	vm.Status = mod.Status

	vm.CreatedAt = utils.ISOTime(mod.CreatedAt)
	vm.UpdatedAt = utils.ISOTime(mod.UpdatedAt)

	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": vm})
}