# Copyright 2009-2013 Eucalyptus Systems, Inc.
#
# Redistribution and use of this software in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from requestbuilder import Arg

from euca2ools.commands.bundle.mixins import BundleDownloadingMixin
from euca2ools.commands.s3 import S3Request
from euca2ools.commands.s3.deletebucket import DeleteBucket
from euca2ools.commands.s3.deleteobject import DeleteObject


class DeleteBundle(S3Request, BundleDownloadingMixin):
    DESCRIPTION = 'Delete a previously-uploaded bundle'
    ARGS = [Arg('--clear', dest='clear', action='store_true',
                help='attempt to delete the bucket as well')]

    def main(self):
        manifest = self.fetch_manifest(self.service)
        for part, part_s3path in self.map_bundle_parts_to_s3paths(manifest):
            req = DeleteObject(config=self.config, service=self.service,
                               path=part_s3path)
            req.main()
            print '>>>', part, part_s3path
        manifest_s3path = self.get_manifest_s3path()
        if manifest_s3path:
            req = DeleteObject(config=self.config, service=self.service,
                               path=manifest_s3path)
            req.main()
            print '>>> manifest', manifest_s3path

        if self.args.get('clear'):
            req = DeleteBucket(service=self.service, config=self.config,
                               bucket=self.args['bucket'].split('/')[0])
            req.main()
            print '>>> bucket', self.args['bucket'].split('/')[0]

@@ -1370,16 +1370,66 @@
 port Arg
+%0Afrom requestbuilder.exceptions import ServerError
 %0A%0Afrom e
@@ -1912,16 +1912,33 @@
 (self):%0A
+        try:%0A    
         
@@ -1994,149 +1994,329 @@
     
-for part, part_s3path in self.map_bundle_parts_to_s3paths(manifest):%0A            req = DeleteObject(config=self.config, service=self.service,
+except ServerError as err:%0A            if err.status_code == 404 and self.args.get('clear'):%0A                try:%0A                    # We are supposed to try to delete the bucket even%0A                    # if the manifest isn't there.  If it works, the%0A                    # bundle is also gone and we can safely return.
 %0A   
@@ -2319,32 +2319,39 @@
 %0A               
+     #%0A
                 
@@ -2354,155 +2354,460 @@
     
-path=part_s3path)%0A            req.main()%0A            print '%3E%3E%3E', part, part_s3path%0A        manifest_s3path = self.get_manifest_s3path()%0A        if
+    # https://eucalyptus.atlassian.net/browse/TOOLS-379%0A                    self.__delete_bucket()%0A                    return%0A                except ServerError:%0A                    # If the bucket wasn't empty then we'll go back to%0A                    # complaining about the missing manifest.%0A                    self.log.error(%0A                        'failed to delete bucket %25s after a failed '%0A                        'attempt to fetch the bundle
  man
@@ -2815,16 +2815,10 @@
 fest
-_s3path:
+',
 %0A   
@@ -2830,194 +2830,456 @@
     
-req = DeleteObject(config=self.config, service=self.service,%0A                               path=manifest_s3path)%0A            req.main()%0A            print '%3E%3E%3E manifest', manifest_s3path
+            bucket=self.args%5B'bucket'%5D.split('/')%5B0%5D,%0A                        exc_info=True)%0A            raise%0A%0A        for _, part_s3path in self.map_bundle_parts_to_s3paths(manifest):%0A            req = DeleteObject.from_other(self, path=part_s3path)%0A            req.main()%0A        manifest_s3path = self.get_manifest_s3path()%0A        if manifest_s3path:%0A            req = DeleteObject.from_other(self, path=manifest_s3path)%0A            req.main()
 %0A%0A  
@@ -3327,29 +3327,58 @@
     
-req = D
+self.__delete_bucket()%0A%0A    def __d
 elete
-B
+_b
 ucket(se
 rvic
@@ -3377,48 +3377,64 @@
 t(se
-rvice=self.service, config=self.config,%0A
+lf):%0A        req = DeleteBucket.from_other(self,%0A       
     
@@ -3502,36 +3502,32 @@
 /')%5B0%5D)%0A        
-    
 req.main()%0A     
@@ -3525,70 +3525,4 @@
 n()%0A
-            print '%3E%3E%3E bucket', self.args%5B'bucket'%5D.split('/')%5B0%5D%0A

delete the bucket even with no manifest