push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / resources / mock / services / heattotosca / securityrulestoportconnection / securityrulestoportconnectiongetresource / inputfiles / register_status.py
1 #!/usr/local/bin/python2.7
2 """
3 This script is a combination of the AddComponentScript and the OnBoardingStatus
4 scripts. Depending on the arguments given, it will either post an
5 "addMachineCommand" or an "logOnBoardingInfo" request.
6 """
7 import argparse
8 import json
9 import netifaces
10 import requests
11 from socket import getfqdn
12 import sys
13 from time import time
14
15
16 parser = argparse.ArgumentParser()
17 parser.add_argument("scribe_ip", type=str,
18                     help="The IP where the Scribe can be reached.")
19 parser.add_argument("--component-type", type=str,
20                     help="The component type.", required=False)
21 parser.add_argument("--component-version", type=str,
22                     help="The component version.", required=False)
23 parser.add_argument("--stage", type=str, required=False)
24 parser.add_argument("--status", required=False,
25                     choices=["WARNING", "INFO", "ERROR", "OK", "FAILURE"])
26 parser.add_argument("--description", type=str, required=False)
27 args = parser.parse_args()
28
29 add_machine_ip = "http://{0}:8084/MaveriQManager/api/Inventory/addComponent".format(args.scribe_ip)
30 log_onboarding_info_ip = "http://{0}:8084/MaveriQManager/api/Inventory/logOnBoardingInfo".format(args.scribe_ip)
31 user = 'omniq'
32 password = 'radcom'
33
34 REGION = ""
35 TENANT = ""
36 CLUSTER_NAME = ""
37 VERSION_NUMBER = ""
38 PROBE_ID = ""
39 OAM_DIRECT_IP = ""
40 MACHINE_TYPE = args.component_type
41 MACHINE_NAME = getfqdn()
42 HEALTH_STATUS = {}
43 ADD_COMPONENT_BODY = {}
44
45
46 def read_metadata():
47     """Read the instance metadata"""
48     global REGION
49     global TENANT
50     global CLUSTER_NAME
51     global VERSION_NUMBER
52     global PROBE_ID
53     global OAM_DIRECT_IP
54     with open('/meta.js', 'r') as json_file:
55         json_data = json.loads(json_file.read())
56         TENANT = json_data["tenant"]
57         REGION = json_data["region"]
58         CLUSTER_NAME = json_data["cluster_name"]
59         VERSION_NUMBER = json_data["version_number"]
60         OAM_DIRECT_IP = json_data["oam_private_network_ip"]
61         if MACHINE_TYPE == 'vProbe':
62             PROBE_ID = json_data["probe_id"]
63
64
65 def build_health_json():
66     """Builds the actual health status"""
67     HEALTH_STATUS["Region"] = REGION
68     HEALTH_STATUS["Tenant"] = TENANT
69     HEALTH_STATUS["MachineType"] = args.component_type
70     HEALTH_STATUS["MachineName"] = getfqdn()
71     HEALTH_STATUS["MachineIP"] = OAM_DIRECT_IP
72     HEALTH_STATUS["Time"] = long(time())
73     HEALTH_STATUS["Description"] = args.description
74     HEALTH_STATUS["Status"] = args.status
75     HEALTH_STATUS["Stage"] = args.stage
76     return HEALTH_STATUS
77
78
79 def build_add_json():
80     """Builds the actual health status"""
81     ADD_COMPONENT_BODY["region"] = REGION
82     ADD_COMPONENT_BODY["tenant"] = TENANT
83     ADD_COMPONENT_BODY["componentType"] = args.component_type
84     ADD_COMPONENT_BODY["clusterName"] = CLUSTER_NAME
85     ADD_COMPONENT_BODY["componentVersionNumber"] = VERSION_NUMBER
86     ADD_COMPONENT_BODY["machineName"] = MACHINE_NAME
87     ADD_COMPONENT_BODY["machineNetworkInterfaces"] = []
88     ADD_COMPONENT_BODY["OAM_IP"] = OAM_DIRECT_IP
89
90     for interface in netifaces.interfaces():
91         ADD_COMPONENT_BODY["machineNetworkInterfaces"].append({"name": interface, "value": netifaces.ifaddresses(interface)[2][0]['addr']})
92
93     if PROBE_ID is not "":
94         ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\
95             CLUSTER_NAME + '_' + MACHINE_NAME + '_' + PROBE_ID
96     else:
97         ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\
98             CLUSTER_NAME + '_' + MACHINE_NAME
99     return ADD_COMPONENT_BODY
100
101
102 def send_postage(p_url, p_url_user, p_url_password, p_json_data):
103     json_header = {'Content-type': 'application/json'}
104     request = requests.post(p_url, json.dumps(p_json_data), json_header, auth=requests.auth.HTTPBasicAuth(p_url_user, p_url_password))
105     print request.status_code
106     if (request.status_code != 200):
107         sys.exit(1)
108     return request.status_code
109
110
111 def post_health():
112     read_metadata()
113     return send_postage(log_onboarding_info_ip, user, password,
114                         build_health_json())
115
116
117 def post_add_machine():
118     read_metadata()
119     return send_postage(add_machine_ip, user, password, build_add_json())
120
121 if args.stage is None and args.status is None and args.description is None:
122     print "adding machine"
123     print post_add_machine()
124 else:
125     print "logging health"
126     print post_health()