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

     1  // Copyright (c) 2019-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package schedulewatcher
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	cloudevents "github.com/cloudevents/sdk-go/v2"
    12  
    13  	"github.com/choria-io/go-choria/aagent/watchers/event"
    14  )
    15  
    16  // StateNotification describes the current state of the watcher
    17  // described by io.choria.machine.watcher.exec.v1.state
    18  type StateNotification struct {
    19  	event.Event
    20  
    21  	State string `json:"state"`
    22  }
    23  
    24  // JSON creates a JSON representation of the notification
    25  func (s *StateNotification) JSON() ([]byte, error) {
    26  	return json.Marshal(s.CloudEvent())
    27  }
    28  
    29  // CloudEvent creates a CloudEvent from the state notification
    30  func (s *StateNotification) CloudEvent() cloudevents.Event {
    31  	return s.Event.CloudEvent(s)
    32  }
    33  
    34  // String is a string representation of the notification suitable for printing
    35  func (s *StateNotification) String() string {
    36  	return fmt.Sprintf("%s %s#%s schedule state: %s", s.Identity, s.Machine, s.Name, s.State)
    37  }