You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
api/routes.go

247 lines
6.4 KiB
Go

package main
import (
"errors"
"lsm-api/models"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// @Summary Get all hosts in LSM
// @Description Lists all the hosts in LSM
// @Tags Hosts
// @Accept json
// @Produce json
// @Success 200 {object} []models.Host
// @Router /hosts/hosts [get]
func GetHosts(c *gin.Context) {
var hosts []models.Host
DB.Find(&hosts)
c.JSON(http.StatusOK, hosts)
}
// @Summary Get a host by hostname
// @Description Get a host by hostname
// @Tags Hosts
// @Accept json
// @Produce json
// @Param hostname path string true "The hostname to get the host by"
// @Success 200 {object} models.Host
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /hosts/get_host_by_hostname/{hostname} [get]
func GetHostByHostname(c *gin.Context) {
var host models.Host
hostname := c.Param("hostname")
if err := DB.Where("hostname = ?", hostname).First(&host).Error; err == gorm.ErrRecordNotFound {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": "record not found",
})
return
} else if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": err.Error,
})
}
c.JSON(http.StatusOK, host)
}
// @Summary Get a host ID for hostname
// @Description Get a host ID for hostname
// @Tags Hosts
// @Accept json
// @Produce json
// @Param hostname path string true "The hostname to get the host ID by"
// @Success 200 {object} int
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /hosts/get_host_id_for_hostname/{hostname} [get]
func GetHostIDForHostname(c *gin.Context) {
var host models.Host
hostname := c.Param("hostname")
if err := DB.Where("hostname = ?", hostname).First(&host).Error; err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
"Status": "failed",
"error": "record not found",
})
return
} else if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"Status": "failed",
"Error": err.Error,
})
return
}
c.JSON(http.StatusOK, host.ID)
}
// AddHost godoc
// @Summary Create a host
// @Description Create a host
// @Tags Hosts
// @Accept json
// @Produce json
// @Param host body models.AddHost true "The host to create"
// @Success 201 {object} models.AddHost
// @Failure 400 {object} models.Error
// @Failure 409 {object} models.Error
// @Router /hosts/add_host [post]
func AddHost(c *gin.Context) {
var input models.AddHost
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": err.Error(),
})
return
}
host := models.Host{Hostname: input.Hostname}
result := DB.Create(&host)
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
c.JSON(http.StatusConflict, gin.H{
"status": "failure",
"error": "host already exists in lsm",
})
return
} else if result.Error != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "failure",
"error": result.Error,
})
return
}
c.JSON(http.StatusCreated, input)
}
// DeleteHost godoc
// @Summary Delete a host
// @Description Delete a host
// @Tags Hosts
// @Accept json
// @Produce json
// @Param host_id path string true "The ID of the host to be deleted"
// @Success 204
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /hosts/delete_host/{host_id} [delete]
func DeleteHost(c *gin.Context) {
var host models.Host
host_id := c.Param("host_id")
if err := DB.Where("ID = ?", host_id).Delete(&host).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": err.Error,
})
return
} else if DB.RowsAffected < 1 {
c.JSON(http.StatusNotFound, gin.H{
"status": "error",
"error": "record not found",
})
return
}
c.Status(http.StatusNoContent)
}
// DeleteHostByHostname godoc
// @Summary Delete a host using its hostname
// @Description Delete a host using it hostname
// @Tags Hosts
// @Accept json
// @Produce json
// @Param hostname path string true "The hostname of the host to be deleted"
// @Success 204
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /hosts/delete_host_by_hostname/{hostname} [delete]
func DeleteHostByHostname(c *gin.Context) {
hostname := c.Param("hostname")
var host models.Host
if result := DB.Where("hostname = ?", hostname).Unscoped().Delete(&host); result.Error != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": result.Error,
})
return
} else if result.RowsAffected < 1 {
c.JSON(http.StatusNotFound, gin.H{
"status": "error",
"error": "record not found",
})
return
}
c.Status(http.StatusNoContent)
}
// @Summary Get all CPUs in LSM
// @Description Lists all the CPUs in LSM for every host
// @Tags CPUs
// @Accept json
// @Produce json
// @Success 200 {object} []models.CPU
// @Router /cpus/cpus [get]
func GetCPUs(c *gin.Context) {
var CPUs []models.CPU
DB.Find(&CPUs)
c.JSON(http.StatusOK, CPUs)
}
// AddCPUToHost godoc
// @Summary Create a CPU
// @Description Create a CPU
// @Tags CPUs
// @Accept json
// @Produce json
// @Param host body models.CPU true "The host to create"
// @Success 201 {object} models.CPU
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 409 {object} models.Error
// @Router /cpus/add_cpu_to_host [post]
func AddCPUToHost(c *gin.Context) {
var cpu models.CPU
if err := c.ShouldBindJSON(&cpu); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": err.Error,
})
return
}
var host models.Host
if err := DB.Where("ID = ?", cpu.HostID).First(&host).Error; err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
"status": "failure",
"error": "host not found",
})
}
result := DB.Create(&cpu)
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
c.JSON(http.StatusConflict, gin.H{
"status": "failure",
"error": "host already exists in lsm",
})
return
} else if result.Error != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "failure",
"error": result.Error,
})
return
}
c.JSON(http.StatusCreated, cpu)
}