Пример #1
0
    def initial_request(self, uri, options):
        """Performs the initial POST request, to create the boot resource."""
        # Bundle things up ready to throw over the wire.
        body, headers = self.prepare_initial_payload(options.data)

        # Headers are returned as a list, but they must be a dict for
        # the signing machinery.
        headers = dict(headers)

        # Sign request if credentials have been provided.
        if self.credentials is not None:
            self.sign(uri, headers, self.credentials)

        # Use httplib2 instead of urllib2 (or MAASDispatcher, which is based
        # on urllib2) so that we get full control over HTTP method. TODO:
        # create custom MAASDispatcher to use httplib2 so that MAASClient can
        # be used.
        response, content = http_request(uri,
                                         self.method,
                                         body=body,
                                         headers=headers,
                                         insecure=options.insecure)

        # 2xx status codes are all okay.
        if response.status // 100 != 2:
            if options.debug:
                utils.dump_response_summary(response)
            utils.print_response_content(response, content)
            raise CommandError(2)
        return content
Пример #2
0
 def test__writes_binary_response(self):
     # Non-textual response content is written to the output stream
     # using write(), so it carries no trailing newline, even if
     # stdout is connected to a tty
     response = httplib2.Response({
         "content": b"Lorem ipsum dolor sit amet.",
         "content-type": "image/jpeg",
     })
     buf = io.BytesIO()
     self.patch(buf, "isatty").return_value = True
     utils.print_response_content(response, response["content"], buf)
     self.assertEqual(response["content"], buf.getvalue())
Пример #3
0
 def test__prints_textual_response_when_redirected(self):
     # If the response content is textual and sys.stdout is not connected
     # to a TTY, print_response_content() prints the response without a
     # trailing newline.
     response = httplib2.Response({
         "status": http.client.FOUND,
         "content": b"Lorem ipsum dolor sit amet.",
         "content-type": "text/unicode",
     })
     buf = io.BytesIO()
     utils.print_response_content(response, response["content"], buf)
     self.assertEqual(response["content"], buf.getvalue())
Пример #4
0
 def test__prints_textual_response_with_newline(self):
     # If the response content is textual and sys.stdout is connected to a
     # TTY, print_response_content() prints the response with a trailing
     # newline.
     response = httplib2.Response({
         "status": http.client.NOT_FOUND,
         "content": b"Lorem ipsum dolor sit amet.",
         "content-type": "text/unicode",
     })
     buf = io.BytesIO()
     self.patch(buf, "isatty").return_value = True
     utils.print_response_content(response, response["content"], buf)
     self.assertEqual(response["content"] + b"\n", buf.getvalue())
Пример #5
0
 def put_upload(self, upload_uri, data, insecure=False):
     """Send PUT method to upload data."""
     headers = {
         "Content-Type": "application/octet-stream",
         "Content-Length": "%s" % len(data),
     }
     if self.credentials is not None:
         self.sign(upload_uri, headers, self.credentials)
     # httplib2 expects the body to be file-like if its binary data
     data = BytesIO(data)
     response, content = http_request(
         upload_uri, "PUT", body=data, headers=headers, insecure=insecure
     )
     if response.status != 200:
         utils.print_response_content(response, content)
         raise CommandError(2)
Пример #6
0
 def test__prints_textual_response_with_success_msg(self):
     # When the response has a status code of 2XX, and the response body is
     # textual print_response_content() will print a success message to the
     # TTY.
     status_code = random.randrange(200, 300)
     response = httplib2.Response({
         'status': status_code,
         'content': b"Lorem ipsum dolor sit amet.",
         'content-type': 'text/unicode',
     })
     buf = io.BytesIO()
     self.patch(buf, 'isatty').return_value = True
     utils.print_response_content(response, response['content'], buf)
     self.assertEqual(
         b"Success.\n"
         b"Machine-readable output follows:\n" + response['content'] +
         b"\n", buf.getvalue())
Пример #7
0
    def __call__(self, options):
        # TODO: this is el-cheapo URI Template
        # <http://tools.ietf.org/html/rfc6570> support; use uritemplate-py
        # <https://github.com/uri-templates/uritemplate-py> here?
        uri = self.uri.format(**vars(options))

        # Bundle things up ready to throw over the wire.
        uri, body, headers = self.prepare_payload(self.op, self.method, uri,
                                                  options.data)

        # Headers are returned as a list, but they must be a dict for
        # the signing machinery.
        headers = dict(headers)

        # Sign request if credentials have been provided.
        if self.credentials is not None:
            self.sign(uri, headers, self.credentials)

        # Use httplib2 instead of urllib2 (or MAASDispatcher, which is based
        # on urllib2) so that we get full control over HTTP method. TODO:
        # create custom MAASDispatcher to use httplib2 so that MAASClient can
        # be used.
        insecure = options.insecure
        response, content = http_request(uri,
                                         self.method,
                                         body=body,
                                         headers=headers,
                                         insecure=insecure)

        # Compare API hashes to see if our version of the API is old.
        self.compare_api_hashes(self.profile, response)

        # Output.
        if options.debug:
            utils.dump_response_summary(response)
        utils.print_response_content(response, content)

        # 2xx status codes are all okay.
        if response.status // 100 != 2:
            raise CommandError(2)