forked from eclipse-iofog/iofog-go-sdk
/
microservices.go
159 lines (143 loc) · 4.14 KB
/
microservices.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
157
158
159
/*
* *******************************************************************************
* * Copyright (c) 2019 Edgeworx, Inc.
* *
* * 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 apps
import (
"bytes"
"fmt"
"net/url"
"strings"
"github.com/datasance/iofog-go-sdk/v3/pkg/client"
"gopkg.in/yaml.v2"
)
const (
errParseControllerURL = "failed to parse Controller endpoint as URL: %s"
)
// ApplicationData is data fetched from controller at init time
type ApplicationData struct {
MicroserviceByName map[string]*client.MicroserviceInfo
AgentsByName map[string]*client.AgentInfo
CatalogByID map[int]*client.CatalogItemInfo
RegistryByID map[int]*client.RegistryInfo
CatalogByName map[string]*client.CatalogItemInfo
FlowInfo *client.FlowInfo
}
type microserviceExecutor struct {
controller IofogController
msvc interface{}
name string
appName string
uuid string
client *client.Client
}
func ParseFQMsvcName(fqName string) (appName, name string, err error) {
if fqName == "" {
return "", "", NewInputError(fmt.Sprintf("Invalid microservice name %s", fqName))
}
splittedName := strings.Split(fqName, "/")
switch len(splittedName) {
case 1:
return "", splittedName[0], nil
case 2:
return splittedName[0], splittedName[1], nil
default:
return "", "", NewInputError(fmt.Sprintf("Invalid microservice name %s", fqName))
}
}
func newMicroserviceExecutor(controller IofogController, msvc interface{}, appName, name string) *microserviceExecutor {
exe := µserviceExecutor{
controller: controller,
msvc: msvc,
name: name,
appName: appName,
}
return exe
}
func (exe *microserviceExecutor) execute() error {
// Init remote resources
if err := exe.init(); err != nil {
return err
}
// Deploy microservice
if _, err := exe.deploy(); err != nil {
return err
}
return nil
}
func (exe *microserviceExecutor) init() (err error) {
baseURL, err := url.Parse(exe.controller.Endpoint)
if err != nil {
return fmt.Errorf(errParseControllerURL, err.Error())
}
if exe.controller.Token != "" {
exe.client, err = client.NewWithToken(client.Options{BaseURL: baseURL}, exe.controller.Token)
} else {
exe.client, err = client.NewAndLogin(client.Options{BaseURL: baseURL}, exe.controller.Email, exe.controller.Password)
}
if err != nil {
return err
}
if exe.appName == "" {
return NewInputError(fmt.Sprintf("Application name missing for microservice %s", exe.name))
}
listMsvcs, err := exe.client.GetMicroservicesByApplication(exe.appName)
if err != nil {
return err
}
for i := 0; i < len(listMsvcs.Microservices); i++ {
// If msvc already exists, set UUID
if listMsvcs.Microservices[i].Name == exe.name {
if exe.uuid == "" {
exe.uuid = listMsvcs.Microservices[i].UUID
}
}
}
return err
}
func (exe *microserviceExecutor) deploy() (newMsvc *client.MicroserviceInfo, err error) {
if exe.uuid != "" {
// Update microservice
return exe.update()
}
// Create microservice
return exe.create()
}
func (exe *microserviceExecutor) create() (newMsvc *client.MicroserviceInfo, err error) {
file := IofogHeader{
APIVersion: "datasance.com/v1",
Kind: MicroserviceKind,
Metadata: HeaderMetadata{
Name: strings.Join([]string{exe.appName, exe.name}, "/"),
},
Spec: exe.msvc,
}
yamlBytes, err := yaml.Marshal(file)
if err != nil {
return nil, err
}
return exe.client.CreateMicroserviceFromYAML(bytes.NewReader(yamlBytes))
}
func (exe *microserviceExecutor) update() (newMsvc *client.MicroserviceInfo, err error) {
file := IofogHeader{
APIVersion: "datasance.com/v1",
Kind: MicroserviceKind,
Metadata: HeaderMetadata{
Name: strings.Join([]string{exe.appName, exe.name}, "/"),
},
Spec: exe.msvc,
}
yamlBytes, err := yaml.Marshal(file)
if err != nil {
return nil, err
}
return exe.client.UpdateMicroserviceFromYAML(exe.uuid, bytes.NewReader(yamlBytes))
}