-
Notifications
You must be signed in to change notification settings - Fork 66
/
scene.go
125 lines (93 loc) · 4.12 KB
/
scene.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package service
import (
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/repo"
)
type SceneService struct {
ScenarioNodeRepo *repo.ScenarioNodeRepo `inject:""`
EnvironmentRepo *repo.EnvironmentRepo `inject:""`
DebugInterfaceRepo *repo.DebugInterfaceRepo `inject:""`
EndpointInterfaceRepo *repo.EndpointInterfaceRepo `inject:""`
EndpointRepo *repo.EndpointRepo `inject:""`
ServeServerRepo *repo.ServeServerRepo `inject:""`
ShareVarService *ShareVarService `inject:""`
EnvironmentService *EnvironmentService `inject:""`
DatapoolService *DatapoolService `inject:""`
ScenarioNodeService *ScenarioNodeService `inject:""`
}
func (s *SceneService) LoadEnvVarMapByScenario(tenantId consts.TenantId, scene *domain.ExecScene, scenarioId, environmentId uint) {
scene.EnvToVariables = domain.EnvToVariables{}
scene.DebugInterfaceToEnvMap = domain.InterfaceToEnvMap{}
// load baseUrl for interface processors
processors, _ := s.ScenarioNodeRepo.ListByScenario(tenantId, scenarioId)
for _, processor := range processors {
if processor.EntityType != consts.ProcessorInterfaceDefault {
continue
}
var server = s.GetExecServer(tenantId, processor.EntityId, processor.EndpointInterfaceId, environmentId)
scene.DebugInterfaceToEnvMap[processor.EntityId] = server.EnvironmentId
scene.EnvToVariables[server.EnvironmentId] = append(scene.EnvToVariables[server.EnvironmentId], domain.GlobalVar{
Name: consts.KEY_BASE_URL,
LocalValue: server.Url,
RemoteValue: server.Url,
})
}
// load vars by env
vars, _ := s.EnvironmentRepo.GetVars(tenantId, environmentId)
for _, v := range vars {
scene.EnvToVariables[environmentId] = append(scene.EnvToVariables[environmentId], domain.GlobalVar{
Name: v.Name,
LocalValue: v.LocalValue,
RemoteValue: v.RemoteValue,
})
}
return
}
func (s *SceneService) GetExecServer(tenantId consts.TenantId, debugInterfaceId, endpointInterfaceId, environmentId uint) (server model.ServeServer) {
interf, _ := s.EndpointInterfaceRepo.Get(tenantId, endpointInterfaceId)
if environmentId > 0 { // select a env to exec
var serveId uint
if debugInterfaceId > 0 {
debugInterface, _ := s.DebugInterfaceRepo.Get(tenantId, debugInterfaceId)
serveId = debugInterface.ServeId
} else {
endpoint, _ := s.EndpointRepo.Get(tenantId, interf.EndpointId)
serveId = endpoint.ServeId
}
server, _ = s.ServeServerRepo.FindByServeAndExecEnv(tenantId, serveId, environmentId)
} else { // TODO: may not used, since now environmentId always not empty
var serverId uint
if debugInterfaceId > 0 { // from debug interface
debugInterface, _ := s.DebugInterfaceRepo.Get(tenantId, debugInterfaceId)
serverId = debugInterface.ServerId
} else { // from endpoint interface
endpoint, _ := s.EndpointRepo.Get(tenantId, interf.EndpointId)
serverId = endpoint.ServerId
}
server, _ = s.ServeServerRepo.Get(tenantId, serverId)
}
return
}
func (s *SceneService) LoadEnvVars(tenantId consts.TenantId, scene *domain.ExecScene, serverId, debugInterfaceId uint) (projectId uint, err error) {
scene.EnvToVariables = domain.EnvToVariables{}
scene.DebugInterfaceToEnvMap = domain.InterfaceToEnvMap{}
serveServer, _ := s.ServeServerRepo.Get(tenantId, serverId)
envId := serveServer.EnvironmentId
scene.DebugInterfaceToEnvMap[debugInterfaceId] = envId
vars, _ := s.EnvironmentRepo.GetVars(tenantId, envId)
for _, v := range vars {
scene.EnvToVariables[envId] = append(scene.EnvToVariables[envId], domain.GlobalVar{
Name: v.Name,
LocalValue: v.LocalValue,
RemoteValue: v.RemoteValue,
})
}
return
}
func (s *SceneService) LoadProjectSettings(tenantId consts.TenantId, scene *domain.ExecScene, projectId uint) {
scene.GlobalParams, _ = s.EnvironmentService.GetGlobalParams(tenantId, projectId)
scene.GlobalVars, _ = s.EnvironmentService.GetGlobalVars(tenantId, projectId)
scene.Datapools, _ = s.DatapoolService.ListForExec(tenantId, projectId)
}