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

     1  // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package lifecycle
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  )
    11  
    12  // ProvisionedEvent is a io.choria.lifecycle.v1.provisioned event
    13  type ProvisionedEvent struct {
    14  	basicEvent
    15  }
    16  
    17  func init() {
    18  	eventTypes["provisioned"] = Provisioned
    19  
    20  	eventJSONParsers[Provisioned] = func(j []byte) (Event, error) {
    21  		return newProvisionedEventFromJSON(j)
    22  	}
    23  
    24  	eventFactories[Provisioned] = func(opts ...Option) Event {
    25  		return newProvisionedEvent(opts...)
    26  	}
    27  }
    28  
    29  func newProvisionedEvent(opts ...Option) *ProvisionedEvent {
    30  	event := &ProvisionedEvent{basicEvent: newBasicEvent("provisioned")}
    31  
    32  	for _, o := range opts {
    33  		o(event)
    34  	}
    35  
    36  	return event
    37  }
    38  
    39  func newProvisionedEventFromJSON(j []byte) (*ProvisionedEvent, error) {
    40  	event := &ProvisionedEvent{basicEvent: newBasicEvent("provisioned")}
    41  
    42  	err := json.Unmarshal(j, event)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	switch event.EventProtocol {
    48  	case "io.choria.lifecycle.v1.provisioned":
    49  	case "choria:lifecycle:provisioned:1":
    50  		event.EventProtocol = "io.choria.lifecycle.v1.provisioned"
    51  	default:
    52  		return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
    53  	}
    54  
    55  	return event, nil
    56  }