Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nemoinho opened this issue Mar 19, 2024 · 0 comments · May be fixed by #187

Comments

@nemoinho
Copy link

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!

_ = resourceData.Set("name", deployment.Name)
_ = resourceData.Set("environment", gobb.RepositoryEnvironmentTypeOption(deployment.Rank).String())
resourceData.SetId(deployment.Uuid)

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).

nemoinho pushed a commit to nemoinho/terraform-provider-bitbucket that referenced this issue Mar 19, 2024
nemoinho pushed a commit to nemoinho/terraform-provider-bitbucket that referenced this issue Mar 19, 2024
The documentation from bitbucket is actually not that good, because it
doesn't explain any of the fields in question. However I observed on the
bitbucket-cloud instance that the previous implementation lead to errors
during imports of deployment configurations into a terraform state.

Steps to reproduce the error:
1. Create a repository in bitbucket-cloud
2. Create a deployment for the repository via GUI for Production or
   Staging (not Test!)
3. write a terraform module (which exactly matches the deployment) and
   perform `terraform import` to import the existing deployment into the
   terraform state
4. perform `terraform apply` and terraform wants to change the resource!

This change reads the rank from the `environment_type` which is the only
valid source if the deployment was created via UI and still a valid
source for a deployment which was created via this provider.
nemoinho pushed a commit to nemoinho/terraform-provider-bitbucket that referenced this issue Mar 19, 2024
The documentation from bitbucket is actually not that good, because it
doesn't explain any of the fields in question. However I observed on the
bitbucket-cloud instance that the previous implementation lead to errors
during imports of deployment configurations into a terraform state.

Steps to reproduce the error:
1. Create a repository in bitbucket-cloud
2. Create a deployment for the repository via GUI for Production or
   Staging (not Test!)
3. write a terraform module (which exactly matches the deployment) and
   perform `terraform import` to import the existing deployment into the
   terraform state
4. perform `terraform apply` and terraform wants to change the resource!

This change reads the rank from the `environment_type` which is the only
valid source if the deployment was created via UI and still a valid
source for a deployment which was created via this provider.

Reference: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-deployments/#api-repositories-workspace-repo-slug-environments-environment-uuid-get-response
Closes: zahiar#186
nemoinho pushed a commit to nemoinho/terraform-provider-bitbucket that referenced this issue Mar 19, 2024
The documentation from bitbucket is actually not that good, because it
doesn't explain any of the fields in question. However I observed on the
bitbucket-cloud instance that the previous implementation lead to errors
during imports of deployment configurations into a terraform state.

Steps to reproduce the error:
1. Create a repository in bitbucket-cloud
2. Create a deployment for the repository via GUI for Production or
   Staging (not Test!)
3. write a terraform module (which exactly matches the deployment) and
   perform `terraform import` to import the existing deployment into the
   terraform state
4. perform `terraform apply` and terraform wants to change the resource!

This change reads the rank from the `environment_type` which is the only
valid source if the deployment was created via UI and still a valid
source for a deployment which was created via this provider.

Reference: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-deployments/#api-repositories-workspace-repo-slug-environments-environment-uuid-get-response
Closes: zahiar#186
@nemoinho nemoinho linked a pull request Mar 19, 2024 that will close this issue
@nemoinho nemoinho changed the title Please use the Rank from the environmentType when reading a deployment to allow valid imports of bitbucket_deployment Use the Rank from the environmentType when reading a deployment to allow valid imports of bitbucket_deployment Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant