简体   繁体   中英

How can I get tags for all tapes in AWS Storage Gateway?

In the past you used to be able to view tags for tapes in the AWS Storage gateway on the web console, but it seems AWS has removed that option now. The only way to view tags is to individually select each tape and click on the tags tab.

I have over 100 tapes and doing this manually would be rather time consuming. Is there a command I can output a list of every tape barcode and it's associated tag?

Alternatively, we only use about 10 or so tags across all these tapes (one tag per tape) and I've tried using a Python/boto3 script (I'm not python guru though) to output all the tapes of a particular tag, sadly the below script isn't working entirely. It will output some tapes but not everything. Example if I search for the tag "Warehouse" it will return about 8 tapes - which didn't sound right. So looking manually in the AWS console I've already manually found 3-4 tapes with this tag and was NOT returned in the list using this code:

#Import the boto3 library
import boto3
 
#Create the tape gateway client
tgw_client = boto3.client('storagegateway')
 
#Create a list of the tapes
tapes = tgw_client.list_tapes()['TapeInfos']
 
#Create a for loop to process each tape in the tapes list
for tape in tapes:
    #Set the TapeARN variable
    tape_arn = tape['TapeARN']
    #Describe the tags for the tape using the tape ARN
    tape_tag = tgw_client.list_tags_for_resource(ResourceARN = tape_arn)
    #Exception handing
    try:
        #Check to see if the first tag value matches job001
        if tape_tag['Tags'][0]['Value'] == 'Warehouse':
            #If the tag value matches job001, then print the list_tags_for_resource response for that tape
            print(tape_tag)
        #Ignore tapes which do not have the job001 tag
        else:
            pass
    except:
        pass

Any help in getting an output of tapes and tags or how to fix this python script would be GREATLY appreciated!

The script is not returning all of the results because the account has 206 tape instances, and the list_tapes method returns a max of 100 by default. To handle this and pull the complete list, we need to the evaluate the list_tapes response and use the Marker element to make a subsequent request to retrieve the next set(s) of tapes.

Below is a reference script for listing all Lambda functions that can be easily updated to pull the tape instances from Storage Gateway. Just update the client and response mapping, and then add your own logic for evaluating.

import boto3
    
def list_functions(marker):
    try:
        client = boto3.client('lambda')
        if marker is not None:
            print("Retrieving next page...")
            response = client.list_functions(
            Marker=marker,
            )
        else:
            print("Retrieving first page..")
            response = client.list_functions(
            )
        return response
    except Exception as e: print(e)

def runtime():
    try:
        flist = []
        marker = None
        data = list_functions(marker)
        if 'NextMarker' in data:
            marker = data['NextMarker']
        else:
            marker = None
        for i in data['Functions']:
            flist.append(i['FunctionName'])
        
        while marker is not None:
            if '==' in marker:
                data = list_functions(marker)
                if 'NextMarker' in data:
                    marker = data['NextMarker']
                else:
                    marker = None

                for i in data['Functions']:
                    flist.append(i['FunctionName'])
            else:
                marker = None

        print(flist)
    except Exception as e: print(e)

runtime()

Boto3 documentation for reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/storagegateway.html#StorageGateway.Client.list_tapes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM