refactor: more implementation to refactor and deleted files

This commit is contained in:
David Allen 2025-08-04 22:32:41 -06:00
parent ba684bd149
commit 50e6b53091
Signed by: towk
GPG key ID: 0430CDBE22619155
17 changed files with 84 additions and 945 deletions

View file

@ -1,56 +0,0 @@
package generator
import (
"fmt"
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type Conman struct{}
func (g *Conman) GetName() string {
return "conman"
}
func (g *Conman) GetVersion() string {
return util.GitCommit()
}
func (g *Conman) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Conman) Generate(config *config.Config, params Params) (FileMap, error) {
var (
smdClient = client.NewSmdClient(params.ClientOpts...)
eps = []configurator.RedfishEndpoint{}
err error = nil
consoles = ""
)
// fetch required data from SMD to create config
eps, err = smdClient.FetchRedfishEndpoints(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch redfish endpoints with client: %v", err)
}
// format output to write to config file
consoles = "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
for _, ep := range eps {
consoles += fmt.Sprintf("CONSOLE name=%s dev=ipmi:%s-bmc ipmiopts=U:%s,P:%s,W:solpayloadsize\n", ep.Name, ep.Name, ep.User, ep.Password)
}
consoles += "# ====================================================================="
// apply template substitutions and return output as byte array
return ApplyTemplates(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"server_opts": "",
"global_opts": "",
"consoles": consoles,
}, params.Templates)
}

View file

@ -1,26 +0,0 @@
package generator
import (
"fmt"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type CoreDhcp struct{}
func (g *CoreDhcp) GetName() string {
return "coredhcp"
}
func (g *CoreDhcp) GetVersion() string {
return util.GitCommit()
}
func (g *CoreDhcp) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s' to generate config files. (WIP)", g.GetName())
}
func (g *CoreDhcp) Generate(config *config.Config, params Params) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -1,64 +0,0 @@
package generator
import (
"fmt"
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type DHCPd struct{}
func (g *DHCPd) GetName() string {
return "dhcpd"
}
func (g *DHCPd) GetVersion() string {
return util.GitCommit()
}
func (g *DHCPd) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *DHCPd) Generate(config *config.Config, params Params) (FileMap, error) {
var (
smdClient = client.NewSmdClient(params.ClientOpts...)
eths = []configurator.EthernetInterface{}
computeNodes = ""
err error = nil
)
//
eths, err = smdClient.FetchEthernetInterfaces(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %w", err)
}
// check if we have the required params first
if eths == nil {
return nil, fmt.Errorf("invalid ethernet interfaces (variable is nil)")
}
if len(eths) <= 0 {
return nil, fmt.Errorf("no ethernet interfaces found")
}
// format output to write to config file
computeNodes = "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
for _, eth := range eths {
if len(eth.IpAddresses) == 0 {
continue
}
computeNodes += fmt.Sprintf("host %s { hardware ethernet %s; fixed-address %s} ", eth.ComponentId, eth.MacAddress, eth.IpAddresses[0])
}
computeNodes += "# ====================================================================="
return ApplyTemplates(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"compute_nodes": computeNodes,
"node_entries": "",
}, params.Templates)
}

View file

@ -1,71 +0,0 @@
package generator
import (
"fmt"
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type DNSMasq struct{}
func (g *DNSMasq) GetName() string {
return "dnsmasq"
}
func (g *DNSMasq) GetVersion() string {
return util.GitCommit()
}
func (g *DNSMasq) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *DNSMasq) Generate(config *config.Config, params Params) (FileMap, error) {
// make sure we have a valid config first
if config == nil {
return nil, fmt.Errorf("invalid config (config is nil)")
}
// set all the defaults for variables
var (
smdClient = client.NewSmdClient(params.ClientOpts...)
eths = []configurator.EthernetInterface{}
err error = nil
)
// if we have a client, try making the request for the ethernet interfaces
eths, err = smdClient.FetchEthernetInterfaces(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", err)
}
// check if we have the required params first
if eths == nil {
return nil, fmt.Errorf("invalid ethernet interfaces (variable is nil)")
}
if len(eths) <= 0 {
return nil, fmt.Errorf("no ethernet interfaces found")
}
// format output to write to config file
output := "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
for _, eth := range eths {
if eth.Type == "NodeBMC" {
output += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
} else {
output += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
}
}
output += "# ====================================================================="
// apply template substitutions and return output as byte array
return ApplyTemplates(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"dhcp_hosts": output,
}, params.Templates)
}

View file

@ -1,31 +0,0 @@
package generator
import (
"fmt"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type Example struct {
Message string
}
func (g *Example) GetName() string {
return "example"
}
func (g *Example) GetVersion() string {
return util.GitCommit()
}
func (g *Example) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Example) Generate(config *config.Config, params Params) (FileMap, error) {
g.Message = `
This is an example generator plugin. See the file in 'internal/generator/plugins/example/example.go' on
information about constructing plugins and plugin requirements.`
return FileMap{"example": []byte(g.Message)}, nil
}

View file

@ -1,264 +0,0 @@
package generator
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"plugin"
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
"github.com/rs/zerolog/log"
)
type (
Mappings map[string]any
FileMap map[string][]byte
FileList [][]byte
// Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program.
Generator interface {
GetName() string
GetVersion() string
GetDescription() string
Generate(config *config.Config, params Params) (FileMap, error)
}
)
var DefaultGenerators = createDefaultGenerators()
func createDefaultGenerators() map[string]Generator {
var (
generatorMap = map[string]Generator{}
generators = []Generator{
&Conman{}, &DHCPd{}, &DNSMasq{}, &Warewulf{}, &Example{}, &CoreDhcp{},
}
)
for _, g := range generators {
generatorMap[g.GetName()] = g
}
return generatorMap
}
// Converts the file outputs from map[string][]byte to map[string]string.
func ConvertContentsToString(f FileMap) map[string]string {
n := make(map[string]string, len(f))
for k, v := range f {
n[k] = string(v)
}
return n
}
// Loads files without applying any Jinja 2 templating.
func LoadFiles(paths ...string) (FileMap, error) {
var outputs = FileMap{}
for _, path := range paths {
expandedPaths, err := filepath.Glob(path)
if err != nil {
return nil, fmt.Errorf("failed to glob path: %w", err)
}
for _, expandedPath := range expandedPaths {
info, err := os.Stat(expandedPath)
if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("failed to stat file or directory: %w", err)
}
// skip any directories found
if info.IsDir() {
continue
}
b, err := os.ReadFile(expandedPath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
outputs[expandedPath] = b
}
}
return outputs, nil
}
// Loads a single generator plugin given a single file path.
func LoadPlugin(path string) (Generator, error) {
// skip loading plugin if path is a directory with no error
if isDir, err := util.IsDirectory(path); err == nil && isDir {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("failed to test if plugin path is directory: %w", err)
}
// try and open the plugin
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open plugin: %w", err)
}
// load the "Generator" symbol from plugin
symbol, err := p.Lookup("Generator")
if err != nil {
return nil, fmt.Errorf("failed to look up symbol at path '%s': %w", path, err)
}
// assert that the plugin loaded has a valid generator
gen, ok := symbol.(Generator)
if !ok {
return nil, fmt.Errorf("failed to load the correct symbol type at path '%s'", path)
}
return gen, nil
}
// Loads all generator plugins in a given directory.
//
// Returns a map of generators. Each generator can be accessed by the name
// returned by the generator.GetName() implemented.
func LoadPlugins(dirpath string, opts ...Option) (map[string]Generator, error) {
// check if verbose option is supplied
var (
generators = make(map[string]Generator)
params = ToParams(opts...)
)
//
err := filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error {
// skip trying to load generator plugin if directory or error
if info.IsDir() || err != nil {
return nil
}
// only try loading if file has .so extension
if filepath.Ext(path) != ".so" {
return nil
}
// load the generator plugin from current path
gen, err := LoadPlugin(path)
if err != nil {
return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
}
// show the plugins found if verbose flag is set
if params.Verbose {
log.Info().Str("plugin_name", gen.GetName()).Msg("found plugin")
}
// map each generator plugin by name for lookup
generators[gen.GetName()] = gen
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk directory: %w", err)
}
return generators, nil
}
// Generate() is the main function to generate a collection of files and returns them as a map.
// This function only expects a path to a plugin and paths to a collection of templates to
// be used. This function will only load the plugin on-demand and fetch resources as needed.
//
// This function requires that a target and plugin path be set at minimum.
func Generate(config *config.Config, plugin string, params Params) (FileMap, error) {
var (
gen Generator
ok bool
err error
)
// check if generator is built-in first before loading external plugin
log.Debug().Any("generators", DefaultGenerators).Msg("available generators")
gen, ok = DefaultGenerators[plugin]
if !ok {
// only load the plugin needed for this target if we don't find default
log.Error().Str("plugin", plugin).Msg("could not find target in default generators")
gen, err = LoadPlugin(plugin)
if err != nil {
return nil, fmt.Errorf("failed to load plugin from file: %v", err)
}
}
return gen.Generate(config, params)
}
// Main function to generate a collection of files as a map with the path as the key and
// the contents of the file as the value. This function currently expects a list of plugin
// paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget()
// function is called for each target specified.
//
// This function is the corresponding implementation for the "generate" CLI subcommand.
// It is also call when running the configurator as a service with the "/generate" route.
//
// TODO: Separate loading plugins so we can load them once when running as a service.
func GenerateWithTarget(config *config.Config, target string) (FileMap, error) {
// load generator plugins to generate configs or to print
var (
opts []client.Option
targetInfo configurator.Target
generator Generator
params Params
err error
ok bool
)
// check if a target is supplied
if target == "" {
return nil, fmt.Errorf("must specify a target")
}
// load target information from config
targetInfo, ok = config.Targets[target]
if !ok {
log.Warn().Str("target", target).Msg("target not found in config")
}
// if no plugin supplied in config target, then using the target supplied
if targetInfo.Plugin == "" {
targetInfo.Plugin = target
}
// check if generator is built-in first before loading
generator, ok = DefaultGenerators[target]
if !ok {
// only load the plugin needed for this target if we don't find default
log.Warn().Str("target", target).Msg("could not find target in default generators")
generator, err = LoadPlugin(targetInfo.Plugin)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %v", err)
}
}
// check if there's at least one template available
if len(targetInfo.TemplatePaths) <= 0 {
return nil, fmt.Errorf("expects at least one template to be available")
}
// prepare params to pass into generator
params.Templates = map[string]Template{}
for _, templatePath := range targetInfo.TemplatePaths {
template := Template{}
template.LoadFromFile(templatePath)
params.Templates[templatePath] = template
}
// set the client options
if config.AccessToken != "" {
params.ClientOpts = append(opts, client.WithAccessToken(config.AccessToken))
}
if config.CertPath != "" {
params.ClientOpts = append(opts, client.WithCertPoolFile(config.CertPath))
}
// load files that are not to be copied
params.Files, err = LoadFiles(targetInfo.FilePaths...)
if err != nil {
return nil, fmt.Errorf("failed to load files to copy: %v", err)
}
// run the generator plugin from target passed
return generator.Generate(config, params)
}

