/
utils.go
156 lines (137 loc) · 5.16 KB
/
utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* *******************************************************************************
* * Copyright (c) 2023 Datasance Teknoloji A.S.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package deployagentconfig
import (
"fmt"
rsc "github.com/datasance/potctl/internal/resource"
"github.com/datasance/potctl/pkg/iofog"
"github.com/datasance/potctl/pkg/util"
"github.com/datasance/iofog-go-sdk/v3/pkg/client"
)
type RouterMode string
const (
EdgeRouter RouterMode = "edge"
InteriorRouter RouterMode = "interior"
NoneRouter RouterMode = "none"
)
func getRouterMode(config *rsc.AgentConfiguration) RouterMode {
if config.RouterConfig.RouterMode != nil {
return RouterMode(*config.RouterConfig.RouterMode)
}
return EdgeRouter
}
func Validate(config *rsc.AgentConfiguration) error {
routerMode := getRouterMode(config)
if routerMode != EdgeRouter && routerMode != InteriorRouter && routerMode != NoneRouter {
msg := "agent config %s validation failed. RouterMode has to be one of edge, interior, none. Default is: edge"
return util.NewInputError(fmt.Sprintf(msg, config.Name))
}
if routerMode != NoneRouter && config.NetworkRouter != nil {
msg := "agent config %s validation failed. Cannot have a network if routerMode is different from none. Current router mode is: %s"
return util.NewInputError(fmt.Sprintf(msg, config.Name, routerMode))
}
if routerMode == NoneRouter && config.UpstreamRouters != nil && len(*config.UpstreamRouters) > 0 {
msg := "agent config %s validation failed. Cannot have a upstreamRouters if routerMode is none"
return util.NewInputError(fmt.Sprintf(msg, config.Name))
}
if routerMode != InteriorRouter && (config.RouterConfig.EdgeRouterPort != nil || config.RouterConfig.InterRouterPort != nil) {
msg := "agent config %s validation failed. Cannot have an edgeRouterPort or interRouterPort if routerMode is different from interior. Current router mode is: %s"
return util.NewInputError(fmt.Sprintf(msg, config.Name, routerMode))
}
return nil
}
func findAgentUUIDInList(list []client.AgentInfo, name string) (uuid string, err error) {
if name == iofog.VanillaRouterAgentName {
return name, nil
}
for idx := range list {
agent := &list[idx]
if agent.Name == name {
return agent.UUID, nil
}
}
return "", util.NewNotFoundError(fmt.Sprintf("Could not find router: %s\n", name))
}
// Process update the config to translate agent names into uuids, and sets the host value if needed
func Process(agentConfig *rsc.AgentConfiguration, name, agentIP string, otherAgents []client.AgentInfo) error {
routerMode := getRouterMode(agentConfig)
if agentConfig.UpstreamRouters != nil {
upstreamRoutersUUID := []string{}
for _, agentName := range *agentConfig.UpstreamRouters {
uuid, err := findAgentUUIDInList(otherAgents, agentName)
if err != nil {
return err
}
upstreamRoutersUUID = append(upstreamRoutersUUID, uuid)
}
agentConfig.UpstreamRouters = &upstreamRoutersUUID
}
if agentConfig.NetworkRouter != nil {
uuid, err := findAgentUUIDInList(otherAgents, *agentConfig.NetworkRouter)
if err != nil {
return err
}
agentConfig.NetworkRouter = &uuid
}
if routerMode != NoneRouter && agentConfig.Host == nil {
agentConfig.Host = &agentIP
}
return nil
}
func getAgentUpdateRequestFromAgentConfig(agentConfig *rsc.AgentConfiguration, tags *[]string) (request client.AgentUpdateRequest) {
var fogTypePtr *int64
if agentConfig.FogType != nil {
fogType, found := rsc.FogTypeStringMap[*agentConfig.FogType]
if !found {
fogType = 0
}
fogTypePtr = &fogType
}
request.Location = agentConfig.Location
request.Latitude = agentConfig.Latitude
request.Longitude = agentConfig.Longitude
request.Description = agentConfig.Description
request.Name = agentConfig.Name
request.FogType = fogTypePtr
request.AgentConfiguration = agentConfig.AgentConfiguration
request.Tags = tags
return
}
func createAgentFromConfiguration(agentConfig *rsc.AgentConfiguration, tags *[]string, name string, clt *client.Client) (uuid string, err error) {
updateAgentConfigRequest := getAgentUpdateRequestFromAgentConfig(agentConfig, tags)
createAgentRequest := &client.CreateAgentRequest{
AgentUpdateRequest: updateAgentConfigRequest,
}
if createAgentRequest.AgentUpdateRequest.Name == "" {
createAgentRequest.AgentUpdateRequest.Name = name
}
if createAgentRequest.AgentUpdateRequest.FogType == nil {
fogType := int64(0)
createAgentRequest.AgentUpdateRequest.FogType = &fogType
}
agent, err := clt.CreateAgent(createAgentRequest)
if err != nil {
return "", err
}
return agent.UUID, nil
}
func updateAgentConfiguration(agentConfig *rsc.AgentConfiguration, tags *[]string, uuid string, clt *client.Client) (err error) {
if agentConfig != nil {
updateAgentConfigRequest := getAgentUpdateRequestFromAgentConfig(agentConfig, tags)
updateAgentConfigRequest.UUID = uuid
if _, err = clt.UpdateAgent(&updateAgentConfigRequest); err != nil {
return
}
}
return nil
}