BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1 | # ============LICENSE_START=============================================== |
| 2 | # Copyright (C) 2020 Nordix Foundation. All rights reserved. |
| 3 | # ======================================================================== |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============LICENSE_END================================================= |
| 16 | # |
| 17 | |
| 18 | # This script create/update policies spread over a number rics |
| 19 | # Intended for parallel processing |
| 20 | # Returns a string with result, either "0" for ok, or "1<fault description>" |
| 21 | |
| 22 | import os |
| 23 | import json |
| 24 | import sys |
| 25 | import requests |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 26 | import traceback |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 27 | |
| 28 | # disable warning about unverified https requests |
| 29 | from requests.packages import urllib3 |
| 30 | |
| 31 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 32 | |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 33 | #arg responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 34 | try: |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 35 | if len(sys.argv) != 11: |
| 36 | print("1Expected 10 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id") |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 37 | sys.exit() |
| 38 | |
| 39 | responsecode=int(sys.argv[1]) |
| 40 | baseurl=sys.argv[2] |
| 41 | ric_base=sys.argv[3] |
| 42 | num_rics=int(sys.argv[4]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 43 | uuid=sys.argv[5] |
| 44 | start=int(sys.argv[6]) |
| 45 | templatepath=sys.argv[7] |
| 46 | count=int(sys.argv[8]) |
| 47 | pids=int(sys.argv[9]) |
| 48 | pid_id=int(sys.argv[10]) |
| 49 | |
| 50 | if uuid == "NOUUID": |
| 51 | uuid="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 52 | |
| 53 | with open(templatepath, 'r') as file: |
| 54 | template = file.read() |
| 55 | |
| 56 | start=start |
| 57 | stop=count*num_rics+start |
| 58 | |
| 59 | for i in range(start,stop): |
| 60 | if (i%pids == (pid_id-1)): |
| 61 | payload=template.replace("XXX",str(i)) |
| 62 | ric_id=(i%num_rics)+1 |
| 63 | ric=ric_base+str(ric_id) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 64 | url=baseurl+"&id="+uuid+str(i)+"&ric="+str(ric) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 65 | try: |
| 66 | headers = {'Content-type': 'application/json'} |
| 67 | resp=requests.put(url, json.dumps(json.loads(payload)), headers=headers, verify=False, timeout=90) |
| 68 | except Exception as e1: |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 69 | print("1Put failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc()) |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 70 | sys.exit() |
| 71 | if (resp.status_code == None): |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 72 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+responsecode+", got: None") |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 73 | sys.exit() |
| 74 | if (resp.status_code != responsecode): |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 75 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+responsecode+", got: "+str(resp.status_code)) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 76 | sys.exit() |
| 77 | |
| 78 | print("0") |
| 79 | sys.exit() |
| 80 | |
| 81 | except Exception as e: |
| 82 | print("1"+str(e)) |
| 83 | sys.exit() |