Closed zonArt closed 2 years ago
I'm also trying to figure out the preferred way to do this, and I'm hitting the exact same problem.
It might depend on your state definition. How are you referring to that user in schedule / team? can you post you state definition? Interested in terraform? Check out my articles!
Sure:
users.yml
usertest:
email: usertest@example.com
full_name: User Test
role: Admin
locale: fr_CH
timezone: Europe/Belgrade
otherexample:
email: otherexample@foobar.com
full_name: Other Example
role: User
locale: en_US
timezone: Asia/Kolkata
users.tf
locals {
users = yamldecode(file("users.yml"))
}
resource "opsgenie_user" "users" {
for_each = local.users
username = each.value.email
full_name = each.value.full_name
role = each.value.role
locale = each.value.locale
timezone = each.value.timezone
}
teams.yml
otherexample:
description: Other example
members:
usertest@example.com: user
otherexample@foobar.com: admin
teamtest:
description: Teamtest
members:
usertest@example.com:user
teams.tf
locals {
teams = yamldecode(file("teams.yml"))
}
resource "opsgenie_team" "teams" {
for_each = local.teams
name = each.key
description = each.value.description
delete_default_resources = true
dynamic "member" {
for_each = each.value.members
content {
id = opsgenie_user.users[member.key].id
role = member.value
}
}
}
schedule_rotations.yml
rotationtest:
start_date: '2019-08-05T05:30:00Z'
schedule_id: teamtest_schedule
participants:
- otherexample@foobar.com
- usertest@example.com
anotherrotation:
start_date: '2020-07-19T22:00:00Z'
schedule_id: otherexample_schedule
participants:
- otherexample@foobar.com
schedule_rotations.tf
locals {
schedule_rotations_file = yamldecode(file("schedule_rotations.yml"))
schedule_rotations = { for k, v in local.schedule_rotations_file : k => local.schedule_rotations_file[k] if v.participants != [] }
}
resource "opsgenie_schedule_rotation" "schedule_rotations" {
for_each = local.schedule_rotations
name = each.key
start_date = each.value.start_date
length = 1
type = "weekly"
schedule_id = opsgenie_schedule.schedules[each.value.schedule_id].id
dynamic "participant" {
for_each = each.value.participants
content {
type = "user"
id = opsgenie_user.users[participant.value].id
}
}
}
schedules.yml
otherexample_schedule:
description: ""
timezone: "Asia/Kolkata"
owner_team_id: "otherexample"
enabled: true
teamtest_schedule:
description: ""
timezone: "Europe/Paris"
owner_team_id: "teamtest"
enabled: true
schedules.tf
locals {
schedules = yamldecode(file("schedules.yml"))
}
resource "opsgenie_schedule" "schedules" {
for_each = local.schedules
name = each.key
description = each.value.description
timezone = each.value.timezone
owner_team_id = opsgenie_team.teams[each.value.owner_team_id].id
enabled = each.value.enabled
}
Now when I remove usertest it will be removed from the users.yml file but also from teams.yml and schedule_rotations.yml, then terraform apply will fail as mentioned in the ticket description
any progress with this?
Would really be nice to have a solution since otherwise it's not possible to manage users through the provider. By the way I notice the same similar stuff is happening when doing a removal from let's say JIRA, you receive an email telling that the person has been removed from an Atlassian product but cannot be removed from opsgenie for the same reasons
Hello, I'm having the exact same problem. Can't think of a non-hacky solution
Hello, thanks for reporting. I've tried to reproduce the exact same scenario with the modified version of example @zonArt shared. The Terraform version is : v1.3.1 The Opsgenie Terraform Provider version is : 0.6.15
The actual behavior is correct indeed. Opsgenie API does not allow you to delete the user unless removing it from teams and schedule rotations(any dependent place actually). The problem here is when you update all your ymls and remove the user from everywhere with 1 apply, the terraform updates/destroys resources out of order.
The solution is 2 different apply,
Tested and confirmed.
Sorry, but this is NOT a solution, but a mere workaround. Please consider reviewing and merging https://github.com/opsgenie/terraform-provider-opsgenie/pull/335 since this actually solves the problem that you can do this in one apply.
Sorry, but this is NOT a solution, but a mere workaround. Please consider reviewing and merging #335 since this actually solves the problem that you can do this in one apply.
Hi @stefanandres, thanks for your collaboration. You can be sure it's reviewed and tested. The user is being deleted from teams only with pr mentioned and it's not solving the problem in one apply as you said. The user must be deleted from all the other resources. The test result can be seen below.
╷
│ Error: Error occurred with Status code: 428, Message: User cannot be modified. Please remove it from schedules: [teamtest_schedule,otherexample_schedule], Took: 0.058000, RequestId: 5114acfe-a2b1-4820-a0b9-2d4203b722e9
Hi there,
I've got some issue when trying to remove a user from an opsgenie_user resource when this user is also referenced as a team member or a participant to a schedule rotation
Terraform Version
Terraform v0.12.29
Affected Resource(s)
Debug Output
Expected Behavior
Terraform should remove the user from the team(s) and the schedule rotation(s) and then remove the user
Actual Behavior
An error occurs mentioning that the user is still part of team(s) and/or schedule rotation(s)
Steps to Reproduce