zahiar / terraform-provider-bitbucket

Terraform Provider for Bitbucket Cloud
https://registry.terraform.io/providers/zahiar/bitbucket/latest
Mozilla Public License 2.0
19 stars 22 forks source link

Use the Rank from the environmentType when reading a deployment to allow valid imports of `bitbucket_deployment` #186

Open nemoinho opened 5 months ago

nemoinho commented 5 months ago

The deployment-resource is a rather seldom used but very useful resource. Unfortunately the bitbucket-devs were a bit lazy with the requirements of this and didn't even bother to create a proper documentation of the actual response.

This lead to the confusing situation that the API respond with two different fields for the rank, one in the root of the response and one as part of environment_type. When you're going to create a deployment with the terraform-resource this is fine, because both occurences are going to be changed. But if you've set the rank through the UI, only the rank within environment_type is changed!

If you then try to import a resource through terraform import it's impossible to import them correctly without a change!

The The simpliest solution to address this issue is a change of the attached line to look for deployment.EnvironmentType.Rank. As I said it doesn't change the behavior of a deployment created with this resource but enables a clean import!

https://github.com/zahiar/terraform-provider-bitbucket/blob/49edc9f32c07af741307980e9dd421d4880c6935/bitbucket/resource_bitbucket_deployment.go#L99-L101

A sample for such an UI created deployment might look like this:

{
  "type": "deployment_environment",
  "name": "nemoinho test",
  "slug": "nemoinho-test",
  "rank": 0,
  "environment_type": {
    "name": "Staging",
    "rank": 1,
    "type": "deployment_environment_type"
  },
  "deployment_gate_enabled": false,
  "lock": {
    "name": "OPEN",
    "type": "deployment_environment_lock_open"
  },
  "restrictions": {
    "type": "deployment_restrictions_configuration",
    "admin_only": false
  },
  "hidden": false,
  "environment_lock_enabled": true,
  "uuid": "{7AB3C806-81F1-45F3-8E75-426D62A872E7}",
  "category": {
    "name": "Nemoinho test"
  }
}

While a deployment via the resource might look like this:

{
  "type": "deployment_environment",
  "name": "nemoinho test",
  "slug": "nemoinho-test",
  "rank": 1,
  "environment_type": {
    "name": "Staging",
    "rank": 1,
    "type": "deployment_environment_type"
  },
  "deployment_gate_enabled": false,
  "lock": {
    "name": "OPEN",
    "type": "deployment_environment_lock_open"
  },
  "restrictions": {
    "type": "deployment_restrictions_configuration",
    "admin_only": false
  },
  "hidden": false,
  "environment_lock_enabled": true,
  "uuid": "{31A1F238-4646-4084-B94B-3DCBF26575DC}",
  "category": {
    "name": "Nemoinho test"
  }
}

The only diff is the rank on the root (and obviously the uuid).