-
Notifications
You must be signed in to change notification settings - Fork 184
/
main.go
176 lines (150 loc) · 4 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
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
// 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"
"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/resources/armresources"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/servicebus/armservicebus"
"log"
"os"
)
var (
subscriptionID string
location = "westus"
resourceGroupName = "sample-resource-group2"
namespaceName = "sample-sb-namespace2"
topicName = "sample-sb-topic2"
)
var (
resourcesClientFactory *armresources.ClientFactory
servicebusClientFactory *armservicebus.ClientFactory
)
var (
resourceGroupClient *armresources.ResourceGroupsClient
namespacesClient *armservicebus.NamespacesClient
topicsClient *armservicebus.TopicsClient
)
func main() {
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
if len(subscriptionID) == 0 {
log.Fatal("AZURE_SUBSCRIPTION_ID is not set.")
}
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
resourcesClientFactory, err = armresources.NewClientFactory(subscriptionID, cred, nil)
if err != nil {
log.Fatal(err)
}
resourceGroupClient = resourcesClientFactory.NewResourceGroupsClient()
servicebusClientFactory, err = armservicebus.NewClientFactory(subscriptionID, cred, nil)
if err != nil {
log.Fatal(err)
}
namespacesClient = servicebusClientFactory.NewNamespacesClient()
topicsClient = servicebusClientFactory.NewTopicsClient()
resourceGroup, err := createResourceGroup(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("resources group:", *resourceGroup.ID)
namespace, err := createNamespace(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("service bus namespace:", *namespace.ID)
topic, err := createTopic(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("service bus topic:", *topic.ID)
topicGet, err := getTopic(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("get service bus topic:", *topicGet.ID)
keepResource := os.Getenv("KEEP_RESOURCE")
if len(keepResource) == 0 {
err = cleanup(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("cleaned up successfully.")
}
}
func createNamespace(ctx context.Context) (*armservicebus.SBNamespace, error) {
pollerResp, err := namespacesClient.BeginCreateOrUpdate(
ctx,
resourceGroupName,
namespaceName,
armservicebus.SBNamespace{
Location: to.Ptr(location),
SKU: &armservicebus.SBSKU{
Name: to.Ptr(armservicebus.SKUNameStandard),
Tier: to.Ptr(armservicebus.SKUTierStandard),
},
},
nil,
)
if err != nil {
return nil, err
}
resp, err := pollerResp.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &resp.SBNamespace, nil
}
func createTopic(ctx context.Context) (*armservicebus.SBTopic, error) {
resp, err := topicsClient.CreateOrUpdate(
ctx,
resourceGroupName,
namespaceName,
topicName,
armservicebus.SBTopic{
Properties: &armservicebus.SBTopicProperties{
EnableExpress: to.Ptr(true),
},
},
nil,
)
if err != nil {
return nil, err
}
return &resp.SBTopic, nil
}
func getTopic(ctx context.Context) (*armservicebus.SBTopic, error) {
resp, err := topicsClient.Get(ctx, resourceGroupName, namespaceName, topicName, nil)
if err != nil {
return nil, err
}
return &resp.SBTopic, nil
}
func createResourceGroup(ctx context.Context) (*armresources.ResourceGroup, error) {
resourceGroupResp, err := resourceGroupClient.CreateOrUpdate(
ctx,
resourceGroupName,
armresources.ResourceGroup{
Location: to.Ptr(location),
},
nil)
if err != nil {
return nil, err
}
return &resourceGroupResp.ResourceGroup, nil
}
func cleanup(ctx context.Context) error {
pollerResp, err := resourceGroupClient.BeginDelete(ctx, resourceGroupName, nil)
if err != nil {
return err
}
_, err = pollerResp.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}