Class: Nexpose::Tag
- Inherits:
-
TagSummary
- Object
- TagSummary
- Nexpose::Tag
- Defined in:
- lib/nexpose/tag.rb,
lib/nexpose/tag/criteria.rb
Overview
Tag object containing tag details
Defined Under Namespace
Modules: Type Classes: Criteria, Criterion
Instance Attribute Summary collapse
-
#asset_group_ids ⇒ Object
(also: #group_ids)
Array containing Asset Group IDs to be associated with tag.
-
#asset_ids ⇒ Object
Array containing Asset IDs to be associated with tag.
-
#associated_asset_ids ⇒ Object
Array containing Asset IDs directly associated with the tag.
-
#color ⇒ Object
HEX color code of tag.
-
#risk_modifier ⇒ Object
Risk modifier.
-
#search_criteria ⇒ Object
A TagCriteria.
-
#site_ids ⇒ Object
Array containing Site IDs to be associated with tag.
-
#source ⇒ Object
Creation source.
Attributes inherited from TagSummary
Class Method Summary collapse
-
.create(hash) ⇒ Object
Create tag object from hash.
-
.load(connection, tag_id) ⇒ Tag
Retrieve detailed description of a single tag.
-
.load_tags(tags) ⇒ Object
Create list of tag objects from hash.
- .parse(json) ⇒ Object
Instance Method Summary collapse
-
#add_to_asset(connection, asset_id) ⇒ Fixnum
Adds a tag to an asset.
-
#add_to_group(connection, group_id) ⇒ Fixnum
(also: #add_to_asset_group)
Adds a tag to an asset group.
-
#add_to_site(connection, site_id) ⇒ Fixnum
Adds a tag to a site.
-
#delete(connection) ⇒ Object
Delete this tag from Nexpose console.
-
#initialize(name, type, id = -1)) ⇒ Tag
constructor
A new instance of Tag.
-
#save(connection) ⇒ Fixnum
Creates and saves a tag to Nexpose console.
- #to_h ⇒ Object
- #to_json ⇒ Object
Methods inherited from TagSummary
Constructor Details
Instance Attribute Details
#asset_group_ids ⇒ Object Also known as: group_ids
Array containing Asset Group IDs to be associated with tag
200 201 202 |
# File 'lib/nexpose/tag.rb', line 200 def asset_group_ids @asset_group_ids end |
#asset_ids ⇒ Object
Array containing Asset IDs to be associated with tag
194 195 196 |
# File 'lib/nexpose/tag.rb', line 194 def asset_ids @asset_ids end |
#associated_asset_ids ⇒ Object
Array containing Asset IDs directly associated with the tag
197 198 199 |
# File 'lib/nexpose/tag.rb', line 197 def associated_asset_ids @associated_asset_ids end |
#color ⇒ Object
HEX color code of tag
185 186 187 |
# File 'lib/nexpose/tag.rb', line 185 def color @color end |
#risk_modifier ⇒ Object
Risk modifier
188 189 190 |
# File 'lib/nexpose/tag.rb', line 188 def risk_modifier @risk_modifier end |
#search_criteria ⇒ Object
A TagCriteria
205 206 207 |
# File 'lib/nexpose/tag.rb', line 205 def search_criteria @search_criteria end |
#site_ids ⇒ Object
Array containing Site IDs to be associated with tag
191 192 193 |
# File 'lib/nexpose/tag.rb', line 191 def site_ids @site_ids end |
#source ⇒ Object
Creation source
182 183 184 |
# File 'lib/nexpose/tag.rb', line 182 def source @source end |
Class Method Details
.create(hash) ⇒ Object
Create tag object from hash
234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/nexpose/tag.rb', line 234 def self.create(hash) attributes = hash[:attributes] color = attributes.find { |attr| attr[:tag_attribute_name] == 'COLOR' } color = color[:tag_attribute_value] if color source = attributes.find { |attr| attr[:tag_attribute_name] == 'SOURCE' } source = source[:tag_attribute_value] if source tag = Tag.new(hash[:tag_name], hash[:tag_type], hash[:tag_id]) tag.color = color tag.source = source tag end |
.load(connection, tag_id) ⇒ Tag
Retrieve detailed description of a single tag
286 287 288 289 |
# File 'lib/nexpose/tag.rb', line 286 def self.load(connection, tag_id) json = JSON.parse(AJAX.get(connection, "/api/2.0/tags/#{tag_id}")) Tag.parse(json) end |
.load_tags(tags) ⇒ Object
Create list of tag objects from hash
224 225 226 227 228 229 230 231 |
# File 'lib/nexpose/tag.rb', line 224 def self.() unless .nil? = .map do |hash| self.create(hash) end end end |
.parse(json) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/nexpose/tag.rb', line 318 def self.parse(json) color = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'COLOR' } color = color['tag_attribute_value'] if color source = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'SOURCE' } source = source['tag_attribute_value'] if source tag = Tag.new(json['tag_name'], json['tag_type'], json['tag_id']) tag.color = color tag.source = source tag.asset_ids = json['asset_ids'] if json['tag_config'] tag.site_ids = json['tag_config']['site_ids'] tag.associated_asset_ids = json['tag_config']['tag_associated_asset_ids'] tag.asset_group_ids = json['tag_config']['asset_group_ids'] criteria = json['tag_config']['search_criteria'] tag.search_criteria = criteria ? Criteria.parse(criteria) : nil end modifier = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'RISK_MODIFIER' } if modifier tag.risk_modifier = modifier['tag_attribute_value'].to_i end tag end |
Instance Method Details
#add_to_asset(connection, asset_id) ⇒ Fixnum
Adds a tag to an asset
347 348 349 350 351 352 |
# File 'lib/nexpose/tag.rb', line 347 def add_to_asset(connection, asset_id) params = to_json_for_add url = "/api/2.0/assets/#{asset_id}/tags" uri = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON) @id = uri.split('/').last.to_i end |
#add_to_group(connection, group_id) ⇒ Fixnum Also known as: add_to_asset_group
Adds a tag to an asset group
373 374 375 376 377 378 |
# File 'lib/nexpose/tag.rb', line 373 def add_to_group(connection, group_id) params = to_json_for_add url = "/api/2.0/asset_groups/#{group_id}/tags" uri = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON) @id = uri.split('/').last.to_i end |
#add_to_site(connection, site_id) ⇒ Fixnum
Adds a tag to a site
360 361 362 363 364 365 |
# File 'lib/nexpose/tag.rb', line 360 def add_to_site(connection, site_id) params = to_json_for_add url = "/api/2.0/sites/#{site_id}/tags" uri = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON) @id = uri.split('/').last.to_i end |
#delete(connection) ⇒ Object
Delete this tag from Nexpose console
314 315 316 |
# File 'lib/nexpose/tag.rb', line 314 def delete(connection) connection.delete_tag(@id) end |
#save(connection) ⇒ Fixnum
Creates and saves a tag to Nexpose console
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/nexpose/tag.rb', line 269 def save(connection) params = to_json if @id == -1 uri = AJAX.post(connection, '/api/2.0/tags', params, AJAX::CONTENT_TYPE::JSON) @id = uri.split('/').last.to_i else AJAX.put(connection, "/api/2.0/tags/#{@id}", params, AJAX::CONTENT_TYPE::JSON) end @id end |
#to_h ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/nexpose/tag.rb', line 246 def to_h { tag_id: id, tag_name: name, tag_type: type, attributes:[ { tag_attribute_name: "COLOR", tag_attribute_value: color }, { tag_attribute_name: "SOURCE", tag_attribute_value: source } ] } end |
#to_json ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/nexpose/tag.rb', line 291 def to_json json = { 'tag_name' => @name, 'tag_type' => @type, 'tag_id' => @id, 'attributes' => [{ 'tag_attribute_name' => 'SOURCE', 'tag_attribute_value' => @source }], 'tag_config' => { 'site_ids' => @site_ids, 'tag_associated_asset_ids' => @associated_asset_ids, 'asset_group_ids' => @asset_group_ids, 'search_criteria' => @search_criteria ? @search_criteria.to_h : nil } } if @type == Type::Generic::CUSTOM json['attributes'] << { 'tag_attribute_name' => 'COLOR', 'tag_attribute_value' => @color } end JSON.generate(json) end |