Hello WM team, Is there a way to find and update o...
# general
k
Hello WM team, Is there a way to find and update or override existing mappings stub through wiremock admin REST APIs? I am able to create mappings using 'POST /__admin/mappings'. But, I need to find and update the same stub when there is change on the same stub. I found update mapping API 'PUT /__admin/mappings/:stubMappingId'. But, finding stub mapping id would be challenging from existing collection of stubs. Can someone guide on this?
l
Hi, could you elaborate on what you mean when you say that it would be challenging to find the stub mapping id ?
k
Hello, sorry for any confusion. when i create a new mapping file, it creates automatically the mapping ID or UUID in the persisted file. In fact, the same mapping/UUID would be part of the file name. Now when I want to update or overwrite the same mapping file(stub), then I need to call '`PUT /__admin/mappings/:stubMappingId`'. API. Isn't it? I know I can call again create REST APIs, it will create another stub file with different UUID and latest change will be reflected. But, in order to avoid the duplicate persisted stubs in mappings folder, I have to update the existing stub file only. So, if need to call update service, I need to find the current
stubMappingId
. So, Is there a way I can find (
GET service
) to get current
stubMappingId
? so that based on
stubMappingId
, i can update or overwrite the existing stub?
l
I think you have a couple of options here. As you mentioned, to update the mapping you will need to know the id so you can call the
PUT
endpoint. To do this you could call the
GET
mappings endpoint and then figure out the id from there. This might not be as easy as it sounds depending on how many mappings you have. Another option is to specify the id in the mappings file so wiremock uses that one instead of generating one for you. This way you would know the id upfront and wouldn't need to call the
GET
endpoint. Each option has its pros and cons. If you haven't seen it there is a full reference on the admin API here - https://wiremock.org/docs/standalone/admin-api-reference/ An example of a mapping with and id is:
Copy code
{
  "id": "711f58fd-d436-4ec1-839e-a1e73dc2d61d",
  "name": "orders",
  "request": {
    "url": "/orders",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "jsonBody": [
      {
        "id": 1,
        "name": "order 1"
      },
      {
        "id": 2,
        "name": "order 2"
      }
    ],
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
You can then call the
PUT
endpoint with the id - http://localhost:8080/__admin/mappings/711f58fd-d436-4ec1-839e-a1e73dc2d61d
k
Hello, thank you. Predefine the ID when create first time would work and then update to that ID. Can I use same ID for multiple stubs which is different URL pattern. Basically, I am automating the stub creation through the WM admin services instead of SSH and upload the mappings and _files folder. Also, is there a way to customize the file name of the mapping file which generate by the WM service while run the create service. The WM POST mapping create service have file name format <HTTP_METHOD>-<URLPATTERN>-<UUID>.json. If filename do not have UUID, then I know which file to overwrite in the case of update mode.?
Hello, Any further help this query?
l
Hi, sorry for the delay in getting back to you. I don't know of a way to customise the name of the file that is generated unfortunately. I think this is all handled by Wiremock. One thing I don't quite understand is why you would need access to the file? Would you just use the
PUT
endpoint with the mapping id to do any updates? Sorry if I am not quite understanding your requirements
k
Sorry again confusion about the requirement. Today my clients will generate the stubs and upload into wirmock server (in mappings folder) through SSH automation process. It is fine as long as the wiremock instance is running in VM. But, SSH/SCP operation would be challenge on while wiremock is running in public cloud like Azure. So, I wanted to make use of the wiremock's admin service. It is completely fine when developer creating a brand new stubs. The
POST
(create) new stubs creates and persist in mappings folder. But, the later time if the developer wants to update the same stub, the option is call the
PUT
(update) service. But, in order to update the same existing stub file and overwrite in the mapping folder, I have to identify the stub/mapping ID. Hope it is clear till now. Because the wiremock originally created the stub file name in the mappings folder with the stub ID. Until i do not know the stub ID, i can not go overwrite the existing stub file's content with latest content. So, question how do i isolate that particular stub ID in order call the
PUT
(update) service?
Hello Lee, Any traction on my last questions?
l
Hi, I don't know if there is a way to configure the name of the file created in the mapping folder. I don't think there is as I have never seen this being used anywhere. To do what you are trying to do, you will need to get the mapping ids from somewhere - either from the GET request to return all mappings or by creating the mappings with the ids already in place like we discussed before. Even with that you would still need to maintain that list of ids somewhere for when the mapping needs to change and you need to call the PUT endpoint. Is your main usecase for this because you are running a long lived wiremock instance in the cloud?
t
You can set a custom Handlebars template to control how files are named, either by
.filenameTemplate(…)
on the config object or
--filename-template
from the CLI
l
Nice, didn't know that 🙂