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 sporadic panic when processing service dependencies #376

Merged
merged 5 commits into from
Oct 8, 2021

Conversation

pdecat
Copy link
Contributor

@pdecat pdecat commented Aug 19, 2021

This PR fixes panic: runtime error: invalid memory address or nil pointer dereference errors that happen from time to time when running acceptance tests:

# make testacc TEST=./pagerduty TESTARGS='-run=TestAccPagerDutyServiceDependency_import -count=1'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./pagerduty -v -run=TestAccPagerDutyServiceDependency_import -count=1 -timeout 120m
=== RUN   TestAccPagerDutyServiceDependency_import
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xf20d08]

goroutine 365 [running]:
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyServiceDependencyRead(0xc0002de690, {0x11215a0, 0xc000372280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/pagerduty/resource_pagerduty_service_dependency.go:195 +0x108
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc00014df80, 0xc00090b360, {0x11215a0, 0xc000372280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/schema/resource.go:455 +0x1aa
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc000010878, {0xc00090b270, 0x503ae6}, 0xc00090b270)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin/grpc_provider.go:525 +0x34b
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0x1166600, 0xc000010878}, {0x145ad90, 0xc000aaa690}, 0xc00039c9c0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5/tfplugin5.pb.go:3153 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000b361c0, {0x14701c0, 0xc000298600}, 0xc0007ac300, 0xc0008e8150, 0x1c0d7b0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1210 +0xc8f
google.golang.org/grpc.(*Server).handleStream(0xc000b361c0, {0x14701c0, 0xc000298600}, 0xc0007ac300, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1533 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:871 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:869 +0x294
FAIL    github.com/terraform-providers/terraform-provider-pagerduty/pagerduty   11.996s
FAIL
make: *** [GNUmakefile:17: testacc] Error 1

It also enforces that one and only one dependency block must be defined for a pagerduty_service_dependency resource given the current implementation:

@pdecat
Copy link
Contributor Author

pdecat commented Aug 19, 2021

Oops, the error still happens:

# make testacc TEST=./pagerduty TESTARGS='-run=TestAccPagerDutyServiceDependency_import -count=10'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./pagerduty -v -run=TestAccPagerDutyServiceDependency_import -count=10 -timeout 120m
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (20.03s)
=== RUN   TestAccPagerDutyServiceDependency_import
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xf20dc8]

goroutine 1625 [running]:
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyServiceDependencyRead(0xc00068e620, {0x11215a0, 0xc00098e280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/pagerduty/resource_pagerduty_service_dependency.go:200 +0x108
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc00055e000, 0xc0005af9a0, {0x11215a0, 0xc00098e280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/schema/resource.go:455 +0x1aa
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc000124470, {0xc0005af8b0, 0x503ae6}, 0xc0005af8b0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin/grpc_provider.go:525 +0x34b
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0x1166600, 0xc000124470}, {0x145adb0, 0xc000896c60}, 0xc0001a0960, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5/tfplugin5.pb.go:3153 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0007b4380, {0x14701e0, 0xc000372180}, 0xc0002e0500, 0xc0003d5bf0, 0x1c0d7b0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1210 +0xc8f
google.golang.org/grpc.(*Server).handleStream(0xc0007b4380, {0x14701e0, 0xc000372180}, 0xc0002e0500, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1533 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:871 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:869 +0x294
FAIL    github.com/terraform-providers/terraform-provider-pagerduty/pagerduty   30.870s
FAIL
make: *** [GNUmakefile:17: testacc] Error 1

@pdecat
Copy link
Contributor Author

pdecat commented Aug 19, 2021

With additional traces:

# TF_LOG=trace make testacc TEST=./pagerduty TESTARGS='-run=TestAccPagerDutyServiceDependency_import -count=10
...
2021/08/19 10:51:42 [TRACE] dag/walk: visiting "pagerduty_service_dependency.foo (import id \"PHZT489.business_service.DHVYHT2729LCKQUC14\")"
2021/08/19 10:51:42 [TRACE] vertex "pagerduty_service_dependency.foo (import id \"PHZT489.business_service.DHVYHT2729LCKQUC14\")": starting visit (*terraform.graphNodeImportState)
2021/08/19 10:51:42 [TRACE] vertex "pagerduty_service_dependency.foo (import id \"PHZT489.business_service.DHVYHT2729LCKQUC14\")": evaluating
2021/08/19 10:51:42 [TRACE] [walkImport] Entering eval tree: pagerduty_service_dependency.foo (import id "PHZT489.business_service.DHVYHT2729LCKQUC14")
2021/08/19 10:51:42 [TRACE] <root>: eval: *terraform.EvalSequence
2021/08/19 10:51:42 [TRACE] <root>: eval: *terraform.EvalGetProvider
2021/08/19 10:51:42 [TRACE] <root>: eval: *terraform.EvalImportState
2021/08/19 10:51:42 [TRACE] GRPCProvider: ImportResourceState
2021/08/19 10:51:43 [DEBUG] Waiting for state to become: [success]
2021/08/19 10:51:43 [DEBUG] PagerDuty - Preparing GET request to /service_dependencies/business_services/PHZT489 with body: %!s(<nil>)
2021/08/19 10:51:43 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
GET /service_dependencies/business_services/PHZT489 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip


-----------------------------------------------------
2021/08/19 10:51:43 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Content-Length: 20
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/vnd.pagerduty+json; charset=UTF-8
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 08:51:43 GMT
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: dfb95c67f9bacc6dc1b155ffd2083efb
X-Xss-Protection: 1; mode=block
X-Xss-Protection: 1; mode=block

{
 "relationships": []
}
-----------------------------------------------------
2021/08/19 10:51:43 [TRACE] EvalImportState: import pagerduty_service_dependency.foo "PHZT489.business_service.DHVYHT2729LCKQUC14" produced instance object of type pagerduty_service_dependency
2021/08/19 10:51:43 [TRACE] [walkImport] Exiting eval tree: pagerduty_service_dependency.foo (import id "PHZT489.business_service.DHVYHT2729LCKQUC14")
2021/08/19 10:51:43 [TRACE] vertex "pagerduty_service_dependency.foo (import id \"PHZT489.business_service.DHVYHT2729LCKQUC14\")": expanding dynamic subgraph
2021/08/19 10:51:43 [TRACE] vertex "pagerduty_service_dependency.foo (import id \"PHZT489.business_service.DHVYHT2729LCKQUC14\")": entering dynamic subgraph
2021/08/19 10:51:43 [TRACE] dag/walk: updating graph
2021/08/19 10:51:43 [TRACE] dag/walk: added new vertex: "import pagerduty_service_dependency.foo result"
2021/08/19 10:51:43 [TRACE] dag/walk: visiting "import pagerduty_service_dependency.foo result"
2021/08/19 10:51:43 [TRACE] vertex "import pagerduty_service_dependency.foo result": starting visit (*terraform.graphNodeImportStateSub)
2021/08/19 10:51:43 [TRACE] vertex "import pagerduty_service_dependency.foo result": evaluating
2021/08/19 10:51:43 [TRACE] [walkImport] Entering eval tree: import pagerduty_service_dependency.foo result
2021/08/19 10:51:43 [TRACE] <root>: eval: *terraform.EvalSequence
2021/08/19 10:51:43 [TRACE] <root>: eval: *terraform.EvalGetProvider
2021/08/19 10:51:43 [TRACE] <root>: eval: *terraform.EvalRefresh
2021/08/19 10:51:43 [TRACE] GRPCProvider: ReadResource
2021/08/19 10:51:43 [INFO] Reading PagerDuty dependency
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xf20ec8]

goroutine 1841 [running]:
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyServiceDependencyRead(0xc00021ca10, {0x11215a0, 0xc00058a0a0})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/pagerduty/resource_pagerduty_service_dependency.go:203 +0x108
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc000126180, 0xc00029ebe0, {0x11215a0, 0xc00058a0a0})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/schema/resource.go:455 +0x1aa
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc00060ef00, {0xc0008cdf90, 0x503ae6}, 0xc0008cdf90)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin/grpc_provider.go:525 +0x34b
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0x1166600, 0xc00060ef00}, {0x145adf0, 0xc0002d3aa0}, 0xc000033aa0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5/tfplugin5.pb.go:3153 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0003ed340, {0x1470220, 0xc00061d980}, 0xc00075cc00, 0xc00099f020, 0x1c0d7b0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1210 +0xc8f
google.golang.org/grpc.(*Server).handleStream(0xc0003ed340, {0x1470220, 0xc00061d980}, 0xc00075cc00, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1533 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:871 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:869 +0x294
FAIL    github.com/terraform-providers/terraform-provider-pagerduty/pagerduty   29.748s
FAIL
make: *** [GNUmakefile:17: testacc] Error 1

@pdecat
Copy link
Contributor Author

pdecat commented Aug 19, 2021

I've added checks to avoid the panic. In real situation, the user will be able to retry in the import.

Turns out the https://api.pagerduty.com/service_dependencies/disassociate API is also inconsistent with https://api.pagerduty.com/service_dependencies/business_services when called rapidly after dependency creation for the same business service:

2021/08/19 15:16:43 [INFO] Reading PagerDuty dependency DHVZWMSK7PFONAHJR2
2021/08/19 15:16:44 [DEBUG] Waiting for state to become: [success]
2021/08/19 15:16:44 [DEBUG] PagerDuty - Preparing GET request to /service_dependencies/business_services/PL5N8W7 with body: %!s(<nil>)
2021/08/19 15:16:44 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
GET /service_dependencies/business_services/PL5N8W7 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip


-----------------------------------------------------
2021/08/19 15:16:44 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Content-Length: 265
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/vnd.pagerduty+json; charset=UTF-8
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 13:16:44 GMT
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: 5e93f25d4441b468e4d9358bcc0cf2f5
X-Xss-Protection: 1; mode=block
X-Xss-Protection: 1; mode=block

{
 "relationships": [
  {
   "dependent_service": {
    "id": "PL5N8W7",
    "relationships": null,
    "type": "business_service_reference"
   },
   "id": "DHVZWMSK7PFONAHJR2",
   "supporting_service": {
    "id": "P0C52BK",
    "relationships": null,
    "type": "technical_service_reference"
   },
   "type": "service_dependency"
  }
 ]
}
-----------------------------------------------------
2021/08/19 15:16:44 [DEBUG] provider has no plugin.Client
2021/08/19 15:16:44 [DEBUG] New state was assigned lineage "75eefbce-709e-0932-7546-6a47700cc8f3"
2021/08/19 15:16:44 [DEBUG] PagerDuty - Preparing GET request to /service_dependencies/business_services/PL5N8W7 with body: %!s(<nil>)
2021/08/19 15:16:44 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
GET /service_dependencies/business_services/PL5N8W7 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip


-----------------------------------------------------
2021/08/19 15:16:44 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Content-Length: 20
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/vnd.pagerduty+json; charset=UTF-8
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 13:16:44 GMT
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: 44faf74ae2abeb349a44463b1d010469
X-Xss-Protection: 1; mode=block
X-Xss-Protection: 1; mode=block

{
 "relationships": []
}
-----------------------------------------------------

...

2021/08/19 15:16:45 [INFO] Reading PagerDuty dependency DHVZWMSK7PFONAHJR2
2021/08/19 15:16:46 [DEBUG] Waiting for state to become: [success]
2021/08/19 15:16:46 [DEBUG] PagerDuty - Preparing GET request to /service_dependencies/business_services/PL5N8W7 with body: %!s(<nil>)
2021/08/19 15:16:46 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
GET /service_dependencies/business_services/PL5N8W7 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip


-----------------------------------------------------
2021/08/19 15:16:46 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Content-Length: 20
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/vnd.pagerduty+json; charset=UTF-8
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 13:16:46 GMT
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: 11a5ab9b38dd216a7864aa0df57825e3
X-Xss-Protection: 1; mode=block
X-Xss-Protection: 1; mode=block

{
 "relationships": []
}

2021/08/19 15:16:47 [INFO] Disassociating PagerDuty dependency PL5N8W7
2021/08/19 15:16:47 [DEBUG] PagerDuty - Preparing GET request to /service_dependencies/business_services/PL5N8W7 with body: %!s(<nil>)
2021/08/19 15:16:47 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
GET /service_dependencies/business_services/PL5N8W7 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip


-----------------------------------------------------
2021/08/19 15:16:47 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Content-Length: 265
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/vnd.pagerduty+json; charset=UTF-8
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 13:16:47 GMT
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: 8582efec73d39367f9a3fd425d8a5b6d
X-Xss-Protection: 1; mode=block
X-Xss-Protection: 1; mode=block

{
 "relationships": [
  {
   "dependent_service": {
    "id": "PL5N8W7",
    "relationships": null,
    "type": "business_service_reference"
   },
   "id": "DHVZWMSK7PFONAHJR2",
   "supporting_service": {
    "id": "P0C52BK",
    "relationships": null,
    "type": "technical_service_reference"
   },
   "type": "service_dependency"
  }
 ]
}
-----------------------------------------------------
2021/08/19 15:16:47 [DEBUG] PagerDuty - Preparing POST request to /service_dependencies/disassociate with body: {"relationships":[{"id":"DHVZWMSK7PFONAHJR2","type":"service_dependency","supporting_service":{"id":"P0C52BK","type":"service"},"dependent_service":{"id":"PL5N8W7","type":"business_service"}}]}
2021/08/19 15:16:47 [DEBUG] PagerDuty API Request Details:
---[ REQUEST ]---------------------------------------
POST /service_dependencies/disassociate HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Content-Length: 194
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip

{
 "relationships": [
  {
   "id": "DHVZWMSK7PFONAHJR2",
   "type": "service_dependency",
   "supporting_service": {
    "id": "P0C52BK",
    "type": "service"
   },
   "dependent_service": {
    "id": "PL5N8W7",
    "type": "business_service"
   }
  }
 ]
}

-----------------------------------------------------
2021/08/19 15:16:47 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 404 Not Found
Content-Length: 123
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json
Cross-Origin-Window-Policy: deny
Date: Thu, 19 Aug 2021 13:16:47 GMT
Server: nginx
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Request-Id: e075221ac589a3772d88d83b58abfb36
X-Xss-Protection: 1; mode=block

{
 "error": {
  "code": 2100,
  "errors": [
   "Business/Technical Service not found or lack permissions against"
  ],
  "message": "Not Found"
 }
}
-----------------------------------------------------
2021/08/19 15:16:47 [DEBUG] pagerduty_service_dependency.foo: apply errored, but we're indicating that via the Error pointer rather than returning it: POST API call to https://api.pagerduty.com/service_dependencies/disassociate failed 404 Not Found. Code: 2100, Errors: [Business/Technical Service not found or lack permissions against], Message: Not Found

When this happens, the acceptance test fails with the following error:

=== RUN   TestAccPagerDutyServiceDependency_import
    testing.go:654: Step 0 error: Check failed: Check 1/4 error: Service Dependency not found: DHVZWMSK7PFONAHJR2
    testing.go:715: Error destroying resource! WARNING: Dangling resources
        may exist. The full state and error is shown below.

        Error: errors during apply: POST API call to https://api.pagerduty.com/service_dependencies/disassociate failed 404 Not Found. Code: 2100, Errors: [Business/Technical Service not found or lack permissions against], Message: Not Found

        State: pagerduty_business_service.foo:
          ID = PL5N8W7
          provider = provider.pagerduty
          description = Managed by Terraform
          html_url =
          name = tf-nb62b
          point_of_contact =
          self = https://api.pagerduty.com/business_services/PL5N8W7
          summary = tf-nb62b
          type = business_service
        pagerduty_escalation_policy.foo:
          ID = PV14EFL
          provider = provider.pagerduty
          description = bar
          name = tf-2w1mm
          num_loops = 2
          rule.# = 1
          rule.0.escalation_delay_in_minutes = 10
          rule.0.id = P2A6V54
          rule.0.target.# = 1
          rule.0.target.0.id = PPKG8RH
          rule.0.target.0.type = user_reference
          teams.# = 0

          Dependencies:
            pagerduty_user.foo
        pagerduty_service.foo:
          ID = P0C52BK
          provider = provider.pagerduty
          acknowledgement_timeout = 1800
          alert_creation = create_incidents
          alert_grouping_parameters.# = 1
          alert_grouping_parameters.0.config.# = 1
          alert_grouping_parameters.0.config.0.aggregate =
          alert_grouping_parameters.0.config.0.fields.# = 0
          alert_grouping_parameters.0.config.0.timeout = 0
          alert_grouping_parameters.0.type =
          auto_resolve_timeout = 1800
          created_at = 2021-08-19T15:16:41+02:00
          description = foo
          escalation_policy = PV14EFL
          html_url = https://dev-claranet.pagerduty.com/service-directory/P0C52BK
          incident_urgency_rule.# = 1
          incident_urgency_rule.0.during_support_hours.# = 0
          incident_urgency_rule.0.outside_support_hours.# = 0
          incident_urgency_rule.0.type = constant
          incident_urgency_rule.0.urgency = high
          last_incident_timestamp =
          name = tf-9pcvj
          scheduled_actions.# = 0
          status = active
          support_hours.# = 0

          Dependencies:
            pagerduty_escalation_policy.foo
        pagerduty_service_dependency.foo:
          ID = DHVZWMSK7PFONAHJR2
          provider = provider.pagerduty
          dependency.# = 1
          dependency.0.dependent_service.# = 1
          dependency.0.dependent_service.0.id = PL5N8W7
          dependency.0.dependent_service.0.type = business_service
          dependency.0.supporting_service.# = 1
          dependency.0.supporting_service.0.id = P0C52BK
          dependency.0.supporting_service.0.type = service
          dependency.0.type =
        pagerduty_user.foo:
          ID = PPKG8RH
          provider = provider.pagerduty
          avatar_url = https://secure.gravatar.com/avatar/f90d736c65a6faa5a1e19616d2ca8b81.png?d=mm&r=PG
          color = green
          description = foo
          email = tf-bafr4@foo.com
          html_url = https://dev-claranet.pagerduty.com/users/PPKG8RH
          invitation_sent = true
          job_title = foo
          name = tf-bafr4
          role = user
          teams.# = 0
          time_zone = Europe/Paris

@pdecat
Copy link
Contributor Author

pdecat commented Aug 19, 2021

I've added checks to avoid crashing the provider and leave dangling resources when disassociation fails.

Tests sometimes still fail, but it feels way less critical like that:

make testacc TEST=./pagerduty TESTARGS='-run=TestAccPagerDutyServiceDependency_import -count=10'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./pagerduty -v -run=TestAccPagerDutyServiceDependency_import -count=10 -timeout 120m
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (27.80s)
=== RUN   TestAccPagerDutyServiceDependency_import
    testing.go:654: Step 1 error: dependent service not found for dependency: P2BYY36.business_service.DHW074F30HAJ676SGV
--- FAIL: TestAccPagerDutyServiceDependency_import (21.91s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (24.85s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (19.33s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (18.90s)
=== RUN   TestAccPagerDutyServiceDependency_import
    testing.go:654: Step 1 error: dependent service not found for dependency: P203S14.business_service.DHW087CKWRL4VJ3GDL
--- FAIL: TestAccPagerDutyServiceDependency_import (18.61s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (24.66s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (19.04s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (18.62s)
=== RUN   TestAccPagerDutyServiceDependency_import
--- PASS: TestAccPagerDutyServiceDependency_import (21.60s)
FAIL
FAIL    github.com/terraform-providers/terraform-provider-pagerduty/pagerduty   215.335s
FAIL
make: *** [GNUmakefile:17: testacc] Error 1

@pdecat
Copy link
Contributor Author

pdecat commented Sep 10, 2021

Rebased on master, @stmcallister PTAL.

…ference` error:

```
make testacc TEST=./pagerduty TESTARGS='-run=TestAccPagerDutyServiceDependency_import -count=1'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./pagerduty -v -run=TestAccPagerDutyServiceDependency_import -count=1 -timeout 120m
=== RUN   TestAccPagerDutyServiceDependency_import
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xf20d08]

goroutine 365 [running]:
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyServiceDependencyRead(0xc0002de690, {0x11215a0, 0xc000372280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/pagerduty/resource_pagerduty_service_dependency.go:195 +0x108
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc00014df80, 0xc00090b360, {0x11215a0, 0xc000372280})
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/schema/resource.go:455 +0x1aa
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc000010878, {0xc00090b270, 0x503ae6}, 0xc00090b270)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin/grpc_provider.go:525 +0x34b
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler({0x1166600, 0xc000010878}, {0x145ad90, 0xc000aaa690}, 0xc00039c9c0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5/tfplugin5.pb.go:3153 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000b361c0, {0x14701c0, 0xc000298600}, 0xc0007ac300, 0xc0008e8150, 0x1c0d7b0, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1210 +0xc8f
google.golang.org/grpc.(*Server).handleStream(0xc000b361c0, {0x14701c0, 0xc000298600}, 0xc0007ac300, 0x0)
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:1533 +0xa2a
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:871 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/patrick/go/src/github.com/PagerDuty/terraform-provider-pagerduty/vendor/google.golang.org/grpc/server.go:869 +0x294
FAIL    github.com/terraform-providers/terraform-provider-pagerduty/pagerduty   11.996s
FAIL
make: *** [GNUmakefile:17: testacc] Error 1
```
…e_dependencies/* APIs are eventually consistent
… as everything is already available in /service_dependencies/associate response
@pdecat pdecat force-pushed the fix_service_dependencies branch from 5c80209 to 45e9c1c Compare September 28, 2021 06:52
Copy link
Contributor

@stmcallister stmcallister left a comment

Choose a reason for hiding this comment

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

Thank you! Totally appreciate the resources and tests being more stable. 🎉

@stmcallister stmcallister merged commit 0ae400a into PagerDuty:master Oct 8, 2021
@pdecat pdecat deleted the fix_service_dependencies branch October 8, 2021 06:38
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 this pull request may close these issues.

2 participants