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

     1  // Copyright (c) 2019-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package notifier
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/choria-io/go-choria/aagent/machine"
    14  )
    15  
    16  // Notifier implements machine.NotificationService
    17  type Notifier struct {
    18  	fw     ChoriaProvider
    19  	logger *logrus.Entry
    20  }
    21  
    22  // ChoriaProvider provider key Choria service
    23  type ChoriaProvider interface {
    24  	PublishRaw(target string, data []byte) error
    25  	Logger(component string) *logrus.Entry
    26  }
    27  
    28  // New creates a new choria notifier
    29  func New(fw ChoriaProvider) (n *Notifier, err error) {
    30  	n = &Notifier{
    31  		fw:     fw,
    32  		logger: fw.Logger("machine"),
    33  	}
    34  
    35  	return n, nil
    36  }
    37  
    38  // Debugf implements machine.NotificationService
    39  func (n *Notifier) Debugf(m machine.InfoSource, name string, format string, args ...any) {
    40  	n.logger.Debugf("%s#%s: %s", m.Name(), name, fmt.Sprintf(format, args...))
    41  }
    42  
    43  // Infof implements machine.NotificationService
    44  func (n *Notifier) Infof(m machine.InfoSource, name string, format string, args ...any) {
    45  	n.logger.Infof("%s#%s: %s", m.Name(), name, fmt.Sprintf(format, args...))
    46  }
    47  
    48  // Warnf implements machine.NotificationService
    49  func (n *Notifier) Warnf(m machine.InfoSource, name string, format string, args ...any) {
    50  	n.logger.Warnf("%s#%s: %s", m.Name(), name, fmt.Sprintf(format, args...))
    51  }
    52  
    53  // Errorf implements machine.NotificationService
    54  func (n *Notifier) Errorf(m machine.InfoSource, name string, format string, args ...any) {
    55  	n.logger.Errorf("%s#%s: %s", m.Name(), name, fmt.Sprintf(format, args...))
    56  }
    57  
    58  // NotifyPostTransition implements machine.NotificationService
    59  func (n *Notifier) NotifyPostTransition(transition *machine.TransitionNotification) (err error) {
    60  	n.logger.Infof("%s transitioned via event %s: from %s into %s", transition.Machine, transition.Transition, transition.FromState, transition.ToState)
    61  
    62  	j, err := json.Marshal(transition.CloudEvent())
    63  	if err != nil {
    64  		return fmt.Errorf("could not JSON encode transition notification: %s", err)
    65  	}
    66  
    67  	err = n.fw.PublishRaw("choria.machine.transition", j)
    68  	if err != nil {
    69  		return fmt.Errorf("could not publish notification: %s", err)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // NotifyWatcherState implements machine.NotificationService
    76  func (n *Notifier) NotifyWatcherState(name string, detail machine.WatcherStateNotification) error {
    77  	j, err := detail.JSON()
    78  	if err != nil {
    79  		n.logger.Errorf("Could not json marshal watcher state: %v: %s", detail, err)
    80  		return err
    81  	}
    82  
    83  	wtype := detail.WatcherType()
    84  	if wtype == "" {
    85  		n.logger.Errorf("Received a watcher state without a valid type associated")
    86  		return fmt.Errorf("invalid watcher type in watcher state")
    87  	}
    88  
    89  	n.logger.Info(detail.String())
    90  
    91  	err = n.fw.PublishRaw(fmt.Sprintf("choria.machine.watcher.%s.state", wtype), j)
    92  	if err != nil {
    93  		return fmt.Errorf("could not publish notification: %s", err)
    94  	}
    95  
    96  	return nil
    97  }