Esempio n. 1
0
    def authenticate(self):
        """Authenticate user."""
        kwargs = {
            'auth_url': self.auth_url,
            'user_id': self.user_id,
            'username': self.username,
            'user_domain_id': self.user_domain_id,
            'user_domain_name': self.user_domain_name,
            'project_id': self.project_id,
            'project_name': self.project_name,
            'project_domain_id': self.project_domain_id,
            'project_domain_name': self.project_domain_name,
            'password': self.password,
        }
        resp, body = self.get_raw_token_from_identity_service(**kwargs)

        try:
            resp_data = body['token']
        except (KeyError, ValueError):
            raise exception.AuthorizationFailure(body)

        self._expires_at = timeutils.parse_isotime(resp_data['expires_at'])
        self.auth_token = resp.headers['X-Subject-Token']
        self._management_url = self._get_endpoint(resp_data['catalog'],
                                                  version=self.version,
                                                  service_type=self.service_type)
        return True
Esempio n. 2
0
 def change_monthly_job_time(self, ctxt, order_id, run_date,
                             clear_date_jobs=None):
     if isinstance(run_date, basestring):
         run_date = timeutils.parse_isotime(run_date)
     self._delete_monthly_job(order_id)
     self._create_monthly_job(order_id, run_date)
     if clear_date_jobs:
         self._delete_date_job(order_id)
         self._delete_30_days_job(order_id)
Esempio n. 3
0
    def _create_monthly_job(self, order_id, run_date):
        job_id = self._make_monthly_job_id(order_id)
        if isinstance(run_date, basestring):
            run_date = timeutils.parse_isotime(run_date)

        self.apsched.add_job(self._handle_monthly_order,
                             'date',
                             args=[order_id],
                             id=job_id,
                             run_date=run_date)
        LOG.warn('create monthly job for order: %s', order_id)
Esempio n. 4
0
    def _create_monthly_job(self, order_id, run_date):
        job_id = self._make_monthly_job_id(order_id)
        if isinstance(run_date, basestring):
            run_date = timeutils.parse_isotime(run_date)

        self.apsched.add_job(self._handle_monthly_order,
                             'date',
                             args=[order_id],
                             id=job_id,
                             run_date=run_date)
        LOG.warn('create monthly job for order: %s', order_id)
Esempio n. 5
0
 def change_monthly_job_time(self,
                             ctxt,
                             order_id,
                             run_date,
                             clear_date_jobs=None):
     if isinstance(run_date, basestring):
         run_date = timeutils.parse_isotime(run_date)
     self._delete_monthly_job(order_id)
     self._create_monthly_job(order_id, run_date)
     if clear_date_jobs:
         self._delete_date_job(order_id)
         self._delete_30_days_job(order_id)
Esempio n. 6
0
def get_new_accounts(y):
    __, accounts = client.get('/accounts')

    user_ids = []
    start_time = datetime.datetime(y.year, y.month, y.day, 1, 0, 0).replace(tzinfo=None)
    end_time = start_time + datetime.timedelta(days=1)

    for account in accounts['accounts']:
        created_at = timeutils.parse_isotime(account['created_at']).replace(tzinfo=None)
        if created_at > start_time and created_at < end_time:
            user_ids.append(account['user_id'])

    return user_ids
def get_new_accounts(y):
    __, accounts = client.get('/accounts')

    user_ids = []
    start_time = datetime.datetime(y.year, y.month, y.day, 1, 0,
                                   0).replace(tzinfo=None)
    end_time = start_time + datetime.timedelta(days=1)

    for account in accounts['accounts']:
        created_at = timeutils.parse_isotime(
            account['created_at']).replace(tzinfo=None)
        if created_at > start_time and created_at < end_time:
            user_ids.append(account['user_id'])

    return user_ids
