-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathdelete-rg.go
46 lines (39 loc) · 1.38 KB
/
delete-rg.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
package azure
import (
"context"
"fmt"
"log"
"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"
)
type DeleteResourceGroup struct {
SubscriptionID string
ResourceGroupName string
Location string
}
func (d *DeleteResourceGroup) Run() error {
log.Printf("deleting resource group \"%s\"...", d.ResourceGroupName)
cred, err := azidentity.NewAzureCLICredential(nil)
if err != nil {
return fmt.Errorf("failed to obtain a credential: %w", err)
}
ctx := context.Background()
clientFactory, err := armresources.NewClientFactory(d.SubscriptionID, cred, nil)
if err != nil {
return fmt.Errorf("failed to create resource group client: %w", err)
}
forceDeleteType := "Microsoft.Compute/virtualMachines,Microsoft.Compute/virtualMachineScaleSets"
_, err = clientFactory.NewResourceGroupsClient().BeginDelete(ctx, d.ResourceGroupName, &armresources.ResourceGroupsClientBeginDeleteOptions{ForceDeletionTypes: to.Ptr(forceDeleteType)})
if err != nil {
return fmt.Errorf("failed to finish the delete resource group request: %w", err)
}
log.Printf("resource group \"%s\" deleted successfully", d.ResourceGroupName)
return nil
}
func (d *DeleteResourceGroup) Prevalidate() error {
return nil
}
func (d *DeleteResourceGroup) Stop() error {
return nil
}