-
Notifications
You must be signed in to change notification settings - Fork 12
/
actions.go
77 lines (66 loc) · 2.38 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
package views
import (
"context"
"encoding/json"
"strings"
"github.com/lawrencegripper/azbrowse/internal/pkg/handlers"
"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 {
list.statusView.Status("Getting available Actions", true)
var namespace string
var armType string
currentItem := list.CurrentItem()
if currentItem.ItemType == handlers.ResourceType {
namespace = currentItem.Namespace
armType = currentItem.ArmType
}
currentExpandedItem := list.CurrentExpandedItem()
if currentExpandedItem.ItemType == handlers.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.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 armclient.OperationsRequest
err = json.Unmarshal([]byte(data), &opsRequest)
if err != nil {
panic(err)
}
items := []*handlers.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, &handlers.TreeNode{
Name: op.DisplayName,
Display: op.DisplayName,
ExpandURL: currentItem.ID + "/" + actionURL,
ExpandReturnType: handlers.ActionType,
ItemType: "action",
ID: currentItem.ID,
})
}
}
}
if len(items) > 1 {
list.SetNodes(items)
}
list.statusView.Status("Fetched available Actions", false)
return nil
}