View file

@ -1,26 +0,0 @@
package generator
import (
"fmt"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type Hostfile struct{}
func (g *Hostfile) GetName() string {
return "hostfile"
}
func (g *Hostfile) GetVersion() string {
return util.GitCommit()
}
func (g *Hostfile) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Hostfile) Generate(config *config.Config, opts ...Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -1,43 +0,0 @@
package generator
import (
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
)
type (
// Params used by the generator
Params struct {
Templates map[string]Template
Files map[string][]byte
ClientOpts []client.Option
Verbose bool
}
Option func(Params)
)
func ToParams(opts ...Option) Params {
params := Params{}
for _, opt := range opts {
opt(params)
}
return params
}
func WithClientOpts(opts ...client.Option) Option {
return func(p Params) {
p.ClientOpts = opts
}
}
func WithTemplates(templates map[string]Template) Option {
return func(p Params) {
p.Templates = templates
}
}
// Helper function to get the target in generator.Generate() plugin implementations.
func GetTarget(config *config.Config, key string) configurator.Target {
return config.Targets[key]
}

View file

@ -1,26 +0,0 @@
package generator
import (
"fmt"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type Powerman struct{}
func (g *Powerman) GetName() string {
return "powerman"
}
func (g *Powerman) GetVersion() string {
return util.GitCommit()
}
func (g *Powerman) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Powerman) Generate(config *config.Config, opts ...Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -1,26 +0,0 @@
package generator
import (
"fmt"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
)
type Syslog struct{}
func (g *Syslog) GetName() string {
return "syslog"
}
func (g *Syslog) GetVersion() string {
return util.GitCommit()
}
func (g *Syslog) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Syslog) Generate(config *config.Config, opts ...Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -1,98 +0,0 @@
package generator
import (
"bytes"
"fmt"
"os"
"git.towk2.me/towk/configurator/pkg/util"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
"github.com/rs/zerolog/log"
)
type Template struct {
Contents []byte `json:"contents"`
}
func (t *Template) LoadFromFile(path string) error {
// skip loading template if path is a directory with no error
if isDir, err := util.IsDirectory(path); err == nil && isDir {
return nil
} else if err != nil {
return fmt.Errorf("failed to test if template path is directory: %w", err)
}
// try and read the contents of the file
// NOTE: we don't care if this is actually a Jinja template
// or not...at least for now.
contents, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}
t.Contents = contents
return nil
}
func (t *Template) IsEmpty() bool {
return len(t.Contents) <= 0
}
// Wrapper function to slightly abstract away some of the nuances with using gonja
// into a single function call. This function is *mostly* for convenience and
// simplication. If no paths are supplied, then no templates will be applied and
// there will be no output.
//
// The "FileList" returns a slice of byte arrays in the same order as the argument
// list supplied, but with the Jinja templating applied.
func ApplyTemplates(mappings Mappings, templates map[string]Template) (FileMap, error) {
var (
data = exec.NewContext(mappings)
outputs = FileMap{}
)
for path, template := range templates {
// load jinja template from file
t, err := gonja.FromBytes(template.Contents)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %w", err)
}
// execute/render jinja template
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %w", err)
}
outputs[path] = b.Bytes()
}
log.Debug().Any("templates", templates).Any("outputs", outputs).Any("mappings", mappings).Msg("apply templates")
return outputs, nil
}
// Wrapper function similiar to "ApplyTemplates" but takes file paths as arguments.
// This function will load templates from a file instead of using file contents.
func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error) {
var (
data = exec.NewContext(mappings)
outputs = FileMap{}
)
for _, path := range paths {
// load jinja template from file
t, err := gonja.FromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %w", err)
}
// execute/render jinja template
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %w", err)
}
outputs[path] = b.Bytes()
}
return outputs, nil
}

