/
deployments.go
200 lines (181 loc) · 6.63 KB
/
deployments.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
package expanders
import (
"context"
"encoding/json"
"github.com/lawrencegripper/azbrowse/internal/pkg/style"
"github.com/lawrencegripper/azbrowse/pkg/armclient"
)
// Check interface
var _ Expander = &DeploymentsExpander{}
// DeploymentsExpander expands RGs under a subscription
type DeploymentsExpander struct {
ExpanderBase
client *armclient.Client
}
func (e *DeploymentsExpander) setClient(c *armclient.Client) {
e.client = c
}
// Name returns the name of the expander
func (e *DeploymentsExpander) Name() string {
return "DeploymentsExpander"
}
// DoesExpand checks if this is an RG
func (e *DeploymentsExpander) DoesExpand(ctx context.Context, currentItem *TreeNode) (bool, error) {
switch currentItem.ItemType {
case deploymentsType, deploymentType:
return true, nil
}
return false, nil
}
// Expand returns Resources in the RG
func (e *DeploymentsExpander) Expand(ctx context.Context, currentItem *TreeNode) ExpanderResult {
method := "GET"
isPrimaryResponse := true
data, err := e.client.DoRequest(ctx, method, currentItem.ExpandURL)
if err != nil {
return ExpanderResult{
Err: err,
Response: ExpanderResponse{Response: ""},
SourceDescription: "Deployments Subdeployment",
IsPrimaryResponse: true,
}
}
newItems := []*TreeNode{}
if currentItem.ItemType == deploymentsType {
var deployments DeploymentsResponse
err = json.Unmarshal([]byte(data), &deployments)
if err != nil {
panic(err)
}
value, err := fastJSONParser.Parse(data)
if err != nil {
panic(err)
}
for i, dep := range deployments.Value {
// Update the existing state as we have more up-to-date info
objectJSON := string(value.GetArray("value")[i].MarshalTo([]byte("")))
newItems = append(newItems, &TreeNode{
Name: dep.Name,
Display: dep.Name + "\n " + style.Subtle("Started: "+dep.Properties.Timestamp) + "\n " + style.Subtle("Duration: "+dep.Properties.Duration) + "\n " + style.Subtle("DeploymentStatus: "+dep.Properties.ProvisioningState+""),
ID: dep.ID,
Parentid: currentItem.ID + "/operations/",
ExpandURL: dep.ID + "/operations/?api-version=2017-05-10",
ItemType: deploymentType,
DeleteURL: dep.ID + "?api-version=2017-05-10",
SubscriptionID: currentItem.SubscriptionID,
StatusIndicator: DrawStatus(dep.Properties.ProvisioningState),
Metadata: map[string]string{
"jsonItem": objectJSON,
},
})
}
} else if currentItem.ItemType == deploymentType {
var operations DeploymentOperationsResponse
err = json.Unmarshal([]byte(data), &operations)
if err != nil {
panic(err)
}
value, err := fastJSONParser.Parse(data)
if err != nil {
panic(err)
}
for i, operation := range operations.Value {
// Update the existing state as we have more up-to-date info
objectJSON := string(value.GetArray("value")[i].MarshalTo([]byte("")))
title := operation.OperationID
if operation.Properties.TargetResource.ResourceType != "" {
title = operation.Properties.TargetResource.ResourceName
}
display := title + "\n " + style.Subtle("Started:"+operation.Properties.Timestamp) + "\n " + style.Subtle("Duration: "+operation.Properties.Duration) + "\n " + style.Subtle("DeploymentStatus: "+operation.Properties.ProvisioningState+"")
if operation.Properties.TargetResource.ResourceType != "" {
display += "\n " + style.Subtle("ResourceType:"+operation.Properties.TargetResource.ResourceType)
}
newItems = append(newItems, &TreeNode{
Name: operation.OperationID,
Display: display,
ID: operation.ID,
Parentid: currentItem.ID,
ExpandURL: ExpandURLNotSupported,
ItemType: deploymentOperationType,
SubscriptionID: currentItem.SubscriptionID,
Metadata: map[string]string{
"jsonItem": objectJSON,
},
})
}
isPrimaryResponse = false
}
return ExpanderResult{
Err: err,
Response: ExpanderResponse{Response: string(data), ResponseType: ResponseJSON},
SourceDescription: "Deployments request",
Nodes: newItems,
IsPrimaryResponse: isPrimaryResponse,
}
}
// DeploymentsResponse is returned by a request for deployments in an RG
type DeploymentsResponse struct {
Value []struct {
ID string `json:"id"`
Name string `json:"name"`
Properties struct {
CorrelationID string `json:"correlationId"`
Dependencies []struct {
DependsOn []struct {
ID string `json:"id"`
ResourceName string `json:"resourceName"`
ResourceType string `json:"resourceType"`
} `json:"dependsOn"`
ID string `json:"id"`
ResourceName string `json:"resourceName"`
ResourceType string `json:"resourceType"`
} `json:"dependencies"`
Duration string `json:"duration"`
Mode string `json:"mode"`
OutputResources []struct {
ID string `json:"id"`
} `json:"outputResources"`
Outputs map[string]interface{} `json:"outputs"`
Parameters map[string]interface{} `json:"parameters"`
Providers []struct {
Namespace string `json:"namespace"`
ResourceTypes []struct {
Locations []string `json:"locations"`
ResourceType string `json:"resourceType"`
} `json:"resourceTypes"`
} `json:"providers"`
ProvisioningState string `json:"provisioningState"`
TemplateHash string `json:"templateHash"`
Template map[string]interface{} `json:"template"`
TemplateLink struct {
ContentVersion string `json:"contentVersion"`
URI string `json:"uri"`
} `json:"templateLink"`
Timestamp string `json:"timestamp"`
} `json:"properties"`
} `json:"value"`
}
// DeploymentOperationsResponse is a struct to enable splitting out json value array
type DeploymentOperationsResponse struct {
Value []struct {
ID string `json:"id"`
OperationID string `json:"operationId"`
Properties struct {
StatusCode string `json:"statusCode"`
StatusMessage interface{} `json:"statusMessage"`
Timestamp string `json:"timestamp"`
Duration string `json:"duration"`
ProvisioningOperation string `json:"provisioningOperation"`
ProvisioningState string `json:"provisioningState"`
TrackingID string `json:"trackingId"`
TargetResource struct {
ID string `json:"id"`
ResourceType string `json:"resourceType"`
ResourceName string `json:"resourceName"`
} `json:"targetResource"`
} `json:"properties"`
} `json:"value"`
}
func (e *DeploymentsExpander) testCases() (bool, *[]expanderTestCase) {
return false, nil
}