-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal_download.go
205 lines (161 loc) · 5.4 KB
/
portal_download.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strings"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/jake-scott/apim-tools/internal/pkg/devportal"
"github.com/jake-scott/apim-tools/internal/pkg/logging"
)
var portalDownloadCmd = &cobra.Command{
Use: "download",
Short: "Download the APIM developer portal content to a ZIP archive",
RunE: func(cmd *cobra.Command, args []string) error {
if err := doPortalDownload(); err != nil {
return err
}
return nil
},
}
func init() {
portalDownloadCmd.Flags().StringVar(&portalCmdOpts.apimName, "apim", "", "API Manager instance")
portalDownloadCmd.Flags().StringVar(&portalCmdOpts.backupFile, "out", "", "Output archive")
portalDownloadCmd.Flags().StringVar(&portalCmdOpts.resourceGroup, "rg", "", "Resource group containing the APIM instance")
portalDownloadCmd.Flags().BoolVarP(&portalCmdOpts.force, "force", "f", false, "Overwrite existing archive")
errPanic(portalDownloadCmd.MarkFlagRequired("apim"))
errPanic(portalDownloadCmd.MarkFlagRequired("out"))
errPanic(portalDownloadCmd.MarkFlagRequired("rg"))
errPanic(viper.GetViper().BindPFlag("apim", portalDownloadCmd.Flags().Lookup("apim")))
errPanic(viper.GetViper().BindPFlag("out", portalDownloadCmd.Flags().Lookup("out")))
errPanic(viper.GetViper().BindPFlag("rg", portalDownloadCmd.Flags().Lookup("rg")))
errPanic(viper.GetViper().BindPFlag("force", portalDownloadCmd.Flags().Lookup("force")))
portalCmd.AddCommand(portalDownloadCmd)
}
func doPortalDownload() error {
info, err := buildApimInfo(azureAPIVersion)
if err != nil {
return err
}
// Create a ZIP archive
aw, err := devportal.NewArchiveWriter(viper.GetString("out"))
if err != nil {
return err
}
defer aw.Close()
// run the download
if err := getPortalContentItems(aw, info.apimClient, info.apimMgmtURL); err != nil {
return err
}
return downloadPortalBlobs(aw, info.devPortalBlobStorageURL)
}
func downloadPortalBlobs(aw *devportal.ArchiveWriter, blobURLString string) error {
logging.Logger().Infof("Downloading media...")
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("Found blob: %s", blobInfo.Name)
blobURL := containerURL.NewBlobURL(blobInfo.Name)
if err := aw.AddBlob(blobURL); err != nil {
logging.Logger().WithError(err).Errorf("Writing BLOB %s", blobInfo.Name)
cErr++
} else {
cOK++
}
}
}
logging.Logger().Infof(" -> Total %d blobs, %d errors", cOK, cErr)
return nil
}
func getPortalContentItems(aw *devportal.ArchiveWriter, cli *apimClient, mgmtURL string) error {
logging.Logger().Infof("Processing 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 = make([]interface{}, 0, 200)
for _, ct := range contentTypes {
subItems, err := getContentItems(cli, mgmtURL, ct)
if err != nil {
return err
}
contentItems = append(contentItems, subItems...)
logging.Logger().Infof(" -> %d %s items", len(subItems), ct)
}
// Write data.json
data, err := json.Marshal(contentItems)
if err != nil {
return err
}
if err := aw.AddContentItems(data); err != nil {
return err
}
logging.Logger().Infof(" -> Total %d items", len(contentItems))
return nil
}
// Get a list of supported content types from the portal
func getContentTypes(cli *apimClient, mgmtURL string) ([]string, error) {
reqURL := fmt.Sprintf("%s/contentTypes", apimMgmtURL(mgmtURL))
resp, err := cli.Get(reqURL)
if err != nil {
return nil, err
}
// Only accept HTTP 2xx codes
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("status %s received", resp.Status)
}
// Grab the body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ctResp := apimPortalContentTypesResponse{}
if err := json.Unmarshal(respBody, &ctResp); err != nil {
return nil, err
}
// Extract the IDs minus the /contentTypes/ prefix
types := make([]string, 0, 10)
for _, ct := range ctResp.Value {
s := strings.TrimPrefix(ct.ID, "/contentTypes/")
types = append(types, s)
}
logging.Logger().Debugf("Content types: %s", types)
return types, nil
}
// Get a list of content items for a given content type
func getContentItems(cli *apimClient, mgmtURL string, contentType string) ([]interface{}, error) {
reqURL := fmt.Sprintf("%s/contentTypes/%s/contentItems", apimMgmtURL(mgmtURL), contentType)
resp, err := cli.Get(reqURL)
if err != nil {
return nil, err
}
// Only accept HTTP 2xx codes
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("status %s received", resp.Status)
}
// Grab the body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ciResp := apimPortalContentItemsResponse{}
if err := json.Unmarshal(respBody, &ciResp); err != nil {
return nil, err
}
logging.Logger().Debugf("%d %s items found", len(ciResp.Value), contentType)
return ciResp.Value, nil
}