Problem
When trying to add workspaces to the agent pool using API, we may get the following error:
{"errors":[{"status":"422","title":"unprocessable entity","detail":"Agent pool is still being used by workspaces in your organization."}]}% Cause
This error occurs because the payload.json file being referred, that contains the list of workspaces that need to be added to Grant access to specific workspaces, does not contain the workspace ID already using this specified agent pool.
Sample payload.json
{
"data": {
"type": "agent-pools",
"attributes": {
"name": "Docker-agent-pool",
"organization-scoped": false
},
"relationships": {
"allowed-workspaces": {
"data": [
{
"id": "ws-a39b5uxxxxxe9wFX",
"type": "workspaces"
},
{
"id": "ws-9zXbiNAExxxxxVdg",
"type": "workspaces"
}
]
}
}
}
}Solution
In order to prevent this error, make sure the above payload.json consists of all the workspaces in the allowed-workspaces section that are using the agent pool. For example, in this case, agent pool with name Docker-agent-pool.
Outcome
Once all the workspaces ID is listed in the allowed-workspaces section of the payload.json, run the below curl to add all the workspaces to the list of workspaces using the specified agent pool:
curl \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ --request PATCH \ --data @payload.json \ https://app.terraform.io/api/v2/agent-pools/<<agent-pool-id>>
Note: If you continue to experience issues, please contact HashiCorp Support.