159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.beisel.it/florian/hostname-service/db"
|
|
"git.beisel.it/florian/hostname-service/rules"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
|
|
switch category {
|
|
case "notebook":
|
|
return &rules.NotebookRule{}, nil
|
|
// ... other categories
|
|
default:
|
|
return nil, errors.New("unknown category")
|
|
}
|
|
}
|
|
|
|
// CreateOrUpdateHostname handles the hostname generation
|
|
func CreateOrUpdateHostname(c *gin.Context, isUpdate bool) {
|
|
category := c.Param("category")
|
|
var oldHostname string
|
|
if isUpdate {
|
|
oldHostname = c.Param("oldhostname")
|
|
}
|
|
|
|
var params map[string]interface{}
|
|
if err := c.BindJSON(¶ms); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid parameters"})
|
|
return
|
|
}
|
|
|
|
rule, err := getHostnameRuleByCategory(category)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
hostname, paramsJSON, err := rule.Generate(params)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if isUpdate {
|
|
err = rule.Update(category, oldHostname, hostname, paramsJSON)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "does not exist") {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
} else {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
err = rule.Insert(category, hostname, paramsJSON)
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error processing hostname"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"hostname": hostname})
|
|
}
|
|
|
|
// @Summary Delete a hostname from the database
|
|
// @Description List all details for a given category
|
|
// @ID delete-hostnames-by-category-and-name
|
|
// @Produce json
|
|
// @Param category path string true "Category of the hostname"
|
|
// @Param hostname path string true "Hostname to delete"
|
|
// @Success 200 {json} json "Hostname"
|
|
// @Security Bearer
|
|
// @Tags Manipulate existing Hostnames
|
|
// @Router /{category}/{hostname} [delete]
|
|
func DeleteHostname(c *gin.Context) {
|
|
category := c.Param("category")
|
|
hostname := c.Param("hostname")
|
|
|
|
err := db.DeleteHostname(category, hostname)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"hostname": hostname})
|
|
}
|
|
|
|
// @Summary Return a list of hosts and their details filtered by category
|
|
// @Description List all details for a given category
|
|
// @ID list-hostnames-by-category
|
|
// @Produce json
|
|
// @Param category path string true "Category of the hostname"
|
|
// @Success 200 {json} json "Hostname"
|
|
// @Security Bearer
|
|
// @Tags Querying Hostnames
|
|
// @Router /{category} [get]
|
|
func ListHostnamesByCategory(c *gin.Context) {
|
|
category := c.Param("category")
|
|
|
|
hostnames, err := db.GetHostnamesByCategory(category)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no rows found") {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "hostname not found"})
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving hostnames"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, hostnames)
|
|
}
|
|
|
|
// @Summary Return a single hostname by Category and Name
|
|
// @Description Return details for a single hostname identified by its category
|
|
// @ID get-hostname-by-category-and-name
|
|
// @Produce json
|
|
// @Param category path string true "Category of the hostname"
|
|
// @Param hostname path string true "Category of the hostname"
|
|
// @Security Bearer
|
|
// @Success 200 {json} json "Hostname"
|
|
// @Tags Querying Hostnames
|
|
// @Router /{category}/{hostname} [get]
|
|
func GetHostnameByCategoryAndName(c *gin.Context) {
|
|
category := c.Param("category")
|
|
hostname := c.Param("hostname")
|
|
|
|
hostinfo, err := db.GetHostnameByCategoryAndName(category, hostname)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no rows found") {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "hostname not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving hostname"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, hostinfo)
|
|
}
|
|
|
|
// Helloworld godoc
|
|
//
|
|
// @Summary Check your authentication
|
|
// @Schemes
|
|
// @Description Checks whether the user is successfully authenticated
|
|
// @ID hello
|
|
// @Tags Authentication
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Success 200 {string} Helloworld
|
|
// @Router /hello [get]
|
|
func Helloworld(g *gin.Context) {
|
|
g.JSON(http.StatusOK, "helloworld")
|
|
}
|