This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
common-provision.go
143 lines (130 loc) · 4.05 KB
/
common-provision.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
package cosmosdb
import (
"context"
"fmt"
"github.com/Azure/open-service-broker-azure/pkg/generate"
"github.com/Azure/open-service-broker-azure/pkg/service"
log "github.com/Sirupsen/logrus"
uuid "github.com/satori/go.uuid"
)
const enabled = "enabled"
const disabled = "disabled"
func generateAccountName(location string) string {
databaseAccountName := uuid.NewV4().String()
// CosmosDB currently limits database account names to 50 characters,
// which includes location and a - character. Check if we will
// exceed this and generate a shorter random identifier if needed.
effectiveNameLength := len(location) + len(databaseAccountName)
if effectiveNameLength > 49 {
nameLength := 49 - len(location)
databaseAccountName = generate.NewIdentifierOfLength(nameLength)
logFields := log.Fields{
"name": databaseAccountName,
"length": len(databaseAccountName),
}
log.WithFields(logFields).Debug(
"returning fallback database account name",
)
}
return databaseAccountName
}
func (c *cosmosAccountManager) preProvision(
_ context.Context,
instance service.Instance,
) (service.InstanceDetails, error) {
l := instance.ProvisioningParameters.GetString("location")
return &cosmosdbInstanceDetails{
ARMDeploymentName: uuid.NewV4().String(),
DatabaseAccountName: generateAccountName(l),
}, nil
}
func (c *cosmosAccountManager) buildGoTemplateParams(
pp *service.ProvisioningParameters,
dt *cosmosdbInstanceDetails,
kind string,
) (map[string]interface{}, error) {
// In Azure portal, "region" is used to indicate a place, while in REST api,
// "location" is used to indicate a place. So we use "readRegions" when
// communicating with users and use "readLocations" in the code.
readLocations := pp.GetStringArray("readRegions")
readLocations = append([]string{pp.GetString("location")}, readLocations...)
return c.buildGoTemplateParamsCore(
pp,
dt,
kind,
readLocations,
)
}
// The deployment will return success once the write region is created,
// ignoring the status of read regions , so we must implement detection logic
// by ourselves.
func (c *cosmosAccountManager) waitForReadLocationsReady(
ctx context.Context,
instance service.Instance,
) (service.InstanceDetails, error) {
dt := instance.Details.(*cosmosdbInstanceDetails)
resourceGroupName := instance.ProvisioningParameters.GetString("resourceGroup")
accountName := dt.DatabaseAccountName
databaseAccountClient := c.databaseAccountsClient
err := pollingUntilReadLocationsReady(
ctx,
resourceGroupName,
accountName,
databaseAccountClient,
instance.ProvisioningParameters.GetString("location"),
instance.ProvisioningParameters.GetStringArray("readRegions"),
false,
)
if err != nil {
return nil, err
}
return dt, nil
}
func getTags(pp *service.ProvisioningParameters) map[string]string {
tagsObj := pp.GetObject("tags")
tags := make(map[string]string, len(tagsObj.Data))
for k := range tagsObj.Data {
tags[k] = tagsObj.GetString(k)
}
return tags
}
func (c *cosmosAccountManager) deployARMTemplate(
pp *service.ProvisioningParameters,
dt *cosmosdbInstanceDetails,
goParams map[string]interface{},
tags map[string]string,
) (string, string, error) {
outputs, err := c.armDeployer.Deploy(
dt.ARMDeploymentName,
pp.GetString("resourceGroup"),
pp.GetString("location"),
armTemplateBytes,
goParams, // Go template params
map[string]interface{}{},
tags,
)
if err != nil {
return "", "", fmt.Errorf("error deploying ARM template: %s", err)
}
fqdn, primaryKey, err := c.handleOutput(outputs)
if err != nil {
return "", "", fmt.Errorf("error deploying ARM template: %s", err)
}
return fqdn, primaryKey, nil
}
func (c *cosmosAccountManager) handleOutput(
outputs map[string]interface{},
) (string, string, error) {
var ok bool
fqdn, ok := outputs["fullyQualifiedDomainName"].(string)
if !ok {
return "", "", fmt.Errorf(
"error retrieving fully qualified domain name from deployment",
)
}
primaryKey, ok := outputs["primaryKey"].(string)
if !ok {
return "", "", fmt.Errorf("error retrieving primary key from deployment")
}
return fqdn, primaryKey, nil
}