示例#1
0
class PasswordRes(ColRes):
    def __init__(self):
        self.smm = ServiceManagerMailer()
        self.collection = 'users'
        self.fields = 'user_name,email,keystone_user_id,activated'.split(",")
        self.keystone = Keystone()
        self.item = None
        self.status_code = 204
        self.u = User()

    def post(self):
        data = get_json()
        user = self.u.get_by_field('email', data['email'])
        if user is None:
            self.item = create_message_error(404, '')
            return create_response(404, self.item)
        new_password = generate_password()
        response = self.keystone.patch_user(user['keystone_user_id'],
                                            new_password)
        if response.status_code == 200:
            self.smm.change_password(user['email'], new_password)
            self.u.update(str(user['_id']), {"password": True})
        else:
            self.status_code = response.status_code
            self.item = create_message_error(self.status_code,
                                             json.loads(response.text))

        return create_response(self.status_code, self.item)
示例#2
0
 def create_admin(self):
     self.admin = User(email=cfg.admin__email,
                       user_name=cfg.admin__user,
                       admin=True,
                       staff=True,
                       phone_number="555 55 55 55",
                       nif='5555555R')
     self.admin.save()
示例#3
0
 def __init__(self):
     self.smm = ServiceManagerMailer()
     self.collection = 'users'
     self.fields = 'user_name,email,keystone_user_id,activated'.split(",")
     self.keystone = Keystone()
     self.item = None
     self.status_code = 204
     self.u = User()
示例#4
0
 def __init__(self):
     self.keystone = Keystone()
     self.collection = 'users'
     self.smm = ServiceManagerMailer()
     self.u = User()
     self.fields = "_id,_cls,user_name,activated,name,surname,email,company,address,phone,position," \
                   "legal,identification".split(",")
     self.item = None
     self.data_delete = {'activated': False, "deleted": True}
示例#5
0
class UserCrudRes(ColRes):
    def __init__(self):
        self.keystone = Keystone()
        self.collection = 'users'
        self.smm = ServiceManagerMailer()
        self.u = User()
        self.fields = "_id,_cls,user_name,activated,name,surname,email,company,address,phone,position," \
                      "legal,identification".split(",")
        self.item = None
        self.data_delete = {'activated': False, "deleted": True}

    @get_exists_user('User.Administrator')
    def put(self, id):
        data = get_json()
        user = find_one_in_collection(self.collection, {"_id": ObjectId(id)})
        if 'activated' in data:
            response = self.keystone.patch_user(user['keystone_user_id'],
                                                data['activated'])
            if response.status_code == 200:
                if user['activated'] and not data['activated']:
                    self.smm.ban(user['email'])
                if data['activated'] and not user['activated']:
                    self.smm.welcome(user['email'])
        if '_id' in data:
            del data['_id']
        try:
            self.u.update(id, data)
            status_code = 204
        except Exception as e:
            print e
            status_code = 400
            self.item = create_message_error(status_code, "USER_NOT_FOUND")
            if 'activated' in data:
                self.keystone.patch_user(user['keystone_user_id'],
                                         not (data['activated']))
        return create_response(status_code, self.item)

    @get_exists_user('User.Administrator')
    def patch(self, id):
        return self.put(id)

    @get_exists_user('User.Administrator')
    def delete(self, id):
        self.u.update(id, self.data_delete)
        user = self.u.get(id)
        response = self.keystone.patch_user(user['keystone_user_id'], False)
        if response.status_code == 200:
            status_code = 204
            item = None
        else:
            status_code = 404
            item = create_message_error(404, "USER_NOT_FOUND")
        return create_response(status_code, item)
示例#6
0
class Password(ItemRes):
    def __init__(self):
        self.smm = ServiceManagerMailer()
        self.collection = 'users'
        self.fields = 'user_name,email,keystone_user_id,activated'.split(",")
        self.keystone = Keystone()
        self.item = None
        self.status_code = 204
        self.u = User()

    def get(self, id):
        user = self.get_user(id)
        if user is None:
            return create_response(
                404, create_message_error(404, 'USER_NOT_ACTIVATED'))
        new_password = generate_password()
        response = self.keystone.patch_user(user['keystone_user_id'],
                                            new_password)
        if response.status_code == 200:
            self.smm.change_password(user['email'], new_password)
            self.u.update(id, {"password": True})
        else:
            self.status_code = response.status_code
            self.item = create_message_error(self.status_code,
                                             json.loads(response.text))

        return create_response(self.status_code, self.item)

    @get_exists_user(None)
    @get_authorize(None, 'users', False, '_id', True)
    def put(self, id):
        data = get_json()
        user = self.get_user(id)
        if user is None:
            return create_response(
                404, create_message_error(404, 'USER_NOT_ACTIVATED'))
        response = self.keystone.change_password(user['keystone_user_id'],
                                                 data)
        if response.status_code is not 204:
            self.status_code = response.status_code
            self.item = create_message_error(self.status_code,
                                             json.loads(response.text))
        else:
            self.u.update(id, {"password": False})
        return create_response(self.status_code, self.item)

    def get_user(self, id):
        user = super(Password, self).get(id)
        if not user['activated']: return None
        return user
