Class: OpenvasCli::VasSchedule

Inherits:
VasBase
  • Object
show all
Defined in:
lib/openvas-cli/vas_schedule.rb

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#destroy, #destroy!, get_by_id, #initialize, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

This class inherits a constructor from OpenvasCli::VasBase

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



7
8
9
# File 'lib/openvas-cli/vas_schedule.rb', line 7

def comment
  @comment
end

#durationObject

Returns the value of attribute duration.



11
12
13
# File 'lib/openvas-cli/vas_schedule.rb', line 11

def duration
  @duration
end

#first_timeObject

Returns the value of attribute first_time.



8
9
10
# File 'lib/openvas-cli/vas_schedule.rb', line 8

def first_time
  @first_time
end

#in_useObject

Returns the value of attribute in_use.



12
13
14
# File 'lib/openvas-cli/vas_schedule.rb', line 12

def in_use
  @in_use
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/openvas-cli/vas_schedule.rb', line 6

def name
  @name
end

#next_timeObject

Returns the value of attribute next_time.



9
10
11
# File 'lib/openvas-cli/vas_schedule.rb', line 9

def next_time
  @next_time
end

#periodObject

Returns the value of attribute period.



10
11
12
# File 'lib/openvas-cli/vas_schedule.rb', line 10

def period
  @period
end

#task_idsObject

Returns the value of attribute task_ids.



13
14
15
# File 'lib/openvas-cli/vas_schedule.rb', line 13

def task_ids
  @task_ids
end

Class Method Details

.get_all(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/openvas-cli/vas_schedule.rb', line 112

def self.get_all(options = {})
  
  manual_sort = false
  params = {:details => '1'}
  params[:schedule_id] = options[:id] if options[:id]
  case options[:sort_by]
  when :schedule_id
    params[:sort_field] = 'id'
  when :next_time
    manual_sort = true
  when :first_time
    params[:sort_field] = 'first_time'
  else
    params[:sort_field] = 'name'
  end
  
  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_schedules(params)
  }
  ret = []
  
  begin
    schedules = connection.send_receive(req.doc)
    schedules.xpath("//schedule").each { |s|
      sch               = VasSchedule.new
      sch.id            = extract_value_from("@id", s)
      sch.name          = extract_value_from("name", s)
      sch.comment       = extract_value_from("comment", s)
      t_time            = extract_value_from("first_time", s)
      sch.first_time    = Time.parse(t_time) unless t_time == ""
      t_time            = extract_value_from("next_time", s)
      sch.next_time     = Time.parse(t_time) unless t_time == "" or t_time == "done"

      period_num = extract_value_from("period", s).to_i
      if period_num > 0
        sch.period = VasPeriod.from_seconds(period_num)
      end
      
      period_num = extract_value_from("period_months", s).to_i
      if period_num > 0
        sch.period = VasPeriod.from_months(period_num)
      end
      
      t_time            = extract_value_from("duration", s)
      unless t_time == ""
        sch.duration = t_time.to_i unless t_time == 0
      end

      sch.in_use        = extract_value_from("in_use", s).to_i > 0
      sch.task_ids         = []
      s.xpath('tasks/task/@id') { |t|
        sch.task_ids << t.value
      }
      ret << sch
    }
   
    if manual_sort
      if options[:sort_by] == :next_time
        ret.sort!{ |a,b| a.next_time <=> b.next_time }
      end
    end
  rescue VasExceptions::CommandException => e
    raise e unless e.message =~ /Failed to find schedule/i
  end    
  ret
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/openvas-cli/vas_schedule.rb', line 21

def changed?
  local_changes = false
  local_changes = @period.changed? if @period
  
  local_changes || super
end

#create_or_updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openvas-cli/vas_schedule.rb', line 68

def create_or_update
  return unless changed? || @id == nil
  
  if @id
    return unless destroy
  end
  
  req = Nokogiri::XML::Builder.new { |xml|
    xml.create_schedule {
      xml.name    { xml.text(@name) }
      xml.comment { xml.text(@comment) } if @comment
      xml.first_time {
        xml.minute       { xml.text(@first_time.min) }
        xml.hour         { xml.text(@first_time.hour) }
        xml.day_of_month { xml.text(@first_time.day) }
        xml.month        { xml.text(@first_time.month) }
        xml.year         { xml.text(@first_time.year) }
      }
      xml.duration      { xml.text(@duration ? @duration : 0) }
      xml.period { 
        if @period
          xml.text(@period.number)
          xml.unit { xml.text(@period.period.to_s) }
        else
          xml.text(0)
          xml.unit { xml.text("second") }
        end
      }
    }
  }
  
  begin
    resp = VasSchedule.connection.send_receive(req.doc)
    @id = VasSchedule.extract_value_from("/create_schedule_response/@id", resp) unless @id
    reset_changes
  
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end
end

#delete_recordObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/openvas-cli/vas_schedule.rb', line 48

def delete_record
  return unless @id
  
  req = Nokogiri::XML::Builder.new { |xml|
    xml.delete_schedule(:schedule_id => @id)
  }
  
  begin
    VasSchedule.connection.send_receive(req.doc)
    @id = nil
    reset_changes
    
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end
end