MyPureCloud / terraform-provider-genesyscloud

Terraform Provider Genesyscloud
MIT License
35 stars 78 forks source link

knowledge_document is created as a draft but is not under state when published flag is true #1166

Open ric-go opened 2 months ago

ric-go commented 2 months ago

Hi,

I'm trying to find the best way to create large knowledge bases. A document can't be published without variation, which is understandable and the provider throws an error at the document creation if the published flag is true, but the document is created as draft and is not added to the state, which is weird and could lead to duplicate values.

I think the document shouldn't be created, or should be added to the state.

Here is the example:

terraform {
  required_version = "~> 1.8.0"
  required_providers {
    genesyscloud = {
      source  = "MyPureCloud/genesyscloud"
      version = "~> 1.41.0"
    }
  }
}

variable "published_document_variations" {
  default     = true
  nullable    = false
  sensitive   = false
  type        = bool
}

variable "published_documents" {
  default     = true
  nullable    = false
  sensitive   = false
  type        = bool
}

resource "genesyscloud_knowledge_knowledgebase" "essai" {
  core_language = "fr-FR"
  description   = ""
  name          = "Essai"
  published     = true
}

resource "genesyscloud_knowledge_document" "essai_1" {
  knowledge_base_id = genesyscloud_knowledge_knowledgebase.essai.id
  published         = var.published_documents
  knowledge_document {
    category_name = ""
    label_names   = []
    title         = "Essai 1"
    visible       = true
    alternatives {
      autocomplete = false
      phrase       = "Essai 1"
    }
  }
}

resource "genesyscloud_knowledge_document_variation" "essai_1" {
  knowledge_base_id     = genesyscloud_knowledge_knowledgebase.essai.id
  knowledge_document_id = genesyscloud_knowledge_document.essai_1.id
  published             = var.published_document_variations
  knowledge_document_variation {
    body {
      blocks {
        type = "Paragraph"
        paragraph {
          blocks {
            text {
              text = "Essai 1."
            }
            type = "Text"
          }
        }
      }
    }
  }
}

And the command line results:

PS C:\essais> terraform apply     

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # genesyscloud_knowledge_document.essai_1 will be created
  + resource "genesyscloud_knowledge_document" "essai_1" {
      + id                = (known after apply)
      + knowledge_base_id = (known after apply)
      + published         = true

      + knowledge_document {
          + label_names   = []
          + title         = "Essai 1"
          + visible       = true
            # (1 unchanged attribute hidden)

          + alternatives {
              + autocomplete = false
              + phrase       = "Essai 1"
            }
        }
    }

  # genesyscloud_knowledge_document_variation.essai_1 will be created
  + resource "genesyscloud_knowledge_document_variation" "essai_1" {
      + id                    = (known after apply)
      + knowledge_base_id     = (known after apply)
      + knowledge_document_id = (known after apply)
      + published             = true

      + knowledge_document_variation {
          + body {
              + blocks {
                  + type = "Paragraph"

                  + paragraph {
                      + blocks {
                          + type = "Text"

                          + text {
                              + text = "Essai 1."
                            }
                        }
                    }
                }
            }
        }
    }

  # genesyscloud_knowledge_knowledgebase.essai will be created
  + resource "genesyscloud_knowledge_knowledgebase" "essai" {
      + core_language = "fr-FR"
      + id            = (known after apply)
      + name          = "Essai"
        # (1 unchanged attribute hidden)
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions in workspace "dev"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

genesyscloud_knowledge_knowledgebase.essai: Creating...
genesyscloud_knowledge_knowledgebase.essai: Creation complete after 5s [id=bf16b32e-b1a6-49e6-ba07-87df91ff9f4e]
genesyscloud_knowledge_document.essai_1: Creating...
╷
│ Error: Failed to publish knowledge document error: %!s(<nil>)
│
│   with genesyscloud_knowledge_document.essai_1,
│   on resources_knowledge_documents.tf line 1, in resource "genesyscloud_knowledge_document" "essai_1":
│    1: resource "genesyscloud_knowledge_document" "essai_1" {
│
│ {"resourceName":"genesyscloud_knowledge_document","method":"POST","path":"/api/v2/knowledge/knowledgebases/bf16b32e-b1a6-49e6-ba07-87df91ff9f4e/documents/31f71e4d-3db4-4f06-8480-6033855dd142/versions","statusCode":400,"errorMessage":"API
│ Error: 400 - Document 31f71e4d-3db4-4f06-8480-6033855dd142 has no variation, which is mandatory to publish a document
│ (c54e2480-8184-4d90-8a3b-6835cd16628d)","correlationId":"c54e2480-8184-4d90-8a3b-6835cd16628d"}
╵
PS C:\essais> terraform state list
genesyscloud_knowledge_knowledgebase.essai
charliecon commented 1 month ago

Hi @ric-go

This is a bug within the provider. We'll create a ticket and get a dev working on it as soon as we can. Thanks for bringing this to our attention.

-Charlie

(Tracking with DEVTOOLING-784)