Skip to content

Commit

Permalink
Amazon Q checks and Data Source
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMoyles committed Jan 21, 2025
1 parent 59665bb commit 696c9cc
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 104 deletions.
39 changes: 5 additions & 34 deletions docs/resources/knowledge_document_variation.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ resource "genesyscloud_knowledge_document_variation" "example_document_variation
### Required

- `knowledge_base_id` (String) Knowledge base id of the label
- `knowledge_document_id` (String) Knowledge base id of the label
- `knowledge_document_id` (String) Knowledge document id of the label
- `knowledge_document_variation` (Block List, Min: 1, Max: 1) Knowledge document variation (see [below for nested schema](#nestedblock--knowledge_document_variation))

### Optional

- `contexts` (Block List) The context values associated with the variation (see [below for nested schema](#nestedblock--contexts))
- `published` (Boolean) If true, the document will be published with the new variation. If false, the updated document will be in a draft state.

### Read-Only
Expand Down Expand Up @@ -215,7 +214,7 @@ Optional:

Required:

- `text` (String) Text.
- `text` (String) Text

Optional:

Expand Down Expand Up @@ -332,7 +331,7 @@ Optional:

Required:

- `text` (String) Text.
- `text` (String) Text

Optional:

Expand Down Expand Up @@ -428,15 +427,15 @@ Optional:

Required:

- `id` (String) The globally unique identifier for the knowledge context
- `context_id` (String) The globally unique identifier for the knowledge context


<a id="nestedblock--knowledge_document_variation--contexts--values"></a>
### Nested Schema for `knowledge_document_variation.contexts.values`

Required:

- `id` (String) The globally unique identifier for the knowledge context value
- `value_id` (String) The globally unique identifier for the knowledge context value



Expand All @@ -447,31 +446,3 @@ Optional:

- `id` (String) Id



<a id="nestedblock--contexts"></a>
### Nested Schema for `contexts`

Required:

- `context` (Block List, Min: 1) The knowledge context associated with the variation (see [below for nested schema](#nestedblock--contexts--context))

Optional:

- `values` (Block List) The list of knowledge context values associated with the variation (see [below for nested schema](#nestedblock--contexts--values))

<a id="nestedblock--contexts--context"></a>
### Nested Schema for `contexts.context`

Required:

- `context_id` (String) The globally unique identifier for the knowledge context


<a id="nestedblock--contexts--values"></a>
### Nested Schema for `contexts.values`

Required:

- `value_id` (String) The globally unique identifier for the knowledge context value

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package knowledgedocumentvariation

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strings"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"time"
)

func dataSourceKnowledgeDocumentVariationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
proxy := newVariationRequestProxy(sdkConfig)

name := d.Get("name").(string)
knowledgeBaseID := d.Get("knowledge_base_id").(string)
fullID := d.Get("knowledge_document_id").(string)
knowledgeDocumentId := strings.Split(fullID, ",")[0]

return util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
documentVariationRequestId, resp, retryable, err := proxy.getVariationRequestIdByName(ctx, name, knowledgeBaseID, knowledgeDocumentId)

if err != nil && !retryable {
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(ResourceType, fmt.Sprintf("Error searching variation request %s | error: %s", name, err), resp))
}

if retryable {
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(ResourceType, fmt.Sprintf("No variation request found with name %s", name), resp))
}

d.SetId(documentVariationRequestId)
return nil
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package knowledgedocumentvariation

import (
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"strings"
gcloud "terraform-provider-genesyscloud/genesyscloud"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"testing"
)

func TestAccDataSourceVariationRequest(t *testing.T) {
var (
// Knowledge Base
knowledgeBaseResourceLabel1 = "test-knowledgebase1"
knowledgeBaseName1 = uuid.NewString()
knowledgeBaseDescription1 = "test-knowledgebase-description1"
coreLanguage1 = "en-US"

// Knowledge Document
knowledgeDocumentResourceLabel1 = "test-knowledge-document1"
title = "Terraform Knowledge Document"
visible = true
docPublished = false
phrase = "Terraform Knowledge Document"
autocomplete = true

// Knowledge Document Variation
variationResourceLabel = "test-variation"
published = true
bodyBlockType = "Paragraph"
contentBlockType1 = "Text"
imageUrl = "https://example.com/image"
hyperlink = "https://example.com/hyperlink"
videoUrl = "https://example.com/video"
listType = "ListItem"
documentText = "stuff"
marks = []string{"Bold", "Italic", "Underline"}
name = "Terraform Test Knowledge Document Variation"
contextId = uuid.NewString()
valueId = uuid.NewString()
paragraphTestProperties = map[string]string{
"fSize": "Large",
"fType": "Heading1",
"tColor": "#FFFFFF",
"bgColor": "#000000",
"align": "Right",
"indent": "3.14",
}
)

resource.Test(t, resource.TestCase{
PreCheck: func() { util.TestAccPreCheck(t) },
ProviderFactories: provider.GetProviderFactories(providerResources, providerDataSources),
Steps: []resource.TestStep{
{
// Create Type List
Config: gcloud.GenerateKnowledgeKnowledgebaseResource(
knowledgeBaseResourceLabel1,
knowledgeBaseName1,
knowledgeBaseDescription1,
coreLanguage1,
) +
generateKnowledgeDocumentBasic(
knowledgeDocumentResourceLabel1,
knowledgeBaseResourceLabel1,
title,
visible,
docPublished,
phrase,
autocomplete,
) + generateKnowledgeDocumentVariation(
variationResourceLabel,
knowledgeBaseResourceLabel1,
knowledgeDocumentResourceLabel1,
published,
bodyBlockType,
contentBlockType1,
imageUrl,
hyperlink,
videoUrl,
listType,
documentText,
marks,
name,
contextId,
valueId,
paragraphTestProperties,
) + generateKnowledgeDocumentVariationDataSource(
variationResourceLabel,
name,
knowledgeBaseResourceLabel1,
knowledgeDocumentResourceLabel1,
),
Check: resource.ComposeTestCheckFunc(
// As the ID is a concatenation of multiple IDs, this function will be used to test the ids
func(state *terraform.State) error {

// Get the Resource ID
rs, ok := state.RootModule().Resources[ResourceType+"."+variationResourceLabel]
if !ok {
return fmt.Errorf("not found: %s", ResourceType+"."+variationResourceLabel)
}
id := rs.Primary.ID

// Split the IDs
resourceIDs, err := parseResourceIDs(id)
if err != nil {
return err
}

// Get the Data Source ID
rs2, ok := state.RootModule().Resources["data."+ResourceType+"."+variationResourceLabel]
if !ok {
return fmt.Errorf("not found: %s", ResourceType+"."+variationResourceLabel)
}

variationID := rs2.Primary.ID
knowledgeBaseID := rs2.Primary.Attributes["knowledge_base_id"]
KnowledgeDocumentID := strings.Split(rs2.Primary.Attributes["knowledge_document_id"], ",")[0]

// Ensure IDs are equal
assert.Equal(t, resourceIDs.variationID, variationID, "Variation ID should be equal")
assert.Equal(t, resourceIDs.knowledgeBaseID, knowledgeBaseID, "Knowledge Base ID should be equal")
assert.Equal(t, resourceIDs.knowledgeDocumentID, KnowledgeDocumentID, "Knowledge Document ID should be equal")
return nil
},
),
},
{
// Import/Read
ResourceName: ResourceType + "." + variationResourceLabel,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func generateKnowledgeDocumentVariationDataSource(resourceLabel, variationName, knowledgeBaseID, knowledgeDocumentID string) string {
return fmt.Sprintf(`data "genesyscloud_knowledge_document_variation" "%s" {
knowledge_base_id = genesyscloud_knowledge_knowledgebase.%s.id
knowledge_document_id = genesyscloud_knowledge_document.%s.id
name = "%s"
}
`, resourceLabel, knowledgeBaseID, knowledgeDocumentID, variationName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (

// providerResources holds a map of all registered resources
var providerResources map[string]*schema.Resource
var providerDataSources map[string]*schema.Resource

type registerTestInstance struct {
resourceMapMutex sync.RWMutex
resourceMapMutex sync.RWMutex
datasourceMapMutex sync.RWMutex
}

// registerTestResources registers all resources used in the tests
Expand All @@ -27,13 +29,23 @@ func (r *registerTestInstance) registerTestResources() {
providerResources[knowledgeDocument.ResourceType] = knowledgeDocument.ResourceKnowledgeDocument()
}

// registerTestDataSources registers all data sources used in the tests.
func (r *registerTestInstance) registerTestDataSources() {
r.datasourceMapMutex.Lock()
defer r.datasourceMapMutex.Unlock()

providerDataSources[ResourceType] = dataSourceKnowledgeDocumentVariation()
}

// initTestResources initializes all test resources and data sources.
func initTestResources() {
providerResources = make(map[string]*schema.Resource)
providerDataSources = make(map[string]*schema.Resource)

regInstance := &registerTestInstance{}

regInstance.registerTestResources()
regInstance.registerTestDataSources()
}

// TestMain is a "setup" function called by the testing framework when run the test
Expand Down
Loading

0 comments on commit 696c9cc

Please sign in to comment.