def test_capability_url_included_with_capability_hash(self):
        """
        If a capability_hash parameter is passed in, an extra link is added to
        the JSON blob containing a capability URL.  But in the non-formatted
        URL, nothing changes.
        """
        pairs = [("group_id", "1"), ("policy_id", "2"), ("webhook_id", "3")]
        expected = [
            '/v1.0/11111/groups/1/',
            '/v1.0/11111/groups/1/policies/2/',
            '/v1.0/11111/groups/1/policies/2/webhooks/3/'
        ]
        for i in range(3):
            self.assertEqual(
                get_autoscale_links(
                    '11111', format=None, capability_hash='xxx',
                    **dict(pairs[:(i + 1)])),
                expected[i])

            json_blob = get_autoscale_links(
                '11111', capability_hash='xxx', **dict(pairs[:(i + 1)]))

            self.assertEqual(len(json_blob), 2)
            self.assertIn({'rel': 'capability', 'href': '/v1.0/execute/1/xxx/'},
                          json_blob)
Beispiel #2
0
    def test_capability_version(self):
        """
        There is a default capability version of 1, but whatever capability
        version is passed is the one used
        """
        # default version
        json_blob = get_autoscale_links('11111',
                                        group_id='1',
                                        policy_id='2',
                                        webhook_id='3',
                                        capability_hash='xxx')
        self.assertIn({
            'rel': 'capability',
            'href': '/v1.0/execute/1/xxx/'
        }, json_blob)

        json_blob = get_autoscale_links('11111',
                                        group_id='1',
                                        policy_id='2',
                                        webhook_id='3',
                                        capability_hash='xxx',
                                        capability_version="8")
        self.assertIn({
            'rel': 'capability',
            'href': '/v1.0/execute/8/xxx/'
        }, json_blob)
Beispiel #3
0
    def test_capability_url_included_with_capability_hash(self):
        """
        If a capability_hash parameter is passed in, an extra link is added to
        the JSON blob containing a capability URL.  But in the non-formatted
        URL, nothing changes.
        """
        pairs = [("group_id", "1"), ("policy_id", "2"), ("webhook_id", "3")]
        expected = [
            '/v1.0/11111/groups/1/', '/v1.0/11111/groups/1/policies/2/',
            '/v1.0/11111/groups/1/policies/2/webhooks/3/'
        ]
        for i in range(3):
            self.assertEqual(
                get_autoscale_links('11111',
                                    format=None,
                                    capability_hash='xxx',
                                    **dict(pairs[:(i + 1)])), expected[i])

            json_blob = get_autoscale_links('11111',
                                            capability_hash='xxx',
                                            **dict(pairs[:(i + 1)]))

            self.assertEqual(len(json_blob), 2)
            self.assertIn({
                'rel': 'capability',
                'href': '/v1.0/execute/1/xxx/'
            }, json_blob)
Beispiel #4
0
    def test_get_tenant_group_policy_and_webhook_id(self):
        """
        If the tenant ID, the group ID, the policy ID, and a webhook ID
        (not blank) are passed, the returned based link is
        /v<api>/<tenant>/groups/<group>/policies/<policy>/webhooks/<webhook>
        """
        self.assertEqual(
            get_autoscale_links('11111',
                                group_id='1',
                                policy_id="2",
                                webhook_id="3",
                                api_version='3',
                                format=None),
            '/v3/11111/groups/1/policies/2/webhooks/3/')

        expected_url = '/v1.0/11111/groups/1/policies/2/webhooks/3/'
        # test default API
        self.assertEqual(
            get_autoscale_links('11111',
                                group_id='1',
                                policy_id="2",
                                webhook_id="3",
                                format=None), expected_url)
        # test JSON formatting
        self.assertEqual(
            get_autoscale_links('11111',
                                group_id='1',
                                policy_id="2",
                                webhook_id="3"),
            self._expected_json(expected_url))
Beispiel #5
0
 def _format_output(result):
     uuid = result['id']
     result["state"] = format_state_dict(result["state"])
     request.setHeader(
         "Location", get_autoscale_links(self.tenant_id, uuid, format=None))
     result["links"] = get_autoscale_links(self.tenant_id, uuid)
     linkify_policy_list(result['scalingPolicies'], self.tenant_id, uuid)
     result['scalingPolicies_links'] = get_policies_links(
         result['scalingPolicies'], self.tenant_id, uuid, rel='policies')
     return {"group": result}
 def test_capability_urls_unicode_escaped(self):
     """
     Even if unicode path bits are provided, only bytes urls are returned
     """
     self.assertTrue(isinstance(
         get_autoscale_links(u'11111', group_id=u'1', policy_id=u'2',
                             format=None),
         str))
     snowman = get_autoscale_links('☃', group_id='☃', format=None)
     self.assertEqual(snowman, '/v1.0/%E2%98%83/groups/%E2%98%83/')
     self.assertTrue(isinstance(snowman, str))
 def test_get_only_groups_link_for_varying_other_args(self):
     """
     So long as the group ID is not a valid number, we still get the groups
     link /v<api>/<tenant>/groups/
     """
     equivalents = [
         get_autoscale_links('11111', group_id='', format=None),
         get_autoscale_links('11111', policy_id='5', format=None),
         get_autoscale_links('11111', policy_id='', format=None)
     ]
     for equivalent in equivalents:
         self.assertEqual(equivalent, '/v1.0/11111/groups/')