Esempio n. 8
0
    def get_all(self, user_id=None, type=None, duration=None):
        """Get all projects."""
        user_id = acl.get_limited_to_user(request.headers,
                                          'projects_get') or user_id
        self.conn = pecan.request.db_conn
        result = []

        if not type or type.lower() == 'pay':
            # if admin call this api, limit to admin's user_id
            if not user_id:
                user_id = request.context.user_id

            try:
                user_projects = self.conn.get_user_projects(request.context,
                                                            user_id=user_id)
            except Exception as e:
                LOG.exception('Fail to get all projects')
                raise exception.DBError(reason=e)

            project_ids = [up.project_id for up in user_projects]

            if not project_ids:
                LOG.warn('User %s has no payed projects' % user_id)
                return []

            projects = self._list_keystone_projects()

            for u, p in itertools.product(user_projects, projects):
                if u.project_id == p['id']:
                    billing_owner = p['users']['billing_owner']
                    project_owner = p['users']['project_owner']
                    project_creator = p['users']['project_creator']
                    up = models.UserProject(user_id=user_id,
                                            project_id=u.project_id,
                                            project_name=p['name'],
                                            user_consumption=u.user_consumption,
                                            project_consumption=u.project_consumption,
                                            billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None,
                                                               user_name=billing_owner.get('name') if billing_owner else None),
                                            project_owner=dict(user_id=project_owner.get('id') if project_owner else None,
                                                               user_name=project_owner.get('name') if project_owner else None),
                                            project_creator=dict(user_id=project_creator.get('id') if project_creator else None,
                                                                 user_name=project_creator.get('name') if project_creator else None),
                                            is_historical=u.is_historical,
                                            created_at=timeutils.parse_isotime(p['created_at']) if p['created_at'] else None)
                    result.append(up)
        elif type.lower() == 'all':
            # if admin call this api, limit to admin's user_id
            if not user_id:
                user_id = request.context.user_id

            k_projects = keystone.get_project_list(name=user_id)
            project_ids = [p['id'] for p in k_projects]

            if not project_ids:
                LOG.warn('User %s has no projects' % user_id)
                return []

            try:
                g_projects = self.conn.get_projects_by_project_ids(request.context,
                                                                   project_ids)
            except Exception as e:
                LOG.exception('Fail to get all projects')
                raise exception.DBError(reason=e)
            for k, g in itertools.product(k_projects, g_projects):
                if k['id'] == g.project_id:
                    billing_owner = k['users']['billing_owner']
                    project_owner = k['users']['project_owner']
                    project_creator = k['users']['project_creator']
                    up = models.UserProject(user_id=user_id,
                                            project_id=g.project_id,
                                            project_name=k['name'],
                                            project_consumption=g.consumption,
                                            billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None,
                                                               user_name=billing_owner.get('name') if billing_owner else None),
                                            project_owner=dict(user_id=project_owner.get('id') if project_owner else None,
                                                               user_name=project_owner.get('name') if project_owner else None),
                                            project_creator=dict(user_id=project_creator.get('id') if project_creator else None,
                                                                 user_name=project_creator.get('name') if project_creator else None),
                                            is_historical=False,
                                            created_at=timeutils.parse_isotime(k['created_at']) if k['created_at'] else None)
                    result.append(up)

        elif type.lower() == 'simple':
            duration = gringutils.normalize_timedelta(duration)
            if duration:
                active_from = datetime.datetime.utcnow() - duration
            else:
                active_from = None
            g_projects = list(self.conn.get_projects(request.context,
                                                     user_id=user_id,
                                                     active_from=active_from))
            project_ids = [p.project_id for p in g_projects]

            if not project_ids:
                LOG.warn('User %s has no payed projects' % user_id)
                return []

            k_projects = self._list_keystone_projects()

            for k, g in itertools.product(k_projects, g_projects):
                if k.id == g.project_id:
                    up = models.UserProject(project_id=g.project_id,
                                            project_name=k.name,
                                            domain_id=g.domain_id,
                                            billing_owner=dict(user_id=g.user_id))
                    result.append(up)

        return result
Esempio n. 9
0
 def create_monthly_job(self, ctxt, order_id, run_date):
     """Create a date job for monthly/yearly billing order
     """
     if isinstance(run_date, basestring):
         run_date = timeutils.parse_isotime(run_date)
     self._create_monthly_job(order_id, run_date)
Esempio n. 10
0
    def get_all(self, user_id=None, type=None, duration=None):
        """Get all projects."""
        user_id = acl.get_limited_to_user(request.headers,
                                          'projects_get') or user_id
        self.conn = pecan.request.db_conn
        result = []

        if not type or type.lower() == 'pay':
            # if admin call this api, limit to admin's user_id
            if not user_id:
                user_id = request.context.user_id

            try:
                user_projects = self.conn.get_user_projects(request.context,
                                                            user_id=user_id)
            except Exception as e:
                LOG.exception('Fail to get all projects')
                raise exception.DBError(reason=e)

            project_ids = [up.project_id for up in user_projects]

            if not project_ids:
                LOG.warn('User %s has no payed projects' % user_id)
                return []

            projects = self._list_keystone_projects()

            for u, p in itertools.product(user_projects, projects):
                if u.project_id == p['id']:
                    billing_owner = p['users']['billing_owner']
                    project_owner = p['users']['project_owner']
                    project_creator = p['users']['project_creator']
                    up = models.UserProject(
                        user_id=user_id,
                        project_id=u.project_id,
                        project_name=p['name'],
                        user_consumption=u.user_consumption,
                        project_consumption=u.project_consumption,
                        billing_owner=dict(user_id=billing_owner.get('id')
                                           if billing_owner else None,
                                           user_name=billing_owner.get('name')
                                           if billing_owner else None),
                        project_owner=dict(user_id=project_owner.get('id')
                                           if project_owner else None,
                                           user_name=project_owner.get('name')
                                           if project_owner else None),
                        project_creator=dict(
                            user_id=project_creator.get('id')
                            if project_creator else None,
                            user_name=project_creator.get('name')
                            if project_creator else None),
                        is_historical=u.is_historical,
                        created_at=timeutils.parse_isotime(p['created_at'])
                        if p['created_at'] else None)
                    result.append(up)
        elif type.lower() == 'all':
            # if admin call this api, limit to admin's user_id
            if not user_id:
                user_id = request.context.user_id

            k_projects = keystone.get_project_list(name=user_id)
            project_ids = [p['id'] for p in k_projects]

            if not project_ids:
                LOG.warn('User %s has no projects' % user_id)
                return []

            try:
                g_projects = self.conn.get_projects_by_project_ids(
                    request.context, project_ids)
            except Exception as e:
                LOG.exception('Fail to get all projects')
                raise exception.DBError(reason=e)
            for k, g in itertools.product(k_projects, g_projects):
                if k['id'] == g.project_id:
                    billing_owner = k['users']['billing_owner']
                    project_owner = k['users']['project_owner']
                    project_creator = k['users']['project_creator']
                    up = models.UserProject(
                        user_id=user_id,
                        project_id=g.project_id,
                        project_name=k['name'],
                        project_consumption=g.consumption,
                        billing_owner=dict(user_id=billing_owner.get('id')
                                           if billing_owner else None,
                                           user_name=billing_owner.get('name')
                                           if billing_owner else None),
                        project_owner=dict(user_id=project_owner.get('id')
                                           if project_owner else None,
                                           user_name=project_owner.get('name')
                                           if project_owner else None),
                        project_creator=dict(
                            user_id=project_creator.get('id')
                            if project_creator else None,
                            user_name=project_creator.get('name')
                            if project_creator else None),
                        is_historical=False,
                        created_at=timeutils.parse_isotime(k['created_at'])
                        if k['created_at'] else None)
                    result.append(up)

        elif type.lower() == 'simple':
            duration = gringutils.normalize_timedelta(duration)
            if duration:
                active_from = datetime.datetime.utcnow() - duration
            else:
                active_from = None
            g_projects = list(
                self.conn.get_projects(request.context,
                                       user_id=user_id,
                                       active_from=active_from))
            project_ids = [p.project_id for p in g_projects]

            if not project_ids:
                LOG.warn('User %s has no payed projects' % user_id)
                return []

            k_projects = self._list_keystone_projects()

            for k, g in itertools.product(k_projects, g_projects):
                if k.id == g.project_id:
                    up = models.UserProject(
                        project_id=g.project_id,
                        project_name=k.name,
                        domain_id=g.domain_id,
                        billing_owner=dict(user_id=g.user_id))
                    result.append(up)

        return result
Esempio n. 11
0
 def datetime_from_isotime_str(self, isotime_str):
     return timeutils.parse_isotime(isotime_str)
Esempio n. 12
0
 def create_monthly_job(self, ctxt, order_id, run_date):
     """Create a date job for monthly/yearly billing order
     """
     if isinstance(run_date, basestring):
         run_date = timeutils.parse_isotime(run_date)
     self._create_monthly_job(order_id, run_date)