github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/plugin/plugin.go (about)

     1  // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package plugin
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"time"
    11  
    12  	"github.com/choria-io/go-choria/aagent/model"
    13  	"github.com/choria-io/go-choria/build"
    14  	"github.com/choria-io/go-choria/inter"
    15  	"golang.org/x/text/cases"
    16  	"golang.org/x/text/language"
    17  )
    18  
    19  func NewWatcherPlugin(wtype string, version string, notification func() any, new func(machine model.Machine, name string, states []string, failEvent string, successEvent string, interval string, ai time.Duration, properties map[string]any) (any, error)) *WatcherPlugin {
    20  	return &WatcherPlugin{
    21  		Name: wtype,
    22  		Creator: &watcherCreator{
    23  			wtype:        wtype,
    24  			version:      version,
    25  			notification: notification,
    26  			new:          new,
    27  		},
    28  	}
    29  }
    30  
    31  type WatcherPlugin struct {
    32  	Name    string
    33  	Creator any
    34  }
    35  
    36  type watcherCreator struct {
    37  	wtype        string
    38  	version      string
    39  	notification func() any
    40  	new          func(machine model.Machine, name string, states []string, failEvent string, successEvent string, interval string, ai time.Duration, properties map[string]any) (any, error)
    41  }
    42  
    43  func (c *watcherCreator) Type() string {
    44  	return c.wtype
    45  }
    46  
    47  func (c *watcherCreator) EventType() string {
    48  	return fmt.Sprintf("io.choria.machine.watcher.%s.%s.state", c.wtype, c.version)
    49  }
    50  
    51  func (c *watcherCreator) UnmarshalNotification(n []byte) (any, error) {
    52  	state := c.notification()
    53  	err := json.Unmarshal(n, state)
    54  
    55  	return state, err
    56  }
    57  
    58  func (c *watcherCreator) New(machine model.Machine, name string, states []string, failEvent string, successEvent string, interval string, ai time.Duration, properties map[string]any) (any, error) {
    59  	return c.new(machine, name, states, failEvent, successEvent, interval, ai, properties)
    60  }
    61  
    62  // PluginInstance implements plugin.Pluggable
    63  func (p *WatcherPlugin) PluginInstance() any {
    64  	return p.Creator
    65  }
    66  
    67  // PluginVersion implements plugin.Pluggable
    68  func (p *WatcherPlugin) PluginVersion() string {
    69  	return build.Version
    70  }
    71  
    72  // PluginName implements plugin.Pluggable
    73  func (p *WatcherPlugin) PluginName() string {
    74  	return fmt.Sprintf("%s Watcher version %s", cases.Title(language.AmericanEnglish).String(p.Name), build.Version)
    75  }
    76  
    77  // PluginType implements plugin.Pluggable
    78  func (p *WatcherPlugin) PluginType() inter.PluginType {
    79  	return inter.MachineWatcherPlugin
    80  }