Beispiel #8
0
 def test_get_only_groups_link_for_varying_other_args(self):
     """
     So long as the group ID is not a valid number, we still get the groups
     link /v<api>/<tenant>/groups/
     """
     equivalents = [
         get_autoscale_links('11111', group_id='', format=None),
         get_autoscale_links('11111', policy_id='5', format=None),
         get_autoscale_links('11111', policy_id='', format=None)
     ]
     for equivalent in equivalents:
         self.assertEqual(equivalent, '/v1.0/11111/groups/')
Beispiel #9
0
 def test_capability_urls_unicode_escaped(self):
     """
     Even if unicode path bits are provided, only bytes urls are returned
     """
     self.assertTrue(
         isinstance(
             get_autoscale_links(u'11111',
                                 group_id=u'1',
                                 policy_id=u'2',
                                 format=None), str))
     snowman = get_autoscale_links('☃', group_id='☃', format=None)
     self.assertEqual(snowman, '/v1.0/%E2%98%83/groups/%E2%98%83/')
     self.assertTrue(isinstance(snowman, str))
Beispiel #10
0
 def _format_output(result):
     uuid = result['id']
     result["state"] = format_state_dict(result["state"])
     request.setHeader(
         "Location",
         get_autoscale_links(self.tenant_id, uuid, format=None))
     result["links"] = get_autoscale_links(self.tenant_id, uuid)
     linkify_policy_list(
         result['scalingPolicies'], self.tenant_id, uuid)
     result['scalingPolicies_links'] = get_policies_links(
         result['scalingPolicies'],
         self.tenant_id, uuid, rel='policies')
     return {"group": result}
 def test_get_groups_and_group_id_link_for_varying_other_args(self):
     """
     So long as the policy ID is None, we still get the groups
     link /v<api>/<tenant>/groups/<group_id>
     """
     equivalents = [
         get_autoscale_links('11111', group_id='1', webhook_id='1',
                             format=None),
         get_autoscale_links('11111', group_id='1', webhook_id='',
                             format=None),
     ]
     for equivalent in equivalents:
         self.assertEqual(equivalent, '/v1.0/11111/groups/1/')
Beispiel #12
0
 def format_policies_and_send_redirect(policy_list):
     request.setHeader(
         "Location",
         get_autoscale_links(self.tenant_id, self.scaling_group_id, "", format=None)
     )
     linkify_policy_list(policy_list, self.tenant_id, self.scaling_group_id)
     return {'policies': policy_list}
Beispiel #13
0
    def test_get_only_groups_link(self):
        """
        If only the tenant ID is passed, and the rest of the arguments are
        blank, then the returned base link is /v<api>/<tenant>/groups/
        """
        self.assertEqual(
            get_autoscale_links('11111', api_version='3', format=None),
            '/v3/11111/groups/')

        expected_url = '/v1.0/11111/groups/'
        # test default API
        self.assertEqual(get_autoscale_links('11111', format=None),
                         expected_url)
        # test JSON formatting
        self.assertEqual(get_autoscale_links('11111'),
                         self._expected_json(expected_url))
    def test_get_only_groups_link(self):
        """
        If only the tenant ID is passed, and the rest of the arguments are
        blank, then the returned base link is /v<api>/<tenant>/groups/
        """
        self.assertEqual(
            get_autoscale_links('11111', api_version='3', format=None),
            '/v3/11111/groups/')

        expected_url = '/v1.0/11111/groups/'
        # test default API
        self.assertEqual(get_autoscale_links('11111', format=None),
                         expected_url)
        # test JSON formatting
        self.assertEqual(get_autoscale_links('11111'),
                         self._expected_json(expected_url))
Beispiel #15
0
 def openstack_formatting(data, uuid):
     data["links"] = get_autoscale_links(self.tenant_id, uuid)
     data["state"] = format_state_dict(data["state"])
     linkify_policy_list(data["scalingPolicies"], self.tenant_id, uuid)
     data['scalingPolicies_links'] = get_policies_links(
         data['scalingPolicies'], self.tenant_id, uuid, rel='policies')
     return {"group": data}
Beispiel #16
0
 def test_get_groups_and_group_id_link_for_varying_other_args(self):
     """
     So long as the policy ID is None, we still get the groups
     link /v<api>/<tenant>/groups/<group_id>
     """
     equivalents = [
         get_autoscale_links('11111',
                             group_id='1',
                             webhook_id='1',
                             format=None),
         get_autoscale_links('11111',
                             group_id='1',
                             webhook_id='',
                             format=None),
     ]
     for equivalent in equivalents:
         self.assertEqual(equivalent, '/v1.0/11111/groups/1/')
    def test_capability_version(self):
        """
        There is a default capability version of 1, but whatever capability
        version is passed is the one used
        """
        # default version
        json_blob = get_autoscale_links(
            '11111', group_id='1', policy_id='2', webhook_id='3',
            capability_hash='xxx')
        self.assertIn({'rel': 'capability', 'href': '/v1.0/execute/1/xxx/'},
                      json_blob)

        json_blob = get_autoscale_links(
            '11111', group_id='1', policy_id='2', webhook_id='3',
            capability_hash='xxx', capability_version="8")
        self.assertIn({'rel': 'capability', 'href': '/v1.0/execute/8/xxx/'},
                      json_blob)
Beispiel #18
0
 def format_policies_and_send_redirect(policy_list):
     request.setHeader(
         "Location",
         get_autoscale_links(self.tenant_id,
                             self.scaling_group_id,
                             "",
                             format=None))
     linkify_policy_list(policy_list, self.tenant_id,
                         self.scaling_group_id)
     return {'policies': policy_list}
Beispiel #19
0
 def format_list(group_states):
     groups = [{
         'id': state.group_id,
         'links': get_autoscale_links(state.tenant_id, state.group_id),
         'state': format_state_dict(state)
     } for state in group_states]
     return {
         "groups": groups,
         "groups_links": get_groups_links(groups, self.tenant_id, None, **paginate)
     }
Beispiel #20
0
        def format_webhooks_and_send_redirect(webhook_list):
            request.setHeader(
                "Location",
                get_autoscale_links(self.tenant_id, self.group_id, self.policy_id, "", format=None)
            )

            webhook_list = [_format_webhook(webhook_model, self.tenant_id,
                                            self.group_id, self.policy_id)
                            for webhook_model in webhook_list]

            return {'webhooks': webhook_list}
Beispiel #21
0
 def openstack_formatting(results):
     data, active = results
     data["links"] = get_autoscale_links(self.tenant_id, self.group_id)
     data["state"] = format_state_dict(data["state"], active)
     linkify_policy_list(
         data["scalingPolicies"], self.tenant_id, self.group_id)
     data['scalingPolicies_links'] = get_policies_links(
         data['scalingPolicies'], self.tenant_id, self.group_id,
         rel='policies')
     if with_webhooks(request):
         add_webhooks_links(data["scalingPolicies"])
     return {"group": data}
Beispiel #22
0
 def format_list(results):
     group_states, actives = results
     groups = [{
         'id': state.group_id,
         'links': get_autoscale_links(state.tenant_id, state.group_id),
         'state': format_state_dict(state, active)
     } for state, active in zip(group_states, actives)]
     return {
         "groups": groups,
         "groups_links": get_groups_links(
             groups, self.tenant_id, None, **paginate)
     }
Beispiel #23
0
 def openstack_formatting(results):
     data, active = results
     data["links"] = get_autoscale_links(self.tenant_id, self.group_id)
     data["state"] = format_state_dict(data["state"], active)
     linkify_policy_list(
         data["scalingPolicies"], self.tenant_id, self.group_id)
     data['scalingPolicies_links'] = get_policies_links(
         data['scalingPolicies'], self.tenant_id, self.group_id,
         rel='policies')
     if with_webhooks(request):
         add_webhooks_links(data["scalingPolicies"])
     return {"group": data}
Beispiel #24
0
 def format_list(results):
     group_states, actives = results
     groups = [{
         'id': state.group_id,
         'links': get_autoscale_links(state.tenant_id, state.group_id),
         'state': format_state_dict(state, active)
     } for state, active in zip(group_states, actives)]
     return {
         "groups": groups,
         "groups_links": get_groups_links(
             groups, self.tenant_id, None, **paginate)
     }
    def test_get_tenant_id_and_group_id_and_policy_id(self):
        """
        If the tenant ID, the group ID, and a policy ID (not blank) are
        passed, the returned based link is
        /v<api>/<tenant>/groups/<group>/policies/<policy>
        """
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="5",
                                api_version='3', format=None),
            '/v3/11111/groups/1/policies/5/')

        expected_url = '/v1.0/11111/groups/1/policies/5/'
        # test default API
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="5",
                                format=None),
            expected_url)
        # test JSON formatting
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="5"),
            self._expected_json(expected_url))
    def test_get_tenant_group_policy_ids_and_blank_webhook_id(self):
        """
        If the tenant ID, the group ID, the policy ID, and an empty wbehook ID
        (not None) are passed, the returned based link is
        /v<api>/<tenant>/groups/<group>/policies/<policy>/webhooks
        """
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="2",
                                webhook_id="", api_version='3', format=None),
            '/v3/11111/groups/1/policies/2/webhooks/')

        expected_url = '/v1.0/11111/groups/1/policies/2/webhooks/'
        # test default API
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="2",
                                webhook_id="", format=None),
            expected_url)
        # test JSON formatting
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id="2",
                                webhook_id=""),
            self._expected_json(expected_url))
Beispiel #27
0
    def test_get_tenant_id_and_group_id_and_blank_policy_id(self):
        """
        If the tenant ID, the group ID, and an empty policy ID (not None) are
        passed, the returned based link is
        /v<api>/<tenant>/groups/<group>/policies
        """
        self.assertEqual(
            get_autoscale_links('11111',
                                group_id='1',
                                policy_id="",
                                api_version='3',
                                format=None), '/v3/11111/groups/1/policies/')

        expected_url = '/v1.0/11111/groups/1/policies/'
        # test default API
        self.assertEqual(
            get_autoscale_links('11111',
                                group_id='1',
                                policy_id="",
                                format=None), expected_url)
        # test JSON formatting
        self.assertEqual(
            get_autoscale_links('11111', group_id='1', policy_id=""),
            self._expected_json(expected_url))
Beispiel #28
0
def _format_webhook(webhook_model, tenant_id, group_id, policy_id,
                    webhook_id=None):
    """
    Take a webhook format that looks like
    :class:`otter.json_schema.model_schemas.view_webhook` and format it to
    instead look like :class:`otter.json_schema.rest_schemas.view_webhook`
    """
    if webhook_id is not None:
        webhook_model['id'] = webhook_id
    webhook_model['links'] = get_autoscale_links(
        tenant_id, group_id=group_id, policy_id=policy_id,
        webhook_id=webhook_model['id'],
        capability_hash=webhook_model['capability']['hash'],
        capability_version=webhook_model['capability']['version'])
    del webhook_model['capability']
    return webhook_model
Beispiel #29
0
        def format_webhooks_and_send_redirect(webhook_list):
            request.setHeader(
                "Location",
                get_autoscale_links(self.tenant_id,
                                    self.group_id,
                                    self.policy_id,
                                    "",
                                    format=None))

            webhook_list = [
                _format_webhook(webhook_model, self.tenant_id, self.group_id,
                                self.policy_id)
                for webhook_model in webhook_list
            ]

            return {'webhooks': webhook_list}
Beispiel #30
0
def _format_webhook(webhook_model,
                    tenant_id,
                    group_id,
                    policy_id,
                    webhook_id=None):
    """
    Take a webhook format that looks like
    :class:`otter.json_schema.model_schemas.view_webhook` and format it to
    instead look like :class:`otter.json_schema.rest_schemas.view_webhook`
    """
    if webhook_id is not None:
        webhook_model['id'] = webhook_id
    webhook_model['links'] = get_autoscale_links(
        tenant_id,
        group_id=group_id,
        policy_id=policy_id,
        webhook_id=webhook_model['id'],
        capability_hash=webhook_model['capability']['hash'],
        capability_version=webhook_model['capability']['version'])
    del webhook_model['capability']
    return webhook_model
Beispiel #31
0
def linkify_policy_list(policy_list, tenantId, groupId):
    """
    Takes list of policies and adds 'links'.
    """
    for policy in policy_list:
        policy['links'] = get_autoscale_links(tenantId, groupId, policy['id'])
Beispiel #32
0
 def openstackify(policy_dict):
     policy_dict['id'] = self.policy_id
     policy_dict['links'] = get_autoscale_links(self.tenant_id,
                                                self.scaling_group_id,
                                                self.policy_id)
     return {'policy': policy_dict}
Beispiel #33
0
 def openstackify(policy_dict):
     policy_dict['id'] = self.policy_id
     policy_dict['links'] = get_autoscale_links(self.tenant_id,
                                                self.scaling_group_id,
                                                self.policy_id)
     return {'policy': policy_dict}
Beispiel #34
0
def linkify_policy_list(policy_list, tenantId, groupId):
    """
    Takes list of policies and adds 'links'.
    """
    for policy in policy_list:
        policy['links'] = get_autoscale_links(tenantId, groupId, policy['id'])