示例#7
0
    def test_user_save(self):
        user = User(email='*****@*****.**', auth_id=23)
        user.save()
        self.assertIsNotNone(user.pk)

        with self.assertRaises(NotUniqueError):
            user = User(email='*****@*****.**')
            user.save()
示例#8
0
 def create_user(self):
     self.user = User(email='*****@*****.**', phone_number="666 66 66 66", nif='6666666R')
     self.user.save()
示例#9
0
class DocLoader(object):

    def create_admin(self):
        self.admin = User(email=cfg.admin__email, user_name=cfg.admin__user,
                         admin=True, staff=True, phone_number="555 55 55 55", nif='5555555R' )
        self.admin.save()

    def create_user(self):
        self.user = User(email='*****@*****.**', phone_number="666 66 66 66", nif='6666666R')
        self.user.save()

    def create_provider(self):
        # user = User(email='*****@*****.**')
        # user.save()
        contact = Contact( email='*****@*****.**' )
        self.provider = Provider(name='prov1', 
                            contact = contact,
                            # users=[user,],
                            nif='B77777777',
                            sectors=[ sector[0] for sector in ANELLA_SECTORS],
                           )
        self.provider.save()

    def create_client(self):
        # user = User(email='*****@*****.**')
        # user.save()
        contact = Contact(email='*****@*****.**')
        self.client = Client(name='client1', 
                            contact = contact,
                            # users=[user,],
                            partner_type='research',
                            nif='B88888888',
                            sectors=[ sector[0] for sector in ANELLA_SECTORS],
                           )
        self.client.save()

    def create_generic(self):
        from anella.model.service import AppService

#         image = DockerImage(name='orambla/anella')
        self.generic = AppService(name='service1',
                                 summary = "summary 1",
                                 description = "description 1",
                                 link = "http://link1.com",
                                 provider = self.provider,
                                 keywords= [ "storage" ],
                                 sectors= [ "industry" ],
                                 properties={'context_type':'ssh'}
#                                  images = [image,],
                                )
        self.generic.save()

    def create_cloud(self):
        from anella.model.service import ISService
#         image = DockerImage(name='orambla/anella')
        self.cloud = ISService(name='cloud_service2',
                                 summary = "summary 2",
                                 description = "description 2",
                                 link = "http://link2.com",
                                 provider = self.provider,
                                 keywords= [ "analysis" ],
                                 sectors= [ "cities" ],
                                 properties={'context_type':'openstack'}
#                                  images = [image,],
                                )
        self.cloud.save()


    def create_scontext(self):
        from anella.model.scontext import SContext

#         self.sc1 = SContext(name='scontext1', schema_file_name='schemas/schema-test.json' )
        context={
        "service_name" : "apache2.service",
        "host" : "87.236.219.21",
        "user_name" : "test",
        "password" : "test",
        "port" : 22,
#             'host': '',
#             'port': 22,
#             'user_name': 'oscar.rambla',
#             'password': '******',
#             'service_name': 'apachectl',
        "openstack" : {
            "name_instance" : "test-openstack-1",
            "auth_url" : "http://87.236.219.10:5000/v3",
            "username" : "i2cat",
            "password" : "c5QWZTSv9gn0",
            "project_id" : "579eaa7245d04e778f0effd77565794c",
            "user_domain_name" : "i2cat",
            "flavor" : "VM.M1",
            "image" : "CentOS Cloud Base",
            "network" : "tenant-i2cat-demo"
            }
        }
     
        self.scontext = SContext(name='openstack',
                                 context_type='openstack',
                                 context=context,
                                )
        self.scontext.save()

