#!/usr/bin/env python
#
# Copyright (C) 2018 Layered Insight - All Rights Reserved
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential

from __future__ import print_function
import argparse
import sys
from pprint import pprint
import layint_runtime_api
from layint_runtime_api.rest import ApiException
from vyper import v
from li_utils.envtools import env_setup

# Parse CLI
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Verbose logging", action="store_true")
parser.add_argument("--containerid", help="ID of container to build behavioral policy from", required=True)
parser.add_argument("--merge-policyid", help="ID of policy to merge built behavioral rules into", required=False, default=None)
parser.add_argument("--log-action", help="types of logs with matching actions to generate rules", required=False, default="all")
args = parser.parse_args()

env_setup()

layint_runtime_api.configuration.api_key['Authorization'] = v.get("api_key")
container_api = layint_runtime_api.ContainerApi()
container_api.api_client.host = v.get("api_host")

if args.verbose:
    print("Requesting behavioral policy build from container %s on API host %s..." % (args.containerid, v.get("api_host")))
try:
    api_response = container_api.build_container_dossier_tempate(args.containerid, merge_policy_id=args.merge_policyid, log_action=args.log_action)
except ApiException as e:
    print("Exception when calling ConfigurationApi->add_config: %s\n" % e)
    sys.exit()

if args.verbose:
    pprint(api_response)

if args.verbose:
    print("Behavioral policy created, ID " + str(api_response.template_id))
