forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aks_virtual_networks_endpoint.go
180 lines (144 loc) · 4.41 KB
/
aks_virtual_networks_endpoint.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
package capabilities
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-05-01/network"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
)
var regex = regexp.MustCompile("/resourceGroups/(.+?)/")
func NewAKSVirtualNetworksHandler() *AKSVirtualNetworksHandler {
return &AKSVirtualNetworksHandler{}
}
type AKSVirtualNetworksHandler struct {
}
type virtualNetworksRequestBody struct {
// credentials
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
SubscriptionID string `json:"subscriptionId"`
TenantID string `json:"tenantId"`
}
type virtualNetworksResponseBody struct {
Name string `json:"name"`
ResourceGroup string `json:"resourceGroup"`
Subnets []subnet `json:"subnets"`
}
type subnet struct {
Name string `json:"name"`
AddressRange string `json:"addressRange"`
}
func (g *AKSVirtualNetworksHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
writer.Header().Set("Content-Type", "application/json")
var body virtualNetworksRequestBody
if err := extractRequestBody(writer, req, &body); err != nil {
handleErr(writer, err)
return
}
if err := validateVirtualNetworksRequestBody(&body); err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, err)
return
}
clientID := body.ClientID
clientSecret := body.ClientSecret
subscriptionID := body.SubscriptionID
tenantID := body.TenantID
oAuthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, tenantID)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to configure azure oauth: %v", err))
return
}
spToken, err := adal.NewServicePrincipalToken(*oAuthConfig, clientID, clientSecret, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to create token: %v", err))
return
}
authorizer := autorest.NewBearerAuthorizer(spToken)
client := network.NewVirtualNetworksClient(subscriptionID)
client.Authorizer = authorizer
var networks []virtualNetworksResponseBody
pointer, err := client.ListAll(context.Background())
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to get networks: %v", err))
return
}
for pointer.NotDone() {
var batch []virtualNetworksResponseBody
for _, azureNetwork := range pointer.Values() {
var subnets []subnet
if azureNetwork.Subnets != nil {
for _, azureSubnet := range *azureNetwork.Subnets {
if azureSubnet.Name != nil {
subnets = append(subnets, subnet{
Name: *azureSubnet.Name,
AddressRange: *azureSubnet.AddressPrefix,
})
}
}
}
if azureNetwork.ID == nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, errors.New("no ID on virtual network"))
return
}
match := regex.FindStringSubmatch(*azureNetwork.ID)
if len(match) < 2 || match[1] == "" {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, errors.New("could not parse virtual network ID"))
return
}
if azureNetwork.Name == nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, errors.New("no name on virtual network"))
return
}
batch = append(batch, virtualNetworksResponseBody{
Name: *azureNetwork.Name,
ResourceGroup: match[1],
Subnets: subnets,
})
}
networks = append(networks, batch...)
err = pointer.Next()
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
}
serialized, err := json.Marshal(networks)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
writer.Write(serialized)
}
func validateVirtualNetworksRequestBody(body *virtualNetworksRequestBody) error {
if body.ClientID == "" {
return fmt.Errorf("invalid clientID")
}
if body.ClientSecret == "" {
return fmt.Errorf("invalid clientSecret")
}
if body.SubscriptionID == "" {
return fmt.Errorf("invalid subscriptionID")
}
if body.TenantID == "" {
return fmt.Errorf("invalid tenantID")
}
return nil
}