How do I get the ID from a snapshot during creation via API?

March 18, 2017 983 views
API

When I create a snapshot via API it returns an ID which I then store in my database. But when I list my snapshots later they all have different ID's, is the snapshot ID changing over time or does the API return the wrong ID for some reason? are there any workarounds to this problem?

2 comments
1 Answer
xMudrii March 18, 2017
Accepted Answer

Hi @chadlwilliams,
As @hansen already said, when you execute Snapshot actions, it returns an action ID, not a snapshot ID.
API docs states it:

Name: id, Type: number, Description: A unique identifier for each Droplet action event. This is used to reference a specific action that was requested.

It's not wrong ID, it's just an action ID which you can use to 'trace' snapshot execution.
Until creation is not complete, you will not have snapshot ID.

Action ID alongside Action endpoints is used to check action (in your case snapshot) details. You can learn more about it in API docs: Retrieve a Droplet Action or Retrieve an existing Action.
There is result difference between above two, but Retrieve a Droplet Action requires you to provide Droplet ID while Retrieve an existing Action doesn't.

When you query any of above Action endpoints, you get status of Action as well as other important details.
If creation is in progress, status will be in-progress. Once it completes (successfully), it will be completed

Workaround is to query Action endpoint, with ID you got when executed snapshot request, until progress is not in-progress. Once it is completed, execute Get Snapshot to obtain its ID.
Reason why we check is it not in-progress is we don't know what status will be in case of fail, so you don't risk stuck-ing into infinity loop.
For example, something similar happens in doctl. It is querying Action endpoint every 5 seconds until Actions status is not in-progress. If you are more interested in it, you can see how does actionWait code looks like on GitHub.

Only thing you could pay attention here is rate limit. Rate limit is how much API requests you can execute per time unit. Once remaining rate limit reaches zero, you will not be able to execute API requests until limit restarts. API docs states that it's currently limited to 5,000 requests per hour per token.

In most of cases you are safe, but for example if you are checking status for 10 snapshots in same time every 5 seconds, you can run out of it. If you get in problem like this, just raise time and you will be good to go.

If you get into any problem, feel free to ask anything.
Maybe I went to much into details, but hopefully it will be helpful to you.

  • I guess I missed that detail then, so my workaround to this would therefore be to poll the "action" API call with the ID returned from snapshot creation until it's status says "completed" and then use "resource_id" (I suppose) to obtain the real ID for the new snapshot. Thank you very much :)

    • Just a single correction - Not a resource_id, it represents object ID on which action was executed. In Snapshot case, it will always be Droplet ID which you snapshot, even when you complete it.

      You will have to list all Snapshots once action is completed and pull the snapshot ID there.
      If you want to be sure you pull right one, try comparing date/time of Action to Snapshot Creation one. I can't test it now, but it should work.

      Also, as I said and note it one more time, when working with actions, it's much safer to check inequality to "in-progress" rather then equality to "complete".
      It's not documented what it can return for status. I never had snapshot failure problem and I done many of them. But better be safe than stuck in infinity loops.

      If you have any more questions, feel free to ask. :)

Have another answer? Share your knowledge.