-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure.go
95 lines (80 loc) · 2.78 KB
/
azure.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
package main
import (
"context"
"fmt"
"regexp"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"sigs.k8s.io/cloud-provider-azure/pkg/provider"
)
type NetworkClient struct {
config *provider.Config
credential azcore.TokenCredential
}
func NewNetworkClient(config *provider.Config, credential azcore.TokenCredential) *NetworkClient {
return &NetworkClient{
config: config,
credential: credential,
}
}
func (c *NetworkClient) GetNsg() armnetwork.SecurityGroup {
ctx := context.TODO()
subnetsClient, err := armnetwork.NewSubnetsClient(c.config.SubscriptionID, c.credential, nil)
if err != nil {
// failure to build a client is a fatal error
panic(err.Error())
}
// TODO: investigate multiple node pools and differences in azure.json. We may need multiple configs
// TODO: handle VnetResourceGroup
subnet, err := subnetsClient.Get(ctx, c.config.ResourceGroup, c.config.VnetName, c.config.SubnetName, &armnetwork.SubnetsClientGetOptions{})
if err != nil {
// failure to read the subnet is a fatal error
panic(err.Error())
}
// TODO: move to verbose logs
fmt.Printf("Found nsgId: %s\n", *subnet.Properties.NetworkSecurityGroup.ID)
nsgClient, err := armnetwork.NewSecurityGroupsClient(c.config.SubscriptionID, c.credential, nil)
if err != nil {
// failure to build a client is a fatal error
panic(err.Error())
}
resourceGroup, name := parseResourceId(*subnet.Properties.NetworkSecurityGroup.ID)
nsg, err := nsgClient.Get(ctx, resourceGroup, name, nil)
if err != nil {
// failure to get the NSG is a fatal error
panic(err.Error())
}
return nsg.SecurityGroup
}
func (c *NetworkClient) UpdateNsg(nsg armnetwork.SecurityGroup) {
ctx := context.TODO()
nsgClient, err := armnetwork.NewSecurityGroupsClient(c.config.SubscriptionID, c.credential, nil)
if err != nil {
// failure to build a client is a fatal error
panic(err.Error())
}
// PUT nsg with new rules
resourceGroup, name := parseResourceId(*nsg.ID)
pollerResp, err := nsgClient.BeginCreateOrUpdate(ctx, resourceGroup, name, nsg, nil)
if err != nil {
// failure to update the NSG is a fatal error
panic(err.Error())
}
_, err = pollerResp.PollUntilDone(ctx, nil)
if err != nil {
// failure to update the NSG is a fatal error
panic(err.Error())
}
fmt.Println("Updated NSG")
}
func parseResourceId(resourceId string) (resourceGroup string, name string) {
re := regexp.MustCompile(`^/subscriptions/(?P<subscriptionId>.*)/resourceGroups/(?P<resourceGroup>.*)/providers/(?P<provider>.*)/(?P<type>.*)/(?P<name>.*)$`)
match := re.FindStringSubmatch(resourceId)
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
return result["resourceGroup"], result["name"]
}