Exemple #1
0
def swift_copy_object(request, orig_container_name, orig_object_name,
                      new_container_name, new_object_name):
    try:
        # FIXME(gabriel): The swift currently fails at unicode in the
        # copy_to method, so to provide a better experience we check for
        # unicode here and pre-empt with an error message rather than
        # letting the call fail.
        str(orig_container_name)
        str(orig_object_name)
        str(new_container_name)
        str(new_object_name)
    except UnicodeEncodeError:
        raise exceptions.HorizonException(
            _("Unicode is not currently "
              "supported for object copy."))

    if swift_object_exists(request, new_container_name, new_object_name):
        raise exceptions.AlreadyExists(new_object_name, 'object')

    headers = {
        "X-Copy-From":
        FOLDER_DELIMITER.join([orig_container_name, orig_object_name])
    }
    return swift_api(request).put_object(new_container_name,
                                         new_object_name,
                                         None,
                                         headers=headers)
Exemple #2
0
def swift_copy_object(request, orig_container_name, orig_object_name,
                      new_container_name, new_object_name):
    try:
        # FIXME(gabriel): Cloudfiles currently fails at unicode in the
        # copy_to method, so to provide a better experience we check for
        # unicode here and pre-empt with an error message rather than
        # letting the call fail.
        str(orig_container_name)
        str(orig_object_name)
        str(new_container_name)
        str(new_object_name)
    except UnicodeEncodeError:
        raise exceptions.HorizonException(_("Unicode is not currently "
                                            "supported for object copy."))
    container = swift_api(request).get_container(orig_container_name)

    if swift_object_exists(request, new_container_name, new_object_name):
        raise exceptions.AlreadyExists(new_object_name, 'object')

    orig_obj = container.get_object(orig_object_name)
    return orig_obj.copy_to(new_container_name, new_object_name)
    def _configure_networking(self, request, project):
        # configuration
        external_network_name = getattr(
            local_settings, 'ASTUDE_ACCOUNT_EXTERNAL_NETWORK_NAME', 'public')
        internal_network_name = getattr(
            local_settings, 'ASTUDE_ACCOUNT_INTERNAL_NETWORK_NAME',
            '{{ account }}-nw')
        internal_network_cidr = getattr(
            local_settings, 'ASTUDE_ACCOUNT_INTERNAL_NETWORK_CIDR',
            '10.0.0.0/24')
        account_router_name = getattr(local_settings,
                                      'ASTUDE_ACCOUNT_ROUTER_NAME',
                                      '{{ account }}-gw')

        rexp = r'\{\{\s*account\s*\}\}'
        external_network_name = re.sub(rexp, project.name,
                                       external_network_name)
        internal_network_name = re.sub(rexp, project.name,
                                       internal_network_name)
        account_router_name = re.sub(rexp, project.name, account_router_name)

        # create network
        #network = neutron.network_create(request, tenant_id=project.id, name=internal_network_name)
        network = create_network(request,
                                 tenant_id=project.id,
                                 name=internal_network_name)

        #subnet = neutron.subnet_create(request, network_id=network.id, tenant_id=project.id, name=internal_network_cidr, cidr=internal_network_cidr, ip_version=4)
        subnet = create_subnet(request,
                               network_id=network.id,
                               tenant_id=project.id,
                               name=internal_network_cidr,
                               cidr=internal_network_cidr,
                               ip_version=4)

        # find external network
        #external_network = [n for n in neutron.network_list(request) if n.name == external_network_name]
        external_network = [
            n for n in list_network(request) if n.name == external_network_name
        ]
        if len(external_network) < 1:
            raise exceptions.HorizonException(
                'Public network `%s` not found.' % external_network_name)
        external_network = external_network[0]
        # create router
        params = {
            "tenant_id": project.id,
            "name": account_router_name,
            "external_gateway_info": {
                "network_id": external_network.id
            }
        }

        #router = neutron.router_create(request, **params)
        router = create_router(request, **params)

        # apply internal network into account router
        #neutron.router_add_interface(request, router.id, subnet_id=subnet.id)
        add_interface_to_router(request, router.id, subnet_id=subnet.id)

        return True
    def handle(self, request, data):

        is_new_project = str(data['project_mapping']) == '0'
        project = None

        #Keystone connection
        ks = get_admin_ksclient()

        # handle project mapping
        if is_new_project:
            # create new project first
            '''
            project = keystone.tenant_create(
                request,
                data.get('project_name'),
                description=data.get('description'),
                enabled=True,
                domain=data.get('domain_id')
            )
            '''
            project = create_project(request,
                                     data.get('project_name'),
                                     description=data.get('description'),
                                     enabled=True,
                                     domain=data.get('domain_id'))

        else:
            # fetch project
            #project = keystone.tenant_get(request, data.get('project_id'))
            project = get_project(request, data.get('project_id'))

        # map project to billing account
        extra_fields = dict([(f, data[f]) for f in ACCOUNT_EXTRA_FIELDS])
        try:
            success = bool(
                create_billing_type_mapping(
                    request, {
                        "user": project.id,
                        "billing_type": data.get('billing_type') or 1,
                        "extra_fields": json.dumps(extra_fields)
                    }))
            if not success:
                raise exceptions.HorizonException(
                    'Unable to create billing type mapping.')
        except:
            # clean up created project in case of error
            if is_new_project:
                #FIXME: Need to check V3 related issues
                #keystone.tenant_delete(request, project.id)
                ks.tenants.delete(project.id)
            raise

        # create user (in case of new project)
        user = None
        try:
            if is_new_project:
                '''
                user = keystone.user_create(
                    request,
                    name=data.get('username'),
                    password=data.get('password'),
                    email=data.get('authorized_officer_email'),
                    enabled=True,
                    description='General user of project `%s`' % project.name,
                    project=project.id,
                    domain=data.get('domain_id')
                )
                '''
                user = create_user(request,
                                   name=data.get('username'),
                                   password=data.get('password'),
                                   email=data.get('authorized_officer_email'),
                                   enabled=True,
                                   description='General user of project `%s`' %
                                   project.name,
                                   project=project.id,
                                   domain=data.get('domain_id'))

        except:
            # clean up created project in case of error
            if is_new_project:
                #keystone.tenant_delete(request, project.id)
                #FIXME: Need to check V3 related issues
                ks.tenants.delete(project.id)
            raise

        # do networking deployment
        if is_new_project and getattr(
                local_settings, 'ASTUDE_CONFIGURE_ACCOUNT_NETWORKING', True):
            try:
                self._configure_networking(request, project)
            except Exception as e:
                print "Exception while adding network"
                print e
                pass

        self.name = project.name

        # send welcome email
        if not getattr(local_settings, 'ASTUTE_ENABLE_WELCOME_EMAIL', True):
            return True

        # send welcome email
        subj = getattr(settings, 'ASTUTE_WELCOME_EMAIL_SUBJ',
                       'Your new M1 Cloud Application Service')
        sender = getattr(settings, 'ASTUTE_WELCOME_EMAIL_FROM',
                         '*****@*****.**')
        host = getattr(settings, 'ASTUTE_SMTP_HOST', 'localhost')
        port = getattr(settings, 'ASTUTE_SMTP_PORT', 25)
        user = getattr(settings, 'ASTUTE_SMTP_USER', None)
        pswd = getattr(settings, 'ASTUTE_SMTP_PASS', None)
        html = render_to_string(WELCOME_EMAIL_TEMPLATE, data)

        # save the email content for the project user
        try:
            success = bool(
                create_user_letter(request, {
                    "user": project.id,
                    "content": encrypt(CIPHER_KEY, html),
                }))

        except Exception as e:
            print '*******mail*****'
            print e
            pass

        try:
            send_mail(subject=subj,
                      sender=sender,
                      to=data.get('authorized_officer_email'),
                      body=None,
                      html=html,
                      smtp_host=host,
                      smtp_port=port,
                      username=user,
                      password=pswd)
        except Exception as e:
            print e
            #raise exceptions.RecoverableError("Account has been created but error ocured on sending welcome email")
            pass

        return True