Example #1
0
def publish_proc_events_filtered(result, jp_expr, expt_value, debug=False):
    if jp_expr is not None:
        if not isinstance(jp_expr, list):
            jp_expr = [jp_expr]
        for expr in jp_expr:
            if callable(expt_value):
                _pass = expt_value(jp.match1(f"$[?({expr})]", [result]))
            else:
                _pass = (jp.match1(f"$[?({expr})]", [result]) == expt_value)
            if not _pass:
                return

    data = result

    event_info = data["event_info"]
    result_subject = subject_factory(data[RuleConst.RULENAME],
                                     event_info=event_info)

    if debug and result["type"] != RULE_PROC_EVENT:
        dispatch_policy = DispatchPolicyConst.NEVER
    else:
        dispatch_policy = DispatchPolicyConst.DIRECT
    event_router_factory().route(RULE_PROC_EVENT,
                                 result_subject,
                                 data,
                                 dispatch_policy=dispatch_policy)
Example #2
0
    def create_issue(self, issue_type_Id, project_Id, summary, description):
        end_point = "rest/api/2/issue"
        payload = {
            "fields": {
                "project": {
                    "id": project_Id
                },
                "summary": summary,
                "description": description,
                "issuetype": {
                    "id": issue_type_Id
                }
            }
        }
        response = requests.post(self.url + end_point,
                                 auth=(self.user, self.token),
                                 json=payload)

        if self.is_json(response.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(response.text)
            # PRINT RESPONSE: pretty print with 4 indent
            print(json.dumps(json_result, indent=4, sort_keys=True))

            issue_Key = jp.match1("key", json_result)
            issue_Id = jp.match1("id", json_result)

            print("Issure create with key {} and ID {}".format(
                issue_Key, issue_Id))
            return (issue_Key, issue_Id)
Example #3
0
    def get_cycle_with_name(self, version_Id, project_Id, cycle_name):
        end_point = "cycles/search"
        canonical_path = 'GET&' + self.RELATIVE_PATH + end_point + '&expand=executionSummaries&projectId=' + project_Id + '&versionId=' + version_Id

        token = self.jwt.generate_jwt(canonical_path)

        # REQUEST HEADER: to create cycle
        headers = {
            'Authorization': 'JWT ' + token,
            'Content-Type': 'text/plain',
            'zapiAccessKey': self.access_key
        }

        print("Getting list of Cycles")
        raw_result = requests.get(self.ZAPI_CLOUD_URL + self.RELATIVE_PATH +
                                  end_point + '?versionId=' + version_Id +
                                  '&expand=executionSummaries' +
                                  '&projectId=' + project_Id,
                                  headers=headers)

        if self.is_json(raw_result.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(raw_result.text)

            # PRINT RESPONSE: pretty print with 4 indent
            # print(json.dumps(json_result, indent=4, sort_keys=True))

            #Get cycle id with name
            cycle_Id = jp.match1("$[?(name='" + cycle_name + "')].id",
                                 json_result)
            print(cycle_Id)
            return cycle_Id
Example #4
0
    def create_cycle(self, cycle_name, project_id, version_id):

        end_point = 'cycle?expand=executionSummaries&clonedCycleId='
        canonical_path = 'POST&' + self.RELATIVE_PATH + 'cycle' + '&clonedCycleId=&expand=executionSummaries'
        token = self.jwt.generate_jwt(canonical_path)

        # REQUEST HEADER: to create cycle
        headers = {
            'Authorization': f"JWT {token}",
            'Content-Type': 'application/json',
            'zapiAccessKey': self.access_key
        }

        # REQUEST PAYLOAD: to create cycle
        cycle = {
            'name': cycle_name,
            'projectId': project_id,
            'versionId': version_id
        }

        # MAKE REQUEST:
        print("Creating Cycle")
        raw_result = requests.post(self.ZAPI_CLOUD_URL + self.RELATIVE_PATH +
                                   end_point,
                                   headers=headers,
                                   json=cycle)
        json_result = tools.get_json_results(raw_result)

        if json_result:
            cycle_id = jp.match1("id", json_result)
            print(f"Cycle created with ID: {cycle_id}")
            return cycle_id
        else:
            raise Exception(f"An error was detected. Details in: {raw_result}")
Example #5
0
def check_string_response(response, json_path_value):
    '''Function will check string in response['response']'''
    response = response.json()
    response = json.loads(response['response'])

    for jsonpath, expected_value in json_path_value.items():
        assert expected_value == jp.match1(jsonpath, response)
Example #6
0
    def get_cycle_with_name(self, version_id, project_id, cycle_name):
        end_point = "cycles/search"
        canonical_path = f"GET&{self.RELATIVE_PATH}{end_point}&expand=executionSummaries&projectId={project_id}" \
                         f"&versionId={version_id}"

        token = self.jwt.generate_jwt(canonical_path)

        # REQUEST HEADER: to create cycle
        headers = {
            'Authorization': f"JWT {token}",
            'Content-Type': 'text/plain',
            'zapiAccessKey': self.access_key
        }

        print("Getting list of Cycles")
        raw_result = requests.get(
            f"{self.ZAPI_CLOUD_URL}{self.RELATIVE_PATH}{end_point}?versionId={version_id}"
            f"&expand=executionSummaries&projectId={project_id}",
            headers=headers)

        json_result = tools.get_json_results(raw_result)

        if json_result:
            cycle_id = jp.match1("$[?(name='" + cycle_name + "')].id",
                                 json_result)
            print(cycle_id)
            return cycle_id
        else:
            raise Exception(f"An error was detected. Details in: {raw_result}")
def publish_proc_events_filtered(result, jp_expr, expt_value):

    if callable(expt_value):
        _pass = expt_value(jp.match1(jp_expr, result))
    else:
        _pass = (jp.match1(jp_expr, result) == expt_value)
    if not _pass:
        return

    data = result
    # data = json.loads(json.dumps(result, cls=_JSONEncoder).encode("utf-8"))

    event_info = data["event_info"]
    result_subject = subject_factory(data[RuleConst.RULENAME], event_info=event_info)

    event_router_factory().route(
        RULE_PROC_EVENT, result_subject, data, dispatch_policy=DispatchPolicyConst.DIRECT
    )
Example #8
0
    def process(self, latitude, longitude):

        action = getattr(self, self.method)
        logger.info(f"Processing {self.name}: {self.method}")
        data = action(latitude, longitude)
        logger.info(f"Result: {data.status_code}")
        if data.status_code == requests.codes.ok:
            answer = float(jp.match1(self.path, data.json()))
            if not answer:
                raise ValueError("Didn't found a correct value or unknown answer")
                logger.error(f"{self.name} error no correct value found")
        return answer
Example #9
0
    def update_execution(self, execution_id, project_id, version_id, cycle_id,
                         issue_id, status):

        end_point = 'execution/'
        canonical_path = f"PUT&{self.RELATIVE_PATH}{end_point}{execution_id}&"

        token = self.jwt.generate_jwt(canonical_path)

        headers = {
            'Authorization': f'JWT {token}',
            'Content-Type': 'application/json',
            'zapiAccessKey': self.access_key
        }

        # REQUEST PAYLOAD: to update execution
        update_test = {
            'status': {
                "id": status
            },
            'issueId': int(issue_id),
            'projectId': int(project_id),
            'versionId': int(version_id),
            'cycleId': cycle_id
        }
        counter = 0
        status_flag = 0
        raw_result = None

        while status_flag != 200 and counter < 3:
            raw_result = requests.put(self.ZAPI_CLOUD_URL +
                                      self.RELATIVE_PATH + end_point +
                                      execution_id,
                                      headers=headers,
                                      json=update_test)
            status_flag = raw_result.status_code
            counter = counter + 1

        # print(raw_result.text)
        if raw_result:
            if tools.is_json(raw_result.text):

                # JSON RESPONSE: convert response to JSON
                json_result = json.loads(raw_result.text)
                status_code = jp.match1("$.execution.status.id", json_result)
                assert status_code == status
                print("Test run updated successfully for issue id {}".format(
                    issue_id))
                # PRINT RESPONSE: pretty print with 4 indent
                # print(json.dumps(json_result, indent=4, sort_keys=True))
        else:
            print("The request didn't run")
Example #10
0
    def get_issue_id_with_issue_name(self, issue_name):
        end_point = f"/rest/api/latest/issue/{issue_name}"
        response = requests.get(self.url + end_point,
                                auth=(self.user, self.token))
        assert response.status_code == 200

        if tools.is_json(response.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(response.text)

            # PRINT RESPONSE: pretty print with 4 indent
            # print(json.dumps(json_result, indent=4, sort_keys=True))
            issue_id = jp.match1("id", json_result)
            print(f"Issue id for key {issue_name} is:{issue_id}")
            return issue_id
Example #11
0
    def get_issue_id_with_key(self, key):
        end_point = '/rest/api/2/search'
        response = requests.get(self.url + end_point,
                                auth=(self.user, self.token))
        assert response.status_code == 200
        if self.is_json(response.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(response.text)

            # PRINT RESPONSE: pretty print with 4 indent
            # print(json.dumps(json_result, indent=4, sort_keys=True))
            issue_id = jp.match1("$.issues[?(key='" + key + "')].id",
                                 json_result)
            print("Issue id for key {} is:{}".format(key, issue_id))
            return issue_id
Example #12
0
    def get_project_id_with_name(self, project_name):

        end_point = "rest/api/2/project"
        response = requests.get(self.url + end_point,
                                auth=(self.user, self.token))
        assert response.status_code == 200

        if tools.is_json(response.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(response.text)

            # PRINT RESPONSE: pretty print with 4 indent
            # print(json.dumps(json_result, indent=4, sort_keys=True))

            project_id = jp.match1("$[?(name='" + project_name + "')].id",
                                   json_result)
            print(f"Project ID {project_id} with Name {project_name}")
            return project_id
Example #13
0
    def get_version_id_with_name(self, version_name, project_id):

        end_point = "rest/api/2/project/" + project_id + "/version"
        response = requests.get(self.url + end_point,
                                auth=(self.user, self.token))
        assert response.status_code == 200
        if self.is_json(response.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(response.text)

            # PRINT RESPONSE: pretty print with 4 indent
            # print(json.dumps(json_result, indent=4, sort_keys=True))

            version_id = jp.match1(
                "$.values[?(name='" + version_name + "')].id", json_result)
            print("Version ID {} with Name {}".format(version_id,
                                                      version_name))
            return version_id
Example #14
0
    def update_execution(self, execution_Id, project_Id, version_Id, cycle_Id,
                         issue_Id, status):

        end_point = 'execution/'
        canonical_path = 'PUT&' + self.RELATIVE_PATH + end_point + execution_Id + '&'

        token = self.jwt.generate_jwt(canonical_path)

        headers = {
            'Authorization': 'JWT ' + token,
            'Content-Type': 'application/json',
            'zapiAccessKey': self.access_key
        }

        # REQUEST PAYLOAD: to update execution
        updateTest = {
            'status': {
                "id": status
            },
            'issueId': int(issue_Id),
            'projectId': int(project_Id),
            'versionId': int(version_Id),
            'cycleId': cycle_Id
        }
        counter = 0
        status_flag = 0
        while (status_flag != 200 and counter < 3):
            raw_result = requests.put(self.ZAPI_CLOUD_URL +
                                      self.RELATIVE_PATH + end_point +
                                      execution_Id,
                                      headers=headers,
                                      json=updateTest)
            status_flag = raw_result.status_code
            counter = counter + 1

        #print(raw_result.text)
        if self.is_json(raw_result.text):

            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(raw_result.text)
            status_code = jp.match1("$.execution.status.id", json_result)
            assert status_code == status
            print("Test run updated successfully for issue id {}".format(
                issue_Id))
Example #15
0
    def create_cycle(self, cycle_name, project_Id, version_Id):

        end_point = 'cycle?expand=executionSummaries&clonedCycleId='
        canonical_path = 'POST&' + self.RELATIVE_PATH + 'cycle' + '&clonedCycleId=&expand=executionSummaries'
        token = self.jwt.generate_jwt(canonical_path)

        # REQUEST HEADER: to create cycle
        headers = {
            'Authorization': 'JWT ' + token,
            'Content-Type': 'application/json',
            'zapiAccessKey': self.access_key
        }

        # REQUEST PAYLOAD: to create cycle
        cycle = {
            'name': cycle_name,
            'projectId': project_Id,
            'versionId': version_Id
        }

        # MAKE REQUEST:
        print("Creating Cycle")
        raw_result = requests.post(self.ZAPI_CLOUD_URL + self.RELATIVE_PATH +
                                   end_point,
                                   headers=headers,
                                   json=cycle)
        if self.is_json(raw_result.text):
            # JSON RESPONSE: convert response to JSON
            json_result = json.loads(raw_result.text)

            # PRINT RESPONSE: pretty print with 4 indent
            #print(json.dumps(json_result, indent=4, sort_keys=True))

            #Get cycle ID
            cycle_id = jp.match1("id", json_result)
            print("Cycle created with ID: {}".format(cycle_id))
            return cycle_id
Example #16
0
def get_value_path():
    return jp.match1("$.[0].mappings[0].source", TOPICS)
Example #17
0
 def _get_path_id(self, path):
     return jp.match1(path + ".id", self.schema)
Example #18
0
                        ## do something when success...,
                        # eg: Route("on-extapi-post.success", payload=resp.json())
                    ))
            ]
        }
    },
    """
    Manage exception
    """,
    {
        rulename: "on-do-extapi-post-errors",
        subscribe_to: "{}-errors".format(os.environ["K_SERVICE"]),
        ruledata: {
            filters: [
                CheckPayload(
                    lambda x: x.get("rule_name") == "on-do-extapi-post" and jp.
                    match1("$.processing[*].exception", x
                           ) == "requests.exceptions.HTTPError" and jp.match1(
                               "$.processing[*].exc_extra_info.response_code",
                               x) == 503)
            ],
            processing: [
                Schedule(message="do-extapi-post",
                         payload=_(lambda _self: _self.payload["payload"]),
                         when=_(lambda _: (datetime.now() + timedelta(
                             seconds=30)).isoformat())),
            ]
        }
    }
]
Example #19
0
    print("EXAMPLE: myvm.json")
    exit(1)
