-
Notifications
You must be signed in to change notification settings - Fork 12
/
deployment.go
69 lines (57 loc) · 1.97 KB
/
deployment.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
package model
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/microsoft/commercial-marketplace-offer-deploy/pkg/deployment"
"gorm.io/gorm"
)
const deploymentNamePartSeparator = "."
type Deployment struct {
gorm.Model
Name string `json:"name"`
Template map[string]any `json:"template" gorm:"json"`
Stages []Stage `json:"stages" gorm:"foreignKey:DeploymentID"`
// azure properties
SubscriptionId string `json:"subscriptionId"`
ResourceGroup string `json:"resourceGroup"`
Location string `json:"location"`
}
// Gets the azure deployment name suitable for azure deployment
// format - modm.<deploymentId>.<deploymentName>
func (d *Deployment) GetAzureDeploymentName() string {
prefix := "modm" + deploymentNamePartSeparator + strconv.FormatUint(uint64(d.ID), 10) + deploymentNamePartSeparator
suffix := d.getSanitizedName()
maxLength := 64
lengthCheck := len(prefix + suffix)
// reduce size of the name if the total length is greater than 64
if lengthCheck > maxLength {
suffix = suffix[:maxLength-len(suffix)]
}
return prefix + suffix
}
func (d *Deployment) ParseAzureDeploymentName(azureDeploymentName string) (*int, error) {
return ParseAzureDeploymentName(azureDeploymentName)
}
// Parses the azure deployment name and returns OUR deployment id
func ParseAzureDeploymentName(azureDeploymentName string) (*int, error) {
isManagedDeployment := strings.HasPrefix(azureDeploymentName, deployment.LookupPrefix)
if isManagedDeployment {
parts := strings.Split(azureDeploymentName, deploymentNamePartSeparator)
idString := parts[1]
id, err := strconv.Atoi(idString)
if err != nil {
return nil, err
}
return &id, nil
}
return nil, fmt.Errorf("[%s] is not a managed deployment", azureDeploymentName)
}
func (d *Deployment) getSanitizedName() string {
r := regexp.MustCompile("[^a-zA-Z0-9 -]")
name := r.ReplaceAllString(d.Name, "")
name = strings.ReplaceAll(name, " ", "-")
name = strings.TrimSuffix(name, "-")
return name
}