/
actions.go
112 lines (100 loc) · 3.53 KB
/
actions.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
package views
import (
"context"
"encoding/json"
"strings"
"github.com/lawrencegripper/azbrowse/internal/pkg/eventing"
"github.com/lawrencegripper/azbrowse/internal/pkg/expanders"
"github.com/lawrencegripper/azbrowse/internal/pkg/tracing"
"github.com/lawrencegripper/azbrowse/pkg/armclient"
)
// TODO: Migrate to a handler...maybe
// LoadActionsView Shows available actions for the current resource
func LoadActionsView(ctx context.Context, list *ListWidget) error {
statusEvent, _ := eventing.SendStatusEvent(&eventing.StatusEvent{
Message: "Getting available actions",
InProgress: true,
})
defer statusEvent.Done()
var namespace string
var armType string
currentItem := list.CurrentItem()
if currentItem.ItemType == expanders.ResourceType {
namespace = currentItem.Namespace
armType = currentItem.ArmType
}
currentExpandedItem := list.CurrentExpandedItem()
if currentExpandedItem == nil {
return nil
}
if currentExpandedItem.ItemType == expanders.ResourceType {
namespace = currentExpandedItem.Namespace
armType = currentExpandedItem.ArmType
}
if namespace == "" || armType == "" {
return nil
}
span, ctx := tracing.StartSpanFromContext(ctx, "actions:"+currentItem.Name, tracing.SetTag("item", currentItem))
defer span.Finish()
data, err := armclient.LegacyInstance.DoRequest(ctx, "GET", "/providers/Microsoft.Authorization/providerOperations/"+namespace+"?api-version=2018-01-01-preview&$expand=resourceTypes")
if err != nil {
list.statusView.Status("Failed to get actions: "+err.Error(), false)
}
var opsRequest OperationsRequest
err = json.Unmarshal([]byte(data), &opsRequest)
if err != nil {
panic(err)
}
items := []*expanders.TreeNode{}
for _, resOps := range opsRequest.ResourceTypes {
if resOps.Name == strings.Split(armType, "/")[1] {
for _, op := range resOps.Operations {
resourceAPIVersion, err := armclient.GetAPIVersion(currentItem.ArmType)
if err != nil {
list.statusView.Status("Failed to find an api version: "+err.Error(), false)
}
stripArmType := strings.Replace(op.Name, currentItem.ArmType, "", -1)
actionURL := strings.Replace(stripArmType, "/action", "", -1) + "?api-version=" + resourceAPIVersion
items = append(items, &expanders.TreeNode{
Name: op.DisplayName,
Display: op.DisplayName,
ExpandURL: currentItem.ID + "/" + actionURL,
ExpandReturnType: expanders.ActionType,
ItemType: "action",
ID: currentItem.ID + "/" + actionURL,
})
}
}
}
if len(items) > 1 {
list.SetNodes(items)
}
return nil
}
// OperationsRequest list the actions that can be performed
type OperationsRequest struct {
DisplayName string `json:"displayName"`
Operations []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Origin interface{} `json:"origin"`
Properties interface{} `json:"properties"`
IsDataAction bool `json:"isDataAction"`
} `json:"operations"`
ResourceTypes []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Operations []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Origin interface{} `json:"origin"`
Properties interface{} `json:"properties"`
IsDataAction bool `json:"isDataAction"`
} `json:"operations"`
} `json:"resourceTypes"`
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}