filename = sys.argv[1]

# validate file existence
if not os.path.isfile(filename):
    print(
        "ERROR could not find json file with govc output {}".format(filename))
    exit(2)

# read file from disk
json_file = open(filename)
json_data = json.load(json_file)

# name of VM
print("name: {}".format(jp.match1("elements[*].Object.Config.Name",
                                  json_data)))
print("State: {}".format(
    jp.match1("elements[*].Object.Runtime.PowerState", json_data)))
print("OS: {}".format(
    jp.match1("elements[*].Object.Summary.Config.GuestFullName", json_data)))

# get path to vm, break apart
fullPath = jp.match1("elements[*].Path", json_data)
fullPath = fullPath[0:fullPath.rfind("/")]
pathsplit = fullPath.split("/")
print("Full path: {}".format(fullPath))
print("Parent Folder: {}".format(pathsplit[len(pathsplit) - 1]))

# show IP address and cidr/netmask
print("Default IpAddress: {}".format(
    jp.match1("elements[*].Object.Guest.IpAddress", json_data)))
Example #20
0
 def jpath_value(self, payload, path_expression):
     r = jp.match1(path_expression, payload)
     return r
Example #21
0
 def test_info_metric_list(self):
     resp = self.runner('info metric list')
     res = jp.match1('$.[*].Metric', resp)
     self.assertIsNotNone(res)
