Skip to content

Commit

Permalink
feature: allow generate name with a randome string
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Sep 5, 2024
1 parent 5c70df9 commit 6b5e9f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
25 changes: 20 additions & 5 deletions pkg/cloudcommon/db/namevalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"regexp"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/util/seclib"
"yunion.io/x/sqlchemy"

"yunion.io/x/onecloud/pkg/cloudcommon/consts"
Expand Down Expand Up @@ -124,16 +125,23 @@ func GenerateAlterName(model IModel, hint string) (string, error) {
}

func GenerateName2(ctx context.Context, manager IModelManager, ownerId mcclient.IIdentityProvider, hint string, model IModel, baseIndex int) (string, error) {
_, pattern, patternLen, offset := stringutils2.ParseNamePattern2(hint)
_, pattern, patternLen, offset, ch := stringutils2.ParseNamePattern2(hint)
var name string
if patternLen == 0 {
name = hint
} else {
if offset > 0 {
baseIndex = offset
}
name = fmt.Sprintf(pattern, baseIndex)
baseIndex += 1
switch ch {
case stringutils2.RandomChar:
name = fmt.Sprintf(pattern, seclib.RandomPassword(patternLen))
case stringutils2.RepChar:
fallthrough
default:
name = fmt.Sprintf(pattern, baseIndex)
baseIndex += 1
}
}
for {
var uniq bool
Expand All @@ -149,8 +157,15 @@ func GenerateName2(ctx context.Context, manager IModelManager, ownerId mcclient.
if uniq {
return name, nil
}
name = fmt.Sprintf(pattern, baseIndex)
baseIndex += 1
switch ch {
case stringutils2.RandomChar:
name = fmt.Sprintf(pattern, seclib.RandomPassword(patternLen))
case stringutils2.RepChar:
fallthrough
default:
name = fmt.Sprintf(pattern, baseIndex)
baseIndex += 1
}
}
}

Expand Down
26 changes: 21 additions & 5 deletions pkg/util/stringutils2/namepattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ import (
"strings"
)

const RepChar = '#'
const RandomChar = '?'

// name##
// name##9#
func ParseNamePattern2(name string) (string, string, int, int) {
const RepChar = '#'
func ParseNamePattern2(name string) (string, string, int, int, byte) {
var match string
var pattern string
var patternLen int
var offset int
var charType byte

start := strings.IndexByte(name, RepChar)
if start >= 0 {
Expand All @@ -45,9 +48,22 @@ func ParseNamePattern2(name string) (string, string, int, int) {
}
match = fmt.Sprintf("%s%%%s", name[:start], name[end:])
pattern = fmt.Sprintf("%s%%0%dd%s", name[:start], patternLen, name[end:])
charType = RepChar
} else {
match = fmt.Sprintf("%s-%%", name)
pattern = fmt.Sprintf("%s-%%d", name)
start := strings.IndexByte(name, RandomChar)
if start >= 0 {
end := start + 1
for end < len(name) && name[end] == RandomChar {
end += 1
}
patternLen = end - start
match = fmt.Sprintf("%s%%%s", name[:start], name[end:])
pattern = fmt.Sprintf("%s%%s%s", name[:start], name[end:])
charType = RandomChar
} else {
match = fmt.Sprintf("%s-%%", name)
pattern = fmt.Sprintf("%s-%%d", name)
}
}
return match, pattern, patternLen, offset
return match, pattern, patternLen, offset, charType
}
18 changes: 15 additions & 3 deletions pkg/util/stringutils2/namepattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,31 @@ func TestParseNamePattern2(t *testing.T) {
pattern string
patternLen int
offset int
charType byte
}{
{
input: "testimg###",
match: "testimg%",
pattern: "testimg%03d",
patternLen: 3,
offset: 0,
charType: RepChar,
},
{
input: "testimg###66#",
match: "testimg%",
pattern: "testimg%03d",
patternLen: 3,
offset: 66,
charType: RepChar,
},
{
input: "testimg###ab#",
match: "testimg%",
pattern: "testimg%03d",
patternLen: 3,
offset: 0,
charType: RepChar,
},
{
input: "testimg",
Expand All @@ -54,11 +58,19 @@ func TestParseNamePattern2(t *testing.T) {
patternLen: 0,
offset: 0,
},
{
input: "testimg???",
match: "testimg%",
pattern: "testimg%s",
patternLen: 3,
offset: 0,
charType: RandomChar,
},
}
for _, c := range cases {
m, p, pl, o := ParseNamePattern2(c.input)
if m != c.match || p != c.pattern || pl != c.patternLen || o != c.offset {
t.Errorf("match got %s want %s, pattern got %s want %s, patternLen got %d want %d, offset got %d want %d", m, c.match, p, c.pattern, pl, c.patternLen, o, c.offset)
m, p, pl, o, ch := ParseNamePattern2(c.input)
if m != c.match || p != c.pattern || pl != c.patternLen || o != c.offset || ch != c.charType {
t.Errorf("match got %s want %s, pattern got %s want %s, patternLen got %d want %d, offset got %d want %d, charType got %c want %c", m, c.match, p, c.pattern, pl, c.patternLen, o, c.offset, ch, c.charType)
}
}
}

0 comments on commit 6b5e9f7

Please sign in to comment.