Python json.dumps() Examples

The following are 30 code examples of json.dumps(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module json , or try the search function .
Example #1
Source File: commands.py    From drydock with Apache License 2.0 8 votes vote down vote up
def task_builddata(ctx, task_id=None, output='yaml'):
    """Show builddata assoicated with ``task_id``."""
    if not task_id:
        ctx.fail('The task id must be specified by --task-id')

    task_bd = TaskBuildData(ctx.obj['CLIENT'], task_id=task_id).invoke()

    if output == 'json':
        click.echo(json.dumps(task_bd))
    else:
        if output != 'yaml':
            click.echo(
                'Invalid output format {}, defaulting to YAML.'.format(output))
        click.echo(
            yaml.safe_dump(
                task_bd, allow_unicode=True, default_flow_style=False)) 
Example #2
Source File: nodes.py    From drydock with Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp, hostname):
        try:
            latest = req.params.get('latest', 'false').upper()
            latest = True if latest == 'TRUE' else False

            node_bd = self.state_manager.get_build_data(
                node_name=hostname, latest=latest)

            if not node_bd:
                self.return_error(
                    resp,
                    falcon.HTTP_404,
                    message="No build data found",
                    retry=False)
            else:
                node_bd = [bd.to_dict() for bd in node_bd]
                resp.status = falcon.HTTP_200
                resp.body = json.dumps(node_bd)
                resp.content_type = falcon.MEDIA_JSON
        except Exception as ex:
            self.error(req.context, "Unknown error: %s" % str(ex), exc_info=ex)
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example #3
Source File: test_api_validation.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_href_error(self, setup_logging, input_files, falcontest):
        url = '/api/v1.0/validatedesign'
        hdr = {
            'Content-Type': 'application/json',
            'X-IDENTITY-STATUS': 'Confirmed',
            'X-USER-NAME': 'Test',
            'X-ROLES': 'admin'
        }
        body = {
            'rel': "design",
            'href': '',
            'type': "application/x-yaml",
        }

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        LOG.debug(result.text)
        assert result.status == falcon.HTTP_400 
Example #4
Source File: test_api_validation.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_invalid_post_resp(self, setup_logging, input_files, falcontest, drydock_state,
                               mock_get_build_data):
        input_file = input_files.join("invalid_validation.yaml")
        design_ref = "file://%s" % str(input_file)

        url = '/api/v1.0/validatedesign'
        hdr = {
            'Content-Type': 'application/json',
            'X-IDENTITY-STATUS': 'Confirmed',
            'X-USER-NAME': 'Test',
            'X-ROLES': 'admin'
        }
        body = {
            'rel': "design",
            'href': design_ref,
            'type': "application/x-yaml",
        }

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        assert result.status == falcon.HTTP_400 
Example #5
Source File: env.py    From indras_net with GNU General Public License v3.0 6 votes vote down vote up
def save_session(self, session_id=None):
        """
        Save the current session to a json file
        """
        try:
            base_dir = self.props["base_dir"]
        except:
            base_dir = ""

        if session_id is None:
            session_id = self.user.ask("Enter session id: ")
        session_id = str(session_id)

        json_output = str(json.dumps(self.to_json()))
        path = os.path.join(base_dir, "json/" + self.model_nm + session_id + ".json")
        with open(path, "w+") as f:
            f.write(json_output)

        #self.print_env()
        #self.user.tell("Session saved") 
Example #6
Source File: commands.py    From drydock with Apache License 2.0 6 votes vote down vote up
def node_list(ctx, output='table'):
    """List nodes."""
    nodelist = NodeList(ctx.obj['CLIENT']).invoke()

    if output == 'table':
        pt = PrettyTable()

        pt.field_names = [
            'Node Name', 'Status', 'CPUs', 'Memory', 'PXE MAC', 'Mgmt IP',
            'IPMI IP', 'Power State'
        ]

        for n in nodelist:
            pt.add_row([
                n['hostname'], n['status_name'], n['cpu_count'], n['memory'],
                n['boot_mac'], n['boot_ip'], n['power_address'],
                n['power_state']
            ])

        click.echo(pt)
    elif output == 'json':
        click.echo(json.dumps(nodelist)) 
Example #7
Source File: test_api_nodes_unit.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_post_nodes_resp(self, input_files, falcontest,
                             mock_process_node_filter):

        input_file = input_files.join("deckhand_fullsite.yaml")
        design_ref = "file://%s" % str(input_file)

        url = '/api/v1.0/nodefilter'
        hdr = self.get_standard_header()
        body = {
            'node_filter': 'filters',
            'design_ref': design_ref,
        }

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        LOG.debug(result.text)
        assert result.status == falcon.HTTP_200
        assert result.text.count('n1') == 1
        assert result.text.count('n2') == 1 
Example #8
Source File: request.py    From jumpserver-python-sdk with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, url, method='get', data=None, params=None,
                 headers=None, content_type='application/json', **kwargs):
        self.url = url
        self.method = method
        self.params = params or {}
        self.kwargs = kwargs

        if not isinstance(headers, dict):
            headers = {}
        self.headers = CaseInsensitiveDict(headers)
        if content_type:
            self.headers['Content-Type'] = content_type
        if data:
            self.data = json.dumps(data)
        else:
            self.data = {} 
Example #9
Source File: test_api_tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_create_task(self, falcontest, blank_state):
        url = '/api/v1.0/tasks'

        req_hdr = {
            'Content-Type': 'application/json',
            'X-IDENTITY-STATUS': 'Confirmed',
            'X-USER-NAME': 'Test',
            'X-ROLES': 'admin',
        }

        json_body = json.dumps({
            'action': 'verify_site',
            'design_ref': 'http://foo.com',
        })

        resp = falcontest.simulate_post(url, headers=req_hdr, body=json_body)

        assert resp.status == falcon.HTTP_201
        assert resp.headers.get('Location') is not None

    # TODO(sh8121att) Make this a general fixture in conftest.py 
Example #10
Source File: test_api_bootaction_status.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_bootaction_schema(self, falcontest, seed_bootaction_status):
        """Test that the API allows boot action status updates."""
        url = "/api/v1.0/bootactions/%s" % seed_bootaction_status['action_id']
        hdr = {
            'X-Bootaction-Key': "%s" % seed_bootaction_status['identity_key'],
            'Content-Type': 'application/json',
        }

        body = {
            'foo': 'Success',
        }

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        assert result.status == falcon.HTTP_400 
Example #11
Source File: ssh.py    From aegea with Apache License 2.0 6 votes vote down vote up
def get_kms_auth_token(session, bless_config, lambda_regional_config):
    logger.info("Requesting new KMS auth token in %s", lambda_regional_config["aws_region"])
    token_not_before = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
    token_not_after = token_not_before + datetime.timedelta(hours=1)
    token = dict(not_before=token_not_before.strftime("%Y%m%dT%H%M%SZ"),
                 not_after=token_not_after.strftime("%Y%m%dT%H%M%SZ"))
    encryption_context = {
        "from": session.resource("iam").CurrentUser().user_name,
        "to": bless_config["lambda_config"]["function_name"],
        "user_type": "user"
    }
    kms = session.client('kms', region_name=lambda_regional_config["aws_region"])
    res = kms.encrypt(KeyId=lambda_regional_config["kms_auth_key_id"],
                      Plaintext=json.dumps(token),
                      EncryptionContext=encryption_context)
    return base64.b64encode(res["CiphertextBlob"]).decode() 
Example #12
Source File: test_app.py    From hydrus with MIT License 6 votes vote down vote up
def test_object_PUT_at_id(self):
        """Create object in collection using PUT at specific ID."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                if "PUT" in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    put_response = self.client.put('{}/{}'.format(
                        endpoints[endpoint], uuid.uuid4()), data=json.dumps(dummy_object))
                    assert put_response.status_code == 201 
Example #13
Source File: test_app.py    From hydrus with MIT License 6 votes vote down vote up
def test_endpointClass_POST(self):
        """Check non collection Class POST."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint not in ["@context", "@id", "@type"]:
                class_name = "/".join(endpoints[endpoint].split(
                    "/{}/".format(self.API_NAME))[1:])
                if class_name not in self.doc.collections:
                    class_ = self.doc.parsed_classes[class_name]["class"]
                    class_methods = [
                        x.method for x in class_.supportedOperation]
                    if "POST" in class_methods:
                        dummy_object = gen_dummy_object(class_.title, self.doc)
                        post_response = self.client.post(
                            endpoints[endpoint], data=json.dumps(dummy_object))
                        assert post_response.status_code == 200 
Example #14
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_relabel_nodes(self, req, resp, json_data):
        """Create async task for relabel nodes."""
        action = json_data.get('action', None)

        if action != 'relabel_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_relabel_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #15
