awspub 0.0.4__py3-none-any.whl → 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
awspub/configmodels.py CHANGED
@@ -101,7 +101,9 @@ class ConfigImageModel(BaseModel):
101
101
 
102
102
  description: Optional[str] = Field(description="Optional image description", default=None)
103
103
  regions: Optional[List[str]] = Field(
104
- description="Optional list of regions for this image. If not given, all available regions will be used",
104
+ description="Optional list of regions for this image. If not given, all available regions will"
105
+ "be used from the currently used partition. If a region doesn't exist in the currently used partition,"
106
+ " it will be ignored.",
105
107
  default=None,
106
108
  )
107
109
  separate_snapshot: bool = Field(description="Use a separate snapshot for this image?", default=False)
awspub/image.py CHANGED
@@ -54,6 +54,7 @@ class Image:
54
54
  self._ctx: Context = context
55
55
  self._image_name: str = image_name
56
56
  self._image_regions: List[str] = []
57
+ self._image_regions_cached: bool = False
57
58
 
58
59
  if self._image_name not in self._ctx.conf["images"].keys():
59
60
  raise ValueError(f"image '{self._image_name}' not found in context configuration")
@@ -119,15 +120,28 @@ class Image:
119
120
  def image_regions(self) -> List[str]:
120
121
  """
121
122
  Get the image regions. Either configured in the image configuration
122
- or all available regions
123
+ or all available regions.
124
+ If a region is listed that is not available in the currently used partition,
125
+ that region will be ignored (eg. having us-east-1 configured but running in the aws-cn
126
+ partition doesn't include us-east-1 here).
123
127
  """
124
- if not self._image_regions:
128
+ if not self._image_regions_cached:
129
+ # get all available regions
130
+ ec2client: EC2Client = boto3.client("ec2", region_name=self._s3.bucket_region)
131
+ resp = ec2client.describe_regions()
132
+ image_regions_all = [r["RegionName"] for r in resp["Regions"]]
133
+
125
134
  if self.conf["regions"]:
126
- self._image_regions = self.conf["regions"]
135
+ # filter out regions that are not available in the current partition
136
+ image_regions_configured_set = set(self.conf["regions"])
137
+ image_regions_all_set = set(image_regions_all)
138
+ self._image_regions = list(image_regions_configured_set.intersection(image_regions_all_set))
139
+ diff = image_regions_configured_set.difference(image_regions_all_set)
140
+ if diff:
141
+ logger.warning(f"configured regions {diff} not available in the current partition. Ignoring those.")
127
142
  else:
128
- ec2client: EC2Client = boto3.client("ec2", region_name=self._s3.bucket_region)
129
- resp = ec2client.describe_regions()
130
- self._image_regions = [r["RegionName"] for r in resp["Regions"]]
143
+ self._image_regions = image_regions_all
144
+ self._image_regions_cached = True
131
145
  return self._image_regions
132
146
 
133
147
  @property
@@ -40,27 +40,34 @@ def test_snapshot_names(imagename, snapshotname):
40
40
 
41
41
 
42
42
  @pytest.mark.parametrize(
43
- "imagename,regions",
43
+ "imagename,regions_in_partition,regions_expected",
44
44
  [
45
45
  # test-image-1 has 2 regions defined
46
- ("test-image-1", ["region1", "region2"]),
46
+ ("test-image-1", ["region1", "region2"], ["region1", "region2"]),
47
+ # test-image-1 has 2 regions defined and there are more regions in the partition
48
+ ("test-image-1", ["region1", "region2", "region3"], ["region1", "region2"]),
49
+ ("test-image-1", ["region1", "region2"], ["region1", "region2"]),
47
50
  # test-image-2 has no regions defined, so whatever the ec2 client returns should be valid
48
- ("test-image-2", ["all-region-1", "all-region-2"]),
51
+ ("test-image-2", ["all-region-1", "all-region-2"], ["all-region-1", "all-region-2"]),
52
+ # test-image-1 has 2 regions defined, but those regions are not in the partition
53
+ ("test-image-1", ["region3", "region4"], []),
54
+ # test-image-1 has 2 regions defined, but those regions are not partially in the partition
55
+ ("test-image-1", ["region2", "region4"], ["region2"]),
56
+ # test-image-2 has no regions defined and the ec2 client doesn't return any regions
57
+ ("test-image-2", [], []),
49
58
  ],
50
59
  )
51
60
  @patch("awspub.s3.S3.bucket_region", return_value="region1")
