Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
807 stars 217 forks source link

Create, update and delete using single resource #196

Open dwivedi6 opened 2 years ago

dwivedi6 commented 2 years ago

Hello Team, Thank you for your contribution for developing this work and it is quite useful. I was trying to create, update and delete ranger policies(https://docs.cloudera.com/HDPDocuments/HDP3/HDP-3.1.0/ranger-apis/content/create_policy.html) using terraform and was able to do when I create separate provider for separate operation and called on separate resource.

Provider for creating ranger policy

provider "restapi" { alias = "postinsert" uri=var.devuri username = var.devusername password = var.devpassword debug = "true" write_returns_object = "true" create_returns_object = "true" id_attribute = "api_response/id" }

Provider for deleting policy

provider "restapi" { alias = "putdelete" uri=var.devuri username = var.devusername password = var.devpassword debug = "true" destroy_method = "DELETE" create_method = "DELETE" }

Provider for updating policy

provider "restapi" { alias = "putupdate" uri=var.devuri username = var.devusername password = var.devpassword debug = "true" update_method = "PUT" create_method = "PUT" } -------------------------------------------------Creating policy--

resource "restapi_object" "policy-insert" { count = var.policy_insert ? 1 : 0 provider = restapi.postinsert path = "/service/public/v2/api/policy" create_path = "/service/public/v2/api/policy" destroy_path = "/service/public/v2/api/policy/{id}" update_path = "/service/public/v2/api/policy/{id}" create_method = "POST" update_method = "PUT" destroy_method = "DELETE" id_attribute = "api_response/id" object_id = "api_response/id" data = jsonencode( { "service": "${var.servicename}", "name": "${var.policyname}", "policyType": 0, "policyPriority": 0, "description": "creating new policy", ......... } ) } -------------------Updating policy-------- resource "restapi_object" "policy-update" { count = var.policy_update ? 1 : 0 provider = restapi.putupdate path = "/service/public/v2/api/policy/{id}" update_path="/service/public/v2/api/policy/{id}" read_path="/service/public/v2/api/policy/{id}" update_method = "PUT" read_method = "GET" id_attribute =var.ID object_id = var.ID data =jsonencode( { "isEnabled": true, "service": "${var.servicename}", "name": "test-policy", })} ------ Deleting policy ------------------------- resource "restapi_object" "policy-delete" { count = var.policy_delete ? 1 : 0 provider = restapi.putdelete path = "/service/public/v2/api/policy/{id}" destroy_path = "/service/public/v2/api/policy/{id}" destroy_method = "DELETE" id_attribute = var.ID object_id = var.ID debug = "true" data ="{}" }

In this example, create and update working fine however it gives error while deleting the policy because after deleting the policies, it calls GET method.

api_data: (map[string]interface {}) { }: timestamp=2022-09-01T12:15:57.757+0530 2022-09-01T12:15:57.939+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: method='DELETE', path='/service/public/v2/api/policy/85', full uri (derived)='https://devecpvm017952.server.banksvcs.net:6182/service/public/v2/api/policy/85', data='{}': timestamp=2022-09-01T12:15:57.757+0530 2022-09-01T12:15:57.967+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: Sending HTTP request to https://devecpvm017952.server.banksvcs.net:6182/service/public/v2/api/policy/85...: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:57.969+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: Request headers:: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:57.970+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: Content-Type: application/json: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:57.971+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: Authorization: Basic YWRtaW46cmFuZ2VyQGRtaW4xMjM=: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:58.007+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 api_client.go: BODY:: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:58.023+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 {}: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:58.026+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:57 Waiting for rate limit availability: timestamp=2022-09-01T12:15:57.758+0530 2022-09-01T12:15:58.432+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response code: 404: timestamp=2022-09-01T12:15:58.431+0530 2022-09-01T12:15:58.432+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response headers:: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.432+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Xss-Protection: 1; mode=block: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.432+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Strict-Transport-Security: max-age=31536000 ; includeSubDomains: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.432+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Cache-Control: no-cache, no-store, max-age=0, must-revalidate: timestamp=2022-09-01T12:15:58.432+0530 restapi_object.policy-update[0]: Destruction complete after 0s 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Pragma: no-cache: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Content-Length: 0: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Date: Thu, 01 Sep 2022 06:45:58 GMT: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Set-Cookie: RANGERADMINSESSIONID=761E9918D11CE595E1D708CB38DC2AAD; Path=/; Secure; HttpOnly: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Frame-Options: DENY: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Server: Apache Ranger: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Content-Type-Options: nosniff: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src 'self'; img-src 'self'; style-src 'self' 'unsafe-inline';font-src 'self': timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Expires: 0: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.433+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: BODY:: timestamp=2022-09-01T12:15:58.432+0530 2022-09-01T12:15:58.484+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response code: 204: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.484+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response headers:: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.484+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Content-Type-Options: nosniff: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.484+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Date: Thu, 01 Sep 2022 06:45:58 GMT: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.485+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Frame-Options: DENY: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.485+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Xss-Protection: 1; mode=block: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.486+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Strict-Transport-Security: max-age=31536000 ; includeSubDomains: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.486+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Pragma: no-cache: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.486+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Server: Apache Ranger: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.486+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Set-Cookie: RANGERADMINSESSIONID=1CB982FB4A88B4DB7CA9A4535D6A4C1D; Path=/; Secure; HttpOnly: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.488+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src 'self'; img-src 'self'; style-src 'self' 'unsafe-inline';font-src 'self': timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.489+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Cache-Control: no-cache, no-store, max-age=0, must-revalidate: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Expires: 0: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: BODY:: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_object.go: Requesting created object from API (write_returns_object=false, create_returns_object=false)...: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: method='GET', path='/service/public/v2/api/policy/85/85', full uri (derived)='https://devecpvm017952.server.banksvcs.net:6182/service/public/v2/api/policy/85/85', data='': timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Sending HTTP request to https://devecpvm017952.server.banksvcs.net:6182/service/public/v2/api/policy/85/85...: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Request headers:: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Authorization: Basic YWRtaW46cmFuZ2VyQGRtaW4xMjM=: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: BODY:: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 : timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.494+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 Waiting for rate limit availability: timestamp=2022-09-01T12:15:58.483+0530 2022-09-01T12:15:58.538+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response code: 404: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.553+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Response headers:: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.554+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Frame-Options: DENY: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.558+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Strict-Transport-Security: max-age=31536000 ; includeSubDomains: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.558+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Cache-Control: no-cache, no-store, max-age=0, must-revalidate: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Pragma: no-cache: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Expires: 0: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Content-Length: 0: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Server: Apache Ranger: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Set-Cookie: RANGERADMINSESSIONID=387622184F4D8CD46AFF49FF0583E461; Path=/; Secure; HttpOnly: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.559+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src 'self'; img-src 'self'; style-src 'self' 'unsafe-inline';font-src 'self': timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Content-Type-Options: nosniff: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: Date: Thu, 01 Sep 2022 06:45:58 GMT: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: X-Xss-Protection: 1; mode=block: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_client.go: BODY:: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [INFO] provider.terraform-provider-restapi_v1.17.0.exe: 2022/09/01 12:15:58 api_object.go: 404 error while refreshing state for '85' at path '/service/public/v2/api/policy/{id}/{id}'. Removing from state.: timestamp=2022-09-01T12:15:58.529+0530 2022-09-01T12:15:58.560+0530 [ERROR] vertex "restapi_object.policy-delete[0]" error: Provider produced inconsistent result after apply ╷ │ Error: Provider produced inconsistent result after apply │ │ When applying changes to restapi_object.policy-delete[0], provider │ "provider[\"registry.terraform.io/mastercard/restapi\"].putdelete" produced an unexpected new value: Root resource was present, │ but now absent. │ │ This is a bug in the provider, which should be reported in the provider's own issue tracker.

If you could help to resolve the issue? Is it possible to call all the operation in single provider and resource?

jgrumboe commented 2 years ago

Would #182 help you to solve the problem?

dwivedi6 commented 2 years ago

Thank you for your response. Could you please release new version so I can validate update_data and delete_data.

jgrumboe commented 2 years ago

I'm not a maintainer of this project but released my changes meanwhile to TF registry to my private account. See here https://github.com/Mastercard/terraform-provider-restapi/pull/182#issuecomment-1216278310

jgrumboe commented 8 months ago

@dwivedi6 I think this issue is resolved with v1.18.0 and can be closed.