-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 2.86 KB
/
main.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package main
import (
"context"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
)
var (
subscriptionID string
clientID string
location string
resourceGroupName string
privateZoneName string
relativeRecordSetName = ""
)
func main() {
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
if len(subscriptionID) == 0 {
log.Fatal("AZURE_SUBSCRIPTION_ID is not set.")
}
clientID = os.Getenv("AZURE_CLIENT_ID")
if len(clientID) == 0 {
log.Fatal("AZURE_CLIENT_ID is not set.")
}
location = os.Getenv("AZURE_LOCATION")
if len(location) == 0 {
log.Fatal("AZURE_LOCATION is not set.")
}
resourceGroupName = os.Getenv("AZURE_RESOURCEGROUP_NAME")
if len(resourceGroupName) == 0 {
log.Fatal("AZURE_RESOURCEGROUP_NAME is not set.")
}
privateZoneName = os.Getenv("AZURE_PRIVATE_DNSZONE")
if len(privateZoneName) == 0 {
log.Fatal("AZURE_PRIVATE_DNSZONE is not set.")
}
// Select user-assigned identity via its clientID.
// Does the clientID come from a secret?
clientID := azidentity.ClientID(clientID)
opts := azidentity.ManagedIdentityCredentialOptions{ID: clientID}
cred, err := azidentity.NewManagedIdentityCredential(&opts)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
privateZone, err := createPrivateZone(ctx, cred)
if err != nil {
log.Fatal(err)
}
log.Println("private zone:", *privateZone.ID)
keepResource := os.Getenv("KEEP_RESOURCE")
if len(keepResource) == 0 {
err = cleanup(ctx, cred)
if err != nil {
log.Fatal(err)
}
log.Println("cleaned up successfully.")
}
}
func createPrivateZone(ctx context.Context, cred azcore.TokenCredential) (*armprivatedns.PrivateZone, error) {
privateZonesClient, err := armprivatedns.NewPrivateZonesClient(subscriptionID, cred, nil)
if err != nil {
return nil, err
}
pollersResp, err := privateZonesClient.BeginCreateOrUpdate(
ctx,
resourceGroupName,
privateZoneName,
armprivatedns.PrivateZone{
Location: to.Ptr(location),
},
nil,
)
if err != nil {
return nil, err
}
resp, err := pollersResp.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &resp.PrivateZone, nil
}
func cleanup(ctx context.Context, cred azcore.TokenCredential) error {
privateZonesClient, err := armprivatedns.NewPrivateZonesClient(subscriptionID, cred, nil)
if err != nil {
return err
}
pollersResp, err := privateZonesClient.BeginDelete(
ctx,
resourceGroupName,
privateZoneName,
nil,
)
_, err = pollersResp.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}