/
tentant.go
166 lines (148 loc) · 4.31 KB
/
tentant.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
package expanders
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/lawrencegripper/azbrowse/internal/pkg/tracing"
"github.com/lawrencegripper/azbrowse/pkg/armclient"
"github.com/nbio/st"
)
const (
// TentantItemType a TreeNode item representing a tenant
TentantItemType = "tentantItemType"
)
// Check interface
var _ Expander = &TenantExpander{}
// TenantExpander expands the subscriptions under a tenant
type TenantExpander struct {
ExpanderBase
client *armclient.Client
}
func (e *TenantExpander) setClient(c *armclient.Client) {
e.client = c
}
// Name returns the name of the expander
func (e *TenantExpander) Name() string {
return "TenantExpander"
}
// DoesExpand checks if this is an RG
func (e *TenantExpander) DoesExpand(ctx context.Context, currentItem *TreeNode) (bool, error) {
if currentItem.ItemType == TentantItemType {
return true, nil
}
return false, nil
}
// Expand returns Resources in the RG
func (e *TenantExpander) Expand(ctx context.Context, currentItem *TreeNode) ExpanderResult {
span, ctx := tracing.StartSpanFromContext(ctx, "expand:subs")
defer span.Finish()
// Get Subscriptions
data, err := e.client.DoRequest(ctx, "GET", "/subscriptions?api-version=2018-01-01")
if err != nil {
return ExpanderResult{
SourceDescription: e.Name(),
Err: err,
Response: ExpanderResponse{
Response: data,
ResponseType: ResponsePlainText,
},
IsPrimaryResponse: true,
}
// return armclient.SubResponse{}, "", fmt.Errorf("Failed to load subscriptions: %s", err)
}
var subRequest SubResponse
err = json.Unmarshal([]byte(data), &subRequest)
if err != nil {
return ExpanderResult{
SourceDescription: e.Name(),
Err: fmt.Errorf("Failed to load subscriptions: %s", err),
Response: ExpanderResponse{
Response: data,
ResponseType: ResponsePlainText,
},
IsPrimaryResponse: true,
}
}
newList := []*TreeNode{}
for _, sub := range subRequest.Subs {
newList = append(newList, &TreeNode{
Display: sub.DisplayName,
Name: sub.DisplayName,
ID: sub.ID,
ExpandURL: sub.ID + "/resourceGroups?api-version=2018-05-01",
ItemType: SubscriptionType,
SubscriptionID: sub.SubscriptionID,
})
}
var newContent string
var newContentType ExpanderResponseType
if err != nil {
newContent = err.Error()
newContentType = ResponsePlainText
} else {
newContent = data
newContentType = ResponseJSON
}
return ExpanderResult{
SourceDescription: e.Name(),
IsPrimaryResponse: true,
Nodes: newList,
Response: ExpanderResponse{
Response: newContent,
ResponseType: newContentType,
},
}
}
// SubResponse Subscriptions REST type
type SubResponse struct {
Subs []struct {
ID string `json:"id"`
SubscriptionID string `json:"subscriptionId"`
DisplayName string `json:"displayName"`
State string `json:"state"`
SubscriptionPolicies struct {
LocationPlacementID string `json:"locationPlacementId"`
QuotaID string `json:"quotaId"`
SpendingLimit string `json:"spendingLimit"`
} `json:"subscriptionPolicies"`
} `json:"value"`
}
func (e *TenantExpander) testCases() (bool, *[]expanderTestCase) {
treeNode := &TreeNode{
ItemType: TentantItemType,
ID: "AvailableSubscriptions",
ExpandURL: ExpandURLNotSupported,
}
return true, &[]expanderTestCase{
{
name: "Tenant->Subs",
nodeToExpand: treeNode,
urlPath: "subscriptions",
responseFile: "./testdata/armsamples/subscriptions/response.json",
statusCode: 200,
treeNodeCheckerFunc: func(t *testing.T, r ExpanderResult) {
st.Expect(t, r.Err, nil)
st.Expect(t, len(r.Nodes), 3)
// Validate content
st.Expect(t, r.Nodes[0].Display, "Thingy1")
st.Expect(t, r.Nodes[0].ExpandURL, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups?api-version=2018-05-01")
},
},
{
name: "Tenant->Subs500Response",
nodeToExpand: &TreeNode{
ItemType: TentantItemType,
ID: "AvailableSubscriptions",
ExpandURL: ExpandURLNotSupported,
},
urlPath: "subscriptions",
statusCode: 500,
treeNodeCheckerFunc: func(t *testing.T, r ExpanderResult) {
if r.Err == nil {
t.Error("Failed expanding resource. Should have errored and didn't", r)
}
},
},
}
}