Source File: test_app.py    From hydrus with MIT License 6 votes vote down vote up
def test_object_PUT_at_ids(self):
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                data_ = {"data": list()}
                objects = list()
                ids = ""
                for index in range(3):
                    objects.append(gen_dummy_object(
                        collection.class_.title, self.doc))
                    ids = "{},".format(uuid.uuid4())
                data_["data"] = objects
                if "PUT" in class_methods:
                    put_response = self.client.put(
                        '{}/add/{}'.format(endpoints[endpoint], ids),
                        data=json.dumps(data_))
                    assert put_response.status_code == 201 
Example #16
Source File: nodes.py    From drydock with Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp):
        try:
            json_data = self.req_json(req)
            node_filter = json_data.get('node_filter', None)
            design_ref = json_data.get('design_ref', None)
            if design_ref is None:
                self.info(req.context,
                          'Missing required input value: design_ref')
                self.return_error(
                    resp,
                    falcon.HTTP_400,
                    message='Missing input required value: design_ref',
                    retry=False)
                return
            _, site_design = self.orchestrator.get_effective_site(design_ref)
            nodes = self.orchestrator.process_node_filter(
                node_filter=node_filter, site_design=site_design)
            resp_list = [n.name for n in nodes if nodes]

            resp.body = json.dumps(resp_list)
            resp.status = falcon.HTTP_200
        except Exception as ex:
            self.error(req.context, "Unknown error: %s" % str(ex), exc_info=ex)
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example #17
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_validate_design(self, req, resp, json_data):
        """Create async task for validate design."""
        action = json_data.get('action', None)

        if action != 'validate_design':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_validate_design"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #18
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_verify_site(self, req, resp, json_data):
        """Create async task for verify site."""
        action = json_data.get('action', None)

        if action != 'verify_site':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_verify_site"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #19
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_prepare_site(self, req, resp, json_data):
        """Create async task for prepare site."""
        action = json_data.get('action', None)

        if action != 'prepare_site':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_prepare_site"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #20
Source File: designs.py    From drydock with Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp, design_id):
        """Method Handler for GET design singleton.

        :param req: Falcon request object
        :param resp: Falcon response object
        :param design_id: UUID of the design resource
        """
        source = req.params.get('source', 'designed')

        try:
            design = None
            if source == 'compiled':
                design = self.orchestrator.get_effective_site(design_id)
            elif source == 'designed':
                design = self.orchestrator.get_described_site(design_id)

            resp.body = json.dumps(design.obj_to_simple())
        except errors.DesignError:
            self.error(req.context, "Design %s not found" % design_id)
            self.return_error(
                resp,
                falcon.HTTP_404,
                message="Design %s not found" % design_id,
                retry=False) 
Example #21
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_verify_nodes(self, req, resp, json_data):
        """Create async task for verify node."""
        action = json_data.get('action', None)

        if action != 'verify_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_verify_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #22
Source File: designs.py    From drydock with Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp):
        """Method handler for GET requests.

        :param req: Falcon request object
        :param resp: Falcon response object
        """
        state = self.state_manager

        try:
            designs = list(state.designs.keys())

            resp.body = json.dumps(designs)
            resp.status = falcon.HTTP_200
        except Exception as ex:
            self.error(req.context, "Exception raised: %s" % str(ex))
            self.return_error(
                resp,
                falcon.HTTP_500,
                message="Error accessing design list",
                retry=True) 
Example #23
Source File: daily_alert.py    From backtrader-cn with GNU General Public License v3.0 6 votes vote down vote up
def send_daily_alert():
    date = dt.datetime.now().strftime('%Y-%m-%d')
    msg = get_market_signal_by_date(date)

    # send notification via wechat
    wx_client = WeChatClient({
        'APP_ID': conf.WECHAT_APP_ID,
        'APP_SECRET': conf.WECHAT_APP_SECRET,
    })

    try:
        response = wx_client.send_all_text_message(
            json.dumps(msg, ensure_ascii=False))
        logger.debug(response)
    except Exception as e:
        logger.error(e, exc_info=True) 
Example #24
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_prepare_nodes(self, req, resp, json_data):
        """Create async task for prepare node."""
        action = json_data.get('action', None)

        if action != 'prepare_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_prepare_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #25
