# 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/util.py
# def get_file_as_string(fn):
#     """
#         Given a filename, returns the file's contents in a string.
#     """
#     r = ''
#     with open(fn) as fh:
#         r = fh.read()
#         fh.close()
#     return r


import logging
from handson.myyaml import stanza
from handson.region import Region
from handson.util import get_file_as_string
# 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 HOLDER 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.
#


log = logging.getLogger(__name__)


class Keypair(Region):

    def __init__(self, args, delegate):
        super(Keypair, self).__init__(args)
        self.args = args
        self._keypair = {
            'delegate': delegate,
            'keypair_obj': None,
            'keyname': None,
        }

    def get_keyname_from_yaml(self):
        if self._keypair['keyname']:
            return self._keypair['keyname']

# 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/util.py
# def get_file_as_string(fn):
#     """
#         Given a filename, returns the file's contents in a string.
#     """
#     r = ''
#     with open(fn) as fh:
#         r = fh.read()
#         fh.close()
#     return r


# Path: handson/keypair.py
import logging
from handson.myyaml import stanza
from handson.region import Region
from handson.util import get_file_as_string

# 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 HOLDER 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.
#


log = logging.getLogger(__name__)


class Keypair(Region):

    def __init__(self, args, delegate):
        super(Keypair, self).__init__(args)
        self.args = args
        self._keypair = {
            'delegate': delegate,
            'keypair_obj': None,
            'keyname': None,
        }

    def get_keyname_from_yaml(self):
        if self._keypair['keyname']:
            return self._keypair['keyname']

        k_stanza = stanza('keypairs')