import boto3
import sys
from ruamel.yaml import YAML

supported_regions = ['us-west-2','us-east-1']

def create_client(service):
     client = boto3.client(service)
     return client

def any_constructor(loader, tag_suffix, node):
    if isinstance(node, yaml.MappingNode):
        return loader.construct_mapping(node)
    if isinstance(node, yaml.SequenceNode):
        return loader.construct_sequence(node)
    return loader.construct_scalar(node)

def get_wireguard_yaml(client):
     bucketname = 'wireguardcf'
     file_to_read = 'WireguardNetwork'
     response = client.get_object(
          Bucket=bucketname,
          Key=file_to_read
     )
     try:
          with YAML() as yaml:
               data = yaml.load(response["Body"])
               data['Mappings']['AWSRegionArch2AMI']['us-east-1']['HVM64'] = 'poop'
               print(data)
          return data
     except yaml.YAMLError as exc:
          return exc

get_wireguard_yaml(create_client('s3'))

def get_latest_debian_ami(cpu,client):
     response = client.describe_images(
          Filters=[
               {
                    'Name': 'architecture',
                    'Values' : [
                         cpu
                    ],
                    'Name': 'name',
                    'Values': [
                         'debian-10*'
                    ],
                    'Name': 'state',
                    'Values': [
                         'available'
                    ],
                    'Name':'description',
                    'Values': [
                         'Debian 10*'
                    ]
               },
          ],
          Owners=[
               '136693071363'
          ]
     )
     response = response.get('Images')
     response.sort(key=image_sort)
     response = response[-1]
     response = response['ImageId']
     return response

def image_sort(elm):
     return elm.get('CreationDate')


def get_x86_ami():
     response = {}
     for r in supported_regions:
          client = boto3.client('ec2', region_name = r)
          ami = get_latest_debian_ami('x86_64',client)
          response[r] = ami
     return response

#print(get_x86_ami())



import boto3
import sys
from ruamel.yaml import YAML

supported_regions = ['us-west-2','us-east-1']

def create_client(service):
     client = boto3.client(service)
     return client

def any_constructor(loader, tag_suffix, node):
    if isinstance(node, yaml.MappingNode):
        return loader.construct_mapping(node)
    if isinstance(node, yaml.SequenceNode):
        return loader.construct_sequence(node)
    return loader.construct_scalar(node)

def get_wireguard_yaml(client):
     bucketname = 'wireguardcf'
     file_to_read = 'WireguardNetwork'
     response = client.get_object(
          Bucket=bucketname,
          Key=file_to_read
     )
     try:
          with YAML() as yaml:
               data = yaml.load(response["Body"])
               data['Mappings']['AWSRegionArch2AMI']['us-east-1']['HVM64'] = 'poop'
               print(data)
          return data
     except yaml.YAMLError as exc:
          return exc

get_wireguard_yaml(create_client('s3'))

def get_latest_debian_ami(cpu,client):
     response = client.describe_images(
          Filters=[
               {
                    'Name': 'architecture',
                    'Values' : [
                         cpu
                    ],
                    'Name': 'name',
                    'Values': [
                         'debian-10*'
                    ],
                    'Name': 'state',
                    'Values': [
                         'available'
                    ],
                    'Name':'description',
                    'Values': [
                         'Debian 10*'
                    ]
               },
          ],
          Owners=[
               '136693071363'
          ]
     )
     response = response.get('Images')
     response.sort(key=image_sort)
     response = response[-1]
     response = response['ImageId']
     return response

def image_sort(elm):
     return elm.get('CreationDate')


def get_x86_ami():
     response = {}
     for r in supported_regions:
          client = boto3.client('ec2', region_name = r)
          ami = get_latest_debian_ami('x86_64',client)
          response[r] = ami
     return response

#print(get_x86_ami())



#print(get_x86_ami())