Beispiel #1
0
    def on_post(self, req, resp):
        data = jsonutils.loads(req.stream.read())
        try:
            jsonschema.validate(data, subscription_schema)
        except jsonschema.ValidationError as e:
            resp.status = falcon.HTTP_400
            resp.body = str(e)
            return
        opts = {
            'os_auth_token': req.context.auth_token,
            'os_auth_url': req.context.auth_url,
            'os_project_id': req.context.project_id,
            'os_service_type': 'messaging'
        }
        auth_opts = {'backend': 'keystone', 'options': opts}
        conf = {'auth_opts': auth_opts}

        endpoint = req.env['keystone.token_auth'].get_endpoint(
            None, 'messaging')

        client = zaqarclient.Client(url=endpoint, conf=conf, version=2.0)
        queue = client.queue(data['target'])
        signed_url_data = queue.signed_url(['messages'], methods=['POST'])
        data['signed_url_data'] = jsonutils.dumps(signed_url_data)
        sub_api = api.SubscriptionAPI(req.context, self.conf)
        resp.body = jsonutils.dumps(sub_api.create(data).items())
        resp.status = falcon.HTTP_201
    def test_signal_queues(self):
        stack_identifier = self.stack_create(template=self.template,
                                             expected_status=None)
        self._wait_for_resource_status(stack_identifier, 'wait_handle',
                                       'CREATE_COMPLETE')
        resource = self.client.resources.get(stack_identifier, 'wait_handle')
        signal = json.loads(resource.attributes['signal'])
        ks = keystoneclient.Client(auth_url=signal['auth_url'],
                                   user_id=signal['user_id'],
                                   password=signal['password'],
                                   project_id=signal['project_id'])
        endpoint = ks.service_catalog.url_for(service_type='messaging',
                                              endpoint_type='publicURL')
        conf = {
            'auth_opts': {
                'backend': 'keystone',
                'options': {
                    'os_auth_token': ks.auth_token,
                    'os_project_id': signal['project_id']
                }
            }
        }

        zaqar = zaqarclient.Client(endpoint, conf=conf)

        queue = zaqar.queue(signal['queue_id'])
        queue.post({'body': {'data': 'here!', 'id': 'data_id'}, 'ttl': 600})
        self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE')
        stack = self.client.stacks.get(stack_identifier)
        self.assertEqual('here!', stack.outputs[0]['output_value'])
Beispiel #3
0
def create_post_delete(queue_name, messages):
    """Auth example

    Creates a queue, posts messages to it and finally deletes it with
    keystone auth strategy enabled on Zaqar server side.

    :params queue_name: The name of the queue
    :type queue_name: `six.text_type`
    :params messages: Messages to post.
    :type messages: list
    """
    auth = password.Password("http://127.0.0.1/identity_v2_admin",
                             username="******",
                             password="******",
                             user_domain_name='default',
                             project_name='admin',
                             project_domain_name='default')
    keystone_session = session.Session(verify=False, cert=None, auth=auth)

    cli = client.Client(session=keystone_session)
    queue = cli.queue(queue_name)
    queue.post(messages)

    for msg in queue.messages(echo=True):
        print(msg.body)
        msg.delete()

    queue.delete()
Beispiel #4
0
def create_post_delete(queue_name, messages):
    """Auth example

    Creates a queue, posts messages to it and finally deletes it with
    keystone auth strategy enabled on Zaqar server side.

    :params queue_name: The name of the queue
    :type queue_name: `six.text_type`
    :params messages: Messages to post.
    :type messages: list
    """
    conf = {
        'auth_opts': {
            'backend': 'keystone',
            'options': {
                'os_username': '******',
                'os_password': '******',
                'os_project_id': 'ccad479c402f43a2994f6e372ab3f8fe',
                'os_project_name': '',
                'os_auth_url': 'http://127.0.0.1:5000/v2.0/',
                'insecure': ''
            }
        }
    }
    cli = client.Client(URL, conf=conf)
    queue = cli.queue(queue_name)
    queue.post(messages)

    for msg in queue.messages(echo=True):
        print(msg.body)
        msg.delete()

    queue.delete()
def create_post_delete(queue_name, messages):
    """Presigned queue example

    Creates a queue, posts messages to it and finally deletes it with
    ``signed-url`` auth strategy enabled on Zaqar server side.

    :params queue_name: The name of the queue
    :type queue_name: `six.text_type`
    :params messages: Messages to post.
    :type messages: list
    """
    conf = {'auth_opts':
            {'backend': 'signed-url',
             'options': {'signature': '',
                         'expires': '',
                         'methods': ['GET', 'PATCH', 'POST', 'PUT'],
                         'paths': ['/v2/queues/beijing/claims'],
                         'os_project_id': '2887aabf368046a3bb0070f1c0413470'}
             }
            }
    cli = client.Client(URL, conf=conf)
    queue = cli.queue(queue_name)
    queue.post(messages)

    for msg in queue.messages(echo=True):
        print(msg.body)
        msg.delete()
Beispiel #6
0
    def test_events(self):
        queue_id = str(uuid.uuid4())
        environment = {
            'event_sinks': [{
                'type': 'zaqar-queue',
                'target': queue_id,
                'ttl': 120
            }]
        }
        stack_identifier = self.stack_create(template=self.template,
                                             environment=environment)
        stack_name, stack_id = stack_identifier.split('/')
        conf = {
            'auth_opts': {
                'backend': 'keystone',
                'options': {
                    'os_username': self.conf.username,
                    'os_password': self.conf.password,
                    'os_project_name': self.conf.project_name,
                    'os_auth_url': self.conf.auth_url,
                    'os_user_domain_id': self.conf.user_domain_id,
                    'os_project_domain_id': self.conf.project_domain_id,
                    'os_user_domain_name': self.conf.user_domain_name,
                    'os_project_domain_name': self.conf.project_domain_name,
                    'insecure': self.conf.disable_ssl_certificate_validation,
                    'cacert': self.conf.ca_file
                }
            }
        }

        zaqar = zaqarclient.Client(conf=conf)
        queue = zaqar.queue(queue_id)

        def validate_messages():
            messages = list(queue.messages())
            if len(messages) < 4:
                return False

            types = [m.body['type'] for m in messages]
            self.assertEqual(['os.heat.event'] * 4, types)
            resources = set(
                [m.body['payload']['resource_name'] for m in messages])
            self.assertEqual(set([stack_name, 'test_resource']), resources)
            stack_ids = [m.body['payload']['stack_id'] for m in messages]
            self.assertEqual([stack_id] * 4, stack_ids)
            statuses = [m.body['payload']['resource_status'] for m in messages]
            statuses.sort()
            self.assertEqual(
                ['COMPLETE', 'COMPLETE', 'IN_PROGRESS', 'IN_PROGRESS'],
                statuses)
            actions = [m.body['payload']['resource_action'] for m in messages]
            self.assertEqual(['CREATE'] * 4, actions)
            return True

        self.assertTrue(test.call_until_true(20, 0, validate_messages))
Beispiel #7
0
 def create_from_signed_url(self, project_id, paths, expires, methods,
                            signature):
     opts = {
         'paths': paths,
         'expires': expires,
         'methods': methods,
         'signature': signature,
         'os_project_id': project_id,
     }
     auth_opts = {'backend': 'signed-url', 'options': opts}
     conf = {'auth_opts': auth_opts}
     endpoint = self.url_for(service_type=self.MESSAGING)
     return zaqarclient.Client(url=endpoint, conf=conf, version=2)
Beispiel #8
0
 def _send(self, subscriber, event):
     """Send event data to the given subscriber using Zaqar."""
     data = json.loads(subscriber.signed_url_data)
     opts = {
         'paths': data['paths'],
         'expires': data['expires'],
         'methods': data['methods'],
         'signature': data['signature'],
         'os_project_id': data['project'],
     }
     auth_opts = {'backend': 'signed-url', 'options': opts}
     conf = {'auth_opts': auth_opts}
     client = zaqarclient.Client(url=self.endpoint, conf=conf, version=2)
     queue = client.queue(subscriber.target, auto_create=False)
     queue.post({'body': event, 'ttl': subscriber.message_ttl})
Beispiel #9
0
    def create_for_tenant(self, tenant_id, token):
        con = self.context
        if token is None:
            LOG.error("Zaqar connection failed, no auth_token!")
            return None

        opts = {
            'os_auth_token': token,
            'os_auth_url': con.auth_url,
            'os_project_id': tenant_id,
            'os_service_type': self.MESSAGING,
        }
        auth_opts = {'backend': 'keystone', 'options': opts}
        conf = {'auth_opts': auth_opts}
        endpoint = self.url_for(service_type=self.MESSAGING)
        return zaqarclient.Client(url=endpoint, conf=conf, version=2)
Beispiel #10
0
def get_client(args):
    keystone = ks_client.Client(auth_url=os.environ.get('OS_AUTH_URL'),
                                username=os.environ.get('OS_USERNAME'),
                                password=os.environ.get('OS_PASSWORD'),
                                tenant_name=os.environ.get('OS_TENANT_NAME'))

    # NOTE: wouldn't have to do this if zaqar didn't require project id
    # (was hoping to just use tenant name and not use keystone client)
    os_opts = {
        'os_auth_token': keystone.auth_ref['token']['id'],
    }
    auth_opts = {'backend': 'keystone',
                 'options': os_opts}
    conf = {'auth_opts': auth_opts}
    zaqar = zq_client.Client(args.server, version=1.1, conf=conf)

    return zaqar
Beispiel #11
0
    def get_messaging_client(self, context):
        zaqar_endpoint = keystone_utils.get_endpoint_for_project(
            context, service_type='messaging')

        auth_uri = context.security.auth_uri or \
            keystone_utils.CONF.keystone_authtoken.auth_uri

        opts = {
            'os_auth_token': context.security.auth_token,
            'os_auth_url': auth_uri,
            'os_project_id': context.security.project_id,
            'insecure': context.security.insecure,
        }
        auth_opts = {
            'backend': 'keystone',
            'options': opts,
        }
        conf = {
            'auth_opts': auth_opts,
            'session': self.get_session(context, 'zaqar')
        }

        return zaqarclient.Client(zaqar_endpoint.url, conf=conf)
Beispiel #12
0
def create_post_delete(queue_name, messages):
    """Simple example

    Creates a queue, posts messages to it
    and finally deletes it.

    :params queue_name: The name of the queue
    :type queue_name: `six.text_type`
    :params messages: Messages to post.
    :type messages: list
    """
    # Note: credential information should be provided
    # using `conf` keyword argument if authentication
    # is enabled at server side. Please refer to
    # keystone_auth.py for more information.
    cli = client.Client(URL, version=2)
    queue = cli.queue(queue_name)
    queue.post(messages)

    for msg in queue.messages(echo=True):
        print(msg.body)
        msg.delete()

    queue.delete()
Beispiel #13
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
from zaqarclient.queues.v2 import client

client = client.Client('http://localhost:8888',
                       conf={
                           'auth_opts': {
                               'options': {
                                   'client_uuid':
                                   '355186cd-d1e8-4108-a3ac-a2183697232a',
                                   'os_auth_token':
                                   '8444886dd9b04a1b87ddb502b508261c',
                                   'os_auth_url':
                                   'http://localhost:5000/v3.0/',
                                   'os_project_id':
                                   '7530fad032ca431e9dc8ed4a5de5d99c'
                               }
                           }
                       })

queue = client.queue('SampleQueue')

claim = queue.claim(ttl=600, grace=600)  # refer to bug #1553387

for msg in claim:
    print(msg)
Beispiel #14
0
 def client(self):
     if self._client is None:
         session = keystone_client.get_session(
             self.conf, group=self.conf.zaqar.auth_section)
         self._client = zaqarclient.Client(session=session)
     return self._client
Beispiel #15
0
 def _create(self):
     return zaqarclient.Client(version=2,
                               session=self.context.keystone_session)
Beispiel #16
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

from zaqarclient.queues.v2 import client

URL = 'http://localhost:8888'

# Note: credential information should be provided using `conf`
# keyword argument if authentication is enabled at server side.
# Please refer to keystone_auth.py for more information.
cli = client.Client(URL)
queue = cli.queue('worker-jobs')


def send_jobs():
    jobs = [{'name': 'fluffy'}, {'name': 'scout'}, {'name': 'jo'}]
    queue.post([{'body': j, 'ttl': 360} for j in jobs])


def process_jobs():
    claim1 = queue.claim(ttl=500, grace=900, limit=2)
    for msg in claim1:
        claim_id = msg.claim_id
        print('{claim_id} =? {id}'.format(claim_id=claim_id, id=claim1.id))
        print('processing job %s' % (msg))
        msg.delete()