Reputation: 213
As part of an automation, I am trying to create a temporary access key for the service account, do changes and then delete this last used key.
Where I am stuck is listing and doing something with this result:
gcloud iam service-accounts keys list [email protected] --format=json
This command will list randomly all the available keys for this service account and I haven't found a good formatting to help me.
Tried using the date value ( year-month-day ) with and without a wildcard *, but it doesn't recognise it.
> gcloud iam service-accounts keys list [email protected]
> --format="value(validAfterTime[2021-07-11])"
Logically I would want to probably compare the values inside validAfterTime for each of the keys and then get the highest one, but I do not know how to do that programatically in shell. In python I would compare the values and get the name of the one with the biggest value.
What would be a good simple way to get the last one created so I can delete it?
Ok, so it was simpler than I thought. What I needed to do is to also add the flag sort-by and there I will put the same validAfterTime.
> gcloud iam service-accounts keys list [email protected]
> --format="value(validAfterTime[2021-07-11])" --sort-by=validAfterTime | tail -1 | awk -F/ '{print $6}'
Sort by will sort in ascending order the values, tail -1 will give me the last result, print 6 will give me the 6th element using / as a delimiter.
Thank you
Upvotes: 0
Views: 722
Reputation: 889
The command you should be using looks like this:
gcloud iam service-accounts keys list [email protected]
--format=json --sort-by=~validAfterTime --limit=1
Based on Google Cloud SDK Reference the --sort-by do:
Comma-separated list of resource field key names to sort by. The default order is ascending. Prefix a field with ``~´´ for descending order on that field
So you can use sort-by
to get the last entry on the SA Keys based on validAfterTime
field. And you can use --limit to limit the number of entries you will get.
Upvotes: 1