52
- def test_image_regions(s3_region_mock, imagename, regions):
61
+ def test_image_regions(s3_region_mock, imagename, regions_in_partition, regions_expected):
53
62
  """
54
63
  Test the regions for a given image
55
64
  """
56
65
  with patch("boto3.client") as bclient_mock:
57
66
  instance = bclient_mock.return_value
58
- instance.describe_regions.return_value = {
59
- "Regions": [{"RegionName": "all-region-1"}, {"RegionName": "all-region-2"}]
60
- }
67
+ instance.describe_regions.return_value = {"Regions": [{"RegionName": r} for r in regions_in_partition]}
61
68
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
62
69
  img = image.Image(ctx, imagename)
63
- assert img.image_regions == regions
70
+ assert sorted(img.image_regions) == sorted(regions_expected)
64
71
 
65
72
 
66
73
  @pytest.mark.parametrize(
@@ -77,6 +84,8 @@ def test_image_cleanup(imagename, cleanup):
77
84
  with patch("boto3.client") as bclient_mock:
78
85
  instance = bclient_mock.return_value
79
86
  instance.describe_images.return_value = {"Images": [{"Name": imagename, "Public": False, "ImageId": "ami-123"}]}
87
+ instance.describe_regions.return_value = {"Regions": [{"RegionName": "region1"}, {"RegionName": "region2"}]}
88
+ instance.list_buckets.return_value = {"Buckets": [{"Name": "bucket1"}]}
80
89
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
81
90
  img = image.Image(ctx, imagename)
82
91
  img.cleanup()
@@ -154,6 +163,10 @@ def test_image_publish(
154
163
  ]
155
164
  }
156
165
  instance.get_parameters.return_value = {"Parameters": []}
166
+ instance.describe_regions.return_value = {
167
+ "Regions": [{"RegionName": "eu-central-1"}, {"RegionName": "us-east-1"}]
168
+ }
169
+ instance.list_buckets.return_value = {"Buckets": [{"Name": "bucket1"}]}
157
170
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
158
171
  img = image.Image(ctx, imagename)
159
172
  img.publish()
@@ -299,6 +312,10 @@ def test_image_list(available_images, expected):
299
312
  with patch("boto3.client") as bclient_mock:
300
313
  instance = bclient_mock.return_value
301
314
  instance.describe_images.return_value = {"Images": available_images}
315
+ instance.describe_regions.return_value = {
316
+ "Regions": [{"RegionName": "eu-central-1"}, {"RegionName": "us-east-1"}]
317
+ }
318
+ instance.list_buckets.return_value = {"Buckets": [{"Name": "bucket1"}]}
302
319
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
303
320
  img = image.Image(ctx, "test-image-6")
304
321
  assert img.list() == expected
@@ -330,6 +347,10 @@ def test_image_create_existing(s3_bucket_mock):
330
347
  }
331
348
  ]
332
349
  }
350
+ instance.describe_regions.return_value = {
351
+ "Regions": [{"RegionName": "eu-central-1"}, {"RegionName": "us-east-1"}]
352
+ }
353
+ instance.list_buckets.return_value = {"Buckets": [{"Name": "bucket1"}]}
333
354
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
334
355
  img = image.Image(ctx, "test-image-6")
335
356
  assert img.create() == {"eu-central-1": image._ImageInfo(image_id="ami-123", snapshot_id="snap-123")}
@@ -375,6 +396,10 @@ def test_image__put_ssm_parameters(
375
396
  instance = bclient_mock.return_value
376
397
  instance.describe_images.return_value = {"Images": describe_images}
377
398
  instance.get_parameters.return_value = {"Parameters": get_parameters}
399
+ instance.describe_regions.return_value = {
400
+ "Regions": [{"RegionName": "eu-central-1"}, {"RegionName": "us-east-1"}]
401
+ }
402
+ instance.list_buckets.return_value = {"Buckets": [{"Name": "bucket1"}]}
378
403
  ctx = context.Context(curdir / "fixtures/config1.yaml", None)
379
404
  img = image.Image(ctx, imagename)
380
405
  img._put_ssm_parameters()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: awspub
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Summary: Publish images to AWS EC2
5
5
  Home-page: https://github.com/canonical/awspub
6
6
  License: GPL-3.0-or-later
@@ -1,10 +1,10 @@
1
1
  awspub/__init__.py,sha256=7hgLrq6k53yaJrjFe7X5Cm45z3SIc1Vxocb5k3G8xPc,124
2
2
  awspub/api.py,sha256=d1gx9LdqdYXRLf8yZ_spIz_93WhB2GNnCG_x3ABrMkI,6497
3
3
  awspub/cli/__init__.py,sha256=-zCBEbnt5zbvSZ8PxQALpPAy0CiQUf-qZnikJ7U4Sf0,5621
4
- awspub/configmodels.py,sha256=8JV_fh9VTCSTRcwoqdxXoNFUurn5Q1iTfp-r3u-OLUQ,6869
4
+ awspub/configmodels.py,sha256=ZHk9sfe3yAg8kBK6FV7Y0rmke9YKlY4t_D-Q4k3XRMM,7004
5
5
  awspub/context.py,sha256=LDkp9Sz5AqRxQq70ICgFIJn5g2qrc5qiVawTyS_rXZE,4064
6
6
  awspub/exceptions.py,sha256=SbGf9XyiGlj6estlraAwWAKLtuEfzwEuAbHXYiCiJD0,447
7
- awspub/image.py,sha256=FRw8CyYo0RBh-ik4I9W4TmaF0LJ2TpgeddwalidlQKA,25763
7
+ awspub/image.py,sha256=YOa4zV9EHGDnYng06gnQNx4TR5haVTGDUNzp09JbcO8,26683
8
8
  awspub/image_marketplace.py,sha256=oiD7yNU5quG5CQG9Ql5Ut9hLWA1yewg6qVwTbyadGwc,5314
9
9
  awspub/s3.py,sha256=vnel5UtASHK_mkGuZDnA1IydWCLD_TCZ_WyWwAds9F8,11271
10
10
  awspub/snapshot.py,sha256=V5e_07SnmCwEPjRmwZh43spWparhH8X4ugG16uQfGuo,10040
@@ -20,12 +20,12 @@ awspub/tests/fixtures/config3-duplicate-keys.yaml,sha256=Cn0tTQawpEFocDNpWxDz165
20
20
  awspub/tests/test_api.py,sha256=7MKm2aCtcvHJ0x_o2qinljfL9xFBWnasUnVpBxB37w8,2504
21
21
  awspub/tests/test_cli.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  awspub/tests/test_context.py,sha256=wMXQqj4vi2U3q5w1xPV-stB3mp3K6puUyXhsShJG4wA,3115
23
- awspub/tests/test_image.py,sha256=4z7UmcUhFBXCFX-iGz4FbHp7mgm_JNTTJsHSONMbwi0,16400
23
+ awspub/tests/test_image.py,sha256=uFY_kfV-ZioqqP2vBTgT3fGy-So50iJc__2ewbt_6t8,18287
24
24
  awspub/tests/test_image_marketplace.py,sha256=JP7PrFjix1AyQg7eEaQ-wCROVoIOb873koseniOqGQQ,1456
25
25
  awspub/tests/test_s3.py,sha256=UJL8CQDEvhA42MwPGeSvSbQFj8h86c1LrLFDvcMcRws,2857
26
26
  awspub/tests/test_snapshot.py,sha256=8KPTqGVyzrpivWuq3HE7ZhgtLllcr3rA_3hZcxu2xjg,4123
27
- awspub-0.0.4.dist-info/LICENSE,sha256=9GbrzFQ3rWjVKj-IZnX1kGDsIGIdjc25KGRmAp03Jn0,35150
28
- awspub-0.0.4.dist-info/METADATA,sha256=FnuoxZOfvW7AtbWTySouXFSiQ_FkcMbvd8mtKkczj-s,1773
29
- awspub-0.0.4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
30
- awspub-0.0.4.dist-info/entry_points.txt,sha256=hrQzy9P5yO58nj6W0UDPdQPUTqEkQLpMvuyDDRu7LRQ,42
31
- awspub-0.0.4.dist-info/RECORD,,
27
+ awspub-0.0.5.dist-info/LICENSE,sha256=9GbrzFQ3rWjVKj-IZnX1kGDsIGIdjc25KGRmAp03Jn0,35150
28
+ awspub-0.0.5.dist-info/METADATA,sha256=Z8H6_DNIlhHGDQjUBCM9SFd4SZNNQrn0ALooc94SjcE,1773
29
+ awspub-0.0.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
30
+ awspub-0.0.5.dist-info/entry_points.txt,sha256=hrQzy9P5yO58nj6W0UDPdQPUTqEkQLpMvuyDDRu7LRQ,42
31
+ awspub-0.0.5.dist-info/RECORD,,
File without changes