✨Implement server.go as a more complete example
This change implements the category / rule Server which will showcase storig arbirtray alongside the hostname which are not part of hostname generation.
This commit is contained in:
parent
600fb0e997
commit
1669a65512
|
@ -0,0 +1,139 @@
|
|||
// 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 rules
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.beisel.it/florian/hostname-service/db"
|
||||
)
|
||||
|
||||
type ServerRule struct {
|
||||
BaseRule
|
||||
OrgUnit string `json:"OrgUnit"`
|
||||
Location string `json:"Location"`
|
||||
Number int `json:"Number"`
|
||||
Description string `json:"Description"`
|
||||
IP string `json:"IP"`
|
||||
ILO string `json:"ILO"`
|
||||
Responsible string `json:"Responsible"`
|
||||
}
|
||||
|
||||
type ServerRuleInput struct {
|
||||
OrgUnit string `json:"OrgUnit"`
|
||||
Location string `json:"Location"`
|
||||
Description string `json:"Description"`
|
||||
IP string `json:"IP"`
|
||||
ILO string `json:"ILO"`
|
||||
Responsible string `json:"Responsible"`
|
||||
}
|
||||
|
||||
// Ensure that ServerRule implements HostnameRule interface
|
||||
var _ HostnameRule = &ServerRule{}
|
||||
|
||||
func (sr *ServerRule) Generate(params map[string]interface{}) (string, []byte, error) {
|
||||
var ok bool
|
||||
|
||||
// Get the OrgUnit
|
||||
if sr.OrgUnit, ok = params["OrgUnit"].(string); !ok {
|
||||
return "", nil, errors.New("OrgUnit parameter is required and must be a string")
|
||||
}
|
||||
|
||||
// Get the Location
|
||||
if sr.Location, ok = params["Location"].(string); !ok {
|
||||
return "", nil, errors.New("location parameter is required and must be a string")
|
||||
}
|
||||
|
||||
// Get the Description
|
||||
if sr.Description, ok = params["Description"].(string); !ok {
|
||||
return "", nil, errors.New("parameter Description is required and must be a string")
|
||||
}
|
||||
|
||||
// Get the IP
|
||||
if sr.IP, ok = params["IP"].(string); !ok {
|
||||
return "", nil, errors.New("IP parameter is required and must be a string")
|
||||
}
|
||||
|
||||
// Get the ILO IP
|
||||
if sr.ILO, ok = params["ILO"].(string); !ok {
|
||||
return "", nil, errors.New("ILO parameter is required and must be a string")
|
||||
}
|
||||
|
||||
// Get the Responsible
|
||||
if sr.Responsible, ok = params["Responsible"].(string); !ok {
|
||||
return "", nil, errors.New("parameter Responsible parameter is required and must be a string")
|
||||
}
|
||||
|
||||
// Get last used number from the database and increment it
|
||||
maxNumber, err := db.GetMaxNumberForCategory("server")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
newNumber := 1
|
||||
if maxNumber >= 1 {
|
||||
newNumber = int(maxNumber) + 1
|
||||
}
|
||||
|
||||
sr.Number = newNumber
|
||||
|
||||
// Generate the hostname (orgUnit + Location + "NB" + Number)
|
||||
hostname := fmt.Sprintf("%s%sSV%04d", sr.OrgUnit, sr.Location, sr.Number)
|
||||
|
||||
// Store the generated hostname in the database
|
||||
// JSON parameters can be stored by marshalling the params map
|
||||
paramsJSON, err := json.Marshal(sr)
|
||||
if err != nil {
|
||||
log.Printf("Error converting Category Struct to JSON: %v", err)
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// Return the generated hostname and marshaled parameters
|
||||
return hostname, paramsJSON, nil
|
||||
|
||||
}
|
||||
|
||||
// @Summary Generate hostname for category "notebook"
|
||||
// @Description Generates a hostname for a notebook based on dynamic rules.
|
||||
// @ID insert-server-hostname
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Tags Generating Hostnames
|
||||
// @Param body body ServerRuleInput true "Input data to generate hostname"
|
||||
// @Success 200 {object} models.SimpleHostnameResponse "Hostname"
|
||||
// @Router /api/server [post]
|
||||
// @Security Bearer
|
||||
func (nr *ServerRule) Insert(category string, params map[string]interface{}) (string, error) {
|
||||
// Generate the hostname
|
||||
|
||||
return nr.baseInsert(nr, category, params)
|
||||
}
|
||||
|
||||
// @Summary Update hostname for category "notebook"
|
||||
// @Description Generates a new hostname for a notebook based on dynamic rules.
|
||||
// @ID update-server-hostname
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Tags Manipulate existing Hostnames
|
||||
// @Param body body ServerRuleInput true "Input data to generate hostname"
|
||||
// @Success 200 {object} models.SimpleHostnameResponse "Hostname"
|
||||
// @Router /api/server [put]
|
||||
// @Security Bearer
|
||||
func (nr *ServerRule) Update(category string, oldhostname string, params map[string]interface{}) (string, error) {
|
||||
return nr.baseUpdate(nr, category, oldhostname, params)
|
||||
}
|
Loading…
Reference in New Issue