代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。