github.com/Jeffail/benthos/v3@v3.65.0/internal/interop/resources.go (about) 1 package interop 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 8 "github.com/Jeffail/benthos/v3/lib/types" 9 ) 10 11 // ProbeCache checks whether a cache resource has been configured, and returns 12 // an error if not. 13 func ProbeCache(ctx context.Context, mgr types.Manager, name string) error { 14 if _, err := mgr.GetCache(name); err != nil { 15 return fmt.Errorf("cache resource '%v' was not found", name) 16 } 17 return nil 18 } 19 20 // AccessCache attempts to access a cache resource by a unique identifier and 21 // executes a closure function with the cache as an argument. Returns an error 22 // if the cache does not exist (or is otherwise inaccessible). 23 func AccessCache(ctx context.Context, mgr types.Manager, name string, fn func(types.Cache)) error { 24 if nm, ok := mgr.(interface { 25 AccessCache(ctx context.Context, name string, fn func(types.Cache)) error 26 }); ok { 27 return nm.AccessCache(ctx, name, fn) 28 } 29 c, err := mgr.GetCache(name) 30 if err != nil { 31 return err 32 } 33 if c == nil { 34 return types.ErrCacheNotFound 35 } 36 fn(c) 37 return nil 38 } 39 40 // ProbeInput checks whether an input resource has been configured, and returns 41 // an error if not. 42 func ProbeInput(ctx context.Context, mgr types.Manager, name string) error { 43 if gi, ok := mgr.(interface { 44 GetInput(name string) (types.Input, error) 45 }); ok { 46 if _, err := gi.GetInput(name); err != nil { 47 return fmt.Errorf("input resource '%v' was not found", name) 48 } 49 } else { 50 return errors.New("manager does not support input resources") 51 } 52 return nil 53 } 54 55 // AccessInput attempts to access an input resource by a unique identifier and 56 // executes a closure function with the input as an argument. Returns an error 57 // if the input does not exist (or is otherwise inaccessible). 58 func AccessInput(ctx context.Context, mgr types.Manager, name string, fn func(types.Input)) error { 59 if nm, ok := mgr.(interface { 60 AccessInput(ctx context.Context, name string, fn func(types.Input)) error 61 }); ok { 62 return nm.AccessInput(ctx, name, fn) 63 } 64 if gi, ok := mgr.(interface { 65 GetInput(name string) (types.Input, error) 66 }); ok { 67 c, err := gi.GetInput(name) 68 if err != nil { 69 return err 70 } 71 if c == nil { 72 return types.ErrInputNotFound 73 } 74 fn(c) 75 return nil 76 } 77 return errors.New("manager does not support input resources") 78 } 79 80 // ProbeOutput checks whether an output resource has been configured, and 81 // returns an error if not. 82 func ProbeOutput(ctx context.Context, mgr types.Manager, name string) error { 83 if gi, ok := mgr.(interface { 84 GetOutput(name string) (types.OutputWriter, error) 85 }); ok { 86 if _, err := gi.GetOutput(name); err != nil { 87 return fmt.Errorf("output resource '%v' was not found", name) 88 } 89 } else { 90 return errors.New("manager does not support output resources") 91 } 92 return nil 93 } 94 95 // AccessOutput attempts to access an output resource by a unique identifier and 96 // executes a closure function with the output as an argument. Returns an error 97 // if the output does not exist (or is otherwise inaccessible). 98 func AccessOutput(ctx context.Context, mgr types.Manager, name string, fn func(types.OutputWriter)) error { 99 if nm, ok := mgr.(interface { 100 AccessOutput(ctx context.Context, name string, fn func(types.OutputWriter)) error 101 }); ok { 102 return nm.AccessOutput(ctx, name, fn) 103 } 104 if gi, ok := mgr.(interface { 105 GetOutput(name string) (types.OutputWriter, error) 106 }); ok { 107 o, err := gi.GetOutput(name) 108 if err != nil { 109 return err 110 } 111 if o == nil { 112 return types.ErrOutputNotFound 113 } 114 fn(o) 115 return nil 116 } 117 return errors.New("manager does not support output resources") 118 } 119 120 // ProbeProcessor checks whether a processor resource has been configured, and 121 // returns an error if not. 122 func ProbeProcessor(ctx context.Context, mgr types.Manager, name string) error { 123 if gi, ok := mgr.(interface { 124 GetProcessor(name string) (types.Processor, error) 125 }); ok { 126 if _, err := gi.GetProcessor(name); err != nil { 127 return fmt.Errorf("processor resource '%v' was not found", name) 128 } 129 } else { 130 return errors.New("manager does not support processor resources") 131 } 132 return nil 133 } 134 135 // AccessProcessor attempts to access a processor resource by a unique 136 // identifier and executes a closure function with the processor as an argument. 137 // Returns an error if the processor does not exist (or is otherwise 138 // inaccessible). 139 func AccessProcessor(ctx context.Context, mgr types.Manager, name string, fn func(types.Processor)) error { 140 if nm, ok := mgr.(interface { 141 AccessProcessor(ctx context.Context, name string, fn func(types.Processor)) error 142 }); ok { 143 return nm.AccessProcessor(ctx, name, fn) 144 } 145 if gi, ok := mgr.(interface { 146 GetProcessor(name string) (types.Processor, error) 147 }); ok { 148 o, err := gi.GetProcessor(name) 149 if err != nil { 150 return err 151 } 152 if o == nil { 153 return types.ErrProcessorNotFound 154 } 155 fn(o) 156 return nil 157 } 158 return errors.New("manager does not support processor resources") 159 } 160 161 // ProbeRateLimit checks whether a rate limit resource has been configured, and 162 // returns an error if not. 163 func ProbeRateLimit(ctx context.Context, mgr types.Manager, name string) error { 164 if _, err := mgr.GetRateLimit(name); err != nil { 165 return fmt.Errorf("rate limit resource '%v' was not found", name) 166 } 167 return nil 168 } 169 170 // AccessRateLimit attempts to access a rate limit resource by a unique 171 // identifier and executes a closure function with the rate limit as an 172 // argument. Returns an error if the rate limit does not exist (or is otherwise 173 // inaccessible). 174 func AccessRateLimit(ctx context.Context, mgr types.Manager, name string, fn func(types.RateLimit)) error { 175 if nm, ok := mgr.(interface { 176 AccessRateLimit(ctx context.Context, name string, fn func(types.RateLimit)) error 177 }); ok { 178 return nm.AccessRateLimit(ctx, name, fn) 179 } 180 c, err := mgr.GetRateLimit(name) 181 if err != nil { 182 return err 183 } 184 if c == nil { 185 return types.ErrRateLimitNotFound 186 } 187 fn(c) 188 return nil 189 }