Browse code

* S3/CloudFront.py: Allow s3:// URI as well as cf:// URI for most CloudFront-related commands.

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/trunk@416 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2010/06/12 22:11:01
Showing 3 changed files
... ...
@@ -1,3 +1,8 @@
1
+2010-06-13  Michal Ludvig  <mludvig@logix.net.nz>
2
+
3
+	* S3/CloudFront.py: Allow s3:// URI as well as cf:// URI 
4
+	  for most CloudFront-related commands.
5
+
1 6
 2010-06-12  Michal Ludvig  <mludvig@logix.net.nz>
2 7
 
3 8
 	* s3cmd, S3/CloudFront.py, S3/Config.py: Support access 
... ...
@@ -4,6 +4,8 @@ s3cmd 0.9.9.92 -  ???
4 4
 * Added access logging for CloudFront distributions 
5 5
   using [cfmodify --log]
6 6
 * Added --acl-grant and --acl-revoke (by Timothee Linden).
7
+* Allow s3:// URI as well as cf:// URI as a distribution
8
+  name for most CloudFront related commands.
7 9
 
8 10
 s3cmd 0.9.9.91 -  2009-10-08
9 11
 ==============
... ...
@@ -395,6 +395,36 @@ class Cmd(object):
395 395
 			setattr(Cmd.options, option, value)
396 396
 
397 397
 	options = Options()
398
+	dist_list = None
399
+
400
+	@staticmethod
401
+	def _get_dist_name_for_bucket(uri):
402
+		cf = CloudFront(Config())
403
+		debug("_get_dist_name_for_bucket(%r)" % uri)
404
+		assert(uri.type == "s3")
405
+		if Cmd.dist_list is None:
406
+			response = cf.GetList()
407
+			Cmd.dist_list = {}
408
+			for d in response['dist_list'].dist_summs:
409
+				Cmd.dist_list[getBucketFromHostname(d.info['Origin'])[0]] = d.uri()
410
+			debug("dist_list: %s" % Cmd.dist_list)
411
+		return Cmd.dist_list[uri.bucket()]
412
+
413
+	@staticmethod
414
+	def _parse_args(args):
415
+		cfuris = []
416
+		for arg in args:
417
+			uri = S3Uri(arg)
418
+			if uri.type == 's3':
419
+				try:
420
+					uri = Cmd._get_dist_name_for_bucket(uri)
421
+				except Exception, e:
422
+					debug(e)
423
+					raise ParameterError("Unable to translate S3 URI to CloudFront distribution name: %s" % uri)
424
+			if uri.type != 'cf':
425
+				raise ParameterError("CloudFront URI required instead of: %s" % arg)
426
+			cfuris.append(uri)
427
+		return cfuris
398 428
 
399 429
 	@staticmethod
400 430
 	def info(args):
... ...
@@ -409,11 +439,7 @@ class Cmd(object):
409 409
 				pretty_output("Enabled", d.info['Enabled'])
410 410
 				output("")
411 411
 		else:
412
-			cfuris = []
413
-			for arg in args:
414
-				cfuris.append(S3Uri(arg))
415
-				if cfuris[-1].type != 'cf':
416
-					raise ParameterError("CloudFront URI required instead of: %s" % arg)
412
+			cfuris = Cmd._parse_args(args)
417 413
 			for cfuri in cfuris:
418 414
 				response = cf.GetDistInfo(cfuri)
419 415
 				d = response['distribution']
... ...
@@ -463,11 +489,7 @@ class Cmd(object):
463 463
 	@staticmethod
464 464
 	def delete(args):
465 465
 		cf = CloudFront(Config())
466
-		cfuris = []
467
-		for arg in args:
468
-			cfuris.append(S3Uri(arg))
469
-			if cfuris[-1].type != 'cf':
470
-				raise ParameterError("CloudFront URI required instead of: %s" % arg)
466
+		cfuris = Cmd._parse_args(args)
471 467
 		for cfuri in cfuris:
472 468
 			response = cf.DeleteDistribution(cfuri)
473 469
 			if response['status'] >= 400:
... ...
@@ -477,12 +499,12 @@ class Cmd(object):
477 477
 	@staticmethod
478 478
 	def modify(args):
479 479
 		cf = CloudFront(Config())
480
-		cfuri = S3Uri(args.pop(0))
481
-		if cfuri.type != 'cf':
482
-			raise ParameterError("CloudFront URI required instead of: %s" % arg)
483
-		if len(args):
480
+		if len(args) > 1:
484 481
 			raise ParameterError("Too many parameters. Modify one Distribution at a time.")
485
-
482
+		try:
483
+			cfuri = Cmd._parse_args(args)[0]
484
+		except IndexError, e:
485
+			raise ParameterError("No valid Distribution URI found.")
486 486
 		response = cf.ModifyDistribution(cfuri,
487 487
 		                                 cnames_add = Cmd.options.cf_cnames_add,
488 488
 		                                 cnames_remove = Cmd.options.cf_cnames_remove,