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)


172
173
174
175
176
177
178
# File 'lib/nexpose/common.rb', line 172

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

#console_startObject

Starting time of the scheduled scan (in ISO 8601 format). Relative to the console timezone



167
168
169
# File 'lib/nexpose/common.rb', line 167

def console_start
  @console_start
end

#console_timezoneObject

The timezone of the console.



169
170
171
# File 'lib/nexpose/common.rb', line 169

def console_timezone
  @console_timezone
end

#dateObject

Returns the value of attribute date.



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

def date
  @date
end

#dayObject

Returns the value of attribute day.



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

def day
  @day
end

#enabledObject

Whether or not this schedule is enabled.



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

def enabled
  @enabled
end

#hourObject

Returns the value of attribute hour.



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

def hour
  @hour
end

#intervalObject

The repeat interval based upon type.



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

def interval
  @interval
end

#is_extendedObject

Extended attributes added with the new scheduler implementation



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

def is_extended
  @is_extended
end

#max_durationObject

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



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

def max_duration
  @max_duration
end

#minuteObject

Returns the value of attribute minute.



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

def minute
  @minute
end

#next_run_timeObject

Returns the value of attribute next_run_time.



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

def next_run_time
  @next_run_time
end

#not_valid_afterObject

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



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

def not_valid_after
  @not_valid_after
end

#occurrenceObject

Returns the value of attribute occurrence.



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

def occurrence
  @occurrence
end

#repeater_typeObject

scan-schedule attributes



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

def repeater_type
  @repeater_type
end

#scan_template_idObject

Scan template to use when starting a scan job.



165
166
167
# File 'lib/nexpose/common.rb', line 165

def scan_template_id
  @scan_template_id
end

#startObject

Starting time of the scheduled scan (in ISO 8601 format).



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

def start
  @start
end

#start_monthObject

Returns the value of attribute start_month.



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

def start_month
  @start_month
end

#timezoneObject

Timezone in which start time run. If not set will default to console timezone. If console timezone is not supported it defaults to utc.



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

def timezone
  @timezone
end

#typeObject

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



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

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Object



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
207
208
209
210
211
212
213
214
215
# File 'lib/nexpose/common.rb', line 180

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]
  schedule.console_start = Nexpose::ISO8601.to_time(hash[:console_start_date]) if hash[:console_start_date]
  schedule.console_timezone = hash[:console_time_zone] if hash[:console_time_zone]

  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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/nexpose/common.rb', line 276

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.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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/nexpose/common.rb', line 251

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['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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/nexpose/common.rb', line 217

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



272
273
274
# File 'lib/nexpose/common.rb', line 272

def to_xml
  as_xml.to_s
end