Class: Nexpose::Schedule

Inherits:
APIObject show all
Defined in:
lib/nexpose/common.rb

Overview

Configuration structure for schedules.

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIObject

#object_from_hash

Constructor Details

#initialize(type, interval, start, enabled = true, scan_template_id = nil) ⇒ Schedule

Returns a new instance of Schedule.

Parameters:

  • start (Time)


165
166
167
168
169
170
171
# File 'lib/nexpose/common.rb', line 165

def initialize(type, interval, start, enabled = true, scan_template_id = nil)
  @type = type
  @interval = interval
  @start = start
  @enabled = enabled
  @scan_template_id = scan_template_id
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



153
154
155
# File 'lib/nexpose/common.rb', line 153

def date
  @date
end

#dayObject

Returns the value of attribute day.



154
155
156
# File 'lib/nexpose/common.rb', line 154

def day
  @day
end

#enabledObject

Whether or not this schedule is enabled.



133
134
135
# File 'lib/nexpose/common.rb', line 133

def enabled
  @enabled
end

#hourObject

Returns the value of attribute hour.



151
152
153
# File 'lib/nexpose/common.rb', line 151

def hour
  @hour
end

#incrementalObject

TODO: Remove this unused attribute



147
148
149
# File 'lib/nexpose/common.rb', line 147

def incremental
  @incremental
end

#intervalObject

The repeat interval based upon type.



137
138
139
# File 'lib/nexpose/common.rb', line 137

def interval
  @interval
end

#is_extendedObject

Extended attributes added with the new scheduler implementation



150
151
152
# File 'lib/nexpose/common.rb', line 150

def is_extended
  @is_extended
end

#max_durationObject

The amount of time, in minutes, to allow execution before stopping.



142
143
144
# File 'lib/nexpose/common.rb', line 142

def max_duration
  @max_duration
end

#minuteObject

Returns the value of attribute minute.



152
153
154
# File 'lib/nexpose/common.rb', line 152

def minute
  @minute
end

#next_run_timeObject

Returns the value of attribute next_run_time.



158
159
160
# File 'lib/nexpose/common.rb', line 158

def next_run_time
  @next_run_time
end

#not_valid_afterObject

The date after which the schedule is disabled, in ISO 8601 format.



144
145
146
# File 'lib/nexpose/common.rb', line 144

def not_valid_after
  @not_valid_after
end

#occurrenceObject

Returns the value of attribute occurrence.



155
156
157
# File 'lib/nexpose/common.rb', line 155

def occurrence
  @occurrence
end

#repeater_typeObject

scan-schedule attributes



161
162
163
# File 'lib/nexpose/common.rb', line 161

def repeater_type
  @repeater_type
end

#scan_template_idObject

Returns the value of attribute scan_template_id.



162
163
164
# File 'lib/nexpose/common.rb', line 162

def scan_template_id
  @scan_template_id
end

#startObject

The earliest date to generate the report on (in ISO 8601 format).



139
140
141
# File 'lib/nexpose/common.rb', line 139

def start
  @start
end

#start_monthObject

Returns the value of attribute start_month.



156
157
158
# File 'lib/nexpose/common.rb', line 156

def start_month
  @start_month
end

#timezoneObject

Returns the value of attribute timezone.



157
158
159
# File 'lib/nexpose/common.rb', line 157

def timezone
  @timezone
end

#typeObject

Valid schedule types: daily, hourly, monthly-date, monthly-day, weekly.



135
136
137
# File 'lib/nexpose/common.rb', line 135

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/nexpose/common.rb', line 173

def self.from_hash(hash)
  start = nil
  start = Nexpose::ISO8601.to_time(hash[:start_date]) if hash[:start_date]
  repeat_scan_hash = hash[:repeat_scan]
  if repeat_scan_hash.nil?
    schedule = new('daily', 0, start)
  else
    schedule = new(repeat_scan_hash[:type], repeat_scan_hash[:interval], start)
  end
  schedule.enabled = hash[:enabled].nil? ? true : hash[:enabled]
  schedule.scan_template_id = hash[:scan_template_id]
  schedule.start = Nexpose::ISO8601.to_time(hash[:start_date]) if hash[:start_date]
  schedule.max_duration = hash[:maximum_scan_duration] if hash[:maximum_scan_duration]
  schedule.not_valid_after = Nexpose::ISO8601.to_time(hash[:not_valid_after_date]) if hash[:not_valid_after_date]
  schedule.timezone = hash[:time_zone] if hash[:time_zone]
  schedule.next_run_time = hash[:next_run_time] if hash[:next_run_time]

  unless repeat_scan_hash.nil?
    schedule.type = repeat_scan_hash[:type]
    schedule.interval = repeat_scan_hash[:interval]
    schedule.repeater_type = 'restart' if repeat_scan_hash[:on_repeat] == 'restart-scan'
    schedule.repeater_type = 'continue' if repeat_scan_hash[:on_repeat] == 'resume-scan'

    schedule.is_extended = repeat_scan_hash[:is_extended] if repeat_scan_hash[:is_extended]
    schedule.hour = repeat_scan_hash[:hour] if repeat_scan_hash[:hour]
    schedule.minute = repeat_scan_hash[:minute] if repeat_scan_hash[:minute]
    schedule.date = repeat_scan_hash[:date] if repeat_scan_hash[:date]
    schedule.day = repeat_scan_hash[:day] if repeat_scan_hash[:day]
    schedule.occurrence = repeat_scan_hash[:occurrence] if repeat_scan_hash[:occurrence]
    schedule.start_month = repeat_scan_hash[:start_month] if repeat_scan_hash[:start_month]
  end

  schedule
end

.parse(xml) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/nexpose/common.rb', line 268

def self.parse(xml)
  schedule = Schedule.new(xml.attributes['type'],
                          xml.attributes['interval'].to_i,
                          xml.attributes['start'],
                          xml.attributes['enabled'] != '0')

  # Optional parameters.
  schedule.max_duration = xml.attributes['maxDuration'].to_i if xml.attributes['maxDuration']
  schedule.not_valid_after = xml.attributes['notValidAfter'] if xml.attributes['notValidAfter']
  schedule.incremental = (xml.attributes['incremental'] && xml.attributes['incremental'] == '1')
  schedule.repeater_type = xml.attributes['repeaterType'] if xml.attributes['repeaterType']
  schedule.is_extended = xml.attributes['is_extended'] if xml.attributes['is_extended']
  schedule.hour = xml.attributes['hour'] if xml.attributes['hour']
  schedule.minute = xml.attributes['minute'] if xml.attributes['minute']
  schedule.date = xml.attributes['date'] if xml.attributes['date']
  schedule.day = xml.attributes['day'] if xml.attributes['day']
  schedule.occurrence = xml.attributes['occurrence'] if xml.attributes['occurrence']
  schedule.start_month = xml.attributes['start_month'] if xml.attributes['start_month']
  schedule.timezone = xml.attributes['timezone'] if xml.attributes['timezone']
  schedule.next_run_time = xml.attributes['next_run_time'] if xml.attributes['next_run_time']
  schedule.scan_template_id = xml.attributes['template'] if xml.attributes['template']
  schedule
end

Instance Method Details

#as_xmlObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/nexpose/common.rb', line 242

def as_xml
  xml = REXML::Element.new('Schedule')
  xml.attributes['enabled'] = @enabled ? 1 : 0
  xml.attributes['type'] = @type
  xml.attributes['interval'] = @interval
  xml.attributes['start'] = @start if @start
  xml.attributes['maxDuration'] = @max_duration if @max_duration
  xml.attributes['notValidAfter'] = @not_valid_after if @not_valid_after
  xml.attributes['incremental'] = @incremental ? 1 : 0 if @incremental
  xml.attributes['repeaterType'] = @repeater_type if @repeater_type
  xml.attributes['is_extended'] = @is_extended if @is_extended
  xml.attributes['hour'] = @hour if @hour
  xml.attributes['minute'] = @minute if @minute
  xml.attributes['date'] = @date if @date
  xml.attributes['day'] = @day if @day
  xml.attributes['occurrence'] = @occurrence if @occurrence
  xml.attributes['start_month'] = @start_month if @start_month
  xml.attributes['timezone'] = @timezone if @timezone
  xml.attributes['template'] = @scan_template_id if @scan_template_id
  xml
end

#to_hObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/nexpose/common.rb', line 208

def to_h
  schedule_hash = {
    enabled: @enabled,
    scan_template_id: @scan_template_id,
    maximum_scan_duration: @max_duration
  }
  schedule_hash[:start_date] = Nexpose::ISO8601.to_string(@start) if @start
  schedule_hash[:not_valid_after_date] = Nexpose::ISO8601.to_string(@not_valid_after) if @not_valid_after
  schedule_hash[:time_zone] = @timezone if @timezone

  unless (@type.nil? || @interval == 0) && !@is_extended
    repeat_scan_hash = {
      type: @type,
      interval: @interval
    }
    repeat_scan_hash[:on_repeat] = 'restart-scan' if @repeater_type == 'restart'
    repeat_scan_hash[:on_repeat] = 'resume-scan' if @repeater_type == 'continue'

    if @is_extended
      repeat_scan_hash[:is_extended] = @is_extended
      repeat_scan_hash[:hour] = @hour if @hour
      repeat_scan_hash[:minute] = @minute if @minute
      repeat_scan_hash[:date] = @date if @date
      repeat_scan_hash[:day] = @day if @day
      repeat_scan_hash[:occurrence] = @occurrence if @occurrence
      repeat_scan_hash[:start_month] = @start_month if @start_month
    end

    schedule_hash[:repeat_scan] = repeat_scan_hash
  end

  schedule_hash
end

#to_xmlObject



264
265
266
# File 'lib/nexpose/common.rb', line 264

def to_xml
  as_xml.to_s
end