2024-01-21 14:54:34 +01:00
|
|
|
// Copyright 2024 Florian Beisel
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2024-01-24 11:03:08 +01:00
|
|
|
"reflect"
|
2024-01-21 14:54:34 +01:00
|
|
|
|
|
|
|
"git.beisel.it/florian/hostname-service/rules"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-01-24 11:03:08 +01:00
|
|
|
// @Summary Returns a list of available rules
|
|
|
|
// @Description Return a list of names of Rules which are known in the RulesRegistry
|
|
|
|
// @ID get-rules
|
|
|
|
// @Produce json
|
|
|
|
// @Security Bearer
|
|
|
|
// @Tags Querying Rules
|
|
|
|
// @Router /rules [get]
|
2024-01-21 14:54:34 +01:00
|
|
|
func ListAvailableRules(c *gin.Context) {
|
|
|
|
descriptions := make(map[string]string)
|
|
|
|
for category, descriptor := range rules.RulesRegistry {
|
|
|
|
descriptions[category] = descriptor.Description
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, descriptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
|
|
|
|
if descriptor, exists := rules.RulesRegistry[category]; exists {
|
|
|
|
return descriptor.Factory(), nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("unknown category")
|
|
|
|
}
|
2024-01-24 11:03:08 +01:00
|
|
|
|
|
|
|
// @Summary Returns details about a rule
|
|
|
|
// @Description Returns two obea an Input Object and an Output Object
|
|
|
|
// @Description describing the Rules stored values and required parameters
|
|
|
|
// @ID get-rule-details
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} models.SimpleHostnameResponse "Hostname"
|
|
|
|
// @Security Bearer
|
|
|
|
// @Tags Querying Rules
|
|
|
|
// @Router /rules/:rule [get]
|
|
|
|
func GetRuleStruct(c *gin.Context) {
|
|
|
|
ruleName := c.Param("rule")
|
|
|
|
|
|
|
|
descriptor, exists := rules.RulesRegistry[ruleName]
|
|
|
|
if !exists {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Rule not found"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create instances of the rule and its input struct
|
|
|
|
ruleInstance := descriptor.Factory()
|
|
|
|
inputInstance := reflect.New(reflect.TypeOf(ruleInstance).Elem()).Interface()
|
|
|
|
|
|
|
|
// Serialize instances to JSON
|
|
|
|
ruleJSON, err := rules.StructToJSON(ruleInstance)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inputJSON, err := rules.StructToJSON(inputInstance)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing input struct"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"input": inputJSON,
|
|
|
|
"output": ruleJSON,
|
|
|
|
})
|
|
|
|
}
|