- Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathclone.go
72 lines (56 loc) · 1.75 KB
/
clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
2021 © Postgres.ai
*/
// Package runci provides a tools to run and check migrations in CI.
package runci
import (
"context"
"github.com/pkg/errors"
"github.com/sethvargo/go-password/password"
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/defaults"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/dblabapi"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/dblabapi/types"
dblabmodels "gitlab.com/postgres-ai/database-lab/v3/pkg/models"
)
// Constants for autogenerated passwords.
const (
PasswordLength=16
PasswordMinDigits=4
PasswordMinSymbols=0
ciNamePrefix="ci"
)
typecloneOptsstruct {
usernamestring
dbnamestring
}
// createDBLabClone creates a new clone.
funccreateDBLabClone(ctx context.Context, dle*dblabapi.Client, optscloneOpts) (*dblabmodels.Clone, error) {
pwd, err:=password.Generate(PasswordLength, PasswordMinDigits, PasswordMinSymbols, false, true)
iferr!=nil {
returnnil, errors.Wrap(err, "failed to generate a password to a new clone")
}
clientRequest:= types.CloneCreateRequest{
DB: &types.DatabaseRequest{
Username: ciNamePrefix+"_"+opts.username,
Password: pwd,
Restricted: true,
DBName: opts.dbname,
},
}
ifclientRequest.DB.DBName=="" {
clientRequest.DB.DBName=defaults.DBName
}
clone, err:=dle.CreateClone(ctx, clientRequest)
iferr!=nil {
returnnil, errors.Wrap(err, "failed to create a new clone")
}
ifclone.Snapshot==nil {
clone.Snapshot=&dblabmodels.Snapshot{}
}
clone.DB.Password=pwd
// To get an accessible address in case running the assistant inside a container.
ifclone.DB.Host=="localhost"||clone.DB.Host=="127.0.0.1" {
clone.DB.Host=dle.URL("").Hostname()
}
returnclone, nil
}