harness / terraform-provider-harness

Terraform provider for provisioning Harness resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent behaviour: re-run "terraform apply" doesn't re-create deleted project

james-ni opened this issue · comments

Terraform Version

1.5.6

Harness Provider Version

0.29.2

Affected Resource(s)

Only harness_platform_project is affected.

Other resource types like harness_platform_service, harness_platform_variable are behaving as expected.

Terraform Configuration Files

terraform {
  required_providers {
    harness = {
      source = harness/harness
    }
  }
}

provider "harness" {
  endpoint      = "https://app.harness.io/gateway"
  account_id  = "xxxxxxx"
}

resource "harness_platform_project" "project" {
  identifier     = "james123"
  name           = "james
  description = "james"
  org_id         = "org1"
}

Expected Behavior

When:

  1. terraform init
  2. terraform apply --auto-approve (this will create the project)
  3. manually delete project from Harness UI
  4. terraform apply --auto-approve

I'm expecting step 4 (re-run terraform apply) will create the project.

Actual Behavior

step 4terraform apply failed, with error message "404 Not Found"

This behaviour is inconsistent with other Harness resources like service, variable...

Possible root cause

Harness GET project behaves differently from other GET xxx APIs (e.g. service, variable...)

  • GET project returns payload {"code": "ENTITY_NOT_FOUND"}, when a given project id doesn't exist
  • GET service (or any other resources) returns payload {"code": "RESOURCE_NOT_FOUND_EXCEPTION"}, when a given service id doesn't exist

The error handling code below only handles {"code": "RESOURCE_NOT_FOUND_EXCEPTION"}, not {"code": "ENTITY_NOT_FOUND"}. That's why it failed to re-create project and error out.

if erro.Code() == nextgen.ErrorCodes.ResourceNotFound {
d.SetId("")
d.MarkNewResource()
return nil

Actually there is another exception

  • GET resource_group API returns payload {"code": "RESOURCE_NOT_FOUND"}, when a given resource group id doesn't exist.

Proposed Change

  • option 1: add {"code": "ENTITY_NOT_FOUND"} to the if condition, like
      if erro.Code() == nextgen.ErrorCodes.ResourceNotFound || erro.Code() == "ENTITY_NOT_FOUND" {
      	d.SetId("")
      	d.MarkNewResource()
      	return nil
      }
  • option 2: modify the GET project API, so that if returns {"code": "RESOURCE_NOT_FOUND_EXCEPTION"} when given project id doesn't exist