Example #22
0
# so this goes unused now
def showJSONValues(json_data, expr):
    jsonpath_expr = parse(expr)
    for match in jsonpath_expr.find(json_data):
        print(match.value)
    return


####### MAIN ##########################################################3

# read file from disk
json_file = open("squad.json")
json_data = json.load(json_file)

# show simple attribute, then values from array
print("Squad: {}".format(jp.match1("squadName", json_data)))
print("\nMembers:")
for name in jp.match("$.members[*].name", json_data):
    print("  {}".format(name))

# get all members, count length of returned list
print("\nCount members in list: {}".format(
    len(jp.match("$.members[*]", json_data))))

# use extensions to provide direct count of number of members in array
print("Count members using len extension: {}".format(
    jp.match1("$.members.`len`", json_data)))

# lookup array element given simple embedded element
lookFor = "Madame Uppercut"
print("\nPowers of {}".format(lookFor))
Example #23
0
def get_config_value(value):
    return jp.match1(value, CONFIG)
Example #24
0
                        # eg: Route("on-extapi-post.success", payload=resp.json())
                    ))
            ]
        }
    },
    """
    Manage exception
    """,
    {
        rulename: "on-do-extapi-post-errors",
        subscribe_to: "{}-errors".format(os.environ["K_SERVICE"]),
        ruledata: {
            filters: [
                IsTrue(lambda payload: payload.get(
                    "rule_name") == "on-do-extapi-post" and jp.match1(
                        "$.processing[*].exception", payload
                    ) == "requests.exceptions.HTTPError" and jp.match1(
                        "$.processing[*].exc_extra_info.response_code", payload
                    ) == 503)
            ],
            processing: [
                Schedule(message="do-extapi-post",
                         payload=lambda payload: payload["payload"],
                         when=lambda _:
                         (datetime.now() + timedelta(seconds=10)).isoformat(),
                         replace=True),
            ]
        }
    }
]
Example #25
0
import os

