Exemplo n.º 1
0
 def test_valid_proxy(self, request):
     make_rest_api_call('GET',
                        'http://something.com/path/to/resource.txt',
                        proxy='http://localhost:3128')
     request.assert_called_with(
         'GET', 'http://something.com/path/to/resource.txt',
         headers=ANY,
         proxies={'https': 'http://localhost:3128',
                  'http': 'http://localhost:3128'},
         timeout=None)
 def test_valid_proxy(self, request):
     transports.make_rest_api_call(
         'GET',
         'http://something.com/path/to/resource.txt',
         proxy='http://localhost:3128')
     request.assert_called_with(
         'GET', 'http://something.com/path/to/resource.txt',
         headers=mock.ANY,
         proxies={'https': 'http://localhost:3128',
                  'http': 'http://localhost:3128'},
         timeout=None)
    def test_json(self, request):
        request().content = '{}'
        resp = transports.make_rest_api_call(
            'GET', 'http://something.com/path/to/resource.json')
        self.assertEqual(resp, {})
        request.assert_called_with(
            'GET', 'http://something.com/path/to/resource.json',
            headers=None,
            proxies=None,
            timeout=None)

        # Test JSON Error
        e = requests.HTTPError('error')
        e.response = mock.MagicMock()
        e.response.status_code = 404
        e.response.content = '''{
            "error": "description",
            "code": "Error Code"
        }'''
        request().raise_for_status.side_effect = e

        self.assertRaises(
            SoftLayer.SoftLayerAPIError,
            transports.make_rest_api_call,
            'GET',
            'http://something.com/path/to/resource.json')
Exemplo n.º 4
0
    def get(self, name, param=None):
        """Retreive a metadata attribute.

        :param string name: name of the attribute to retrieve. See `attribs`
        :param param: Required parameter for some attributes

        """
        if name not in self.attribs:
            raise exceptions.SoftLayerError('Unknown metadata attribute.')

        call_details = self.attribs[name]

        if call_details.get('param_req'):
            if not param:
                raise exceptions.SoftLayerError(
                    'Parameter required to get this attribute.')

        request = transports.Request()
        request.endpoint = self.url
        request.service = 'SoftLayer_Resource_Metadata'
        request.method = self.attribs[name]['call']
        request.transport_headers = {'User-Agent': consts.USER_AGENT}
        request.timeout = self.timeout
        request.identifier = param

        try:
            return transports.make_rest_api_call(request)
        except exceptions.SoftLayerAPIError as ex:
            if ex.faultCode == 404:
                return None
            raise ex
    def test_json(self, request):
        request().content = '{}'
        resp = transports.make_rest_api_call(
            'GET', 'http://something.com/path/to/resource.json')
        self.assertEqual(resp, {})
        request.assert_called_with(
            'GET', 'http://something.com/path/to/resource.json',
            headers=None,
            proxies=None,
            timeout=None)

        # Test JSON Error
        e = requests.HTTPError('error')
        e.response = mock.MagicMock()
        e.response.status_code = 404
        e.response.content = '''{
            "error": "description",
            "code": "Error Code"
        }'''
        request().raise_for_status.side_effect = e

        self.assertRaises(
            SoftLayer.SoftLayerAPIError,
            transports.make_rest_api_call,
            'GET',
            'http://something.com/path/to/resource.json')
Exemplo n.º 6
0
 def make_request(self, path):
     url = '/'.join([self.url, 'SoftLayer_Resource_Metadata', path])
     try:
         return make_rest_api_call('GET', url,
                                   http_headers={'User-Agent': USER_AGENT},
                                   timeout=self.timeout)
     except SoftLayerAPIError as e:
         if e.faultCode == 404:
             return None
         raise e
    def test_text(self, request):
        request().text = "content"
        resp = make_rest_api_call("GET", "http://something.com/path/to/resource.txt")
        self.assertEqual(resp, "content")
        request.assert_called_with("GET", "http://something.com/path/to/resource.txt", headers=None, timeout=None)

        # Test Text Error
        e = HTTPError("error")
        e.response = MagicMock()
        e.response.status_code = 404
        e.response.content = "Error Code"
        request().raise_for_status.side_effect = e

        self.assertRaises(SoftLayerAPIError, make_rest_api_call, "GET", "http://something.com/path/to/resource.txt")
Exemplo n.º 8
0
    def make_request(self, path):
        """ Make a request against the metadata service

        :param string path: path to the specific metadata resource
        """
        url = '/'.join([self.url, 'SoftLayer_Resource_Metadata', path])
        try:
            return make_rest_api_call('GET', url,
                                      http_headers={'User-Agent': USER_AGENT},
                                      timeout=self.timeout)
        except SoftLayerAPIError as ex:
            if ex.faultCode == 404:
                return None
            raise ex
Exemplo n.º 9
0
    def make_request(self, path):
        """ Make a request against the metadata service

        :param string path: path to the specific metadata resource
        """
        url = '/'.join([self.url, 'SoftLayer_Resource_Metadata', path])
        try:
            return transports.make_rest_api_call(
                'GET',
                url,
                http_headers={'User-Agent': consts.USER_AGENT},
                timeout=self.timeout)
        except exceptions.SoftLayerAPIError as ex:
            if ex.faultCode == 404:
                return None
            raise ex
Exemplo n.º 10
0
    def test_json(self, request):
        request().content = "{}"
        resp = make_rest_api_call("GET", "http://something.com/path/to/resource.json")
        self.assertEqual(resp, {})
        request.assert_called_with("GET", "http://something.com/path/to/resource.json", headers=None, timeout=None)

        # Test JSON Error
        e = HTTPError("error")
        e.response = MagicMock()
        e.response.status_code = 404
        e.response.content = """{
            "error": "description",
            "code": "Error Code"
        }"""
        request().raise_for_status.side_effect = e

        self.assertRaises(SoftLayerAPIError, make_rest_api_call, "GET", "http://something.com/path/to/resource.json")
Exemplo n.º 11
0
    def test_text(self, request):
        request().text = 'content'
        resp = transports.make_rest_api_call(
            'GET', 'http://something.com/path/to/resource.txt')
        self.assertEqual(resp, 'content')
        request.assert_called_with('GET',
                                   'http://something.com/path/to/resource.txt',
                                   headers=None,
                                   proxies=None,
                                   timeout=None)

        # Test Text Error
        e = requests.HTTPError('error')
        e.response = mock.MagicMock()
        e.response.status_code = 404
        e.response.content = 'Error Code'
        request().raise_for_status.side_effect = e

        self.assertRaises(SoftLayer.SoftLayerAPIError,
                          transports.make_rest_api_call, 'GET',
                          'http://something.com/path/to/resource.txt')
    def test_text(self, request):
        request().text = 'content'
        resp = make_rest_api_call(
            'GET', 'http://something.com/path/to/resource.txt')
        self.assertEqual(resp, 'content')
        request.assert_called_with(
            'GET', 'http://something.com/path/to/resource.txt',
            headers=None,
            timeout=None)

        # Test Text Error
        e = HTTPError('error')
        e.response = MagicMock()
        e.response.status_code = 404
        e.response.content = 'Error Code'
        request().raise_for_status.side_effect = e

        self.assertRaises(
            SoftLayerAPIError,
            make_rest_api_call,
            'GET',
            'http://something.com/path/to/resource.txt')