Source File: tasks.py    From drydock with Apache License 2.0 6 votes vote down vote up
def task_destroy_nodes(self, req, resp, json_data):
        """Create async task for destroy node."""
        action = json_data.get('action', None)

        if action != 'destroy_nodes':
            self.error(
                req.context,
                "Task body ended up in wrong handler: action %s in task_destroy_nodes"
                % action)
            self.return_error(
                resp, falcon.HTTP_500, message="Error", retry=False)

        try:
            task = self.create_task(json_data, req.context)
            resp.body = json.dumps(task.to_dict())
            resp.append_header('Location',
                               "/api/v1.0/tasks/%s" % str(task.task_id))
            resp.status = falcon.HTTP_201
        except errors.InvalidFormat as ex:
            self.error(req.context, ex.msg)
            self.return_error(
                resp, falcon.HTTP_400, message=ex.msg, retry=False) 
Example #26
Source File: test_app.py    From hydrus with MIT License 6 votes vote down vote up
def test_required_props(self):
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            if endpoint not in ["@context", "@id", "@type"]:
                class_name = "/".join(endpoints[endpoint].split(
                    "/{}/".format(self.API_NAME))[1:])
                if class_name not in self.doc.collections:
                    class_ = self.doc.parsed_classes[class_name]["class"]
                    class_methods = [
                        x.method for x in class_.supportedOperation]
                    if "PUT" in class_methods:
                        dummy_object = gen_dummy_object(class_.title, self.doc)
                        required_prop = ""
                        for prop in class_.supportedProperty:
                            if prop.required:
                                required_prop = prop.title
                                break
                        if required_prop:
                            del dummy_object[required_prop]
                            put_response = self.client.put(
                                endpoints[endpoint], data=json.dumps(dummy_object))
                            assert put_response.status_code == 400 
Example #27
Source File: health.py    From drydock with Apache License 2.0 5 votes vote down vote up
def get(self, req, resp):
        """
        Returns updated response with body if extended.
        """
        health_check = HealthCheck()
        # Test database connection
        try:
            now = self.state_manager.get_now()
            if now is None:
                raise Exception('None received from database for now()')
        except Exception:
            hcm = HealthCheckMessage(
                msg='Unable to connect to database', error=True)
            health_check.add_detail_msg(msg=hcm)

        # Test MaaS connection
        try:
            task = self.orchestrator.create_task(
                action=hd_fields.OrchestratorAction.Noop)
            maas_validation = ValidateNodeServices(task, self.orchestrator,
                                                   self.state_manager)
            maas_validation.start()
            if maas_validation.task.get_status() == ActionResult.Failure:
                raise Exception('MaaS task failure')
        except Exception:
            hcm = HealthCheckMessage(
                msg='Unable to connect to MaaS', error=True)
            health_check.add_detail_msg(msg=hcm)

        if self.extended:
            resp.body = json.dumps(health_check.to_dict())

        if health_check.is_healthy() and self.extended:
            resp.status = falcon.HTTP_200
        elif health_check.is_healthy():
            resp.status = falcon.HTTP_204
        else:
            resp.status = falcon.HTTP_503 
Example #28
Source File: commands.py    From drydock with Apache License 2.0 5 votes vote down vote up
def node_builddata(ctx, nodename, latest=True, output='yaml'):
    """List build data for ``nodename``."""
    node_bd = NodeBuildData(ctx.obj['CLIENT'], nodename, latest).invoke()

    if output == 'json':
        click.echo(json.dumps(node_bd))
    else:
        if output != 'yaml':
            click.echo(
                "Invalid output format {}, default to YAML.".format(output))
        click.echo(
            yaml.safe_dump(
                node_bd, allow_unicode=True, default_flow_style=False)) 
Example #29
Source File: test_api_bootaction_status.py    From drydock with Apache License 2.0 5 votes vote down vote up
def test_bootaction_status(self, falcontest, seed_bootaction_status):
        """Test that the API allows boot action status updates."""
        url = "/api/v1.0/bootactions/%s" % seed_bootaction_status['action_id']
        hdr = {
            'X-Bootaction-Key': "%s" % seed_bootaction_status['identity_key'],
            'Content-Type': 'application/json',
        }

        body = {
            'status': 'Success',
            'details': [
                {
                    'message': 'Test message.',
                    'error': True,
                },
            ]
        }

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        assert result.status == falcon.HTTP_200

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        assert result.status == falcon.HTTP_409 
Example #30
Source File: test_api_nodes_unit.py    From drydock with Apache License 2.0 5 votes vote down vote up
def test_input_error(self, falcontest):
        url = '/api/v1.0/nodefilter'
        hdr = self.get_standard_header()
        body = {}

        result = falcontest.simulate_post(
            url, headers=hdr, body=json.dumps(body))

        LOG.debug(result.text)
        assert result.status == falcon.HTTP_400