terraform-provider-opsgenie icon indicating copy to clipboard operation
terraform-provider-opsgenie copied to clipboard

Not possible to create/update team

Open bendyna-vitalii opened this issue 3 years ago • 5 comments

It's not possible to do any operations with opsgenie_team resource. I've imported previously created with web UII team, and when I run "terraform plan", it tries to change team's admin user id to it's email address. The same thing when I'm creating a new team.

Here's my code that creates user:

module "user_test_user" {
  source = "./modules/users"

  user_name = "[email protected]"
  user_full_name = "Test User"
  user_timezone = "America/Los_Angeles"
}

Module users:

resource "opsgenie_user" "this" {
  full_name = var.user_full_name
  role      = var.user_role
  username  = var.user_name
  locale    = var.user_locale
  timezone = var.user_timezone
}

output "user_id" {
  value = opsgenie_user.this.id
}

My code to create team:

module "test" {
  source = "./modules/teams"

  team_name = "test"
  team_description = "test"
  team_admins = [module.user_test_user.user_id]
  depends_on = [module.user_test_user]
}

Module teams:

resource "opsgenie_team" "this" {
  name = var.team_name
  description = var.team_description
  delete_default_resources = var.delete_default_resources

  dynamic "member" {
    for_each = var.team_admins
    content {
      id = member.value
      role = "admin"
    }
  }

  dynamic "member" {
    for_each = var.team_users
    content {
      id = member.value
      role = "user"
    }
  }
}

When I run terraform apply on imported team, it says that the team will be updated-in-place:

Terraform will perform the following actions:

  # module.test.opsgenie_team.this will be updated in-place
  ~ resource "opsgenie_team" "this" {
        id                       = "xxxxc545-xxxx-xxxx-xxxx-0c3889d9xxxx"
        name                     = "test"
        # (2 unchanged attributes hidden)

      ~ member {
          ~ id   = "xxxx399e-xxxx-xxxx-xxxx-d0fd9f2xxxxx" -> "[email protected]"
            # (1 unchanged attribute hidden)
        }
    }

Actual behaviour:

