Skip to content

Commit

Permalink
Remove regions that aren't in the standard AWS partition (until they …
Browse files Browse the repository at this point in the history
…can be supported) (aws#666)
  • Loading branch information
awschristou authored Jul 8, 2019
1 parent 7e6ae31 commit f0e2178
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to the "aws-vscode-tools" extension will be documented in th
* Added ability to report an issue from the AWS Explorer menu (#613)
* Added SAM Application-related commands to the AWS Explorer menu
* Removed support for nodejs6.10 SAM Applications
* Regions that are not in the standard AWS partition have been removed from the UI until proper partition support can be added

## 0.2.1 (Developer Preview)

Expand Down
40 changes: 26 additions & 14 deletions src/shared/regions/defaultRegionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import { FileResourceLocation, WebResourceLocation } from '../resourceLocation'
import { RegionInfo } from './regionInfo'
import { RegionProvider } from './regionProvider'

interface RawRegion {
export interface RawRegion {
description: string
}

interface RawPartition {
export interface RawPartition {
partition: string
regions: {
[ regionKey: string ]: RawRegion
[regionKey: string]: RawRegion
}
}

interface RawEndpoints {
export interface RawEndpoints {
partitions: RawPartition[]
}

Expand Down Expand Up @@ -59,16 +60,7 @@ export class DefaultRegionProvider implements RegionProvider {
])
const allEndpoints = JSON.parse(endpointsSource) as RawEndpoints

availableRegions = allEndpoints.partitions.reduce(
(accumulator: RegionInfo[], partition: RawPartition) => {
accumulator.push(...Object.keys(partition.regions).map(
regionKey => new RegionInfo(regionKey, `${partition.regions[regionKey].description}`)
))

return accumulator
},
[]
)
availableRegions = getRegionsFromEndpoints(allEndpoints)

this._areRegionsLoaded = true
this._loadedRegions = availableRegions
Expand All @@ -83,3 +75,23 @@ export class DefaultRegionProvider implements RegionProvider {
return availableRegions
}
}

export function getRegionsFromPartition(partition: RawPartition): RegionInfo[] {
return Object.keys(partition.regions).map(
regionKey => new RegionInfo(regionKey, `${partition.regions[regionKey].description}`)
)
}

export function getRegionsFromEndpoints(endpoints: RawEndpoints): RegionInfo[] {
return endpoints.partitions
// TODO : Support other Partition regions : https://github.com/aws/aws-toolkit-vscode/issues/188
.filter(partition => partition.partition && partition.partition === 'aws')
.reduce(
(accumulator: RegionInfo[], partition: RawPartition) => {
accumulator.push(...getRegionsFromPartition(partition))

return accumulator
},
[]
)
}
89 changes: 89 additions & 0 deletions src/test/shared/regions/defaultRegionProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*!
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

import * as assert from 'assert'
import {
getRegionsFromEndpoints,
getRegionsFromPartition,
RawEndpoints,
RawPartition
} from '../../../shared/regions/defaultRegionProvider'
import { RegionInfo } from '../../../shared/regions/regionInfo'

const sampleEndpoints: RawEndpoints = {
partitions: [
{
partition: 'aws',
regions: {
region1: {
description: 'aws region one'
},
region2: {
description: 'aws region two'
},
region3: {
description: 'aws region three'
},
}
},
{
partition: 'aws-cn',
regions: {
awscnregion1: {
description: 'aws-cn region one'
},
}
},
{
partition: 'fake',
regions: {
fakeregion1: {
description: 'fake region one'
},
}
}
]
}

describe('getRegionsFromPartition', async () => {
it('pulls region data from partition', async () => {
const partition = sampleEndpoints.partitions.filter(p => p.partition === 'aws')[0]
const regions = getRegionsFromPartition(partition)

assert.ok(regions, 'Expected to get regions')
assert.strictEqual(regions.length, 3, 'Expected 3 regions')
assertPartitionRegionsExist(partition, regions)
})
})

describe('getRegionsFromEndpoints', async () => {
it('returns expected regions', async () => {
// TODO : Support other Partition regions : https://github.com/aws/aws-toolkit-vscode/issues/188
const partition = sampleEndpoints.partitions.filter(p => p.partition === 'aws')[0]
const regions = getRegionsFromEndpoints(sampleEndpoints)

assert.ok(regions, 'Expected to get regions')
assert.strictEqual(regions.length, 3, 'Expected 3 regions')
assertPartitionRegionsExist(partition, regions)
})
})

/**
* Assert that all regions in expectedPartition exist in actualRegions
*/
function assertPartitionRegionsExist(expectedPartition: RawPartition, actualRegions: RegionInfo[]) {
Object.keys(expectedPartition.regions).forEach(regionCode => {
const expectedRegion = expectedPartition.regions[regionCode]
const candidateRegions = actualRegions.filter(region => region.regionCode === regionCode)
assert.strictEqual(candidateRegions.length, 1, `Region not found for ${regionCode}`)
assert.strictEqual(
candidateRegions[0].regionName,
expectedRegion.description,
`Unexpected Region name for ${regionCode}`
)
})
}

0 comments on commit f0e2178

Please sign in to comment.