Skip to content

Commit

Permalink
Add userdata for list and get nodegroup operations (#74)
Browse files Browse the repository at this point in the history
* replace string pointer by string in UD field

* add userdata for list and get nodegroup operations

* add bash userdata to fixtures
  • Loading branch information
yaroslaver authored Mar 5, 2024
1 parent ce93fd0 commit df0e4b1
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 80 deletions.
5 changes: 0 additions & 5 deletions pkg/testutils/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@ func BoolToPtr(v bool) *bool {
func IntToPtr(v int) *int {
return &v
}

// StringToPtr can be used to convert string value to string pointer.
func StringToPtr(v string) *string {
return &v
}
8 changes: 4 additions & 4 deletions pkg/v1/nodegroup/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Get returns a cluster nodegroup by its id.
func Get(ctx context.Context, client *v1.ServiceClient, clusterID, nodegroupID string) (*View, *v1.ResponseResult, error) {
func Get(ctx context.Context, client *v1.ServiceClient, clusterID, nodegroupID string) (*GetView, *v1.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, v1.ResourceURLCluster, clusterID, v1.ResourceURLNodegroup, nodegroupID}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
Expand All @@ -23,7 +23,7 @@ func Get(ctx context.Context, client *v1.ServiceClient, clusterID, nodegroupID s

// Extract a nodegroup to the response body.
var result struct {
Nodegroup *View `json:"nodegroup"`
Nodegroup *GetView `json:"nodegroup"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
Expand All @@ -34,7 +34,7 @@ func Get(ctx context.Context, client *v1.ServiceClient, clusterID, nodegroupID s
}

// List gets a list of all cluster nodegroups.
func List(ctx context.Context, client *v1.ServiceClient, clusterID string) ([]*View, *v1.ResponseResult, error) {
func List(ctx context.Context, client *v1.ServiceClient, clusterID string) ([]*ListView, *v1.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, v1.ResourceURLCluster, clusterID, v1.ResourceURLNodegroup}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
Expand All @@ -46,7 +46,7 @@ func List(ctx context.Context, client *v1.ServiceClient, clusterID string) ([]*V

// Extract nodegroups from the response body.
var result struct {
Nodegroups []*View `json:"nodegroups"`
Nodegroups []*ListView `json:"nodegroups"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/v1/nodegroup/requests_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type CreateOpts struct {
AutoscaleMaxNodes *int `json:"autoscale_max_nodes,omitempty"`

// UserData represents base64 data which is used to pass a script that worker nodes run on boot.
UserData *string `json:"user_data,omitempty"`
UserData string `json:"user_data,omitempty"`
}

// ResizeOpts represents options for the nodegroup Resize request.
Expand Down
21 changes: 19 additions & 2 deletions pkg/v1/nodegroup/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/selectel/mks-go/pkg/v1/node"
)

// View represents an unmarshalled nodegroup body from an API response.
// BaseView represents a base struct of unmarshalled nodegroup body from an API response.
//
//nolint:maligned
type View struct {
type BaseView struct {
// ID is the identifier of the nodegroup.
ID string `json:"id"`

Expand Down Expand Up @@ -60,6 +60,23 @@ type View struct {
NodegroupType string `json:"nodegroup_type"`
}

// ListView represents an unmarshalled nodegroup body from the list API response.
type ListView struct {
BaseView

// AvailableAdditionalInfo provides additional information about nodegroup like userdata, etc.
// Usually it's large volume of data and here we only show presence of this info.
AvailableAdditionalInfo map[string]bool `json:"available_additional_info"`
}

// GetView represents an unmarshalled nodegroup body from the get API response.
type GetView struct {
BaseView

// UserData represents base64 data which is used to pass a script that worker nodes run on boot.
UserData string `json:"user_data"`
}

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

Expand Down
146 changes: 78 additions & 68 deletions pkg/v1/nodegroup/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,53 @@ const testGetNodegroupResponseRaw = `
"enable_autoscale": false,
"autoscale_min_nodes": 0,
"autoscale_max_nodes": 0,
"nodegroup_type": "STANDARD"
"nodegroup_type": "STANDARD",
"user_data": "IyEvYmluL2Jhc2ggLXYKYXB0IC15IHVwZGF0ZQphcHQgLXkgaW5zdGFsbCBtdHI="
}
}
`

var nodegroupResponseTimestamp, _ = time.Parse(time.RFC3339, "2020-02-19T15:41:45.948646Z")

// expectedGetNodegroupResponse represents an unmarshalled testGetNodegroupResponseRaw.
var expectedGetNodegroupResponse = &nodegroup.View{
ID: "a376745a-fbcb-413d-b418-169d059d79ce",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
ClusterID: "79265515-3700-49fa-af0e-7f547bce788a",
FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d",
VolumeGB: 10,
VolumeType: "basic.ru-1a",
LocalVolume: false,
AvailabilityZone: "ru-1a",
Nodes: []*node.View{
{
ID: "39e5dd4d-5e23-4a00-8173-974bf844f21b",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
Hostname: "test-cluster-node-eegp9",
IP: "198.51.100.11",
NodegroupID: "a376745a-fbcb-413d-b418-169d059d79ce",
OSServerID: "dc56abe9-d0d4-4099-9b5f-e5cabfccf276",
var expectedGetNodegroupResponse = &nodegroup.GetView{
BaseView: nodegroup.BaseView{
ID: "a376745a-fbcb-413d-b418-169d059d79ce",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
ClusterID: "79265515-3700-49fa-af0e-7f547bce788a",
FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d",
VolumeGB: 10,
VolumeType: "basic.ru-1a",
LocalVolume: false,
AvailabilityZone: "ru-1a",
Nodes: []*node.View{
{
ID: "39e5dd4d-5e23-4a00-8173-974bf844f21b",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
Hostname: "test-cluster-node-eegp9",
IP: "198.51.100.11",
NodegroupID: "a376745a-fbcb-413d-b418-169d059d79ce",
OSServerID: "dc56abe9-d0d4-4099-9b5f-e5cabfccf276",
},
},
},
Labels: map[string]string{
"test-label-key": "test-label-value",
},
Taints: []nodegroup.Taint{
{
Key: "test-key-0",
Value: "test-value-0",
Effect: nodegroup.NoScheduleEffect,
Labels: map[string]string{
"test-label-key": "test-label-value",
},
Taints: []nodegroup.Taint{
{
Key: "test-key-0",
Value: "test-value-0",
Effect: nodegroup.NoScheduleEffect,
},
},
EnableAutoscale: false,
AutoscaleMinNodes: 0,
AutoscaleMaxNodes: 0,
NodegroupType: "STANDARD",
},
EnableAutoscale: false,
AutoscaleMinNodes: 0,
AutoscaleMaxNodes: 0,
NodegroupType: "STANDARD",
UserData: "IyEvYmluL2Jhc2ggLXYKYXB0IC15IHVwZGF0ZQphcHQgLXkgaW5zdGFsbCBtdHI=",
}

// testListNodegroupsResponseRaw represents a raw response from the List method.
Expand Down Expand Up @@ -128,49 +132,55 @@ const testListNodegroupsResponseRaw = `
"enable_autoscale": false,
"autoscale_min_nodes": 0,
"autoscale_max_nodes": 0,
"nodegroup_type": "STANDARD"
"nodegroup_type": "STANDARD",
"available_additional_info": {
"user_data": true
}
}
]
}
`

// expectedListNodegroupsResponse represents an unmarshalled testListNodegroupsResponseRaw.
var expectedListNodegroupsResponse = []*nodegroup.View{
var expectedListNodegroupsResponse = []*nodegroup.ListView{
{
ID: "a376745a-fbcb-413d-b418-169d059d79ce",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
ClusterID: "79265515-3700-49fa-af0e-7f547bce788a",
FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d",
VolumeGB: 10,
VolumeType: "basic.ru-1a",
LocalVolume: false,
AvailabilityZone: "ru-1a",
Nodes: []*node.View{
{
ID: "39e5dd4d-5e23-4a00-8173-974bf844f21b",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
Hostname: "test-cluster-node-eegp9",
IP: "198.51.100.11",
NodegroupID: "a376745a-fbcb-413d-b418-169d059d79ce",
OSServerID: "dc56abe9-d0d4-4099-9b5f-e5cabfccf276",
BaseView: nodegroup.BaseView{
ID: "a376745a-fbcb-413d-b418-169d059d79ce",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
ClusterID: "79265515-3700-49fa-af0e-7f547bce788a",
FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d",
VolumeGB: 10,
VolumeType: "basic.ru-1a",
LocalVolume: false,
AvailabilityZone: "ru-1a",
Nodes: []*node.View{
{
ID: "39e5dd4d-5e23-4a00-8173-974bf844f21b",
CreatedAt: &nodegroupResponseTimestamp,
UpdatedAt: &nodegroupResponseTimestamp,
Hostname: "test-cluster-node-eegp9",
IP: "198.51.100.11",
NodegroupID: "a376745a-fbcb-413d-b418-169d059d79ce",
OSServerID: "dc56abe9-d0d4-4099-9b5f-e5cabfccf276",
},
},
},
Labels: map[string]string{
"test-label-key": "test-label-value",
},
Taints: []nodegroup.Taint{
{
Key: "test-key-0",
Value: "test-value-0",
Effect: nodegroup.NoScheduleEffect,
Labels: map[string]string{
"test-label-key": "test-label-value",
},
Taints: []nodegroup.Taint{
{
Key: "test-key-0",
Value: "test-value-0",
Effect: nodegroup.NoScheduleEffect,
},
},
EnableAutoscale: false,
AutoscaleMinNodes: 0,
AutoscaleMaxNodes: 0,
NodegroupType: "STANDARD",
},
EnableAutoscale: false,
AutoscaleMinNodes: 0,
AutoscaleMaxNodes: 0,
NodegroupType: "STANDARD",
AvailableAdditionalInfo: map[string]bool{"user_data": true},
},
}

Expand Down Expand Up @@ -198,7 +208,7 @@ const testCreateNodegroupOptsRaw = `
"enable_autoscale": true,
"autoscale_min_nodes": 1,
"autoscale_max_nodes": 10,
"user_data": "cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCmZ1bmMgbWFpbigpIHsKCWZtdC5QcmludGxuKCJIZWxsbyIpCn0="
"user_data": "IyEvYmluL2Jhc2ggLXYKYXB0IC15IHVwZGF0ZQphcHQgLXkgaW5zdGFsbCBtdHI="
}
}
`
Expand All @@ -225,7 +235,7 @@ var testCreateNodegroupOpts = &nodegroup.CreateOpts{
EnableAutoscale: testutils.BoolToPtr(true),
AutoscaleMinNodes: testutils.IntToPtr(1),
AutoscaleMaxNodes: testutils.IntToPtr(10),
UserData: testutils.StringToPtr("cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCmZ1bmMgbWFpbigpIHsKCWZtdC5QcmludGxuKCJIZWxsbyIpCn0="),
UserData: "IyEvYmluL2Jhc2ggLXYKYXB0IC15IHVwZGF0ZQphcHQgLXkgaW5zdGFsbCBtdHI=",
}

// testUpdateNodegroupOptsRaw represents marshalled options for the Update request.
Expand Down

0 comments on commit df0e4b1

Please sign in to comment.