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
Constant Summary collapse
- DEFAULT_COLOR =
'#F6F6F6'
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
-
.load(connection, tag_id) ⇒ Tag
Retrieve detailed description of a single tag.
- .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_json ⇒ Object
Methods inherited from TagSummary
Constructor Details
#initialize(name, type, id = -1)) ⇒ Tag
Returns a new instance of Tag.
199 200 201 202 203 |
# File 'lib/nexpose/tag.rb', line 199 def initialize(name, type, id = -1) @name, @type, @id = name, type, id @source = 'nexpose-client' @color = @type == Type::Generic::CUSTOM ? DEFAULT_COLOR : nil end |
Instance Attribute Details
#asset_group_ids ⇒ Object Also known as: group_ids
Array containing Asset Group IDs to be associated with tag
192 193 194 |
# File 'lib/nexpose/tag.rb', line 192 def asset_group_ids @asset_group_ids end |
#asset_ids ⇒ Object
Array containing Asset IDs to be associated with tag
186 187 188 |
# File 'lib/nexpose/tag.rb', line 186 def asset_ids @asset_ids end |
#associated_asset_ids ⇒ Object
Array containing Asset IDs directly associated with the tag
189 190 191 |
# File 'lib/nexpose/tag.rb', line 189 def associated_asset_ids @associated_asset_ids end |
#color ⇒ Object
HEX color code of tag
177 178 179 |
# File 'lib/nexpose/tag.rb', line 177 def color @color end |
#risk_modifier ⇒ Object
Risk modifier
180 181 182 |
# File 'lib/nexpose/tag.rb', line 180 def risk_modifier @risk_modifier end |
#search_criteria ⇒ Object
A TagCriteria
197 198 199 |
# File 'lib/nexpose/tag.rb', line 197 def search_criteria @search_criteria end |
#site_ids ⇒ Object
Array containing Site IDs to be associated with tag
183 184 185 |
# File 'lib/nexpose/tag.rb', line 183 def site_ids @site_ids end |
#source ⇒ Object
Creation source
174 175 176 |
# File 'lib/nexpose/tag.rb', line 174 def source @source end |
Class Method Details
.load(connection, tag_id) ⇒ Tag
Retrieve detailed description of a single tag
227 228 229 230 |
# File 'lib/nexpose/tag.rb', line 227 def self.load(connection, tag_id) json = JSON.parse(AJAX.get(connection, "/api/2.0/tags/#{tag_id}")) Tag.parse(json) end |
.parse(json) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/nexpose/tag.rb', line 261 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
290 291 292 293 294 |
# File 'lib/nexpose/tag.rb', line 290 def add_to_asset(connection, asset_id) params = _to_json_for_add uri = AJAX.post(connection, "/api/2.0/assets/#{asset_id}/tags", 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
314 315 316 317 318 |
# File 'lib/nexpose/tag.rb', line 314 def add_to_group(connection, group_id) params = _to_json_for_add uri = AJAX.post(connection, "/api/2.0/asset_groups/#{group_id}/tags", 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
302 303 304 305 306 |
# File 'lib/nexpose/tag.rb', line 302 def add_to_site(connection, site_id) params = _to_json_for_add uri = AJAX.post(connection, "/api/2.0/sites/#{site_id}/tags", params, AJAX::CONTENT_TYPE::JSON) @id = uri.split('/').last.to_i end |
#delete(connection) ⇒ Object
Delete this tag from Nexpose console
257 258 259 |
# File 'lib/nexpose/tag.rb', line 257 def delete(connection) connection.delete_tag(@id) end |
#save(connection) ⇒ Fixnum
Creates and saves a tag to Nexpose console
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/nexpose/tag.rb', line 210 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_json ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/nexpose/tag.rb', line 232 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_map : nil } } if @type == Type::Generic::CUSTOM json['attributes'] << { 'tag_attribute_name' => 'COLOR', 'tag_attribute_value' => @color } end JSON.generate(json) end |