#     def create_sc2(self):
# #         self.sc2 = SContext(name='scontext2', schema_file_name='schemas/schema-test.json' )
#         self.sc2 = SContext(name='scontext2' )
#         self.sc2.save()

    def create_scontext_ssh(self):
        from anella.model.scontext import SContext
        self.scontext_ssh = SContext(name='context_apache',
                                 context_type='ssh',
                                 context={
        "service_name" : "apache2.service",
        "host" : "84.88.34.24",
        "openstack" : {},
        "password" : "segmngr",
        "user_name" : "test",
        "port" : 22
    }
                                )
        self.scontext_ssh.save()

    def create_scontext1(self):
        from anella.model.scontext import SContext

#         self.sc1 = SContext(name='scontext1', schema_file_name='schemas/schema-test.json' )
        context={
#             'parameters':{
#                 'service_name': '${service_name}',
#                 'host': 'localhost',
#                 'port': 22,
#                 'user_name': 'oscar.rambla',
#                 'password': '******'
#             },
#             'lifecycle_events': {
#                 'start': {
#                     'cmd': 'start ${service_name}'
#                  },
#                 'stop': {
#                     'cmd': 'stop ${service_name}'
#                  }
#             }
#         "openstack" : {
            "name_instance" : "test-openstack-1",
            "auth_url" : "http://87.236.219.10:5000/v3",
            "username" : "i2cat",
            "password" : "c5QWZTSv9gn0",
            "project_id" : "579eaa7245d04e778f0effd77565794c",
            "user_domain_name" : "i2cat",
            "flavor" : "VM.M1",
            "image" : "CentOS Cloud Base",
            "network" : "tenant-i2cat-demo"
#             }
        }
     
        self.scontext1 = SContext(name='cloud',
                                 context_type='cloud',
                                 context=context,
                                )
        self.scontext1.save()

    def create_apache_service(self):
        from anella.model.service import AppService

#         image = DockerImage(name='orambla/anella')
        properties={
            'service_name' : 'apache2',
            'context_type' : 'ssh',
        }
        self.apache = AppService(name='apache',
                                 summary = "summary apache",
                                 description = "description apache",
                                 link = "http://linkapache.com",
                                 provider = self.provider,
                                 keywords= [ "web", "server" ],
                                 sectors= [ "industry" ],
#                                  images = [image,],
                                 properties=properties
                                )
        self.apache.save()

    def create_apache1_service(self):
        from anella.model.service import AppService, ISService

#         image = DockerImage(name='orambla/anella')
        properties={
            'service_name' : 'apache2',
            'context_type' : 'ssh',
        }
        self.apache1 = AppService(name='apache1',
                                 summary = "summary apache1",
                                 description = "description apache1",
                                 link = "http://linkapache.com",
                                 provider = self.provider,
                                 keywords= [ "web", "server" ],
                                 sectors= [ "industry" ],
#                                  images = [image,],
                                 properties=properties
                                )
        self.apache1.save()


    def create_project(self):
        from anella.model.project import SProject, Project

        self.project = Project(name='project1', client=self.client,) 
#                           services=[ c_generic, c_cloud ]
#                           services = {
#                               service.name : SContext(service=service),
#                               cloud.name : SContext(service=cloud),
#                            }
        self.project.save()

        # context = self.scontext.resolve_parameters(self.apache.properties)
        context = self.scontext.context
        self.sproject= SProject(project=self.project, service=self.apache, 
                                context_type=self.scontext.context_type, context=context )
        self.sproject.save()
        self.project.services.append(self.sproject)
        self.project.save()

    def create_project1(self):
        from anella.model.project import SProject, Project

        self.project1 = Project(name='project2', client=self.client,) 
#                           services=[ c_generic, c_cloud ]
#                           services = {
#                               service.name : SContext(service=service),
#                               cloud.name : SContext(service=cloud),
#                            }
        self.project1.save()

        sproject= SProject(project=self.project1, service=self.generic, 
                                context_type=self.scontext.context_type, 
                                context= self.scontext.context
                          )
        sproject.save()
        self.project1.services.append(sproject)

        sproject= SProject(project=self.project1, service=self.cloud, 
                                context_type=self.scontext1.context_type, 
                                context= self.scontext1.context )
        sproject.save()
        self.project1.services.append(sproject)
        self.project1.save()

    def create_instance(self):
        from random import randint
        from anella.model.instance import Instance
        self.instance= Instance(sproject=self.sproject, 
                                instance_id=str(randint(1,9999)),
                                # context_type=self.scontext.context_type, 
                                # context=self.sproject.context 
                               )
        self.instance.save()
示例#10
0
def get_role_user(user_id):
    u = User()
    return u.get(dict(_id=ObjectId(user_id)))