│ Error: Error occurred with Status code: 422, Message: No user exists with id [[email protected]], Took: 0.005000, RequestId: 64ade5b3-xxxx-xxxx-xxxx-3ed7c6a2xxxx
│ 
│   with module.team_as_server.opsgenie_team.this,
│   on modules/teams/main.tf line 1, in resource "opsgenie_team" "this":
│    1: resource "opsgenie_team" "this" {
│ 

Expected behaviour: Team was added/modified

Seems, that the output of user.id, described here, doesn't work as it should.

Versions:

$ terraform -version
Terraform v1.0.4
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.22.0
+ provider registry.terraform.io/opsgenie/opsgenie v0.6.10

bendyna-vitalii avatar Jul 19 '22 15:07 bendyna-vitalii

See: https://github.com/opsgenie/terraform-provider-opsgenie/issues/288 (Although officially closed, the issue is very much still persistent)

bateller avatar Aug 01 '22 16:08 bateller

When I run terraform apply on imported team, it says that the team will be updated-in-place:

Terraform will perform the following actions:

  # module.test.opsgenie_team.this will be updated in-place
  ~ resource "opsgenie_team" "this" {
        id                       = "xxxxc545-xxxx-xxxx-xxxx-0c3889d9xxxx"
        name                     = "test"
        # (2 unchanged attributes hidden)

      ~ member {
          ~ id   = "xxxx399e-xxxx-xxxx-xxxx-d0fd9f2xxxxx" -> "[email protected]"
            # (1 unchanged attribute hidden)
        }
    }

This plan should give a hint: it looks like you are trying to pass a user email whereas the team is configured with a user ID.

In your user module, you wrote:

output "user_id" {
  value = opsgenie_user.this.id
}

and it seems this is the value that you are passing to your team module.

Can you double check that you are really passing down the opsgenie_user.this's id attribute and not the username attribute instead?

The error message that you get:

422, Message: No user exists with id [[email protected]]

Seems to indicate the same: you are trying to pass in a user email (probably username) instead of a user ID.

multani avatar Aug 10 '22 05:08 multani

I can reproduce this bug on the latest Opsgenie provider version v0.6.14. I have an almost identical setup, except I am creating users using for_each

resource "opsgenie_user" "users" {
  for_each  = var.users
  full_name = "${each.value.first_name} ${each.value.last_name}"
  locale    = "en_US"
  role      = each.value.is_superadmin ? "Admin" : "User"
  timezone  = coalesce(each.value.timezone, "Europe/Berlin")
  username  = each.value.login
}

and creating teams:

resource "opsgenie_team" "this" {
  for_each = var.teams

  name        = each.value.name
  description = each.value.description
  
  dynamic "member" {
    for_each = local.team_users_map[each.key]
    content {
      id   = opsgenie_user.users[member.key].id
      role = member.value
    }
  }
}

This output returns:

output "debug_id" {
  value = opsgenie_user.users["[email protected]"].id
  sensitive = false
}

returns:

{
  "debug_id" = "[email protected]"
}

Not able to create teams with members right now. Previously, we were assigning members to teams via GUI

madianas21 avatar Sep 13 '22 16:09 madianas21

I tried the following configuration:

locals {
  users = {
    john = {
      first_name    = "john"
      last_name     = "doe"
      is_superadmin = false
      timezone      = "Europe/Paris"
      login         = "[email protected]"
    },
    mary = {
      first_name    = "mary"
      last_name     = "jane"
      is_superadmin = false
      timezone      = "Europe/Paris"
      login         = "[email protected]"
    },
  }

  teams = {
    test = {
      name        = "test"
      description = "test team"
    }
  }

  team_users_map = {
    test = {
      john = "User"
      mary = "User"
    }
  }
}

@madianas21 The resources are the same as the one you mentioned above, except for var replaced by local:

resource "opsgenie_user" "users" {
  for_each  = local.users
  full_name = "${each.value.first_name} ${each.value.last_name}"
  locale    = "en_US"
  role      = each.value.is_superadmin ? "Admin" : "User"
  timezone  = coalesce(each.value.timezone, "Europe/Berlin")
  username  = each.value.login
}


resource "opsgenie_team" "this" {
  for_each = local.teams

  name        = each.value.name
  description = each.value.description

  dynamic "member" {
    for_each = local.team_users_map[each.key]
    content {
      id   = opsgenie_user.users[member.key].id
      role = member.value
    }
  }
}

This creates correctly, and this outputs:

output "debug" {
  value     = opsgenie_user.users
  sensitive = false
}

the following:

debug = {
  "john" = {
    "full_name" = "john doe"
    "id" = "3fb1ec25-3861-435f-b666-156d43c05b61"
    "locale" = "en_US"
    "role" = "User"
    "skype_username" = ""
    "tags" = toset([])
    "timezone" = "Europe/Paris"
    "user_address" = tolist([])
    "user_details" = tomap(null) /* of string */
    "username" = "[email protected]"
  }
  "mary" = {
    "full_name" = "mary jane"
    "id" = "3f21b6a3-7cc0-4c8f-9619-031d565e0e19"
    "locale" = "en_US"
    "role" = "User"
    "skype_username" = ""
    "tags" = toset([])
    "timezone" = "Europe/Paris"
    "user_address" = tolist([])
    "user_details" = tomap(null) /* of string */
    "username" = "[email protected]"
  }
}

The IDs here are correctly set.

This is the content of my .terraform.lock.hcl file:

provider "registry.terraform.io/opsgenie/opsgenie" {
  version     = "0.6.14"
  constraints = "0.6.14"
  hashes = [
    "h1:N7/ajmejaQyoO7YtsNYvivbKU8pWiJRMmeMY8XQrw0Y=",
    "zh:1591d84a65af3a2626a3dd8ef62e8694171177018f8787f9d6972ebbca7724df",
    "zh:329eadda950cce4724747113522c21490d1ca4ac218733b90a1eb1fcaf364ffe",
    "zh:621c24408b6624b612a12b2a60975e3630357d0c9f2ea7045e45d5fd725fc5f1",
    "zh:79123177f9cdd94660c7d9feaff439d9f1bcd91959eab5017f7757778d085602",
    "zh:951d6d270f71ca196dda43ee78b9890d5fca63303dcf26bb1f9c46daeaa1d31e",
    "zh:a7ee5eae3253cb2bf4f8fe926ab40dc447434b4af527a0c87c179134339d0ac2",
    "zh:aac4b924baa3ba8da5e056536bf74f10182a0be3a81585e2650db1cbd39e7794",
    "zh:b132f9df2cf123daac3298e1553014fa32b7761e147a560f17a5f76b9c808424",
    "zh:ce2bfac2968d859edc3e6e560d550eb16bd167fbf3d21f9971c4b078b3b4dba5",
    "zh:ddcd99302f6d6c93142e941d0c8534e2c195dba7990209309f17123ec40aa01d",
    "zh:e55a1122386c0714afb2b0b7481c8259e64ff554bed8de5608dcccae03c96ec6",
    "zh:ebc8c6e0a6ab8969df3d3e42e12e25bab52dd3881e6cc1f8d4adc74fd74e3eae",
    "zh:fa75486fd5ca6972ae2210dba751665956debf3e5a3192f5b095a410366db3aa",
  ]
}

multani avatar Sep 13 '22 18:09 multani

Thank you so much @multani Found a problem. As I mentioned users were created on OpsGenie website originally. Then they were imported into Terraform state. Due to some permission issues, users were imported with an email as their user_id instead of UUID. Maybe there is another bug, as to why this is even possible.

I've created a new user with Terraform and was able to add it to the team. Can count this issue resolved for me. Thank you again!

madianas21 avatar Sep 14 '22 09:09 madianas21

Hi @bendyna-vitalii, can you confirm if you're still facing the issue? If not, I will be closing the issue.

koushik-swaminathan avatar Jul 12 '23 10:07 koushik-swaminathan