# Path: otpsetup/client/models.py
# class ManagedDeployment(models.Model):
#     source = models.CharField(max_length=15)
#     description = models.CharField(max_length=200, null=True, blank=True)
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
#     last_osm_key = models.CharField(max_length=200, null=True, blank=True)
# 
#     def __str__(self):
#         return "[%s] %s (%s)" % (self.id, self.source, self.description)
# 
# class DeploymentHost(models.Model):
#     name = models.CharField(max_length=50, blank=True)
#     instance_id = models.CharField(max_length=20)
#     host_ip = models.CharField(max_length=20)
#     otp_version = models.CharField(max_length=20)
#     auth_password = models.CharField(max_length=20)
#     total_memory = models.BigIntegerField()
#     free_memory = models.BigIntegerField()
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
# 
#     def __str__(self):
#         return "(%s) %s" % (self.id, self.name)
# 
# class DeploymentGroup(models.Model):
#     name = models.CharField(max_length=20)
#     description = models.TextField(max_length=1000, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# class GraphBuild(models.Model):
#     deployment = models.ForeignKey(ManagedDeployment)
#     creation_date = models.DateTimeField(default=datetime.now)
#     completion_date = models.DateTimeField(null=True)
#     config = models.TextField(max_length=10000, null=True, blank=True)
#     osm_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_size = models.BigIntegerField(null=True)
#     output_key = models.CharField(max_length=200, null=True, blank=True)
#     success = models.BooleanField(default=False)
#     otp_version = models.CharField(max_length=20, null=True, blank=True)
# 
#     def gtfs_count(self):
#         return len(self.gtfsbuildmapping_set.all())
# 
#     def host_count(self):
#         return len(self.buildhostmapping_set.all())
# 
#     def link(self):
#         return "<a href='/admin/client/graphbuild/%s'>[<b>%s</b>]</a>" % (self.id, self.id)
#     link.allow_tags = True
# 
#     def __str__(self):
#         return "Build #%s / Deployment %s" % (self.id, self.deployment)


from otpsetup.client.models import ManagedDeployment, DeploymentHost, DeploymentGroup, GraphBuild
from kombu import Exchange
from otpsetup.shortcuts import DjangoBrokerConnection
import sys, urllib2, json



def build_deployment_from_config(config_txt):
    config = json.loads(config_txt)

    if not 'metroId' in config:
        return "no metroId provided"
    
    if 'metro' in config:
        metro_name = config['metro'] 
    else:
        metro_name = "N/A"

    response = 'Metro #%s (%s) ' % (config['metroId'], metro_name)
    
    source = 'metro-%s' % config['metroId']
    osm_key = None
    try:
        man_dep = ManagedDeployment.objects.get(source=source)
        response += " has existing record."

# Path: otpsetup/client/models.py
# class ManagedDeployment(models.Model):
#     source = models.CharField(max_length=15)
#     description = models.CharField(max_length=200, null=True, blank=True)
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
#     last_osm_key = models.CharField(max_length=200, null=True, blank=True)
# 
#     def __str__(self):
#         return "[%s] %s (%s)" % (self.id, self.source, self.description)
# 
# class DeploymentHost(models.Model):
#     name = models.CharField(max_length=50, blank=True)
#     instance_id = models.CharField(max_length=20)
#     host_ip = models.CharField(max_length=20)
#     otp_version = models.CharField(max_length=20)
#     auth_password = models.CharField(max_length=20)
#     total_memory = models.BigIntegerField()
#     free_memory = models.BigIntegerField()
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
# 
#     def __str__(self):
#         return "(%s) %s" % (self.id, self.name)
# 
# class DeploymentGroup(models.Model):
#     name = models.CharField(max_length=20)
#     description = models.TextField(max_length=1000, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# class GraphBuild(models.Model):
#     deployment = models.ForeignKey(ManagedDeployment)
#     creation_date = models.DateTimeField(default=datetime.now)
#     completion_date = models.DateTimeField(null=True)
#     config = models.TextField(max_length=10000, null=True, blank=True)
#     osm_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_size = models.BigIntegerField(null=True)
#     output_key = models.CharField(max_length=200, null=True, blank=True)
#     success = models.BooleanField(default=False)
#     otp_version = models.CharField(max_length=20, null=True, blank=True)
# 
#     def gtfs_count(self):
#         return len(self.gtfsbuildmapping_set.all())
# 
#     def host_count(self):
#         return len(self.buildhostmapping_set.all())
# 
#     def link(self):
#         return "<a href='/admin/client/graphbuild/%s'>[<b>%s</b>]</a>" % (self.id, self.id)
#     link.allow_tags = True
# 
#     def __str__(self):
#         return "Build #%s / Deployment %s" % (self.id, self.deployment)


# Path: otpsetup/deploy_ops.py
from otpsetup.client.models import ManagedDeployment, DeploymentHost, DeploymentGroup, GraphBuild
from kombu import Exchange
from otpsetup.shortcuts import DjangoBrokerConnection
import sys, urllib2, json




def build_deployment_from_config(config_txt):
    config = json.loads(config_txt)

    if not 'metroId' in config:
        return "no metroId provided"
    
    if 'metro' in config:
        metro_name = config['metro'] 
    else:
        metro_name = "N/A"

    response = 'Metro #%s (%s) ' % (config['metroId'], metro_name)
    
    source = 'metro-%s' % config['metroId']
    osm_key = None
    try:
        man_dep = ManagedDeployment.objects.get(source=source)
        response += " has existing record."

    except ManagedDeployment.DoesNotExist: