def organization_api_key(): # work with existing organizations if request.method == 'PUT': # update organization org = organizations.Organizations() org_name = request.json.get('name') domain = request.json.get('domain') org.load(name=org_name, domain=domain, api_key=request.headers.get('api_key')) if not check_fields(): e = 'fields not correct: {0}'.format(','.join(error_fields)) return Response(json.dumps(dict(error=e)), status=400, mimetype='application/json') else: for k, v in request.json.items(): if k == 'api_key': continue # users should not be able to update their api_key as this could allow hackers to gain access to other orgs. if k == 'creation_date': continue # users should not be able to update their creation_date as we'll use this for billing purposes. org.set(k, v) org.update() return Response(json.dumps(dict(success='organization updated')), status=200, mimetype='application/json') elif request.method == 'GET': org = organizations.Organizations() print 'called function with get' print request.json print request.headers.get('api_key') try: org.load(name=request.args.get('name'), domain=request.args.get('domain'), api_key=request.headers.get('api_key')) except Exception, e: return Response(json.dumps(dict(error=str(e))), status=400, mimetype='application/json') if org.dump(): org.set('creation_date', str(org.get('creation_date'))) return Response(json.dumps(org.dump()), status=200, mimetype='application/json') else: return Response(json.dumps( dict(error='cannot retrieve requested item')), status=400, mimetype='application/json')
def create_service_node(self, template): vpc = organization_vpc.OrganizationVPC() org = organizations.Organizations() org.load(name=self.organization) vpc.load(organization_id=org.get('id')) vpc = vpc.dump() node = instances.EC2Instance(self.service_name, self.region, self.organization, self.domain) tpl = template.Templates() tpl.load(name=template) if not vpc: instance = node.create_instance(template, allow_ssh=False, set_dns=False, vpc_info=None, **tpl.dump()) else: instance = node.create_instance(template, allow_ssh=False, set_dns=False, vpc_info=vpc, **tpl.dump()) lb_name = '{0}.{1}'.format(self.service_name, self.organization) lb = load_balancer.EC2LoadBalancer(lb_name, self.region) ins = instance.Instances() ins.set('name', self.service_name) ins.set('instance_id', instance.id) ins.set('user', template.get('user')) ins.set('key_name', template.get('key_name')) ins.set('private_ip', instance.private_ip) ins.set('template_id', template.get('id')) ins.insert() lb.register_node(instance.id) return node
def create(self, **organization_details): self.organization = organization_details.get('name') self.domain = organization_details.get('domain') org = organizations.Organizations() org.load_from_dict(organization_details) org.insert() return Organization(self.organization, self.domain)
def get_vpc_info(self): org = organizations.Organizations() org.load(name=self.organization_name) org = org.dump() org_id = org.get('id') vpc = organization_vpc.OrganizationVPC() vpc.load(organization_id=org_id) vpc = vpc.dump() return dict(vpc=org.get('vpc_id'), subnet=org.get('subnet_id'))
def check_api_key(api_key): r = redis.Redis(app_config.redis.get('host'), password=app_config.redis.get('auth')) if api_key == r.get('master_api_key'): return True org = organizations.Organizations() org.load(api_key=api_key) if org.dump(): return True else: return False
def check_fields(): global error_fields error_fields = [] org = organizations.Organizations() error_fields for key in request.json: if key not in org.get_fields(): error_fields.append(key) continue if error_fields: return False else: return True
def remove_vpc(self): c = VPCConnection() for subnet in c.get_all_subnets(): if subnet.tags.get('organization') == self.organization: c.delete_subnet(subnet.id) for vpc in c.get_all_vpcs(): if vpc.tags.get('organization') == self.organization: vpc.delete() db_vpc = organization_vpc.OrganizationVPC() org = organizations.Organizations() org.load(name=self.organization_name) db_vpc.load(organization_id=org.get('id')) db_vpc.delete()
def create(self): svc = service.Services() svc.set_elb_name(self.service_name) svc.set_dns_name('{0}.{1}.{2}'.format(self.service_name, self.organization, self.domain)) svc.set_configuration_url(self.configuration_url) svc.set_name(self.service_name) svc.save() svc.load(name=self.service_name) org = organizations.Organizations() org.load(name=self.organization) org_id = org.get('id') svc_id = svc.get('id') org_svc = organization_services.OrganizationServices() org_svc.load_from_dict(dict(organization_id=org_id, service_id=svc_id)) org_svc.save()
def create_vpc(self, vpc_cidr, subnet_cidr): c = VPCConnection() vpc = c.create_vpc(vpc_cidr) vpc.add_tag('organization', value=self.organization) subnet = c.create_subnet(vpc.id, subnet_cidr) while True: try: subnet.add_tag('organization', value=self.organization) break except: pass db_vpc = organization_vpc.OrganizationVPC() org = organizations.Organizations() org.load(name=self.organization) print org db_vpc.set('organization_id', org.get('id')) db_vpc.set('vpc_id', vpc.id) db_vpc.set('subnet_id', subnet.id) db_vpc.insert() return vpc.id, subnet.id, org.get('id')
def mk_organization(): if request.method == 'POST': # add organization org = organizations.Organizations() org.load(name=request.json.get('name'), domain=request.json.get('domain')) if org.dump(): return Response(json.dumps( dict(error='cannot create organization')), status=400, mimetype='application/json') if not check_fields(): return Response(json.dumps( dict(error='fields not correct: {0}'.format(','.join( error_fields)))), status=400, mimetype='application/json') try: for k, v in request.json.items(): org.set(k, v) org.set('enabled', False) org.set('api_key', uuid.uuid4()) org.set('password', auth.hash_password(request.json.get('password'))) org.insert() org.load(name=request.json.get('name')) return Response(json.dumps( dict(success='organization created', api_key=str(org.get('api_key')))), status=200, mimetype='application/json') except Exception, e: print str(e) return Response(json.dumps( dict(error='cannot create organization')), status=500, mimetypr='applicate/json')
def delete(self, organization_name): org = organizations.Organizations() org.load(name=self.organization_name) org.delete()
def get(self): org = organizations.Organizations() org.load(name=self.organization) return org.dump()