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

State transitions error: Cycle

Open SergeyKovalchuk opened this issue 5 years ago • 14 comments

Hi @davidweterings. We have faced a problem with a state transitioning. We would like to transition from state_1 to state_2 (Just for example) and vice versa.

Here is an example:

resource "commercetools_state" "state_1" {
  key = "state_1"
  ...
  transitions = ["${commercetools_state.state_2.key}"]
}


resource "commercetools_state" "state_2" {
  key = "state_2"
   ....
  transitions = ["${commercetools_state.state_1.key}"]
}

We are getting an error on terraform validation step: Error: Cycle: commercetools_state.step_1, commercetools_state.step_2 (Because both of them creating dependent resources in parallel)

We may suggest to create a separate resource for state transitioning. This way we will have 2 separate resources: state and transition.

Example:

resource "commercetools_state" "state_1" {
  key = "state_1"
  ...
}

resource "commercetools_state" "state_2" {
  key = "state_2"
   ....
}

resource "commercetools_state_transition" "state_1_transition" {
 from = "${commercetools_state.state_1.key}"
 to = [
   "${commercetools_state.state_2.key}"
 ]
}

resource "commercetools_state_transition" "state_2_transition" {
 from = "${commercetools_state.state_2.key}"
 to = [
   "${commercetools_state.state_1.key}"
 ]
}

SergeyKovalchuk avatar Aug 27 '19 14:08 SergeyKovalchuk

Would be really good to hear feedback about this issue :)

AlexSit avatar Sep 16 '19 10:09 AlexSit

I've been thinking about this, and indeed there only seem to be two ways to solve this:

  1. Run terraform twice, once without the state transition restrictions and one with them.
  2. What you suggest, this would be similar to the tax category rate resource, which does not have an explicit endpoint in the Commercetools API. You have to make sure you lock the correct state ids to prevent concurrent modification of the same state.

davidweterings avatar Sep 16 '19 12:09 davidweterings

Any updates on this? Would be a really helpful thing to have.

andrei-selilo avatar Feb 20 '20 08:02 andrei-selilo

I'd definitely accept a pull request if someone made it like this. I do think there might be a creative solution using locks, creating the state resource and then updating the state resource.

However, given that this provider is far from feature complete I don't think I can prioritize creating this over making the provider more feature complete and general maintenance.

davidweterings avatar Feb 27 '20 20:02 davidweterings

We are also running into this issue. Since there is a linked pull request, I can perhaps help with implementing the functionality if help is still required. Is there any update?

wendelb avatar Jul 14 '21 09:07 wendelb

Same problem here, would love to have it solved. Thanks!

markus-mw avatar Aug 19 '22 07:08 markus-mw

Agreed we need to fix this. As mentioned we need to support the following

resource "commercetools_state_transitions" "transition_1" {
  from = commercetools_state.state_1.id
  to = [
    commercetools_state.state_2.id
  ]
}

resource "commercetools_state_transitions" "transition_2" {
  from = commercetools_state.state_2.id
  to = [
    commercetools_state.state_1.id
  ]
}

I think the easiest approach is to then drop support for inline transitions, so that we only have to support transitions as a separate resource

mvantellingen avatar Aug 19 '22 08:08 mvantellingen

@mvantellingen : Don't forget the state id, the transaction should be assigned to ;)

markus-mw avatar Aug 19 '22 12:08 markus-mw

The transition is set to the state defined in the from attribute. I've added a warning when multiple state_transitions are defined for the same from since we can only define one set of transitions per state

mvantellingen avatar Aug 19 '22 13:08 mvantellingen

@mvantellingen : is this some kind of provider/Terraform restriction? Because by API you can define multiple transitions per state...

markus-mw avatar Aug 19 '22 13:08 markus-mw

Yes you can define multiple transitions per state, but only all at once via the setTransitions action. So there is no addTransition or removeTransition.

When managing tax rates, or shipping zones this way we can use separate resources because for example the tax rate has actions like addTaxRate, removeTaxRate, replaceTaxRate

The commercetools API is unfortunately not always consistent :-)

mvantellingen avatar Aug 22 '22 08:08 mvantellingen

@mvantellingen : Now I understand. Have you placed a feature request regarding the missing add/remove update actions? If not, I could do that..

markus-mw avatar Aug 22 '22 09:08 markus-mw

Yeah would appreciate that. You can reference this issue

mvantellingen avatar Aug 22 '22 13:08 mvantellingen

@mvantellingen : The feature request has now been recorded. We'll see if and when it will be implemented.

markus-mw avatar Aug 30 '22 06:08 markus-mw

Will be fixed in 1.5.0

mvantellingen avatar Sep 30 '22 09:09 mvantellingen