|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/fnproject/fn_go/provider" |
| 11 | + fnprovideroracle "github.com/fnproject/fn_go/provider/oracle" |
| 12 | + ociCommon "github.com/oracle/oci-go-sdk/v65/common" |
| 13 | + ocifunctions "github.com/oracle/oci-go-sdk/v65/functions" |
| 14 | + "github.com/urfave/cli" |
| 15 | +) |
| 16 | + |
| 17 | +type OCIRequestControl struct { |
| 18 | + IfMatch string |
| 19 | + WaitForState string |
| 20 | + MaxWaitSeconds int |
| 21 | + WaitIntervalSeconds int |
| 22 | +} |
| 23 | + |
| 24 | +func ExtractOCIRequestControl(c *cli.Context) OCIRequestControl { |
| 25 | + return OCIRequestControl{ |
| 26 | + IfMatch: strings.TrimSpace(c.String("if-match")), |
| 27 | + WaitForState: strings.ToUpper(strings.TrimSpace(c.String("wait-for-state"))), |
| 28 | + MaxWaitSeconds: c.Int("max-wait-seconds"), |
| 29 | + WaitIntervalSeconds: c.Int("wait-interval-seconds"), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (o OCIRequestControl) HasIfMatch() bool { return o.IfMatch != "" } |
| 34 | +func (o OCIRequestControl) HasWait() bool { return o.WaitForState != "" } |
| 35 | + |
| 36 | +func NormalizeWaitSettings(maxWait, interval int) (int, int) { |
| 37 | + if maxWait <= 0 { |
| 38 | + maxWait = 1200 |
| 39 | + } |
| 40 | + if interval <= 0 { |
| 41 | + interval = 5 |
| 42 | + } |
| 43 | + return maxWait, interval |
| 44 | +} |
| 45 | + |
| 46 | +func WarnUnsupportedOCIRequestControl(p provider.Provider, control OCIRequestControl) { |
| 47 | + if IsOracleProvider(p) { |
| 48 | + return |
| 49 | + } |
| 50 | + if control.HasIfMatch() { |
| 51 | + fmt.Fprintln(os.Stderr, "Warning: --if-match is only supported with an oracle provider and will be ignored.") |
| 52 | + } |
| 53 | + if control.HasWait() { |
| 54 | + fmt.Fprintln(os.Stderr, "Warning: wait control flags are only supported with an oracle provider and will be ignored.") |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func BuildOCIManagementClient(p provider.Provider) (*ocifunctions.FunctionsManagementClient, error) { |
| 59 | + oracleProvider, ok := p.(*fnprovideroracle.OracleProvider) |
| 60 | + if !ok || oracleProvider == nil { |
| 61 | + return nil, nil |
| 62 | + } |
| 63 | + client, err := ocifunctions.NewFunctionsManagementClientWithConfigurationProvider(oracleProvider.ConfigurationProvider) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + if oracleProvider.FnApiUrl != nil { |
| 68 | + client.Host = oracleProvider.FnApiUrl.String() |
| 69 | + } else { |
| 70 | + region, _ := oracleProvider.ConfigurationProvider.Region() |
| 71 | + if region != "" { |
| 72 | + client.SetRegion(region) |
| 73 | + } |
| 74 | + } |
| 75 | + return &client, nil |
| 76 | +} |
| 77 | + |
| 78 | +func waitUntil(deadline time.Time, interval time.Duration, check func() (bool, error)) error { |
| 79 | + for { |
| 80 | + done, err := check() |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + if done { |
| 85 | + return nil |
| 86 | + } |
| 87 | + if time.Now().After(deadline) { |
| 88 | + return fmt.Errorf("timed out waiting for requested state") |
| 89 | + } |
| 90 | + time.Sleep(interval) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func WaitForAppState(p provider.Provider, appID, targetState string, maxWaitSeconds, waitIntervalSeconds int) error { |
| 95 | + if strings.TrimSpace(targetState) == "" || !IsOracleProvider(p) { |
| 96 | + return nil |
| 97 | + } |
| 98 | + client, err := BuildOCIManagementClient(p) |
| 99 | + if err != nil || client == nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + maxWaitSeconds, waitIntervalSeconds = NormalizeWaitSettings(maxWaitSeconds, waitIntervalSeconds) |
| 103 | + deadline := time.Now().Add(time.Duration(maxWaitSeconds) * time.Second) |
| 104 | + interval := time.Duration(waitIntervalSeconds) * time.Second |
| 105 | + targetState = strings.ToUpper(strings.TrimSpace(targetState)) |
| 106 | + return waitUntil(deadline, interval, func() (bool, error) { |
| 107 | + res, err := client.GetApplication(context.Background(), ocifunctions.GetApplicationRequest{ApplicationId: &appID}) |
| 108 | + if err != nil { |
| 109 | + if targetState == "DELETED" { |
| 110 | + if serr, ok := err.(ociCommon.ServiceError); ok && serr.GetHTTPStatusCode() == 404 { |
| 111 | + return true, nil |
| 112 | + } |
| 113 | + } |
| 114 | + return false, err |
| 115 | + } |
| 116 | + return strings.EqualFold(string(res.Application.LifecycleState), targetState), nil |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func WaitForFunctionState(p provider.Provider, fnID, targetState string, maxWaitSeconds, waitIntervalSeconds int) error { |
| 121 | + if strings.TrimSpace(targetState) == "" || !IsOracleProvider(p) { |
| 122 | + return nil |
| 123 | + } |
| 124 | + client, err := BuildOCIManagementClient(p) |
| 125 | + if err != nil || client == nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + maxWaitSeconds, waitIntervalSeconds = NormalizeWaitSettings(maxWaitSeconds, waitIntervalSeconds) |
| 129 | + deadline := time.Now().Add(time.Duration(maxWaitSeconds) * time.Second) |
| 130 | + interval := time.Duration(waitIntervalSeconds) * time.Second |
| 131 | + targetState = strings.ToUpper(strings.TrimSpace(targetState)) |
| 132 | + return waitUntil(deadline, interval, func() (bool, error) { |
| 133 | + res, err := client.GetFunction(context.Background(), ocifunctions.GetFunctionRequest{FunctionId: &fnID}) |
| 134 | + if err != nil { |
| 135 | + if targetState == "DELETED" { |
| 136 | + if serr, ok := err.(ociCommon.ServiceError); ok && serr.GetHTTPStatusCode() == 404 { |
| 137 | + return true, nil |
| 138 | + } |
| 139 | + } |
| 140 | + return false, err |
| 141 | + } |
| 142 | + return strings.EqualFold(string(res.Function.LifecycleState), targetState), nil |
| 143 | + }) |
| 144 | +} |
0 commit comments