feat: add Timeweb Cloud provider for Woodpecker CI autoscaler
- Implement timewebcloud provider with DeployAgent, RemoveAgent, ListDeployedAgentNames - Add minimal HTTP API client for Timeweb Cloud (create/list/delete servers) - Register provider in main.go with CLI flags - Add timeweb-list and timeweb-tester utilities - Include Dockerfile and docker-compose.yml for deployment - Update DEPLOY.md with verified OS/preset IDs
This commit is contained in:
18
utils/random.go
Normal file
18
utils/random.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RandomString generates a random string of length n using alphanumeric characters.
|
||||
func RandomString(n int) string {
|
||||
letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letterRunes[rng.Intn(len(letterRunes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
42
utils/random_test.go
Normal file
42
utils/random_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.woodpecker-ci.org/autoscaler/utils"
|
||||
)
|
||||
|
||||
func TestRandomString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
n int
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "zero length",
|
||||
n: 0,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "length 10",
|
||||
n: 10,
|
||||
want: 10,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
str := utils.RandomString(tt.n)
|
||||
assert.Equal(t, tt.want, len(str))
|
||||
})
|
||||
|
||||
t.Run("alphanumeric", func(t *testing.T) {
|
||||
str1 := utils.RandomString(10)
|
||||
for _, r := range str1 {
|
||||
assert.Contains(t, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", string(r))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
39
utils/stringmaps.go
Normal file
39
utils/stringmaps.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SliceToMap converts a slice of strings in the format "key=value"
|
||||
// into a string map, using the provided delimiter to split the pieces.
|
||||
// Returns a map and nil error on success, or nil and an error if a
|
||||
// slice element does not contain the delimiter.
|
||||
func SliceToMap(list []string, del string) (map[string]string, error) {
|
||||
m := make(map[string]string)
|
||||
for _, e := range list {
|
||||
before, after, _ := strings.Cut(e, del)
|
||||
if before == "" || after == "" {
|
||||
return nil, fmt.Errorf("could not split '%s' into key value pair with '=' delimiter", e)
|
||||
}
|
||||
m[strings.TrimSpace(before)] = strings.TrimSpace(after)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// MergeMaps merges two string maps m1 and m2 into a new map.
|
||||
// It copies all key-value pairs from m1 into the result.
|
||||
// It then copies all key-value pairs from m2 into the result,
|
||||
// overwriting any keys that are present in both m1 and m2.
|
||||
// The merged map is returned.
|
||||
func MergeMaps(m1, m2 map[string]string) map[string]string {
|
||||
merged := make(map[string]string)
|
||||
for k, v := range m1 {
|
||||
merged[k] = v
|
||||
}
|
||||
for key, value := range m2 {
|
||||
merged[key] = value
|
||||
}
|
||||
return merged
|
||||
}
|
||||
96
utils/stringmaps_test.go
Normal file
96
utils/stringmaps_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.woodpecker-ci.org/autoscaler/utils"
|
||||
)
|
||||
|
||||
func TestSliceToMap(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input []string
|
||||
del string
|
||||
want map[string]string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "basic",
|
||||
input: []string{"key1=value1", "key2=value2"},
|
||||
del: "=",
|
||||
want: map[string]string{"key1": "value1", "key2": "value2"},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "whitespace",
|
||||
input: []string{"key1 = value1", "key2=value2"},
|
||||
del: "=",
|
||||
want: map[string]string{"key1": "value1", "key2": "value2"},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "missing delimiter",
|
||||
input: []string{"key1", "key2=value2"},
|
||||
del: "=",
|
||||
want: nil,
|
||||
wantErr: assert.AnError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual, err := utils.SliceToMap(tt.input, tt.del)
|
||||
if tt.wantErr != nil {
|
||||
assert.Error(t, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeMaps(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
m1 map[string]string
|
||||
m2 map[string]string
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "nil maps",
|
||||
m1: nil,
|
||||
m2: nil,
|
||||
want: map[string]string{},
|
||||
},
|
||||
{
|
||||
name: "empty maps",
|
||||
m1: map[string]string{},
|
||||
m2: map[string]string{},
|
||||
want: map[string]string{},
|
||||
},
|
||||
{
|
||||
name: "overwrite",
|
||||
m1: map[string]string{"key1": "value1", "key2": "value2"},
|
||||
m2: map[string]string{"key2": "newvalue2", "key3": "value3"},
|
||||
want: map[string]string{"key1": "value1", "key2": "newvalue2", "key3": "value3"},
|
||||
},
|
||||
{
|
||||
name: "no overwrite",
|
||||
m1: map[string]string{"key1": "value1", "key2": "value2"},
|
||||
m2: map[string]string{"key3": "value3", "key4": "value4"},
|
||||
want: map[string]string{"key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
merged := utils.MergeMaps(tt.m1, tt.m2)
|
||||
assert.Equal(t, tt.want, merged)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user