/
storageManagementPolicies.go
85 lines (71 loc) · 3.39 KB
/
storageManagementPolicies.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
package handlers
import (
"context"
)
// The storage API currently doesn't provide a way to list management policies for a storage account
// I.e. no GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/managementPolicies
// This means that there is no way to navigate via swagger from the account to the management policies
// Any policy that exists also currently has to have the name "default" (i.e. can only actually have 0 or 1)
// This expander adds the link from storage account to management policy
// StorageManagementPoliciesExpander expands The default management policy under a storage account
type StorageManagementPoliciesExpander struct{}
// Name returns the name of the expander
func (e *StorageManagementPoliciesExpander) Name() string {
return "StorageManagementPoliciesExpander"
}
// DoesExpand checks if this is a storage account
func (e *StorageManagementPoliciesExpander) DoesExpand(ctx context.Context, currentItem *TreeNode) (bool, error) {
swaggerResourceType := currentItem.SwaggerResourceType
if currentItem.ItemType == "resource" && swaggerResourceType != nil {
if swaggerResourceType.Endpoint.TemplateURL == "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}" {
return true, nil
}
}
return false, nil
}
// Expand returns ManagementPolicies in the StorageAccount
func (e *StorageManagementPoliciesExpander) Expand(ctx context.Context, currentItem *TreeNode) ExpanderResult {
isPrimaryResponse := false
newItems := []*TreeNode{}
swaggerResourceType := currentItem.SwaggerResourceType
if swaggerResourceType.Endpoint.TemplateURL == "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}" {
matchResult := swaggerResourceType.Endpoint.Match(currentItem.ExpandURL) // TODO - return the matches from getHandledTypeForURL to avoid re-calculating!
templateValues := matchResult.Values
if swaggerResourceType.SubResources != nil {
for _, resourceType := range swaggerResourceType.SubResources {
if resourceType.Endpoint.TemplateURL == "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/managementPolicies/{managementPolicyName}" {
// got the management policies endpoint
// now create the expand URL etc etc
defaultPolicyTemplateValues := make(map[string]string)
for k, v := range templateValues {
defaultPolicyTemplateValues[k] = v
}
defaultPolicyTemplateValues["managementPolicyName"] = "default"
url, err := resourceType.Endpoint.BuildURL(defaultPolicyTemplateValues)
if err != nil {
return ExpanderResult{
Err: err,
}
}
newItems = append(newItems, &TreeNode{
Parentid: currentItem.ID,
Namespace: "storageManagementPolicies",
Name: "ManagementPolicy",
Display: "Management Policy",
ItemType: SubResourceType,
ExpandURL: url,
SwaggerResourceType: &resourceType,
})
break
}
}
}
}
return ExpanderResult{
Err: nil,
Response: "TODO",
SourceDescription: "StorageManagementPoliciesExpander request",
Nodes: newItems,
IsPrimaryResponse: isPrimaryResponse,
}
}