37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
aks_virtual_networks_endpoint.go 4.41 KB
一键复制 编辑 原始数据 按行查看 历史
Nathan Jenan 提交于 2018-08-30 14:54 . Adding cidr info to subnets
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.5-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385