stopBeforeDestroy doesn't wait for the environment to be stopped before trying to delete it

Bug Report

When there is an Environment resource configured with stop_before_destroy and the resource gets destroyed, the provider triggers the stop action, but it doesn't wait for stop action to be performed; in other words, it doesn't wait for the status to change from "stopping" to "stopped", but it directly tries to delete it and therefore receive a 403 Forbidden error.

The part of the code where I would have expected to see some kind of wait condition, is here: https://gitlab.com/gitlab-org/terraform-provider-gitlab/-/blob/main/internal/provider/sdk/resource_gitlab_project_environment.go#L186

	if stopBeforeDestroy {
		log.Printf("[DEBUG] Stopping environment %d for Project %s before destruction", environmentID, project)
		_, err = client.Environments.StopEnvironment(project, environmentID, gitlab.WithContext(ctx))
		if err != nil {
			return diag.Errorf("error stopping gitlab project %s environment %q: %v", project, environmentID, err)
		}
	} else {

I think the reason behind this, is that the GitLab API documentation isn't very clear. In fact, from the docs it seems that the API call is syncrhonous:

It returns 200 if the environment was successfully stopped[...]

however, calling the endpoint with a curl I can see that the environemnt goes to an intermediate state "stopping" while the webserver immediately responds with a 200.

$ curl --request POST --header "PRIVATE-TOKEN: <myprivatetoken>" "https://myhost.com/api/v4/projects/123/environments/144/stop"
{
    "id": 144,
    "name": "master",
    "slug": "master",
    "external_url": null,
    "created_at": "2023-04-13T08:39:29.886+02:00",
    "updated_at": "2023-04-18T13:20:32.599+02:00",
    "tier": "other",
    "project": {
        "id": 123,
        "description": "",
        "name": "Example",
        "name_with_namespace": "Example",
        "path": "example",
        "path_with_namespace": "subscribers/bdi-ict/aimlmodelmgmt/example",
        "created_at": "2022-08-11T16:31:34.224+02:00",
        "default_branch": "master",
        "tag_list": [
        ],
        "topics": [
        ],
        ...
    },
    "state": "stopping"
}
$ curl --request DELETE --header "PRIVATE-TOKEN: <myprivatetoken>" "https://myhost.com/api/v4/projects/123/environments/144"
{"message":"403 Forbidden"}

Relevant Terraform Configuration

resource "gitlab_project_environment" "new_environment" {
  project      = var.GITLAB_PROJECT_ID
  name         = "myenvironment"
  stop_before_destroy = true
}

Relevant Terraform Command

    - terraform -chdir=_tf_scripts init
    - terraform -chdir=_tf_scripts destroy -auto-approve

Relevant Log Output

Error: error deleting gitlab project 1234 environment 144: DELETE https://git.myhost.com/api/v4/projects/1234/environments/144: 403 {message: 403 Forbidden}

Additional Details

  • GitLab Terraform Provider Version: 15.8
  • GitLab Version: 15.7.6-ee
  • Terraform Version: 1.2.9