View file

@ -1,78 +0,0 @@
package generator
import (
"fmt"
"maps"
"strings"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/configurator/pkg/config"
"git.towk2.me/towk/configurator/pkg/util"
"github.com/rs/zerolog/log"
)
type Warewulf struct{}
func (g *Warewulf) GetName() string {
return "warewulf"
}
func (g *Warewulf) GetVersion() string {
return util.GitCommit()
}
func (g *Warewulf) GetDescription() string {
return "Configurator generator plugin for 'warewulf' config files."
}
func (g *Warewulf) Generate(config *config.Config, params Params) (FileMap, error) {
var (
smdClient = client.NewSmdClient(params.ClientOpts...)
outputs = make(FileMap, len(params.Templates))
nodeEntries = ""
paths = []string{}
)
// if we have a client, try making the request for the ethernet interfaces
eths, err := smdClient.FetchEthernetInterfaces(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", err)
}
// check if we have the required params first
if eths == nil {
return nil, fmt.Errorf("invalid ethernet interfaces (variable is nil)")
}
if len(eths) <= 0 {
return nil, fmt.Errorf("no ethernet interfaces found")
}
// fetch redfish endpoints and handle errors
eps, err := smdClient.FetchRedfishEndpoints(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch redfish endpoints: %v", err)
}
if len(eps) <= 0 {
return nil, fmt.Errorf("no redfish endpoints found")
}
templates, err := ApplyTemplates(Mappings{
"node_entries": nodeEntries,
}, params.Templates)
if err != nil {
return nil, fmt.Errorf("failed to load templates: %v", err)
}
maps.Copy(outputs, params.Files)
maps.Copy(outputs, templates)
// print message if verbose param is found
if params.Verbose {
for path, _ := range outputs {
paths = append(paths, path)
}
}
log.Info().Str("paths", strings.Join(paths, ":")).Msg("templates and files loaded: \n")
return outputs, err
}

View file

@ -10,6 +10,7 @@ import (
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/tidwall/sjson"
)
type Profile struct {
@ -43,7 +44,7 @@ func (s *Service) GetProfiles() http.HandlerFunc {
// read file contents
var profile *Profile
profile, err = LoadProfile(path)
profile, err = LoadProfileFromFile(path)
if err != nil {
return err
}
@ -91,7 +92,7 @@ func (s *Service) GetProfile() http.HandlerFunc {
err error
)
profile, err = LoadProfile(path)
profile, err = LoadProfileFromFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@ -114,8 +115,8 @@ func (s *Service) GetProfile() http.HandlerFunc {
func (s *Service) CreateProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type input struct {
path string `json:"path"`
profile *Profile `json:"profile"`
Path string `json:"path"`
Profile *Profile `json:"profile"`
}
var (
body, contents []byte
@ -137,37 +138,85 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
// serialize just the profile part
contents, err = json.Marshal(in.profile)
contents, err = json.Marshal(in.Profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// create a new profile on disk
err = os.WriteFile(in.path, contents, os.ModePerm)
err = os.WriteFile(in.Path, contents, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (s *Service) CreateProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
func (s *Service) SetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
body, contents []byte
newContents string
profile *Profile
path string
err error
)
body, err = io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.Unmarshal(body, &profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// make sure the request data sets an ID
if profile.ID == "" {
http.Error(w, "ID must be set to a non-empty value", http.StatusBadRequest)
return
}
// read the contents the file with profile ID
path = s.BuildProfilePath(profile.ID)
contents, err = os.ReadFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// modify the data of the profile's contents
newContents, err = sjson.Set(string(contents), "data", profile.Data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// write only the data to the file with ID
err = os.WriteFile(path, []byte(newContents), os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *Service) DeleteProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
func (s *Service) DeleteProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Service) GetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}
func (s *Service) GetProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}
func (s *Service) CreateProfilePath() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}

View file

@ -62,10 +62,9 @@ func (s *Service) Serve() error {
// router.Post("/profiles", s.CreateProfiles())
router.Get("/profile/{id}", s.GetProfile())
router.Post("/profile/{id}", s.CreateProfile())
router.Post("/profile/{id}/data/{varname}", s.CreateProfileVar())
router.Delete("/profile/{id}/data/{varname}", s.DeleteProfileVar())
router.Get("/profile/{id}/data", s.GetProfileData())
router.Get("/profile/{id}/data/{varname}", s.GetProfileVar())
router.Post("/profile/{id}/data", s.SetProfileData())
router.Delete("/profile/{id}/data", s.DeleteProfileData())
router.Post("/profile/{id}/paths/{path}", s.CreateProfilePath())
router.Delete("/profile/{id}/paths/{path}", s.DeleteProfilePath())
router.Get("/profile/{id}/paths/{path}", s.GetProfilePath())
@ -89,11 +88,11 @@ func (s *Service) FetchJwks(uri string) {
}
func LoadProfile(path string) (*Profile, error) {
func LoadProfileFromFile(path string) (*Profile, error) {
return LoadFromJSONFile[Profile](path)
}
func LoadPlugin(path string) (*configurator.Plugin, error) {
func LoadPluginFromFile(path string) (*configurator.Plugin, error) {
return LoadFromJSONFile[configurator.Plugin](path)
}