-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathazure.go
137 lines (117 loc) · 3.79 KB
/
azure.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
package environments
import (
"encoding/json"
"fmt"
"github.com/Azure/InnovationEngine/internal/az"
"github.com/Azure/InnovationEngine/internal/logging"
"github.com/Azure/InnovationEngine/internal/ui"
)
// codeblock metadata needed for learn mode deployments.
type AzureCodeBlock struct {
Description string `json:"description"`
Command string `json:"command"`
}
// Step metadata needed for learn mode deployments.
type AzureStep struct {
Name string `json:"name"`
CodeBlocks []AzureCodeBlock `json:"codeblocks"`
}
// The status of a one-click deployment or learn mode deployment.
type AzureDeploymentStatus struct {
Steps []AzureStep `json:"steps"`
CurrentStep int `json:"currentStep"`
Status string `json:"status"`
ResourceURIs []string `json:"resourceURIs"`
Error string `json:"error"`
Output string `json:"output"`
}
func NewAzureDeploymentStatus() AzureDeploymentStatus {
return AzureDeploymentStatus{
Steps: []AzureStep{},
CurrentStep: 0,
Status: "Executing",
ResourceURIs: []string{},
Error: "",
}
}
// Get the status as a JSON string.
func (status *AzureDeploymentStatus) AsJsonString() (string, error) {
json, err := json.Marshal(status)
if err != nil {
logging.GlobalLogger.Error("Failed to marshal status", err)
return "", err
}
return string(json), nil
}
func (status *AzureDeploymentStatus) AddStep(step string, codeBlocks []AzureCodeBlock) {
status.Steps = append(status.Steps, AzureStep{
Name: step,
CodeBlocks: codeBlocks,
})
}
func (status *AzureDeploymentStatus) AddResourceURI(uri string) {
status.ResourceURIs = append(status.ResourceURIs, uri)
}
func (status *AzureDeploymentStatus) SetError(err error) {
status.Status = "Failed"
status.Error = err.Error()
}
func (status *AzureDeploymentStatus) SetOutput(output string) {
status.Output = output
}
// Print out the status JSON for azure/cloudshell if in the correct environment.
func ReportAzureStatus(status AzureDeploymentStatus, environment string) {
if !IsAzureEnvironment(environment) {
return
}
statusJson, err := status.AsJsonString()
if err != nil {
logging.GlobalLogger.Error("Failed to marshal status", err)
} else {
// We add these strings to the output so that the portal can find and parse
// the JSON status.
ocdStatus := fmt.Sprintf("ie_us%sie_ue", statusJson)
fmt.Println(ui.OcdStatusUpdateStyle.Render(ocdStatus))
}
}
// Same as ReportAzureStatus, but returns the status string instead of printing it.
func GetAzureStatus(status AzureDeploymentStatus, environment string) string {
if !IsAzureEnvironment(environment) {
return ""
}
statusJson, err := status.AsJsonString()
if err != nil {
logging.GlobalLogger.Error("Failed to marshal status", err)
return ""
} else {
// We add these strings to the output so that the portal can find and parse
// the JSON status.
ocdStatus := fmt.Sprintf("ie_us%sie_ue", statusJson)
return ocdStatus
}
}
// Attach deployed resource URIs to the one click deployment status if we're in
// the correct environment & we have a resource group name.
func AttachResourceURIsToAzureStatus(
status *AzureDeploymentStatus,
resourceGroupName string,
environment string,
) {
if !IsAzureEnvironment(environment) {
logging.GlobalLogger.Info(
"Not fetching resource URIs because we're not in the OCD environment.",
)
}
if resourceGroupName == "" {
logging.GlobalLogger.Warn("No resource group name found.")
return
}
resourceURIs := az.FindAllDeployedResourceURIs(resourceGroupName)
if len(resourceURIs) > 0 {
logging.GlobalLogger.WithField("resourceURIs", resourceURIs).
Info("Found deployed resources.")
status.ResourceURIs = append(status.ResourceURIs, resourceURIs...)
} else {
logging.GlobalLogger.Warn("No deployed resources found.")
}
}