# Path: handson/myyaml.py
# def stanza(k, new_val=None):
#     global _cache
#     load()
#     if new_val is not None:
#         assert k in tree_stanzas.keys(), (
#             "YAML stanza {!r} not permitted".format(k)
#         )
#         _cache[k] = new_val
#         write()
#     stanza_is_sane(k)
#     return _cache[k]
# 
# Path: handson/region.py
# class Region(object):
# 
#     def __init__(self, args):
#         self.args = args
#         self._region = {
#             'ec2_conn': None,
#             'region_str': None,
#             'vpc_conn': None,
#             'az': None
#         }
# 
#     def region(self):
#         """
#             gets region from yaml, default to eu-west-1
#         """
#         if self._region['region_str']:
#             return self._region['region_str']
#         self._region['region_str'] = stanza('region')['region_str']
#         log.debug("Region is {}".format(self._region['region_str']))
#         return self._region['region_str']
# 
#     def availability_zone(self):
#         """
#             gets availability_zone from yaml, default to None
#         """
#         if self._region['az']:
#             return self._region['az']
#         self._region['az'] = stanza('region')['availability_zone']
#         if self._region['az']:
#             log.debug("Availability zone is {}"
#                       .format(self._region['az']))
#         return self._region['az']
# 
#     def ec2(self):
#         """
#             fetch ec2 connection, open if necessary
#         """
#         if self._region['ec2_conn']:
#             return self._region['ec2_conn']
#         region = self.region()
#         if self._region['ec2_conn'] is None:
#             log.debug("Connecting to EC2 region {}".format(region))
#             self._region['ec2_conn'] = boto.ec2.connect_to_region(
#                 region,
#                 is_secure=False
#             )
#         assert self._region['ec2_conn'] is not None, (
#                ("Failed to connect to EC2 service in region {!r}"
#                 .format(region)))
#         return self._region['ec2_conn']
# 
#     def vpc(self):
#         """
#             fetch vpc connection, open if necessary
#         """
#         if self._region['vpc_conn']:
#             return self._region['vpc_conn']
#         region = self.region()
#         if self._region['vpc_conn'] is None:
#             log.debug("Connecting to VPC region {}".format(region))
#             self._region['vpc_conn'] = boto.vpc.connect_to_region(
#                 region,
#                 is_secure=False
#             )
#         assert self._region['vpc_conn'] is not None, (
#                ("Failed to connect to VPC service in region {!r}"
#                 .format(region)))
#         return self._region['vpc_conn']
# 
# Path: handson/tag.py
# def apply_tag(obj, tag='Name', val=None):
#     """
#         tag an AWS object
#     """
#     for x in [1, 1, 2, 4, 8]:
#         error = False
#         try:
#             obj.add_tag(tag, val)
#         except:  # pragma: no cover
#             error = True
#             e = sys.exc_info()[0]
#             log.info("Huh, trying again ({})".format(e))
#             time.sleep(x)
#         if not error:
#             log.info("Object {} tagged with {}={}".format(obj, tag, val))
#             break
#     return None


import logging
from handson.myyaml import stanza
from handson.region import Region
from handson.tag import apply_tag
# 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 handson.util import read_user_data

log = logging.getLogger(__name__)


class VPC(Region):

    def __init__(self, args):
        super(VPC, self).__init__(args)
        self.args = args
        self._vpc = {
            'vpc_obj': None
        }

    def vpc_obj(self, create=False, dry_run=False, quiet=False):
        """
            fetch VPC object, create if necessary
        """
        #
        # cached VPC object
        if self._vpc['vpc_obj'] is not None:
            return self._vpc['vpc_obj']
        #
        # non-cached

# Path: handson/myyaml.py
# def stanza(k, new_val=None):
#     global _cache
#     load()
#     if new_val is not None:
#         assert k in tree_stanzas.keys(), (
#             "YAML stanza {!r} not permitted".format(k)
#         )
#         _cache[k] = new_val
#         write()
#     stanza_is_sane(k)
#     return _cache[k]
# 
# Path: handson/region.py
# class Region(object):
# 
#     def __init__(self, args):
#         self.args = args
#         self._region = {
#             'ec2_conn': None,
#             'region_str': None,
#             'vpc_conn': None,
#             'az': None
#         }
# 
#     def region(self):
#         """
#             gets region from yaml, default to eu-west-1
#         """
#         if self._region['region_str']:
#             return self._region['region_str']
#         self._region['region_str'] = stanza('region')['region_str']
#         log.debug("Region is {}".format(self._region['region_str']))
#         return self._region['region_str']
# 
#     def availability_zone(self):
#         """
#             gets availability_zone from yaml, default to None
#         """
#         if self._region['az']:
#             return self._region['az']
#         self._region['az'] = stanza('region')['availability_zone']
#         if self._region['az']:
#             log.debug("Availability zone is {}"
#                       .format(self._region['az']))
#         return self._region['az']
# 
#     def ec2(self):
#         """
#             fetch ec2 connection, open if necessary
#         """
#         if self._region['ec2_conn']:
#             return self._region['ec2_conn']
#         region = self.region()
#         if self._region['ec2_conn'] is None:
#             log.debug("Connecting to EC2 region {}".format(region))
#             self._region['ec2_conn'] = boto.ec2.connect_to_region(
#                 region,
#                 is_secure=False
#             )
#         assert self._region['ec2_conn'] is not None, (
#                ("Failed to connect to EC2 service in region {!r}"
#                 .format(region)))
#         return self._region['ec2_conn']
# 
#     def vpc(self):
#         """
#             fetch vpc connection, open if necessary
#         """
#         if self._region['vpc_conn']:
#             return self._region['vpc_conn']
#         region = self.region()
#         if self._region['vpc_conn'] is None:
#             log.debug("Connecting to VPC region {}".format(region))
#             self._region['vpc_conn'] = boto.vpc.connect_to_region(
#                 region,
#                 is_secure=False
#             )
#         assert self._region['vpc_conn'] is not None, (
#                ("Failed to connect to VPC service in region {!r}"
#                 .format(region)))
#         return self._region['vpc_conn']
# 
# Path: handson/tag.py
# def apply_tag(obj, tag='Name', val=None):
#     """
#         tag an AWS object
#     """
#     for x in [1, 1, 2, 4, 8]:
#         error = False
#         try:
#             obj.add_tag(tag, val)
#         except:  # pragma: no cover
#             error = True
#             e = sys.exc_info()[0]
#             log.info("Huh, trying again ({})".format(e))
#             time.sleep(x)
#         if not error:
#             log.info("Object {} tagged with {}={}".format(obj, tag, val))
#             break
#     return None


# Path: handson/vpc.py
import logging
from handson.myyaml import stanza
from handson.region import Region
from handson.tag import apply_tag

# 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 handson.util import read_user_data

log = logging.getLogger(__name__)


class VPC(Region):

    def __init__(self, args):
        super(VPC, self).__init__(args)
        self.args = args
        self._vpc = {
            'vpc_obj': None
        }

    def vpc_obj(self, create=False, dry_run=False, quiet=False):
        """
            fetch VPC object, create if necessary
        """
        #
        # cached VPC object
        if self._vpc['vpc_obj'] is not None:
            return self._vpc['vpc_obj']
        #
        # non-cached

        vpc_stanza = stanza('vpc')