-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
fx_strings.go
312 lines (257 loc) · 10.2 KB
/
fx_strings.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package helpers
import (
"fmt"
"strings"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
const (
FxStringPrefixDocker FxStringPrefix = "DOCKER"
FxStringPrefixDotNet FxStringPrefix = "DOTNET"
FxStringPrefixDotNetCore FxStringPrefix = "DOTNETCORE"
FxStringPrefixDotNetIsolated FxStringPrefix = "DOTNET-ISOLATED"
FxStringPrefixGo FxStringPrefix = "GO"
FxStringPrefixJava FxStringPrefix = "JAVA"
FxStringPrefixJBoss FxStringPrefix = "JBOSSEAP"
FxStringPrefixNode FxStringPrefix = "NODE"
FxStringPrefixPhp FxStringPrefix = "PHP"
FxStringPrefixPowerShell FxStringPrefix = "POWERSHELL"
FxStringPrefixPython FxStringPrefix = "PYTHON"
FxStringPrefixRuby FxStringPrefix = "RUBY"
FxStringPrefixTomcat FxStringPrefix = "TOMCAT"
)
type FxStringPrefix string
var urlSchemes = []string{
"https://",
"http://",
}
func decodeApplicationStackLinux(fxString string) ApplicationStackLinux {
parts := strings.Split(fxString, "|")
result := ApplicationStackLinux{}
if len(parts) != 2 {
return result
}
switch FxStringPrefix(strings.ToUpper(parts[0])) {
case FxStringPrefixDotNetIsolated, FxStringPrefixDotNet, FxStringPrefixDotNetCore:
result.NetFrameworkVersion = parts[1]
case FxStringPrefixGo:
result.GoVersion = parts[1]
case FxStringPrefixNode:
result.NodeVersion = parts[1]
case FxStringPrefixJava:
result.JavaServer = LinuxJavaServerJava
javaParts := strings.Split(parts[1], "-")
if strings.HasPrefix(parts[1], "8") {
result.JavaVersion = "8"
}
if strings.HasPrefix(javaParts[0], "11") {
result.JavaVersion = "11"
}
if strings.HasPrefix(javaParts[0], "17") {
result.JavaVersion = "17"
}
result.JavaServerVersion = javaParts[0]
case FxStringPrefixTomcat:
result.JavaServer = LinuxJavaServerTomcat
javaParts := strings.Split(parts[1], "-")
if len(javaParts) == 2 {
result.JavaServerVersion = javaParts[0]
javaVersion := strings.TrimPrefix(javaParts[1], "jre")
javaVersion = strings.TrimPrefix(javaVersion, "java")
result.JavaVersion = javaVersion
}
case FxStringPrefixJBoss:
result.JavaServer = LinuxJavaServerJboss
javaParts := strings.Split(parts[1], "-")
if len(javaParts) == 2 {
result.JavaServerVersion = javaParts[0]
javaVersion := strings.TrimPrefix(javaParts[1], "jre")
javaVersion = strings.TrimPrefix(javaVersion, "java")
result.JavaVersion = javaVersion
}
case FxStringPrefixPhp:
result.PhpVersion = parts[1]
case FxStringPrefixPython:
result.PythonVersion = parts[1]
case FxStringPrefixRuby:
result.RubyVersion = parts[1]
}
return result
}
func EncodeFunctionAppLinuxFxVersion(input []ApplicationStackLinuxFunctionApp) *string {
if len(input) == 0 || input[0].CustomHandler {
return utils.String("")
}
appStack := input[0]
var appType, appString string
switch {
case appStack.NodeVersion != "":
appType = string(FxStringPrefixNode)
appString = appStack.NodeVersion
case appStack.DotNetVersion != "":
if appStack.DotNetIsolated {
appType = string(FxStringPrefixDotNetIsolated)
} else {
appType = string(FxStringPrefixDotNet)
}
appString = appStack.DotNetVersion
case appStack.PythonVersion != "":
appType = string(FxStringPrefixPython)
appString = appStack.PythonVersion
case appStack.JavaVersion != "":
appType = string(FxStringPrefixJava)
appString = appStack.JavaVersion
case appStack.PowerShellCoreVersion != "":
appType = string(FxStringPrefixPowerShell)
appString = appStack.PowerShellCoreVersion
case len(appStack.Docker) > 0 && appStack.Docker[0].ImageName != "":
appType = string(FxStringPrefixDocker)
dockerCfg := appStack.Docker[0]
if dockerCfg.RegistryURL != "" {
dockerUrl := dockerCfg.RegistryURL
httpPrefixes := []string{"https://", "http://"}
for _, prefix := range httpPrefixes {
dockerUrl = strings.TrimPrefix(dockerUrl, prefix)
}
appString = fmt.Sprintf("%s/%s:%s", dockerUrl, dockerCfg.ImageName, dockerCfg.ImageTag)
} else {
appString = fmt.Sprintf("%s:%s", dockerCfg.ImageName, dockerCfg.ImageTag)
}
}
return utils.String(fmt.Sprintf("%s|%s", appType, appString))
}
func DecodeFunctionAppLinuxFxVersion(input string) ([]ApplicationStackLinuxFunctionApp, error) {
if input == "" {
// This is a valid string for "Custom" stack which we picked up earlier, so we can skip here
return nil, nil
}
parts := strings.Split(input, "|")
if len(parts) != 2 {
return nil, fmt.Errorf("unrecognised LinuxFxVersion format received, got %s", input)
}
result := make([]ApplicationStackLinuxFunctionApp, 0)
switch FxStringPrefix(strings.ToUpper(parts[0])) {
case FxStringPrefixDotNet:
appStack := ApplicationStackLinuxFunctionApp{DotNetVersion: parts[1]}
result = append(result, appStack)
case FxStringPrefixDotNetIsolated:
appStack := ApplicationStackLinuxFunctionApp{DotNetVersion: parts[1], DotNetIsolated: true}
result = append(result, appStack)
case FxStringPrefixNode:
appStack := ApplicationStackLinuxFunctionApp{NodeVersion: parts[1]}
result = append(result, appStack)
case FxStringPrefixPython:
appStack := ApplicationStackLinuxFunctionApp{PythonVersion: parts[1]}
result = append(result, appStack)
case FxStringPrefixJava:
appStack := ApplicationStackLinuxFunctionApp{JavaVersion: parts[1]}
result = append(result, appStack)
case FxStringPrefixPowerShell:
appStack := ApplicationStackLinuxFunctionApp{PowerShellCoreVersion: parts[1]}
result = append(result, appStack)
case "docker":
// This is handled as part of unpacking the app_settings using DecodeFunctionAppDockerFxString but included here for signposting as this is not intuitive.
}
return result, nil
}
func DecodeFunctionAppDockerFxString(input string, partial ApplicationStackDocker) ([]ApplicationStackDocker, error) {
if input == "" {
// This is a valid string for "Custom" stack which we picked up earlier, so we can skip here
return nil, nil
}
parts := strings.Split(input, "|")
if len(parts) != 2 {
return nil, fmt.Errorf("unrecognised LinuxFxVersion format received, got %s", input)
}
if !strings.EqualFold(parts[0], "docker") {
return nil, fmt.Errorf("expected a docker FX version, got %q", parts[0])
}
dockerUrl := partial.RegistryURL
for _, prefix := range urlSchemes {
if strings.HasPrefix(dockerUrl, prefix) {
dockerUrl = strings.TrimPrefix(dockerUrl, prefix)
continue
}
}
dockerParts := strings.Split(strings.TrimPrefix(parts[1], dockerUrl), ":")
if len(dockerParts) != 2 {
return nil, fmt.Errorf("invalid docker image reference %q", parts[1])
}
partial.ImageName = strings.TrimPrefix(dockerParts[0], "/")
partial.ImageTag = dockerParts[1]
return []ApplicationStackDocker{partial}, nil
}
func JavaLinuxFxStringBuilder(javaMajorVersion, javaServer, javaServerVersion string) (*string, error) {
switch javaMajorVersion {
case "8":
{
switch javaServer {
case LinuxJavaServerJava:
if strings.Contains(javaServerVersion, "u") {
return pointer.To(fmt.Sprintf("%s|%s", LinuxJavaServerJava, javaServerVersion)), nil // e.g. JAVA|8u302
} else {
return pointer.To(fmt.Sprintf("%s|%s-jre8", LinuxJavaServerJava, javaServerVersion)), nil // e.g. "JAVA|8-jre8"
}
case LinuxJavaServerTomcat:
if len(strings.Split(javaServerVersion, ".")) == 3 {
return pointer.To(fmt.Sprintf("%s|%s-java8", LinuxJavaServerTomcat, javaServerVersion)), nil // e.g. TOMCAT|10.0.20-java8
} else {
return pointer.To(fmt.Sprintf("%s|%s-jre8", LinuxJavaServerTomcat, javaServerVersion)), nil // e.g. TOMCAT|10.0-jre8
}
case LinuxJavaServerJboss:
return pointer.To(fmt.Sprintf("%s|%s-java8", LinuxJavaServerJboss, javaServerVersion)), nil
}
}
case "11":
switch javaServer {
case LinuxJavaServerJava:
if len(strings.Split(javaServerVersion, ".")) == 3 {
return pointer.To(fmt.Sprintf("%s|%s", LinuxJavaServerJava, javaServerVersion)), nil // e.g. JAVA|11.0.13
} else {
return pointer.To(fmt.Sprintf("%s|%s-java11", LinuxJavaServerJava, javaServerVersion)), nil // e.g.JAVA|11-java1
}
case LinuxJavaServerTomcat:
return pointer.To(fmt.Sprintf("%s|%s-java11", LinuxJavaServerTomcat, javaServerVersion)), nil // e.g. TOMCAT|10.0-java11 and TOMCAT|10.0.20-java11
case LinuxJavaServerJboss:
return pointer.To(fmt.Sprintf("%s|%s-java11", LinuxJavaServerJboss, javaServerVersion)), nil // e.g. TOMCAT|10.0-java11 and TOMCAT|10.0.20-java11// e.g. JBOSSEAP|7-java11 / JBOSSEAP|7.4.2-java11
}
case "17":
switch javaServer {
case LinuxJavaServerJava:
if len(strings.Split(javaServerVersion, ".")) == 3 {
return pointer.To(fmt.Sprintf("%s|%s", LinuxJavaServerJava, javaServerVersion)), nil // "JAVA|17.0.2"
} else {
return pointer.To(fmt.Sprintf("%s|%s-java17", LinuxJavaServerJava, javaServerVersion)), nil // "JAVA|17-java17"
}
case LinuxJavaServerTomcat:
return pointer.To(fmt.Sprintf("%s|%s-java17", LinuxJavaServerTomcat, javaServerVersion)), nil // e,g, TOMCAT|10.0-java17 / TOMCAT|10.0.20-java17
case LinuxJavaServerJboss:
return nil, fmt.Errorf("java 17 is not supported on %s", LinuxJavaServerJboss)
default:
return pointer.To(fmt.Sprintf("%s|%s-java17", javaServer, javaServerVersion)), nil
}
default:
return pointer.To(fmt.Sprintf("%s|%s-%s", javaServer, javaServerVersion, javaMajorVersion)), nil
}
return nil, fmt.Errorf("unsupported combination of `java_version`, `java_server`, and `java_server_version`")
}
func EncodeDockerFxString(image string, registryUrl string) string {
template := "DOCKER|%s/%s"
registryUrl = trimURLScheme(registryUrl)
return fmt.Sprintf(template, registryUrl, image)
}
func EncodeDockerFxStringWindows(image string, registryUrl string) string {
template := "DOCKER|%s/%s"
dockerHubTemplate := "DOCKER|%s"
registryUrl = trimURLScheme(registryUrl)
// Windows App Services fail to auth if the index portion of the image ref is `index.docker.io` so it must not be included.
if strings.EqualFold(registryUrl, "index.docker.io") {
return fmt.Sprintf(dockerHubTemplate, image)
}
return fmt.Sprintf(template, registryUrl, image)
}
func FxStringHasPrefix(input string, prefix FxStringPrefix) bool {
return strings.HasPrefix(strings.ToUpper(input), string(prefix))
}