import paho.mqtt.client as mqtt
from jsonpath_rw import jsonpath
from jsonpath_rw_ext import parse

# for calling extended methods
import jsonpath_rw_ext as jp

CONFIG = json.loads(os.getenv("CONFIG"))
TOPICS = json.loads(os.getenv("INPUT"))

print(CONFIG)
print(TOPICS)

test = jp.match1("$.[0].mappings[0].source", TOPICS)

val0 = 0


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe(TOPICS[0]["name"])


def on_message(client, userdata, msg):
    global val0
    message = msg.payload.decode('utf8').replace('"{', '{').replace('"}', '}').replace('\\', '')
    js = json.loads(message)
    val = get_value(js)
    diff = val - val0
Example #26
0
def get_value(js):
    return jp.match1(get_value_path(), js)
 def process(instance, arg):
     return jp.match1(arg._expr, instance.payload)
Example #28
0
def check_jsonpath_value(response, json_path_value):
    '''Function will match json value in response using jsonpath '''
    print_message(response)
    for jsonpath, expected_value in json_path_value.items():
        #assert expected_value == jp.match1(jsonpath, response.json())
        print(expected_value, jp.match1(jsonpath, response.json()))
Example #29
0
            filters: [IsTrue(lambda payload: payload["got_errors"])],
            processing: [
                Route(
                    lambda subject: "{}-errors".format(subject.event_info()[
                        "source"]), lambda payload: payload["subject"],
                    lambda payload: payload)
            ],
        },
    },
    """
    Notify on slack
    """,
    {
        rulename: 'on-errors-notify',
        subscribe_to: TopicsDefault.RESULTS,
        ruledata: {
            filters: [IsTrue(lambda payload: payload["got_errors"])],
            processing: [
                SlackPublishMessage(
                    channel="errors",
                    text=lambda self: ":ambulance: *{}[{}]* \n```\n{}\n```".
                    format(
                        self.subject.event_info()["source"], self.
                        payload["rule_name"], "\n".join(
                            jp.match1("$.processing[*].exc_info", self.payload)
                        ))),
            ]
        }
    },
]
 def match(self, instance):
     return jp.match1(self._expr, instance.payload)
 def test_info_metric_list(self):
     resp = self.runner('info metric list')
     res = jp.match1('$.[*].Metric', resp)
     self.assertIsNotNone(res)