Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Address issue hashicorp#19621 in discoveryengine module #21628

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/13021.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
discoveryengine: fixed bug preventing creation of `google_discovery_engine_target_site` resources
```
34 changes: 16 additions & 18 deletions google/services/discoveryengine/discovery_engine_operation.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package discoveryengine

import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
Expand All @@ -36,10 +22,22 @@ type DiscoveryEngineOperationWaiter struct {

func (w *DiscoveryEngineOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
return nil, fmt.Errorf("cannot query operation, it's unset or nil")
}

opName := w.CommonOperationWaiter.Op.Name

// Extract location from opName. Example:
// "projects/<project>/locations/<location>/operations/<operation-id>"
// Requires error handling if not properly formatted.
parts := strings.Split(opName, "/")
if len(parts) < 5 || parts[2] != "locations" {
return nil, fmt.Errorf("unexpected operation name format: %s", opName) // Handle invalid format appropriately
}
// Returns the proper get.
url := fmt.Sprintf("%s%s", w.Config.DiscoveryEngineBasePath, w.CommonOperationWaiter.Op.Name)
location := parts[3]

basePath := strings.Replace(w.Config.DiscoveryEngineBasePath, "{{location}}", location, 1)
url := fmt.Sprintf("%s%s", basePath, opName)

return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: w.Config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,29 @@ func resourceDiscoveryEngineTargetSiteCreate(d *schema.ResourceData, meta interf
}
d.SetId(id)

err = DiscoveryEngineOperationWaitTime(
config, res, project, "Creating TargetSite", userAgent,
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = DiscoveryEngineOperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating TargetSite", userAgent,
d.Timeout(schema.TimeoutCreate))

if err != nil {
// The resource didn't actually create
d.SetId("")

return fmt.Errorf("Error waiting to create TargetSite: %s", err)
}

// `name` is autogenerated from the api so needs to be set post-create
name, ok := res["name"]
if !ok {
respBody, ok := res["response"]
if !ok {
return fmt.Errorf("Create response didn't contain critical fields. Create may not have succeeded.")
}

name, ok = respBody.(map[string]interface{})["name"]
if !ok {
return fmt.Errorf("Create response didn't contain critical fields. Create may not have succeeded.")
}
if err := d.Set("name", flattenDiscoveryEngineTargetSiteName(opRes["name"], d, config)); err != nil {
return err
}
if err := d.Set("name", name.(string)); err != nil {
return fmt.Errorf("Error setting name: %s", err)

// This may have caused the ID to update - update it if so.
id, err = tpgresource.ReplaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(name.(string))
d.SetId(id)

log.Printf("[DEBUG] Finished creating TargetSite %q: %#v", d.Id(), res)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func testAccDiscoveryEngineTargetSite_discoveryengineTargetsiteBasicExample(cont
resource "google_discovery_engine_target_site" "basic" {
location = google_discovery_engine_data_store.basic.location
data_store_id = google_discovery_engine_data_store.basic.data_store_id
provided_uri_pattern = "http://cloud.google.com/docs/*"
provided_uri_pattern = "cloud.google.com/docs/*"
type = "INCLUDE"
exact_match = false
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func testAccDiscoveryEngineTargetSite_discoveryengineTargetsiteAdvancedExample(c
resource "google_discovery_engine_target_site" "advanced" {
location = google_discovery_engine_data_store.advanced.location
data_store_id = google_discovery_engine_data_store.advanced.data_store_id
provided_uri_pattern = "http://cloud.google.com/docs/*"
provided_uri_pattern = "cloud.google.com/docs/*"
type = "INCLUDE"
exact_match = false
}
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/discovery_engine_target_site.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ To get more information about TargetSite, see:
resource "google_discovery_engine_target_site" "basic" {
location = google_discovery_engine_data_store.basic.location
data_store_id = google_discovery_engine_data_store.basic.data_store_id
provided_uri_pattern = "http://cloud.google.com/docs/*"
provided_uri_pattern = "cloud.google.com/docs/*"
type = "INCLUDE"
exact_match = false
}
Expand Down Expand Up @@ -70,7 +70,7 @@ resource "google_discovery_engine_data_store" "basic" {
resource "google_discovery_engine_target_site" "advanced" {
location = google_discovery_engine_data_store.advanced.location
data_store_id = google_discovery_engine_data_store.advanced.data_store_id
provided_uri_pattern = "http://cloud.google.com/docs/*"
provided_uri_pattern = "cloud.google.com/docs/*"
type = "INCLUDE"
exact_match = false
}
Expand Down