github.com/outbrain/consul@v1.4.5/agent/structs/txn.go (about)

     1  package structs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/consul/api"
     8  	multierror "github.com/hashicorp/go-multierror"
     9  )
    10  
    11  // TxnKVOp is used to define a single operation on the KVS inside a
    12  // transaction.
    13  type TxnKVOp struct {
    14  	Verb   api.KVOp
    15  	DirEnt DirEntry
    16  }
    17  
    18  // TxnKVResult is used to define the result of a single operation on the KVS
    19  // inside a transaction.
    20  type TxnKVResult *DirEntry
    21  
    22  // TxnNodeOp is used to define a single operation on a node in the catalog inside
    23  // a transaction.
    24  type TxnNodeOp struct {
    25  	Verb api.NodeOp
    26  	Node Node
    27  }
    28  
    29  // TxnNodeResult is used to define the result of a single operation on a node
    30  // in the catalog inside a transaction.
    31  type TxnNodeResult *Node
    32  
    33  // TxnServiceOp is used to define a single operation on a service in the catalog inside
    34  // a transaction.
    35  type TxnServiceOp struct {
    36  	Verb    api.ServiceOp
    37  	Node    string
    38  	Service NodeService
    39  }
    40  
    41  // TxnServiceResult is used to define the result of a single operation on a service
    42  // in the catalog inside a transaction.
    43  type TxnServiceResult *NodeService
    44  
    45  // TxnCheckOp is used to define a single operation on a health check inside a
    46  // transaction.
    47  type TxnCheckOp struct {
    48  	Verb  api.CheckOp
    49  	Check HealthCheck
    50  }
    51  
    52  // TxnCheckResult is used to define the result of a single operation on a health
    53  // check inside a transaction.
    54  type TxnCheckResult *HealthCheck
    55  
    56  // TxnKVOp is used to define a single operation on an Intention inside a
    57  // transaction.
    58  type TxnIntentionOp IntentionRequest
    59  
    60  // TxnOp is used to define a single operation inside a transaction. Only one
    61  // of the types should be filled out per entry.
    62  type TxnOp struct {
    63  	KV        *TxnKVOp
    64  	Intention *TxnIntentionOp
    65  	Node      *TxnNodeOp
    66  	Service   *TxnServiceOp
    67  	Check     *TxnCheckOp
    68  }
    69  
    70  // TxnOps is a list of operations within a transaction.
    71  type TxnOps []*TxnOp
    72  
    73  // TxnRequest is used to apply multiple operations to the state store in a
    74  // single transaction
    75  type TxnRequest struct {
    76  	Datacenter string
    77  	Ops        TxnOps
    78  	WriteRequest
    79  }
    80  
    81  func (r *TxnRequest) RequestDatacenter() string {
    82  	return r.Datacenter
    83  }
    84  
    85  // TxnReadRequest is used as a fast path for read-only transactions that don't
    86  // modify the state store.
    87  type TxnReadRequest struct {
    88  	Datacenter string
    89  	Ops        TxnOps
    90  	QueryOptions
    91  }
    92  
    93  func (r *TxnReadRequest) RequestDatacenter() string {
    94  	return r.Datacenter
    95  }
    96  
    97  // TxnError is used to return information about an error for a specific
    98  // operation.
    99  type TxnError struct {
   100  	OpIndex int
   101  	What    string
   102  }
   103  
   104  // Error returns the string representation of an atomic error.
   105  func (e TxnError) Error() string {
   106  	return fmt.Sprintf("op %d: %s", e.OpIndex, e.What)
   107  }
   108  
   109  // TxnErrors is a list of TxnError entries.
   110  type TxnErrors []*TxnError
   111  
   112  // TxnResult is used to define the result of a given operation inside a
   113  // transaction. Only one of the types should be filled out per entry.
   114  type TxnResult struct {
   115  	KV      TxnKVResult      `json:",omitempty"`
   116  	Node    TxnNodeResult    `json:",omitempty"`
   117  	Service TxnServiceResult `json:",omitempty"`
   118  	Check   TxnCheckResult   `json:",omitempty"`
   119  }
   120  
   121  // TxnResults is a list of TxnResult entries.
   122  type TxnResults []*TxnResult
   123  
   124  // TxnResponse is the structure returned by a TxnRequest.
   125  type TxnResponse struct {
   126  	Results TxnResults
   127  	Errors  TxnErrors
   128  }
   129  
   130  // Error returns an aggregate of all errors in this TxnResponse.
   131  func (r TxnResponse) Error() error {
   132  	var errs error
   133  	for _, err := range r.Errors {
   134  		errs = multierror.Append(errs, errors.New(err.Error()))
   135  	}
   136  	return errs
   137  }
   138  
   139  // TxnReadResponse is the structure returned by a TxnReadRequest.
   140  type TxnReadResponse struct {
   141  	TxnResponse
   142  	QueryMeta
   143  }