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)
def _format_output(result): uuid = result['id'] request.setHeader( "Location", get_autoscale_links(tenant_id, uuid, format=None)) result["links"] = get_autoscale_links(tenant_id, uuid) result["scalingPolicies"] = policy_dict_to_list( result["scalingPolicies"], tenant_id, uuid) return {"group": result}
def _format_output(result): uuid = result['id'] request.setHeader("Location", get_autoscale_links(tenant_id, uuid, format=None)) result["links"] = get_autoscale_links(tenant_id, uuid) result["scalingPolicies"] = policy_dict_to_list( result["scalingPolicies"], tenant_id, uuid) return {"group": result}
def format_policies_and_send_redirect(policy_dict): request.setHeader("Location", get_autoscale_links(tenant_id, scaling_group_id, "", format=None)) policy_list = [] for policy_uuid, policy_item in policy_dict.iteritems(): policy_item["id"] = policy_uuid policy_item["links"] = get_autoscale_links(tenant_id, scaling_group_id, policy_uuid) policy_list.append(policy_item) return {"policies": policy_list}
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 openstack_formatting(data, uuid): data["links"] = get_autoscale_links(tenant_id, uuid) policies = [] for policy_id, policy in data["scalingPolicies"].iteritems(): policy["id"] = policy_id policy["links"] = get_autoscale_links(tenant_id, uuid, policy_id) policies.append(policy) data["scalingPolicies"] = policies return {"group": data}
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/')
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 format_policies_and_send_redirect(policy_dict): request.setHeader( "Location", get_autoscale_links(tenantId, groupId, "", format=None) ) policy_list = [] for policy_uuid, policy_item in policy_dict.iteritems(): policy_item['id'] = policy_uuid policy_item['links'] = get_autoscale_links( tenantId, groupId, policy_uuid) policy_list.append(policy_item) return {'policies': policy_list}
def format_policies_and_send_redirect(policy_dict): request.setHeader( "Location", get_autoscale_links(tenant_id, scaling_group_id, "", format=None)) policy_list = [] for policy_uuid, policy_item in policy_dict.iteritems(): policy_item['id'] = policy_uuid policy_item['links'] = get_autoscale_links(tenant_id, scaling_group_id, policy_uuid) policy_list.append(policy_item) return {'policies': policy_list}
def format_state_dict(state): """ Takes a state returned by the model and reformats it to be returned as a response. :param state: a :class:`otter.models.interface.GroupState` object :return: a ``dict`` that looks like what should be respond by the API response when getting state """ return { 'activeCapacity': len(state.active), 'pendingCapacity': len(state.pending), 'desiredCapacity': len(state.active) + len(state.pending), 'paused': state.paused, 'id': state.group_id, 'links': get_autoscale_links(state.tenant_id, state.group_id), 'active': [{ 'id': key, 'links': server_blob['links'], } for key, server_blob in state.active.iteritems()] }
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_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)
def policy_dict_to_list(policy_dict, tenantId, groupId): """ Takes dictionary of policies mapping policy ids to the policy blobs, and transforms them into a list of dictionaries that contain the keys 'id' and 'links'. """ policy_list = [] for policy_uuid, policy_item in policy_dict.iteritems(): policy_item["id"] = policy_uuid policy_item["links"] = get_autoscale_links(tenantId, groupId, policy_uuid) policy_list.append(policy_item) return policy_list
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 format_webhooks_and_send_redirect(webhook_dict): request.setHeader( "Location", get_autoscale_links(tenant_id, group_id, policy_id, "", format=None) ) webhook_list = [] for webhook_id, webhook_model in webhook_dict.iteritems(): webhook_list.append( _format_webhook(webhook_id, webhook_model, tenant_id, group_id, policy_id)) return {'webhooks': webhook_list}
def policy_dict_to_list(policy_dict, tenantId, groupId): """ Takes dictionary of policies mapping policy ids to the policy blobs, and transforms them into a list of dictionaries that contain the keys 'id' and 'links'. """ policy_list = [] for policy_uuid, policy_item in policy_dict.iteritems(): policy_item['id'] = policy_uuid policy_item['links'] = get_autoscale_links(tenantId, groupId, policy_uuid) policy_list.append(policy_item) return policy_list
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))
def _format_webhook(webhook_id, webhook_model, tenant_id, group_id, policy_id): """ 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` """ webhook_model['id'] = webhook_id webhook_model['links'] = get_autoscale_links( tenant_id, group_id=group_id, policy_id=policy_id, webhook_id=webhook_id, capability_hash=webhook_model['capability']['hash'], capability_version=webhook_model['capability']['version']) del webhook_model['capability'] return webhook_model
def format_webhooks_and_send_redirect(webhook_dict): request.setHeader( "Location", get_autoscale_links(tenant_id, group_id, policy_id, "", format=None)) webhook_list = [] for webhook_id, webhook_model in webhook_dict.iteritems(): webhook_list.append( _format_webhook(webhook_id, webhook_model, tenant_id, group_id, policy_id)) return {'webhooks': webhook_list}
def format_state_dict(state): """ Takes a state returned by the model and reformats it to be returned as a response. :param state: a :class:`otter.models.interface.GroupState` object :return: a ``dict`` that looks like what should be respond by the API response when getting state """ return { 'activeCapacity': len(state.active), 'pendingCapacity': len(state.pending), 'desiredCapacity': len(state.active) + len(state.pending), 'paused': state.paused, 'id': state.group_id, 'links': get_autoscale_links(state.tenant_id, state.group_id), 'active': [ { 'id': key, 'links': server_blob['links'], } for key, server_blob in state.active.iteritems() ] }
def openstackify(policy_dict): policy_dict['id'] = policyId policy_dict['links'] = get_autoscale_links(tenantId, groupId, policyId) return {'policy': policy_dict}
def openstackify(policy_dict): policy_dict['id'] = policy_id policy_dict['links'] = get_autoscale_links(tenant_id, scaling_group_id, policy_id) return {'policy': policy_dict}
def openstackify(policy_dict): policy_dict["id"] = policy_id policy_dict["links"] = get_autoscale_links(tenant_id, scaling_group_id, policy_id) return {"policy": policy_dict}