forked from rapid7/nexpose-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rapid7#170 from rapid7/blackouts
Blackouts
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Nexpose | ||
# Constants useful across the Nexpose module. | ||
# Configuration structure for blackouts. | ||
class Blackout < APIObject | ||
# Whether or not this blackout is enabled. | ||
attr_accessor :enabled | ||
# Valid schedule types: daily, hourly, monthly-date, monthly-day, weekly. | ||
attr_accessor :blackout_type | ||
# The repeat interval based upon type. | ||
attr_accessor :blackout_interval | ||
# The earliest date to generate the report on (in ISO 8601 format). | ||
attr_accessor :blackout_start | ||
# The amount of time, in minutes, a blackout period should last. | ||
attr_accessor :blackout_duration | ||
|
||
def initialize(start, enabled=true, duration, type, interval) | ||
@blackout_start = start | ||
@enabled =enabled | ||
@blackout_duration = duration.to_i | ||
@blackout_type = type | ||
@blackout_interval = interval.to_i | ||
end | ||
|
||
def self.from_hash(hash) | ||
repeat_blackout_hash = hash[:repeat_blackout] | ||
blackout = new(hash[:start_date], hash[:blackout_duration], repeat_blackout_hash[:type], repeat_blackout_hash[:interval]) | ||
blackout | ||
end | ||
|
||
def to_h | ||
blackout_hash = { | ||
start_date: @blackout_start, | ||
enabled: @enabled, | ||
blackout_duration: @blackout_duration, | ||
} | ||
repeat_hash= { | ||
type: @blackout_type, | ||
interval: @blackout_interval | ||
} | ||
blackout_hash[:repeat_blackout] = repeat_hash | ||
blackout_hash | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Nexpose | ||
|
||
class GlobalBlackout < APIObject | ||
require 'json' | ||
include JsonSerializer | ||
|
||
# [Array] Blackout starting dates, times and duration for blackout periods. | ||
attr_accessor :blackout | ||
|
||
def initialize(blackout) | ||
@blackout = Array(blackout) | ||
end | ||
|
||
def save(nsc) | ||
params = to_json | ||
AJAX.post(nsc, '/api/2.1/silo_blackout/', params, AJAX::CONTENT_TYPE::JSON) | ||
end | ||
|
||
def to_h | ||
{ | ||
blackouts: | ||
(@blackout || []).map { |blackout| blackout.to_h } | ||
} | ||
end | ||
|
||
def to_json | ||
JSON.generate(to_h) | ||
end | ||
|
||
def self.json_initializer(data) | ||
new(blackout: data) | ||
end | ||
|
||
def self.load(nsc) | ||
uri = '/api/2.1/silo_blackout/' | ||
resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON) | ||
hash = JSON.parse(resp, symbolize_names: true) | ||
blackout = self.json_initializer(hash).deserialize(hash) | ||
blackout.blackout = (hash[:blackouts] || []).map { |blackout| Nexpose::Blackout.from_hash(blackout) } | ||
blackout | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters