-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal_reset.go
147 lines (111 loc) · 3.48 KB
/
portal_reset.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
package cmd
import (
"context"
"net/http"
"net/url"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/jake-scott/apim-tools/internal/pkg/logging"
)
var portalResetCmd = &cobra.Command{
Use: "reset",
Short: "Reset the APIM developer portal",
Long: `Delete all deveoper portal contents.
NOTE: THIS OPTION IS DESTRUCTIVE AND CANNOT BE REVERSED.`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := doPortalReset(); err != nil {
return err
}
return nil
},
}
func init() {
portalResetCmd.Flags().StringVar(&portalCmdOpts.apimName, "apim", "", "API Manager instance")
portalResetCmd.Flags().StringVar(&portalCmdOpts.resourceGroup, "rg", "", "Resource group containing the APIM instance")
errPanic(portalResetCmd.MarkFlagRequired("apim"))
errPanic(portalResetCmd.MarkFlagRequired("rg"))
errPanic(viper.GetViper().BindPFlag("apim", portalResetCmd.Flags().Lookup("apim")))
errPanic(viper.GetViper().BindPFlag("rg", portalResetCmd.Flags().Lookup("rg")))
portalCmd.AddCommand(portalResetCmd)
}
func doPortalReset() error {
info, err := buildApimInfo(azureAPIVersion)
if err != nil {
return err
}
// run the reset
if err := deletePortalContentItems(info.apimClient, info.apimMgmtURL); err != nil {
return err
}
return resetPortalBlobs(info.devPortalBlobStorageURL)
}
func deletePortalContentItems(cli *apimClient, mgmtURL string) error {
logging.Logger().Info("Deleting portal content items")
// Get content types used by the portal
contentTypes, err := getContentTypes(cli, mgmtURL)
if err != nil {
return err
}
// Get content items for each content type
var contentItems []map[string]interface{}
for _, ct := range contentTypes {
subItems, err := getContentItemsAsMap(cli, mgmtURL, ct)
if err != nil {
return err
}
contentItems = append(contentItems, subItems...)
}
var cOK, cErr int
// Delete the content items
for _, item := range contentItems {
id := item["id"].(string)
reqURL := apimMgmtURL(mgmtURL) + id
req, err := http.NewRequest("DELETE", reqURL, nil)
if err != nil {
return err
}
resp, err := cli.Do(req)
if err != nil {
cErr++
logging.Logger().Errorf("Deleting %s: %s", id, err)
continue
}
// Only accept HTTP 2xx codes
if resp.StatusCode >= 300 {
cErr++
logging.Logger().Errorf("Deleting %s: %s", id, resp.Status)
continue
}
cOK++
}
logging.Logger().Infof("Deleted %d content items, %d errors", cOK, cErr)
return nil
}
func resetPortalBlobs(blobURLString string) error {
logging.Logger().Infof("Deleting blobs")
u, _ := url.Parse(blobURLString)
ctx := context.Background()
containerURL := azblob.NewContainerURL(*u, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
var cOK, cErr int
for marker := (azblob.Marker{}); marker.NotDone(); {
listBlobs, err := containerURL.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
if err != nil {
return err
}
marker = listBlobs.NextMarker
for _, blobInfo := range listBlobs.Segment.BlobItems {
logging.Logger().Debugf("Deleting blob: %s", blobInfo.Name)
blobURL := containerURL.NewBlobURL(blobInfo.Name)
_, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
if err != nil {
logging.Logger().WithError(err).Errorf("Deleting BLOB %s", blobInfo.Name)
cErr++
} else {
cOK++
}
}
}
logging.Logger().Infof("Deleted %d blobs, %d errors", cOK, cErr)
return nil
}