How to use the anchorecli.clients.apiexternal.add_policy function in anchorecli

To help you get started, we’ve selected a few anchorecli examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anchore / anchore-cli / anchorecli / cli / policy.py View on Github external
def add(input_policy):
    ecode = 0

    try:
        with open(input_policy, 'r') as FH:
            policybundle = json.loads(FH.read())

        ret = anchorecli.clients.apiexternal.add_policy(config, policybundle=policybundle, detail=True)
        ecode = anchorecli.cli.utils.get_ecode(ret)
        if ret['success']:
            print(anchorecli.cli.utils.format_output(config, 'policy_add', {}, ret['payload']))
        else:
            raise Exception(json.dumps(ret['error'], indent=4))

    except Exception as err:
        print(anchorecli.cli.utils.format_error_output(config, 'policy_add', {}, err))
        if not ecode:
            ecode = 2

    anchorecli.cli.utils.doexit(ecode)
github anchore / anchore-cli / anchorecli / clients / hub.py View on Github external
bundle = _fetch_bundle(config, bundlename=bundlename, auth=auth)

        if target_id:
            bundleid = target_id
        else:
            bundleid = bundle['name']
        bundle['id'] = bundleid

        if not force:
            ret = anchorecli.clients.apiexternal.get_policies(config)
            if ret['success']:
                for installed_policy in ret['payload']:
                    if installed_policy['policyId'] == bundleid:
                        raise Exception("Policy with ID ({}) already installed - use force to override or specify target unique ID".format(bundleid))

        ret = anchorecli.clients.apiexternal.add_policy(config, policybundle=bundle, detail=True)

    except Exception as err:
        ret['success'] = False
        ret['error'] = str(err)

    return(ret)