@@ -10,12 +10,12 @@ const localize = nls.loadMessageBundle()
10
10
11
11
import * as vscode from 'vscode'
12
12
import { getLogger } from '../logger'
13
- import { Endpoints , Region } from './endpoints'
14
- import { EndpointsProvider } from './endpointsProvider'
13
+ import { Endpoints , loadEndpoints , Region } from './endpoints'
15
14
import { AWSTreeNodeBase } from '../treeview/nodes/awsTreeNodeBase'
16
15
import { regionSettingKey } from '../constants'
17
16
import { AwsContext } from '../awsContext'
18
17
import { getIdeProperties , isCloud9 } from '../extensionUtilities'
18
+ import { ResourceFetcher } from '../resourcefetcher/resourcefetcher'
19
19
20
20
export const defaultRegion = 'us-east-1'
21
21
export const defaultPartition = 'aws'
@@ -120,6 +120,7 @@ export class RegionProvider {
120
120
}
121
121
122
122
private loadFromEndpoints ( endpoints : Endpoints ) {
123
+ this . regionData . clear ( )
123
124
endpoints . partitions . forEach ( partition => {
124
125
partition . regions . forEach ( region =>
125
126
this . regionData . set ( region . id , {
@@ -140,40 +141,71 @@ export class RegionProvider {
140
141
} )
141
142
} )
142
143
} )
144
+ this . onDidChangeEmitter . fire ( )
143
145
}
144
146
145
- public static fromEndpointsProvider ( endpointsProvider : EndpointsProvider ) : RegionProvider {
147
+ /**
148
+ * @param endpointsProvider.local Retrieves endpoints manifest from local sources available to the toolkit. Expected
149
+ * to resolve fast, and is both a placeholder until the remote resources are loaded, and
150
+ * is a fallback in case the toolkit is unable to load a remote resource
151
+ * @param endpointsProvider.remote Retrieves endpoints manifest from remote host
152
+ */
153
+ public static fromEndpointsProvider ( endpointsProvider : {
154
+ local : ( ) => Endpoints | Promise < Endpoints >
155
+ remote : ( ) => Endpoints | Promise < Endpoints >
156
+ } ) : RegionProvider {
146
157
const instance = new this ( )
147
158
148
- endpointsProvider
149
- . load ( )
150
- . then ( endpoints => {
151
- instance . regionData . clear ( )
152
- instance . loadFromEndpoints ( endpoints )
153
- instance . onDidChangeEmitter . fire ( )
154
- } )
155
- . catch ( err => {
156
- getLogger ( ) . error ( 'Failure while loading Endpoints Manifest: %s' , err )
157
-
158
- vscode . window . showErrorMessage (
159
- `${ localize (
160
- 'AWS.error.endpoint.load.failure' ,
161
- 'The {0} Toolkit was unable to load endpoints data.' ,
162
- getIdeProperties ( ) . company
163
- ) } ${
164
- isCloud9 ( )
165
- ? localize (
166
- 'AWS.error.impactedFunctionalityReset.cloud9' ,
167
- 'Toolkit functionality may be impacted until the Cloud9 browser tab is refreshed.'
168
- )
169
- : localize (
170
- 'AWS.error.impactedFunctionalityReset.vscode' ,
171
- 'Toolkit functionality may be impacted until VS Code is restarted.'
172
- )
173
- } `
159
+ async function load ( ) {
160
+ getLogger ( ) . info ( 'endpoints: retrieving AWS endpoints data' )
161
+ instance . loadFromEndpoints ( await endpointsProvider . local ( ) )
162
+
163
+ try {
164
+ instance . loadFromEndpoints ( await endpointsProvider . remote ( ) )
165
+ } catch ( err ) {
166
+ getLogger ( ) . warn (
167
+ `endpoints: failed to load from remote source, region data may appear outdated: %s` ,
168
+ err
174
169
)
175
- } )
170
+ }
171
+ }
172
+
173
+ load ( ) . catch ( err => {
174
+ getLogger ( ) . error ( 'Failure while loading Endpoints Manifest: %s' , err )
175
+
176
+ vscode . window . showErrorMessage (
177
+ `${ localize (
178
+ 'AWS.error.endpoint.load.failure' ,
179
+ 'The {0} Toolkit was unable to load endpoints data.' ,
180
+ getIdeProperties ( ) . company
181
+ ) } ${
182
+ isCloud9 ( )
183
+ ? localize (
184
+ 'AWS.error.impactedFunctionalityReset.cloud9' ,
185
+ 'Toolkit functionality may be impacted until the Cloud9 browser tab is refreshed.'
186
+ )
187
+ : localize (
188
+ 'AWS.error.impactedFunctionalityReset.vscode' ,
189
+ 'Toolkit functionality may be impacted until VS Code is restarted.'
190
+ )
191
+ } `
192
+ )
193
+ } )
176
194
177
195
return instance
178
196
}
179
197
}
198
+
199
+ export async function getEndpointsFromFetcher ( fetcher : ResourceFetcher ) : Promise < Endpoints > {
200
+ const endpointsJson = await fetcher . get ( )
201
+ if ( ! endpointsJson ) {
202
+ throw new Error ( 'Failed to get resource' )
203
+ }
204
+
205
+ const endpoints = loadEndpoints ( endpointsJson )
206
+ if ( ! endpoints ) {
207
+ throw new Error ( 'Failed to load endpoints' )
208
+ }
209
+
210
+ return endpoints
211
+ }
0 commit comments