예제 #1
0
    def post_all(self):
        """Create Zone Import"""
        request = pecan.request
        response = pecan.response
        context = request.environ['context']
        if six.PY2:
            body = request.body
        else:
            body = request.body.decode('utf-8')

        if request.content_type != 'text/dns':
            raise exceptions.UnsupportedContentType(
                'Content-type must be text/dns')

        # Create the zone_import
        zone_import = self.central_api.create_zone_import(context, body)
        response.status_int = 202

        LOG.info(_LI("Created %(zone_import)s"), {'zone_import': zone_import})

        zone_import = DesignateAdapter.render('API_v2',
                                              zone_import,
                                              request=request)

        response.headers['Location'] = zone_import['links']['self']
        # Prepare and return the response body
        return zone_import
예제 #2
0
 def post_all(self):
     """ Create Zone """
     request = pecan.request
     response = pecan.response
     context = request.environ['context']
     if request.content_type == 'text/dns':
         return self._post_zonefile(request, response, context)
     elif request.content_type == 'application/json':
         return self._post_json(request, response, context)
     else:
         raise exceptions.UnsupportedContentType(
             'Content-type must be text/dns or application/json')
예제 #3
0
    def post_all(self):

        request = pecan.request
        response = pecan.response
        context = pecan.request.environ['context']

        policy.check('zone_import', context)

        if request.content_type != 'text/dns':
            raise exceptions.UnsupportedContentType(
                'Content-type must be text/dns')

        try:
            dnspython_zone = dnszone.from_text(
                request.body,
                # Don't relativize, otherwise we end up with '@' record names.
                relativize=False,
                # Dont check origin, we allow missing NS records (missing SOA
                # records are taken care of in _create_zone).
                check_origin=False)
            domain = dnsutils.from_dnspython_zone(dnspython_zone)
            domain.type = 'PRIMARY'

            for rrset in list(domain.recordsets):
                if rrset.type in ('NS', 'SOA'):
                    domain.recordsets.remove(rrset)

        except dnszone.UnknownOrigin:
            raise exceptions.BadRequest('The $ORIGIN statement is required and'
                                        ' must be the first statement in the'
                                        ' zonefile.')
        except dnsexception.SyntaxError:
            raise exceptions.BadRequest('Malformed zonefile.')

        zone = self.central_api.create_domain(context, domain)

        if zone['status'] == 'PENDING':
            response.status_int = 202
        else:
            response.status_int = 201

        zone = DesignateAdapter.render('API_v2', zone, request=request)

        zone['links']['self'] = '%s/%s/%s' % (self.BASE_URI, 'v2/zones',
                                              zone['id'])

        response.headers['Location'] = zone['links']['self']

        return zone
예제 #4
0
    def body_dict(self):
        """
        Returns the body content as a dictonary, deserializing per the
        Content-Type header.

        We add this method to ease future XML support, so the main code
        is not hardcoded to call pecans "request.json()" method.
        """
        if self.content_type in JSON_TYPES:
            try:
                return jsonutils.load(self.body_file)
            except ValueError as valueError:
                raise exceptions.InvalidJson(six.text_type(valueError))
        else:
            raise exceptions.UnsupportedContentType(
                'Content-type must be application/json')
예제 #5
0
    def post_all(self):
        """Create ZoneImport"""
        request = pecan.request
        response = pecan.response
        context = request.environ['context']
        body = request.body

        if request.content_type != 'text/dns':
            raise exceptions.UnsupportedContentType(
                'Content-type must be text/dns')

        # Create the zone_import
        zone_import = self.central_api.create_zone_import(context, body)
        response.status_int = 202

        zone_import = ZoneImportAPIv2Adapter.render('API_v2',
                                                    zone_import,
                                                    request=request)

        response.headers['Location'] = zone_import['links']['self']
        # Prepare and return the response body
        return zone_import
예제 #6
0
    def body_dict(self):
        """
        Returns the body content as a dictionary, deserializing per the
        Content-Type header.

        We add this method to ease future XML support, so the main code
        is not hardcoded to call pecans "request.json" method.
        """
        if self.content_type in JSON_TYPES:
            try:
                json_dict = jsonutils.load(self.body_file)
                if json_dict is None:
                    # NOTE(kiall): Somehow, json.load(fp) is returning None.
                    raise exceptions.EmptyRequestBody('Request Body is empty')
                return json_dict
            except ValueError as valueError:
                if len(self.body) == 0 or self.body is None:
                    raise exceptions.EmptyRequestBody('Request Body is empty')
                else:
                    raise exceptions.InvalidJson(six.text_type(valueError))
        else:
            raise exceptions.UnsupportedContentType(
                'Content-type must be application/json')
예제 #7
0
    def _validate_content_type(self):
        if (self.method in ['POST', 'PUT', 'PATCH']
                and self.mimetype != 'application/json'):

            msg = 'Unsupported Content-Type: %s' % self.mimetype
            raise exceptions.UnsupportedContentType(msg)