Skip to content

Commit

Permalink
Merge pull request #80 from n1Z3R/master
Browse files Browse the repository at this point in the history
Add status for nodegroups
  • Loading branch information
n1Z3R authored Jan 17, 2025
2 parents 1df072d + 0f9eae3 commit c15fe83
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/v1/nodegroup/schemas.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
package nodegroup

import (
"encoding/json"
"time"

"github.com/selectel/mks-go/pkg/v1/node"
)

// Status represents custom type for various nodegroup statuses.
type Status string

const (
StatusActive Status = "ACTIVE"
StatusPendingCreate Status = "PENDING_CREATE"
StatusPendingUpdate Status = "PENDING_UPDATE"
StatusPendingDelete Status = "PENDING_DELETE"
StatusPendingScaleUp Status = "PENDING_SCALE_UP"
StatusPendingScaleDown Status = "PENDING_SCALE_DOWN"
StatusPendingNodeReinstall Status = "PENDING_NODE_REINSTALL"
StatusUnknown Status = "UNKNOWN"
StatusError Status = "ERROR"
)

func getSupportedStatuses() []Status {
return []Status{
StatusActive,
StatusPendingCreate,
StatusPendingUpdate,
StatusPendingScaleUp,
StatusPendingScaleDown,
StatusPendingDelete,
StatusPendingNodeReinstall,
StatusError,
}
}

func isStatusSupported(s Status) bool {
for _, v := range getSupportedStatuses() {
if s == v {
return true
}
}

return false
}

// BaseView represents a base struct of unmarshalled nodegroup body from an API response.
//
//nolint:maligned
Expand All @@ -19,6 +58,9 @@ type BaseView struct {
// UpdatedAt is the timestamp in UTC timezone of when the nodegroup has been updated.
UpdatedAt *time.Time `json:"updated_at"`

// Status represents the current status of the nodegroup.
Status Status `json:"-"`

// ClusterID contains cluster identifier.
ClusterID string `json:"cluster_id"`

Expand Down Expand Up @@ -83,6 +125,28 @@ type GetView struct {
UserData string `json:"user_data"`
}

func (result *GetView) UnmarshalJSON(b []byte) error {
type tmp GetView
var s struct {
tmp
Status Status `json:"status"`
}
if err := json.Unmarshal(b, &s); err != nil {
return err
}

*result = GetView(s.tmp)

// Check nodegroup status.
if isStatusSupported(s.Status) {
result.Status = s.Status
} else {
result.Status = StatusUnknown
}

return nil
}

// TaintEffect represents an effect of the node's taint.
type TaintEffect string

Expand Down
2 changes: 2 additions & 0 deletions pkg/v1/nodegroup/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const testGetNodegroupResponseRaw = `
"flavor_id": "99b62670-9d78-43fd-8f55-d184a4800f8d",
"id": "a376745a-fbcb-413d-b418-169d059d79ce",
"local_volume": false,
"status": "ACTIVE",
"nodes": [
{
"created_at": "2020-02-19T15:41:45.948646Z",
Expand Down Expand Up @@ -64,6 +65,7 @@ var expectedGetNodegroupResponse = &nodegroup.GetView{
ClusterID: "79265515-3700-49fa-af0e-7f547bce788a",
FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d",
VolumeGB: 10,
Status: nodegroup.StatusActive,
VolumeType: "basic.ru-1a",
LocalVolume: false,
AvailabilityZone: "ru-1a",
Expand Down

0 comments on commit c15fe83

Please sign in to comment.