Skip to content

Commit

Permalink
Added Cisco Spark hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rgnv committed Apr 12, 2018
1 parent 56ec279 commit 09bcf46
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Check out the [Oxidized TREX 2014 presentation](http://youtu.be/kBQ_CTUuqeU#t=3h
* [Hook: awssns](docs/Hooks.md#hook-type-awssns)
* [Hook: slackdiff](docs/Hooks.md#hook-type-slackdiff)
* [Hook: xmppdiff](docs/Hooks.md#hook-type-xmppdiff)
* [Hook: ciscosparkdiff](docs/Hooks.md#hook-type-ciscosparkdiff)
5. [Creating and Extending Models](docs/Creating-Models.md)
6. [Help](#help)
7. [Ruby API](docs/Ruby-API.md#ruby-api)
Expand Down
37 changes: 37 additions & 0 deletions docs/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,43 @@ hooks:

Note the channel name must be in quotes.

## Hook type: ciscosparkdiff

The `ciscosparkdiff` hook posts config diffs to a [Cisco Spark](https://www.ciscospark.com/) space of your choice. It only triggers for `post_store` events.

You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby):

```shell
gem install cisco_spark
```

### ciscosparkdiff hook configuration example

```yaml
hooks:
ciscospark:
type: ciscosparkdiff
events: [post_store]
accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html)
space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html)
diff: true
```

Optionally you can disable snippets and post a formatted message, for instance linking to a commit in a git repo. Named parameters `%{node}`, `%{group}`, `%{model}` and `%{commitref}` are available.

```yaml
hooks:
ciscospark:
type: ciscosparkdiff
events: [post_store]
accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html)
space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html)
diff: false
message: "%{node} %{group} %{model} updated https://git.intranet/network-changes/commit/%{commitref}"
```

Note the space and access tokens must be in quotes.

## Hook type: xmppdiff

The `xmppdiff` hook posts config diffs to a [XMPP](https://en.wikipedia.org/wiki/XMPP) chatroom of your choice. It only triggers for `post_store` events.
Expand Down
54 changes: 54 additions & 0 deletions lib/oxidized/hook/ciscosparkdiff.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'cisco_spark'

# defaults to posting a diff, if messageformat is supplied them a message will be posted too
# diffenable defaults to true
# Modified from slackdiff

class CiscoSparkDiff < Oxidized::Hook
def validate_cfg!
raise KeyError, 'hook.accesskey is required' unless cfg.has_key?('accesskey')
raise KeyError, 'hook.space is required' unless cfg.has_key?('space')
end

def run_hook(ctx)
if ctx.node
if ctx.event.to_s == "post_store"
log "Connecting to Cisco Spark"
CiscoSpark.configure do |config|
config.api_key = cfg.accesskey
config.proxy = cfg.proxy if cfg.has_key?('proxy')
end
space = cfg.space
client = CiscoSpark::Room.new(id: space)
client.fetch
log "Connected"
# diff snippet - default
diffenable = true
if cfg.has_key?('diff') == true
if cfg.diff == false
diffenable = false
end
end
if diffenable == true
gitoutput = ctx.node.output.new
diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil
title = "#{ctx.node.name.to_s}"
log "Posting diff as snippet to #{cfg.space}"
message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join
)
room = CiscoSpark::Room.new(id: space)
room.send_message(message)
end
# message custom formatted - optional
if cfg.has_key?('message') == true
log cfg.message
msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase}
log msg
log "Posting message to #{cfg.space}"
client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true)
end
log "Finished"
end
end
end
end

0 comments on commit 09bcf46

Please sign in to comment.