pactflow / terraform-provider-pact

Terraform provider for Pact Broker (and Pactflow)
https://pactflow.io
MIT License
24 stars 4 forks source link

pact_webhook resource import fails due to `request.body` parsing issues #12

Closed vgrigoruk closed 3 years ago

vgrigoruk commented 3 years ago

I'm unable to import existing webhooks from pactflow broker. Pact broker API returns webhook with request.body as a JSON object, while https://github.com/pactflow/terraform-provider-pact/blob/c6288d6eaf3611a9c7822d3b3b3a32f056febd2a/broker/webhook.go#L15 expects it to be a string (e.g. this is valid in case of XML bodies). That's why unmarshalling fails (see debug output below)

Terraform Version

❯ terraform -v
Terraform v0.14.3
+ provider registry.terraform.io/pactflow/pact v0.1.4

Affected Resource(s)

Terraform Configuration Files

resource "pact_webhook" "set_github_status_when_contract_published_for_provider_master_branch" {
}

Debug Output

1-22T13:12:28.413+0100 [DEBUG] plugin.terraform-provider-pact_v0.1.4: 2021/01/22 13:12:28 [DEBUG] error decoding response for /webhooks/<webhook_id_goes_here> . Error json: cannot unmarshal object into Go struct field Request.request.body of type string
2021-01-22T13:12:28.413+0100 [DEBUG] plugin.terraform-provider-pact_v0.1.4: 2021/01/22 13:12:28 [DEBUG] response from reading webhook &{ID: Description:Set github status when contract published - master Enabled:true CreatedAt:2019-05-23T12:59:06+00:00 Provider:<nil> Consumer:<nil> Events:[{Name:contract_published}] Request:{Method:POST URL:https://api.buildkite.com/v2/organizations/<org_name>/pipelines/pact-github-hooks/builds Username: Password: Headers:map[Authorization:Bearer ${user.BuildkiteApiToken} Content-Type:application/json] Body:}}
2021-01-22T13:12:28.413+0100 [DEBUG] plugin.terraform-provider-pact_v0.1.4: 2021/01/22 13:12:28 [ERROR] webhook read failed json: cannot unmarshal object into Go struct field Request.request.body of type string
2021/01/22 13:12:28 [WARN] Provider "registry.terraform.io/pactflow/pact" produced an unexpected new value for pact_webhook.set_github_status_when_contract_published_for_provider_master_branch during refresh.
      - Root resource was present, but now absent

Error: Cannot import non-existent remote object

Expected Behavior

As request.body could be either a string (e.g. for XML bodies) or JSON object, API client should detect when request.body is an object and only then convert it to a string.

Actual Behavior

API client fails to parse response from pact broker, which makes it impossible to import these resources.

Steps to Reproduce

  1. terraform import pact_webhook.resource_name webhook_id
mefellows commented 3 years ago

Thanks @vgrigoruk, looks like the fix for https://github.com/pactflow/terraform-provider-pact/issues/6 went too far the other way. I'll ensure both cases are considered in a fix, and re-test the import feature.

If it's possible, a simpler approach would be to delete the webhook from your broker for now, and re-apply this way (but I understand the need for an import feature!)

mefellows commented 3 years ago

I think I've fixed this in #507d4fb. I'll push out a release shorly.

mefellows commented 3 years ago

Fixed in v0.1.5 - I'm going to add some acceptance tests for importing resources as we don't currently have those.