[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-10-02 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r222037379
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/README.md
 ##
 @@ -0,0 +1,74 @@
+
+
+# Adding a Plugin
+
+To add a plugin, create a new `.go` file in the `traffic_ops_golang/plugin` 
directory. This file should have a unique name, to avoid conflicts. Consider 
prefixing it with your company name, website, or a UUID.
+
+The filename, sans `.go`, is the name of your plugin, and will be the key used 
for configuration in the remap file. For example, if your file is 
`f49e54fc-fd17-4e1c-92c6-67028fde8504-hello-world.go`, the name of your plugin 
is `f49e54fc-fd17-4e1c-92c6-67028fde8504-hello-world`.
+
+Plugins are registered via calls to `AddPlugin` inside an `init` function in 
the plugin's file.
+
+The `Funcs` object contains functions for each hook, as well as a load 
function for loading configuration from the remap file. The current hooks are 
`load`, `startup`, and `onRequest`. If your plugin does not use a hook, it may 
be nil.
+
+* `load` is called when the application starts, is given config data, and must 
return the loaded configuration object.
+
+* `startup` is called when the application starts.
+
+* `onRequest` is called immediately when a request is received. It returns a 
boolean indicating whether to stop processing.
+
+The simplest example is the `hello_world` plugin. See `plugin/hello_world.go`.
+
+```go
+import (
+   "strings"
+)
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
 
 Review comment:
   It's the priority, the order the plugin is called in relative to other 
plugins. See 
https://github.com/apache/trafficcontrol/blob/147c3f3ec19eb485de5949f0a50eb70c8d72b4e2/traffic_ops/traffic_ops_golang/plugin/plugin.go#L94
   
   I added a note to the README about that parameter.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-10-02 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r222035630
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/README.md
 ##
 @@ -0,0 +1,74 @@
+
+
+# Adding a Plugin
+
+To add a plugin, create a new `.go` file in the `traffic_ops_golang/plugin` 
directory. This file should have a unique name, to avoid conflicts. Consider 
prefixing it with your company name, website, or a UUID.
+
+The filename, sans `.go`, is the name of your plugin, and will be the key used 
for configuration in the remap file. For example, if your file is 
`f49e54fc-fd17-4e1c-92c6-67028fde8504-hello-world.go`, the name of your plugin 
is `f49e54fc-fd17-4e1c-92c6-67028fde8504-hello-world`.
+
+Plugins are registered via calls to `AddPlugin` inside an `init` function in 
the plugin's file.
+
+The `Funcs` object contains functions for each hook, as well as a load 
function for loading configuration from the remap file. The current hooks are 
`load`, `startup`, and `onRequest`. If your plugin does not use a hook, it may 
be nil.
+
+* `load` is called when the application starts, is given config data, and must 
return the loaded configuration object.
+
+* `startup` is called when the application starts.
+
+* `onRequest` is called immediately when a request is received. It returns a 
boolean indicating whether to stop processing.
 
 Review comment:
   Pre-authentication. I added a comment to the README.md noting that. 
   
   Someone could want an unauthenticated extension, doing auth here would 
prohibit that. Likewise, they could want to check a "role" but not 
"capability," or a "capability" but not "api_capability". There's no good way 
to do that in the plugin system, without significantly limiting its flexibility.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-12 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r202194782
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   @dewrich Ok, I added the list of plugins to the RPM:
   
   ```
   $ rpm -qip dist/traffic_ops-2.3.0-8898.4abdfa71.el7.x86_64.rpm   


   
   Name: traffic_ops
   ⋮
   Description :
   Traffic Ops is the tool for administration (configuration and monitoring) of 
all components in a Traffic Control CDN.
   
   This package provides Traffic Ops with the following plugins:
   hello_config
   hello_context
   hello_shared_config
   hello_startup
   hello_world
   ```
   
   Does that address all your concerns?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-12 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r202194782
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   @dewrich Ok, I added the list of plugins to the RPM:
   
   ```bash
   $ rpm -qip dist/traffic_ops-2.3.0-8898.4abdfa71.el7.x86_64.rpm   


   
   Name: traffic_ops
   ⋮
   Description :
   Traffic Ops is the tool for administration (configuration and monitoring) of 
all components in a Traffic Control CDN.
   
   This package provides Traffic Ops with the following plugins:
   hello_config
   hello_context
   hello_shared_config
   hello_startup
   hello_world
   ```
   
   Does that address all your concerns?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-11 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r201757739
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   >I also don't understand how this plugin (compiled in) approach would be 
more powerful than a traditional MS approach?
   
   Because it's capable of both.
   
   ```
   func init() { AddPlugin(1, Funcs{onRequest: helloMicroService}) }
   const HelloServicePath = "/api/1.2/cdns/routing"
   const MicroserviceURI = "https://example.net";
   func helloMicroService(d OnRequestData) IsRequestHandled {
if !strings.HasPrefix(d.R.URL.Path, HelloServicePath) {
return RequestUnhandled
}
resp, _ := http.Get(MicroserviceURI)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return RequestHandled
   }
   ```
   
   So you see, this plugin system can be used for microservices. And since it 
can also be used for direct code, it's more powerful than a microservice-only 
extension system.
   
   >now we have to build a more sophisticated build system
   
   Our build system already creates an RPM from an internal merge with our 
extensions. We won't need to change it at all. And as @jhg03a said, it'll have 
a unique internal git hash we can use to identify which plugins were put in.
   
   >how could I look at the rpm and determine it had "my custom plugins" in it 
easily?
   
   That's a really good idea, actually: we should make the `build.sh` add the 
list of plugins to the Yum Info in the RPM. I can add that, either to this PR, 
or a following one.
   


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-11 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r201757739
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   >I also don't understand how this plugin (compiled in) approach would be 
more powerful than a traditional MS approach?
   
   Because it's capable of both.
   
   ```
   func init() { AddPlugin(1, Funcs{onRequest: helloMicroService}) }
   const HelloServicePath = "/api/1.2/cdns/routing"
   const MicroserviceURI = "https://example.net";
   func helloMicroService(d OnRequestData) IsRequestHandled {
if !strings.HasPrefix(d.R.URL.Path, HelloServicePath) {
return RequestUnhandled
}
resp, _ := http.Get(MicroserviceURI)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return RequestHandled
   }
   ```
   
   So you see, this plugin system can be used for microservices. And since it 
can also be used for direct code, it's more powerful than a microservice-only 
extension system.
   
   >now we have to build a more sophisticated build system
   
   Our build system already creates an RPM from an internal merge with our 
extensions. We won't need to change it at all. And as @jhg03a said, it'll have 
a unique internal git hash we can use to identify which plugins were put in.
   
   >how could I look at the rpm and determine it had "my custom plugins" in it 
easily?
   
   That's a really good idea, actually: we should make the traffic_ops 
`build.sh` add the list of plugins to the Yum Info in the RPM. I can add that, 
either to this PR, or a following one.
   


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-11 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r201757739
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   >I also don't understand how this plugin (compiled in) approach would be 
more powerful than a traditional MS approach?
   
   Because it's capable of both.
   
   ```
   func init() { AddPlugin(1, Funcs{onRequest: helloMicroService}) }
   const HelloServicePath = "/api/1.2/cdns/routing"
   const MicroserviceURI = "https://example.net";
   func helloMicroService(d OnRequestData) IsRequestHandled {
if !strings.HasPrefix(d.R.URL.Path, HelloServicePath) {
return RequestUnhandled
}
resp, _ := http.Get(MicroserviceURI)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return RequestHandled
   }
   ```
   
   So you see, this plugin system can be used for microservices. And since it 
can also be used for direct code, it's more powerful than a microservice-only 
extension system.
   
   >now we have to build a more sophisticated build system
   
   Our build system already creates an RPM from an internal merge with our 
extensions. We won't need to change it at all. And as @jhg03a said, it'll have 
a unique internal git hash we can use to identify which plugins were put in.
   
   >how could I look at the rpm and determine it had "my custom plugins" in it 
easily?
   
   That's a really good idea, actually: we should make the traffic_ops build.sh 
add the list of plugins to the Yum Info in the RPM. I can add that, either to 
this PR, or a following one.
   


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-09 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r201018252
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   It'd be very easy to create a plugin which calls an external service, or a 
set of plugins with shared config as complex as you like. A specific 
"microservice extensions" framework would be strictly less powerful than this. 
   
   >Was this discussed on the Dev List?
   
   It wasn't, this is just reproducing the existing Perl TO functionality, but 
feel free to.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rob05c commented on a change in pull request #2513: Add TO Go plugin system

2018-07-06 Thread GitBox
rob05c commented on a change in pull request #2513: Add TO Go plugin system
URL: https://github.com/apache/trafficcontrol/pull/2513#discussion_r200752550
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/plugin/hello_world.go
 ##
 @@ -0,0 +1,34 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+   "strings"
+)
+
+func init() {
+   AddPlugin(1, Funcs{onRequest: hello})
+}
+
+const HelloPath = "/_hello"
+
+func hello(d OnRequestData) IsRequestHandled {
+   if !strings.HasPrefix(d.R.URL.Path, HelloPath) {
 
 Review comment:
   I know this feels a little odd, rather than adding a path to serve as part 
of the registration. But with Grove, we found it was a lot more flexible to 
have a generic OnRequest that can do anything, and instruct the main app to 
stop or continue processing. The idiom with Grove is to prefix path plugins 
with `http_`, so e.g. `plugins/http_routing_override.go`, which IMO would be a 
good idiom to continue in TO.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services