Skip to content
Gavin Schneider edited this page Mar 1, 2016 · 5 revisions

Here's a walk-through of how to use Tags, as of version 0.7.0 of the gem.

In order to keep verbosity to a minimum, all of the examples assume that the Nexpose module has been included:

include Nexpose

As with all uses of the gem, in order to interact with a Nexpose console you will need an active, valid connection. The following line can be assumed for all code below:

nsc = Connection.new('10.2.0.1', 'nxadmin', 'secret-password')
nsc.login

Create a new tag

There are four different types of tags: Custom, Owner, Location, and Criticality. In this example we create a Custom tag.

tag = Tag.new('Tag Name', 'CUSTOM')
id = tag.save(nsc)
puts "New tag saved with ID: #{id}."

Get a listing of all tags

tags = nsc.list_tags
tags.each do |tag| 
  puts "Tag ID: #{tag.id}"
  puts "Tag name: #{tag.name}"
  puts "Tag type: #{tag.type}"
  puts "Associated asset IDs: #{tag.asset_ids}"
end

Add tag to an existing site

Adding a tag to a site applies that tag to all of the assets in the site. In this example we add a Medium Criticality tag to a site named 'My Site'

tag_id = nsc.list_tags.find { |t| t.name == Tag::Type::Level.const_get('Medium') }.id
tag = Tag.load(nsc, tag_id)
site = nsc.sites.find { |s| s.name == 'My Site' }
tag.add_to_site(nsc, site.id)
puts "New tag with ID #{tag_id} added to Site #{site.name}."

Add tag to an existing asset group

Adding a tag to an asset group applies that tag to all of the assets in the group. In this example we add a High Criticality tag to an asset group named 'My Group'.

tag_id = nsc.list_tags.find { |t| t.name == Tag::Type::Level.const_get('High') }.id
tag = Tag.load(nsc, tag_id)
group = nsc.groups.find { |g| g.name == 'My Group' } 
tag.add_to_group(nsc, group.id)
puts "New tag with ID #{tag_id} added to Asset Group #{group.name}."