hostname-service/api/rule_handlers.go

103 lines
3.3 KiB
Go
Raw Normal View History

// 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"
"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]
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
}
2024-02-06 21:05:44 +01:00
// Create an instance of the rule struct for output
2024-01-24 11:03:08 +01:00
ruleInstance := descriptor.Factory()
2024-02-06 21:05:44 +01:00
// Check if the rule instance implements EnumProvider for ENUM metadata
enumProvider, implements := ruleInstance.(rules.EnumProvider)
var enumInfo map[string][]string
if implements {
// Retrieve ENUM metadata from the rule instance
enumMetadata := enumProvider.EnumOptions()
enumInfo = make(map[string][]string)
for _, meta := range enumMetadata {
enumInfo[meta.FieldName] = meta.Values
}
}
// Serialize the rule instance to JSON for output
2024-01-24 11:03:08 +01:00
ruleJSON, err := rules.StructToJSON(ruleInstance)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"})
return
}
2024-02-06 21:05:44 +01:00
// Create an instance of the input struct for serialization
inputInstance := reflect.New(descriptor.InputStruct).Elem().Interface()
// Serialize the input struct instance to JSON
2024-01-24 11:03:08 +01:00
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{
2024-02-06 21:05:44 +01:00
"input": inputJSON, // Input struct with possible ENUM values
"enums": enumInfo, // ENUM metadata if available
"output": ruleJSON, // Serialized rule instance
2024-01-24 11:03:08 +01:00
})
}