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

fix refresh failures #191

Merged
merged 1 commit into from
Oct 9, 2019
Merged

fix refresh failures #191

merged 1 commit into from
Oct 9, 2019

Conversation

mandelsoft
Copy link
Contributor

The nsxt resources do not behave correctly during the refresh cycle,
if the described resource has vanished. Here an error 404 is reported
and terraform stops processing (instead of recreating the missing resource).

This is caused by a general implementation error in nearly all
resources based on a wrong assumption about the behaviour of the go SDK.
It assumes that http status codes are never reported as errors, but
all the SDK functions return an error if the status code is above 300.
Therefore the order of the error handling in the read functions of
the resources is wrong.

Instead of

if err != nil {
  return fmt.Errorf("Error during LogicalSwitch read: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
  log.Printf("[DEBUG] LogicalSwitch %s not found", id)
  d.SetId("")
  return nil
}

the correct error handling must always be

if resp != nil && resp.StatusCode == http.StatusNotFound {
  log.Printf("[DEBUG] LogicalSwitch %s not found", id)
  d.SetId("")
  return nil
}
if err != nil {
  return fmt.Errorf("Error during LogicalSwitch read: %v", err)
}

In three cases the order was already fixed, but in two of these cases
the nil pointer check was missing, causing a NPE for http request
execution errors.

The nsxt resources do not behave correctly during the refresh cycle,
if the described resource has vanished. Here an error 404 is reported
and terraform stops processing (instead of recreating the missing resource).

This is caused by a general implementation error in nearly all
resources based on a wrong assumption about the behaviour of the go SDK.
It assumes that http status codes are never reported as errors, but
all the SDK functions return an error if the status code is above 300.
Therefore the order of the error handling in the read functions of
the resources is wrong.

Instead of

```
if err != nil {
  return fmt.Errorf("Error during LogicalSwitch read: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
  log.Printf("[DEBUG] LogicalSwitch %s not found", id)
  d.SetId("")
  return nil
}
```

the correct error handling must always be

```
if resp != nil && resp.StatusCode == http.StatusNotFound {
  log.Printf("[DEBUG] LogicalSwitch %s not found", id)
  d.SetId("")
  return nil
}
if err != nil {
  return fmt.Errorf("Error during LogicalSwitch read: %v", err)
}
```

In three cases the order was already fixed, but in two of these cases
the nil pointer check was missing, causing a NPE for http request
execution errors.
@ghost ghost added the size/XL label Oct 1, 2019
Copy link
Collaborator

@annakhm annakhm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to fix this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants