|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 12 | + "github.com/scaleway/scaleway-cli/internal/human" |
| 13 | + k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1beta4" |
| 14 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + poolActionTimeout = 10 * time.Minute |
| 19 | +) |
| 20 | + |
| 21 | +// |
| 22 | +// Marshalers |
| 23 | +// |
| 24 | + |
| 25 | +// poolStatusMarshalerFunc marshals a k8s.PoolStatus. |
| 26 | +var ( |
| 27 | + poolStatusAttributes = human.Attributes{ |
| 28 | + k8s.PoolStatusScaling: color.FgBlue, |
| 29 | + k8s.PoolStatusReady: color.FgGreen, |
| 30 | + k8s.PoolStatusError: color.FgRed, |
| 31 | + k8s.PoolStatusLocked: color.FgRed, |
| 32 | + k8s.PoolStatusUpdating: color.FgBlue, |
| 33 | + k8s.PoolStatusUpgrading: color.FgBlue, |
| 34 | + k8s.PoolStatusWarning: color.FgHiYellow, |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + poolActionCreate = iota |
| 40 | + poolActionUpdate |
| 41 | + poolActionUpgrade |
| 42 | + poolActionDelete |
| 43 | +) |
| 44 | + |
| 45 | +func poolCreateBuilder(c *core.Command) *core.Command { |
| 46 | + c.WaitFunc = waitForPoolFunc(poolActionCreate) |
| 47 | + return c |
| 48 | +} |
| 49 | + |
| 50 | +func poolDeleteBuilder(c *core.Command) *core.Command { |
| 51 | + c.WaitFunc = waitForPoolFunc(poolActionDelete) |
| 52 | + return c |
| 53 | +} |
| 54 | + |
| 55 | +func poolUpgradeBuilder(c *core.Command) *core.Command { |
| 56 | + c.WaitFunc = waitForPoolFunc(poolActionUpgrade) |
| 57 | + return c |
| 58 | +} |
| 59 | + |
| 60 | +func poolUpdateBuilder(c *core.Command) *core.Command { |
| 61 | + c.WaitFunc = waitForPoolFunc(poolActionUpdate) |
| 62 | + return c |
| 63 | +} |
| 64 | + |
| 65 | +func waitForPoolFunc(action int) core.WaitFunc { |
| 66 | + return func(ctx context.Context, _, respI interface{}) (interface{}, error) { |
| 67 | + pool, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForPool(&k8s.WaitForPoolRequest{ |
| 68 | + Region: respI.(*k8s.Pool).Region, |
| 69 | + PoolID: respI.(*k8s.Pool).ID, |
| 70 | + Timeout: scw.DurationPtr(poolActionTimeout), |
| 71 | + }) |
| 72 | + switch action { |
| 73 | + case poolActionCreate: |
| 74 | + return pool, err |
| 75 | + case poolActionUpdate: |
| 76 | + return pool, err |
| 77 | + case poolActionUpgrade: |
| 78 | + return pool, err |
| 79 | + case poolActionDelete: |
| 80 | + if err != nil { |
| 81 | + // if we get a 404 here, it means the resource was successfully deleted |
| 82 | + notFoundError := &scw.ResourceNotFoundError{} |
| 83 | + responseError := &scw.ResponseError{} |
| 84 | + if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, ¬FoundError) { |
| 85 | + return fmt.Sprintf("Pool %s successfully deleted.", respI.(*k8s.Pool).ID), nil |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return nil, err |
| 90 | + } |
| 91 | +} |
0 commit comments