Merge pull request #2921 from getglad/support_optin_regions

Support OptInStatus for EC2 describe_region calls
This commit is contained in:
Steve Pulec 2020-04-27 19:42:04 -05:00 committed by GitHub
commit f33e810e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 4 deletions

View File

@ -1503,9 +1503,10 @@ class AmiBackend(object):
class Region(object): class Region(object):
def __init__(self, name, endpoint): def __init__(self, name, endpoint, opt_in_status):
self.name = name self.name = name
self.endpoint = endpoint self.endpoint = endpoint
self.opt_in_status = opt_in_status
class Zone(object): class Zone(object):
@ -1516,13 +1517,49 @@ class Zone(object):
class RegionsAndZonesBackend(object): class RegionsAndZonesBackend(object):
regions_opt_in_not_required = [
"af-south-1",
"ap-northeast-1",
"ap-northeast-2",
"ap-northeast-3",
"ap-south-1",
"ap-southeast-1",
"ap-southeast-2",
"ca-central-1",
"eu-central-1",
"eu-north-1",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
]
regions = [] regions = []
for region in Session().get_available_regions("ec2"): for region in Session().get_available_regions("ec2"):
regions.append(Region(region, "ec2.{}.amazonaws.com".format(region))) if region in regions_opt_in_not_required:
regions.append(
Region(
region, "ec2.{}.amazonaws.com".format(region), "opt-in-not-required"
)
)
else:
regions.append(
Region(region, "ec2.{}.amazonaws.com".format(region), "not-opted-in")
)
for region in Session().get_available_regions("ec2", partition_name="aws-us-gov"): for region in Session().get_available_regions("ec2", partition_name="aws-us-gov"):
regions.append(Region(region, "ec2.{}.amazonaws.com".format(region))) regions.append(
Region(region, "ec2.{}.amazonaws.com".format(region), "opt-in-not-required")
)
for region in Session().get_available_regions("ec2", partition_name="aws-cn"): for region in Session().get_available_regions("ec2", partition_name="aws-cn"):
regions.append(Region(region, "ec2.{}.amazonaws.com.cn".format(region))) regions.append(
Region(
region, "ec2.{}.amazonaws.com.cn".format(region), "opt-in-not-required"
)
)
zones = { zones = {
"af-south-1": [ "af-south-1": [

View File

@ -22,6 +22,7 @@ DESCRIBE_REGIONS_RESPONSE = """<DescribeRegionsResponse xmlns="http://ec2.amazon
<item> <item>
<regionName>{{ region.name }}</regionName> <regionName>{{ region.name }}</regionName>
<regionEndpoint>{{ region.endpoint }}</regionEndpoint> <regionEndpoint>{{ region.endpoint }}</regionEndpoint>
<optInStatus>{{ region.opt_in_status }}</optInStatus>
</item> </item>
{% endfor %} {% endfor %}
</regionInfo> </regionInfo>

View File

@ -40,6 +40,15 @@ def test_boto3_describe_regions():
resp = ec2.describe_regions(RegionNames=[test_region]) resp = ec2.describe_regions(RegionNames=[test_region])
resp["Regions"].should.have.length_of(1) resp["Regions"].should.have.length_of(1)
resp["Regions"][0].should.have.key("RegionName").which.should.equal(test_region) resp["Regions"][0].should.have.key("RegionName").which.should.equal(test_region)
resp["Regions"][0].should.have.key("OptInStatus").which.should.equal(
"opt-in-not-required"
)
test_region = "ap-east-1"
resp = ec2.describe_regions(RegionNames=[test_region])
resp["Regions"].should.have.length_of(1)
resp["Regions"][0].should.have.key("RegionName").which.should.equal(test_region)
resp["Regions"][0].should.have.key("OptInStatus").which.should.equal("not-opted-in")
@mock_ec2 @mock_ec2