|
1 | 1 | // Copyright (c) HashiCorp, Inc.
|
2 | 2 | // SPDX-License-Identifier: MPL-2.0
|
3 | 3 | package gkeonprem
|
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 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 | + cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1" |
| 14 | +) |
| 15 | + |
| 16 | +type gkeonpremOpError struct { |
| 17 | + *cloudresourcemanager.Status |
| 18 | +} |
| 19 | + |
| 20 | +func (e gkeonpremOpError) Error() string { |
| 21 | + var validationCheck map[string]interface{} |
| 22 | + |
| 23 | + for _, msg := range e.Details { |
| 24 | + detail := make(map[string]interface{}) |
| 25 | + if err := json.Unmarshal(msg, &detail); err != nil { |
| 26 | + continue |
| 27 | + } |
| 28 | + |
| 29 | + if _, ok := detail["validationCheck"]; ok { |
| 30 | + delete(detail, "@type") |
| 31 | + validationCheck = detail |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if validationCheck != nil { |
| 36 | + bytes, err := json.MarshalIndent(validationCheck, "", " ") |
| 37 | + if err != nil { |
| 38 | + return fmt.Sprintf("Error code %v message: %s validation check: %s", e.Code, e.Message, validationCheck) |
| 39 | + } |
| 40 | + |
| 41 | + return fmt.Sprintf("Error code %v message: %s\n %s", e.Code, e.Message, bytes) |
| 42 | + } |
| 43 | + |
| 44 | + return fmt.Sprintf("Error code %v, message: %s", e.Code, e.Message) |
| 45 | +} |
| 46 | + |
| 47 | +type gkeonpremOperationWaiter struct { |
| 48 | + Config *transport_tpg.Config |
| 49 | + UserAgent string |
| 50 | + Project string |
| 51 | + Op tpgresource.CommonOperation |
| 52 | +} |
| 53 | + |
| 54 | +func (w *gkeonpremOperationWaiter) State() string { |
| 55 | + if w == nil { |
| 56 | + return fmt.Sprintf("Operation is nil!") |
| 57 | + } |
| 58 | + |
| 59 | + return fmt.Sprintf("done: %v", w.Op.Done) |
| 60 | +} |
| 61 | + |
| 62 | +func (w *gkeonpremOperationWaiter) Error() error { |
| 63 | + if w != nil && w.Op.Error != nil { |
| 64 | + return &gkeonpremOpError{w.Op.Error} |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func (w *gkeonpremOperationWaiter) IsRetryable(error) bool { |
| 70 | + return false |
| 71 | +} |
| 72 | + |
| 73 | +func (w *gkeonpremOperationWaiter) SetOp(op interface{}) error { |
| 74 | + if err := tpgresource.Convert(op, &w.Op); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (w *gkeonpremOperationWaiter) OpName() string { |
| 81 | + if w == nil { |
| 82 | + return "<nil>" |
| 83 | + } |
| 84 | + |
| 85 | + return w.Op.Name |
| 86 | +} |
| 87 | + |
| 88 | +func (w *gkeonpremOperationWaiter) PendingStates() []string { |
| 89 | + return []string{"done: false"} |
| 90 | +} |
| 91 | + |
| 92 | +func (w *gkeonpremOperationWaiter) TargetStates() []string { |
| 93 | + return []string{"done: true"} |
| 94 | +} |
| 95 | + |
| 96 | +func (w *gkeonpremOperationWaiter) QueryOp() (interface{}, error) { |
| 97 | + if w == nil { |
| 98 | + return nil, fmt.Errorf("Cannot query operation, it's unset or nil.") |
| 99 | + } |
| 100 | + // Returns the proper get. |
| 101 | + url := fmt.Sprintf("%s%s", w.Config.GkeonpremBasePath, w.Op.Name) |
| 102 | + |
| 103 | + return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 104 | + Config: w.Config, |
| 105 | + Method: "GET", |
| 106 | + Project: w.Project, |
| 107 | + RawURL: url, |
| 108 | + UserAgent: w.UserAgent, |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +func creategkeonpremWaiter(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string) (*gkeonpremOperationWaiter, error) { |
| 113 | + w := &gkeonpremOperationWaiter{ |
| 114 | + Config: config, |
| 115 | + UserAgent: userAgent, |
| 116 | + Project: project, |
| 117 | + } |
| 118 | + if err := w.SetOp(op); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + return w, nil |
| 122 | +} |
| 123 | + |
| 124 | +// nolint: deadcode,unused |
| 125 | +func GkeonpremOperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error { |
| 126 | + w, err := creategkeonpremWaiter(config, op, project, activity, userAgent) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + if err := tpgresource.OperationWait(w, activity, timeout, config.PollInterval); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + return json.Unmarshal([]byte(w.Op.Response), response) |
| 134 | +} |
| 135 | + |
| 136 | +func GkeonpremOperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error { |
| 137 | + if val, ok := op["name"]; !ok || val == "" { |
| 138 | + // This was a synchronous call - there is no operation to wait for. |
| 139 | + return nil |
| 140 | + } |
| 141 | + w, err := creategkeonpremWaiter(config, op, project, activity, userAgent) |
| 142 | + if err != nil { |
| 143 | + // If w is nil, the op was synchronous. |
| 144 | + return err |
| 145 | + } |
| 146 | + return tpgresource.OperationWait(w, activity, timeout, config.PollInterval) |
| 147 | +} |
0 commit comments