Working With Tags in FlashArray using PowerShell
Introduction
Purity is the operating environment that runs Pure Storage products like FlashArray and Cloud Block Store. Starting in Purity 6.0, you can assign tags to objects. This post shows you how to perform some basic tagging operations for volumes.
What’s a Tag and Why Do I Care?
A tag is a key/value pair that can be attached to an object in FlashArray, like a volume or a snapshot. Using tags enables you to attach additional metadata to objects for classification, sorting, and searching. For example, you can assign a tag to a collection of volumes and then come along later and retrieve a listing of volumes that match a particular key or value. You can use tags to add application context to resources inside FlashArray. Specifically, in the examples in this blog post, I want to tag volumes with the names SQL Server Instances.
For a deeper dive into Purity Tags, please see the user guide here
What do I need to get started?
Your FlashArray needs to be on Purity 6.0 or better. This post will focus on operations using the PureStoragePowerShellSDK2
PowerShell module. You can find detailed information about the PowerShell module here, and the GitHub repository is here. You can install the module with the command:
Install-Module PureStoragePowerShellSDK2
Using Tags and the PureStoragePowerShellSDK2 Module
Let’s start by getting a listing of the cmdlets in the module associated with tags.
Get-Command -Module PureStoragePowerShellSDK2 | Where-Object { $_.Name -like "*VolumeTag*"}
We can use three cmdlets to get, set, and remove tags on objects. These are the core cmdlets we will focus on in this post.
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Pfa2VolumeTag 2.16.12.0 PureStoragePowerShellSDK2
Cmdlet Remove-Pfa2VolumeTag 2.16.12.0 PureStoragePowerShellSDK2
Cmdlet Set-Pfa2VolumeTagBatch 2.16.12.0 PureStoragePowerShellSDK2
Adding Tags to Volumes
So let’s get started working with tags. In the code below, we will add tags to a set of volumes. The tag key is SqlInstance
. Further, I’m going to add a tag value that will indicate exactly which instance a volume is associated with, in our examples, that is aen-sql-01
and aen-sql-02
, which are two SQL Servers in my lab environment. With this, I can come along later and retrieve a listing of volumes based on the tag’s key or value.
The first thing we will need to do is to connect to the FlashArray.
$FlashArray = Connect-Pfa2Array –EndPoint <<FlashArrary IP or DNS Name>> -Credential $Credential -IgnoreCertificateError
Next, let’s populate some lists with sets of volume names. In the listing below, I’m creating two lists. The first is a set of volumes for a SQL Server instance named aen-sql-01
. The second listing is a set of volumes for another SQL Server instance, aen-sql-02
.
$VolumesSql1 = @('vvol-aen-sql-01-8b810cff-vg/Data-1040ec00',
'vvol-aen-sql-01-8b810cff-vg/Data-25d897a7')
$VolumesSql2 = @('vvol-aen-sql-02-956b082c-vg/Data-3630056e',
'vvol-aen-sql-02-956b082c-vg/Data-51d5655c')
Next, let’s create variables that we’ll use in our scripts. $TagNamespace
is set to OpenMetricsExporter. A namespace is like a folder, a way to classify a subset of tags. The default namespace is the default
. We’re using the namespace OpenMetricsExporter
. $TagKey
and set it to the string SqlInstance
. Then we’re creating two variables, $TagValueSql1
and $TagValueSql2
used as the tag’s values, one for aen-sql-01
and one for aen-sql-02
.
$TagNamespace = 'OpenMetricsExporter'
$TagKey = 'SqlInstance'
$TagValueSql1 = 'aen-sql-01'
$TagValueSql2 = 'aen-sql-02'
Now, let’s bring all that together and assign the volumes some tags. In the code below, several things are happening. Using the cmdlet Set-Pfa2VolumeTagBatch
, the Array
parameter is FlashArray this code is executed on. The TagNamespace
parameter defines a namespace. If a namespace doesn’t exist yet, it’s created for you automatically. Next, ResourceNames
takes one or more resource names, in our case, $VolumesSql1
, a set of volumes in the array. Then next is TagKey
, which is the key we’re setting in the tag. In our case, it’s a string variable $TagKey
, which has a value of SqlInstance
. Then finally, the parameter TagValue
, which in our example here, indicates the SQL Instance associated with the volumes.
Set-Pfa2VolumeTagBatch -Array $FlashArray `-TagNamespace $TagNamespace -ResourceNames $VolumesSql1 -TagKey $TagKey -TagValue $TagValueSql1
Once we run this code the volumes in the list $VolumesSql1
are tagged with the key $TagKey
and the value of $TagValueSql1
. When complete, it will write the console the following output.
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='0ccd349c-3218-94a5-db70-e076e58459b1'; Name='vvol-aen-sql-01-8b810cff-vg/Data-1040ec00'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='64716180-0a2f-e4ff-f9e9-186fae414059'; Name='vvol-aen-sql-01-8b810cff-vg/Data-25d897a7'}
Value : aen-sql-01
Let’s walk through the output above…
Copyable
- indicates if the tags are copied when a volume is cloned. This is the default, you can disable this when you assign the tag. By setting the parameterTagCopyable
to$false
.Key
- This is the key. In our tagging scheme, we’re adding application context to volumes. This key indicates that the volume tagged is associated with a SQL Server Instance.Namespace
- is like a folder, a way to classify a subset of tags. The default namespace is thedefault
. We’re using the namespaceOpenMetricsExporter
.Resource
- a unique identifier for the resource in the array. You see theResource.Id
andResource.Name
in the output.Value
- This is the value of the tag. In our tagging scheme, this indicates that this volume is associated with a specific SQL Server Instance, and in this output that’saen-sql-01
.
Now for demonstration purposes, let’s add some tags to another set of volumes for a second SQL Server Instance, aen-sql-02
. Here’s that code and its output.
Set-Pfa2VolumeTagBatch -Array $FlashArray -TagNamespace $TagNamespace -ResourceNames $VolumesSql2 -TagKey $TagKey -TagValue $TagValueSql2
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='f24669e3-9e5a-f942-ad9a-d05a82d8154b'; Name='vvol-aen-sql-02-956b082c-vg/Data-3630056e'}
Value : aen-sql-02
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='df241154-8396-d898-841f-126653f762b3'; Name='vvol-aen-sql-02-956b082c-vg/Data-51d5655c'}
Value : aen-sql-02
Ok, now that we have a set of tags applied in our array. Let’s perform some operations using tags.
Getting the Tags Associated with a Single Volume
Let’s start with a simple example: get the tags associated with a specific volume. The value for ResourceNames
came from the output above when I first tagged this volume.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -ResourceNames "vvol-aen-sql-01-8b810cff-vg/Data-25d897a7"
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='64716180-0a2f-e4ff-f9e9-186fae414059'; Name='vvol-aen-sql-01-8b810cff-vg/Data-25d897a7'}
Value : aen-sql-01
Getting All the Tagged Volumes in a Namespace
Next, let’s list all the tagged resources in a namespace. We can use Get-Pfa2VolumeTag
and specify the Array
and Namespaces
parameters. This returns all of the tagged resources in the specified namespace, which in our example here is OpenMetricsExporter
. In the output, you can see all of the tagged resources which is all four of the volumes we’ve tagged.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='0ccd349c-3218-94a5-db70-e076e58459b1'; Name='vvol-aen-sql-01-8b810cff-vg/Data-1040ec00'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='64716180-0a2f-e4ff-f9e9-186fae414059'; Name='vvol-aen-sql-01-8b810cff-vg/Data-25d897a7'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='f24669e3-9e5a-f942-ad9a-d05a82d8154b'; Name='vvol-aen-sql-02-956b082c-vg/Data-3630056e'}
Value : aen-sql-02
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='df241154-8396-d898-841f-126653f762b3'; Name='vvol-aen-sql-02-956b082c-vg/Data-51d5655c'}
Value : aen-sql-02
Getting all of the Volumes in a Namespace with a Specific Tag Key
Next, let’s list all the Volumes in a Namespace with a Key of SqlInstance
. In the output below, this operation returns all of the Volumes in our Namespace since they all have a Key of SqlInstance
.
In our tagging scheme, this is all of the Volumes associated with SQL Server Instances. We can search for strings in a tag using the Filter
parameter. So here we have "Key='SqlInstance'"
which searches for exact, case-sensitive matches of that string. Also, note how the parameter is quoted.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -Filter "Key='SqlInstance'"
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='0ccd349c-3218-94a5-db70-e076e58459b1'; Name='vvol-aen-sql-01-8b810cff-vg/Data-1040ec00'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='64716180-0a2f-e4ff-f9e9-186fae414059'; Name='vvol-aen-sql-01-8b810cff-vg/Data-25d897a7'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='f24669e3-9e5a-f942-ad9a-d05a82d8154b'; Name='vvol-aen-sql-02-956b082c-vg/Data-3630056e'}
Value : aen-sql-02
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='df241154-8396-d898-841f-126653f762b3'; Name='vvol-aen-sql-02-956b082c-vg/Data-51d5655c'}
Value : aen-sql-02
Getting all of the Volumes in a Namespace with a Specific Tag Value
Next, let’s get a listing of all the resources whose tag has a value of aen-sql-02
. Here we’re using the Filter
parameter again. This time the search string is "Value='aen-sql-02'"
. It returns the tagged volumes with an exact case-sensitive string match to the Filter
string. In the output below, you can see just the volumes tagged with a value of aen-sql-02
are returned.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -Filter "Value='aen-sql-02'"
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='f24669e3-9e5a-f942-ad9a-d05a82d8154b'; Name='vvol-aen-sql-02-956b082c-vg/Data-3630056e'}
Value : aen-sql-02
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='df241154-8396-d898-841f-126653f762b3'; Name='vvol-aen-sql-02-956b082c-vg/Data-51d5655c'}
Value : aen-sql-02
Getting all of the Volumes in a Namespace with a Filter Wildcard.
Now, let’s get a listing of the volumes whose tag has a value of aen-sql-0*
. This will match on aen-sql-01
and aen-sql-02
. You can do this by using the Filter
parameter again and then add the search string "Value='aen-sql-0*'"
Not the use of *
as a wildcard search. You can also perform the same operation on the Key and Namespace. You can also build more complex Filter
queries. Check out the User Guide here for more examples.
In the output below, we’re using a wildcard Filter
, which returns all tagged volumes.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -Filter "Value='aen-sql-0*'"
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='0ccd349c-3218-94a5-db70-e076e58459b1'; Name='vvol-aen-sql-01-8b810cff-vg/Data-1040ec00'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='64716180-0a2f-e4ff-f9e9-186fae414059'; Name='vvol-aen-sql-01-8b810cff-vg/Data-25d897a7'}
Value : aen-sql-01
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='f24669e3-9e5a-f942-ad9a-d05a82d8154b'; Name='vvol-aen-sql-02-956b082c-vg/Data-3630056e'}
Value : aen-sql-02
Copyable : True
Key : SqlInstance
Namespace : OpenMetricsExporter
Resource : @{Id='df241154-8396-d898-841f-126653f762b3'; Name='vvol-aen-sql-02-956b082c-vg/Data-51d5655c'}
Value : aen-sql-02
Deleting a Tag on a Volume
Ok, now this has been fun and all. But let’s say you need to clean up some tags. You can remove a specific tag from a volume with the code below. If successful, no output is written to the console, and the tag is removed.
Remove-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -Keys $TagKey -ResourceNames "vvol-aen-sql-01-8b810cff-vg/Data-25d897a7"
Remove All Tags for a Specific Key in a Namespace
But do you want to delete each tag by hand? Nope. So let’s combine our search capabilities and delete all of the tags in a specific namespace for a specific key. And here’s the code for that…let’s use Get-Pfa2VolumeTag
get the tags in our namespace and a Filter
of "Key='SqlInstance'"
to get all of the tags in our namespace that match that filter, which is everything we have tagged in this post.
$Tags = Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -Filter "Key='SqlInstance'"
Then we’ll iterate over that set with a loop and call Remove-Pfa2VolumeTag
for each of the ResourceNames
in the set, deleting the specific Keys
.
foreach($tag in $tags){
Remove-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace -ResourceNames $tag.Resource.Name -Keys $tag.Key
}
Then once we’re done, if we get a listing of all the tagged resources in our namespace, this should return no data.
Get-Pfa2VolumeTag -Array $FlashArray -Namespaces $TagNamespace
Wrapping Up
In this post, I showed you how to create tags for resources in FlashArray, specifically volumes. You also learned to query tagged resources by namespace, key, and value. Using these techniques, you can add application context to resources in your array or build more complex automation schemes without discretely addressing each resource. I plan on using tags to help with the OpenMetrics Exporter projects. So watch this space.