I'm trying to find an example of reverting a snapshot (I create a single snapshot at start of VM life and then use Revert as a cleansing mechanism for developing software deployment scripts).
I have coded up (in Ansible) the various REST API calls to get the bearer token and machine details but can not find a good example of how to find the snapshot ID's for the machine, which seems to be the required parameter for the REST call
First step is to make a GET to /catalog-service/api/consumer/resources/ to get the list of resources
Then make a GET to /catalog-service/api/consumer/resources/{resourceId} to get more information about the specific resource you want to do something with
Then make a GET to /catalog-service/api/consumer/resources/{resourceId}/actions/ to get the list of available actions and check for revert snapshot existing
Then make a GET to /catalog-service/api/consumer/resources/{resourceId}/actions/{actionId}/requests/template to get the request JSON template to submit the request. Take that in code and modify it as necessary.
Then make a POST to /catalog-service/api/consumer/resources/{resourceId}/actions/{actionId}/requests to make the actual request.
An example from my vRA 7.3 environment, assuming I already know my resource ID = b7ddb8b0-9718-4e3e-92d2-83864dc9848e which is a RHEL7 machine I have made a snapshot on and want to revert to.
First, GET /catalog-service/api/consumer/resources/b7ddb8b0-9718-4e3e-92d2-83864dc9848e/actions/
I receive a huge object back, but I look through and find the revertSnapshot action is action ID = 6a41769f-9239-47d9-a34e-dd355c76c296
So I then make a GET to
/catalog-service/api/consumer/resources/b7ddb8b0-9718-4e3e-92d2-83864dc9848e/actions/6a41769f-9239-47d9-a34e-dd355c76c296/requests/template and get the following JSON template back:
{
"type":"com.vmware.vcac.catalog.domain.request.CatalogResourceRequest",
"resourceId":"b7ddb8b0-9718-4e3e-92d2-83864dc9848e",
"actionId":"6a41769f-9239-47d9-a34e-dd355c76c296",
"description":null,
"data":{
"SnapshotReference":{
"componentId":"076628d6-aad7-4567-a3d1-0545e1950605",
"classId":"Infrastructure.Compute.Machine.Snapshot",
"id":"59",
"label":"Test1"
}
}
}
Then I submit that exactly as is because it has the snapshot I want to revert to already in it.
So I POST to /catalog-service/api/consumer/resources/b7ddb8b0-9718-4e3e-92d2-83864dc9848e/actions/6a41769f-9239-47d9-a34e-dd355c76c296/requests/ with the template received above and I go look in the vRA portal and I have a request submitted for me to revert that machine to the snapshot I want.
If memory serves, the action ID's don't change from resource to resource and possibly not from server to server. So once you know that, you should be able to infer it from any resource ID. The catch is still checking to see if it is an available action based on permissions or whether it already has a snapshot.
Let me know if that helps I will try to clarify if I can.
Based on some existing bash code I am following the pattern you describe however when I get to the point of getting the revert_snapshot_template based upon the machine_id as so
# get the revert_snapshot_template for this machine
- name: Get revert_snapshot_template for {{ machine_name }} and save in revert_snapshot_template variable
uri:
url: "{{ vRA_URI + catalog_resources + slash + machine_id + actions + slash + revertSnapshotResourceOperationId + requests_template }}"
method: GET
body_format: json
headers:
Authorization: "{{ bearer_token }}"
register: revert_snapshot_template
I get the following response:
{
"actionId": "8084fff0-71f0-4e67-b80e-be3cd75dc4c1",
"data": {
"provider-SnapshotReference": null
},
"description": null,
"resourceId": "a0d05cad-75fd-4acb-892b-ca41d613d219",
type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest"
},
I believe my problem is that I need to find the value for the "provider-SnapshotReference" and there are no examples on how to get this from the machine_id that I have. The original shel script I'm working from suggests that I should be able to a list of snapshots for a VM:
snapshotReferenceURI="$baseURI/catalog-service/api/consumer/resources/$machineId/actions/$resourceOperationId/forms/request/provider-SnapshotReference/values" |
But the ansible uri equivalent of this call fails with a 405 (Not allowed)
So this is definitely a pain in the butt. I sniffed the API call using Chrome developer tools (network tab) while clicking through the UI. Here is what I found the vRA portal is doing for this section. The following call is made when you click to select the snapshot you want to use as the parameter for the resource action call to revert snapshot.
I then took that JSON request body and made my own POST to /catalog-service/api/consumer/resources/b7ddb8b0-9718-4e3e-92d2-83864dc9848e/actions/6a41769f-9239-47d9-a34e-dd355c76c296/forms/request/provider-SnapshotReference/values
And the HTTP 200 response is as follows:
{"values":[{"underlyingValue":{"type":"entityRef","classId":"Infrastructure.Compute.Machine.Snapshot","id":"59","componentId":"076628d6-aad7-4567-a3d1-0545e1950605","label":"Test1"},"label":"(Current) Test1"}]}
The issue is of course getting the full structure of the object it's expecting to see in the body of the POST call and getting your JSON properly formed (almost always impossible to infer from the API docs). I think you're safe to parameterize that JSON and pass the provider-MachineName and provider-machineId values based on what you pull from the resource information in the GET to /catalog-service/api/consumer/resources/{resourceId}/
These values come in the form of the name property of the resource and the providerBinding.bindingId property of the resource.
I have now managed to get the snapshot information for my VM as so:
[exec] "json": {
[exec] "values": [
[exec] {
[exec] "label": "(Current) sdl12393",
[exec] "underlyingValue": {
[exec] "classId": "Infrastructure.Compute.Machine.Snapshot",
[exec] "componentId": "ab5cad5d-83c5-453a-b099-88b1a277fa33",
[exec] "id": "20424",
[exec] "label": "sdl12393",
[exec] "type": "entityRef"
[exec] }
[exec] },
I did try and construct the SnapshotReference as per jasnyders instructions so:
[exec] "json": {
[exec] "actionId": "8084fff0-71f0-4e67-b80e-be3cd75dc4c1",
[exec] "data": {
[exec] "SnapshotReference": {
[exec] "classId": "Infrastructure.Compute.Machine.Snapshot",
[exec] "componentId": "ab5cad5d-83c5-453a-b099-88b1a277fa33",
[exec] "id": "20424",
[exec] "label": "sdl12393"
[exec] }
[exec] },
[exec] "description": "Revert to sdl12393",
[exec] "resourceId": "a0d05cad-75fd-4acb-892b-ca41d613d219",
[exec] "type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest"
[exec] }
[exec] }
[exec] }
And while the POST to revert the snapshot does get submitted and returns a 201
url: "{{ vRA_URI + catalog_resources + slash + machine_id + actions + slash + revertSnapshotResourceOperationId + requests }}"
The revert fails and within the vRA interface I see the SnapshotReference JSON text against the Snapshot request so I've got something wrong here.
I have a shell script that I know works so I'll try and follow it in the morning to see if I can come up with the correct incarnation, but thanks for all assistance.
MikeC.
Hi
if you can use vRo workflow following below:
Revert Snapshot:
----------------------
POST, auth header, accept,content-type
https://vraportal:443/vco/api/workflows/df1884fd-e8b3-475f-8be6-c52ef3a4b7b9/executions
body
{"parameters":
[
{
"value": {"string":{ "value": "vmname"}},
"type": "string",
"name": "vmname",
"scope": "local"
}
]}
vRA rest-API
-------------------
Name | Value |
Endpoint | https://{vRA}/catalog-service/api/consumer/resources/{resourceId}/actions |
Method | GET |
Header | Authorization: Bearer ${tokenId} |
Body | NA |
MID Server | vRA-FQDNname |
Notes | Available Expected Actions:
Stored Action ID in sys properties Availabe actions dependant on current machine state |
Name | Value |
Endpoint | https://{vRA}/catalog-service/api/consumer/resources/{resourceId}/actions/{resourceActionId}/requests |
Method | POST |
Header | Authorization: Bearer {tokenId} |
Body | Body differs as per ACTION
{ "type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest", "resourceId": "66c60098-8c2a-4eb6-bd7f-7fb19b69cd38", "actionId": "9e193309-d8fe-446d-9713-2773a208f418", "description": null, "data": { "provider-SnapshotInputDescription": null, "provider-SnapshotInputMemoryIncluded": null, "provider-SnapshotInputName": "snapshotname(Tuesday, September 26, 2017 2:10:45 PM +08:00)" } }
{ "type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest", "resourceId": "66c60098-8c2a-4eb6-bd7f-7fb19b69cd38", "actionId": "fc016321-7c9b-432a-a6a6-523e67846e16", "description": null, "data": {} } |
MID Server | vRAFQDNname |
Notes |
|
Hi Mike, I have exactly the same issue. How did you manage to get snapshot information (including id and componentId) for you VM? Were you able to succesfully revert snapshot at the and?
thank you
david
Hi,
Were you able to get this to work? I tried the same with re-construction of SnapShotReference from Chrome Dev tools. However, no luck reverting to a snapshot. Please suggest. Thanks!
Regards,
Karthik SN
{
"resourceId": "f2ec25ff-9b4c-43fe-9e48-f1c37d2f0121",
"actionId": "b338eccc-c28b-4afc-8456-d5f3226dfb5d",
"description": "",
"data": {
"SnapshotReference": {
"label": "test",
"classId": "Infrastructure.Compute.Machine.Snapshot",
"id": "3995",
"componentId": "c19daa01-b23f-424e-85bf-60599d1868aa"
}
},
"type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest"
}