|
| 1 | +package discoveryengine |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 11 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 12 | +) |
| 13 | + |
| 14 | +type DiscoveryEngineOperationWaiter struct { |
| 15 | + Config *transport_tpg.Config |
| 16 | + UserAgent string |
| 17 | + Project string |
| 18 | + tpgresource.CommonOperationWaiter |
| 19 | +} |
| 20 | + |
| 21 | +func (w *DiscoveryEngineOperationWaiter) QueryOp() (interface{}, error) { |
| 22 | + if w == nil { |
| 23 | + return nil, fmt.Errorf("cannot query operation, it's unset or nil") |
| 24 | + } |
| 25 | + |
| 26 | + opName := w.CommonOperationWaiter.Op.Name |
| 27 | + |
| 28 | + // Extract location from opName. Example: |
| 29 | + // "projects/<project>/locations/<location>/operations/<operation-id>" |
| 30 | + // Requires error handling if not properly formatted. |
| 31 | + parts := strings.Split(opName, "/") |
| 32 | + if len(parts) < 5 || parts[2] != "locations" { |
| 33 | + return nil, fmt.Errorf("unexpected operation name format: %s", opName) // Handle invalid format appropriately |
| 34 | + } |
| 35 | + location := parts[3] |
| 36 | + |
| 37 | + basePath := strings.Replace(w.Config.DiscoveryEngineBasePath, "{{location}}", location, 1) |
| 38 | + url := fmt.Sprintf("%s%s", basePath, opName) |
| 39 | + |
| 40 | + return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 41 | + Config: w.Config, |
| 42 | + Method: "GET", |
| 43 | + Project: w.Project, |
| 44 | + RawURL: url, |
| 45 | + UserAgent: w.UserAgent, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func createDiscoveryEngineWaiter(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string) (*DiscoveryEngineOperationWaiter, error) { |
| 50 | + w := &DiscoveryEngineOperationWaiter{ |
| 51 | + Config: config, |
| 52 | + UserAgent: userAgent, |
| 53 | + Project: project, |
| 54 | + } |
| 55 | + if err := w.CommonOperationWaiter.SetOp(op); err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return w, nil |
| 59 | +} |
| 60 | + |
| 61 | +// nolint: deadcode,unused |
| 62 | +func DiscoveryEngineOperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error { |
| 63 | + w, err := createDiscoveryEngineWaiter(config, op, project, activity, userAgent) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + if err := tpgresource.OperationWait(w, activity, timeout, config.PollInterval); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + rawResponse := []byte(w.CommonOperationWaiter.Op.Response) |
| 71 | + if len(rawResponse) == 0 { |
| 72 | + return errors.New("`resource` not set in operation response") |
| 73 | + } |
| 74 | + return json.Unmarshal(rawResponse, response) |
| 75 | +} |
| 76 | + |
| 77 | +func DiscoveryEngineOperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error { |
| 78 | + if val, ok := op["name"]; !ok || val == "" { |
| 79 | + // This was a synchronous call - there is no operation to wait for. |
| 80 | + return nil |
| 81 | + } |
| 82 | + w, err := createDiscoveryEngineWaiter(config, op, project, activity, userAgent) |
| 83 | + if err != nil { |
| 84 | + // If w is nil, the op was synchronous. |
| 85 | + return err |
| 86 | + } |
| 87 | + return tpgresource.OperationWait(w, activity, timeout, config.PollInterval) |
| 88 | +} |
0 commit comments