I have below dictionary
a={
'set1': {'fileName': 'filename1',
'moduleName': 'modulename1',
'connection1.name': 'connection1',
'connection.test':'connectiontest1',
'connection2.name': 'connection2',
'connection.test':'connectiontest2',
'queue1.name': 'queue1',
'queue1.test':'queuetest1',
'topic1.name':'topic1',
'topic1.test':'topic1test',
'topic2.name':'topic2',
'topic2.test':'topic2test',
'ServerName': 'serverone',
'DeploymentName': 'deployment1'
},
'set2':{'fileName': 'filename2',
'moduleName': 'modulename2',
'connection1.name': 'connection1',
'connection.test':'connectiontest1',
'queue1.name': 'queue1',
'queue1.test':'queuetest1',
'topic1.name':'topic1',
'topic1.test':'topic1test',
'topic2.name':'topic2',
'topic2.test':'topic2test',
'ServerName': 'servertwo',
'DeploymentName': 'deployment2'
}}
in each set, I need to pass it to a function to create a server from the above dictionary.
def create_server_config(fileName,moduleName,connection,connectiontest,topic,topictest,queue,queuetest,servername,deploymentname):
create_queue(queue,queuetest)
create_topic(topic,topictest)
create_connection(connection,connectiontest)
In this I want to pass each set into a function as an argument So that it will create a server for each set, the problem here is queue,topic,connection has multiple increment values in each set. so anyone helps me with how to pass it to a function. for example, if I want to create a queue then under create_server_config function , need to pass all the queue and queuetest related to set1 dict.
I have a few questions for you. Based on that, the answer might change.
Below code assumes the following:
In Set1 & Set2, there are two connection.test keys. This may be a typo. I am assuming the first one is connection1.test and second one is connection2.test
connection1.name, queue1.name, and topic1.name will ALWAYS be present in each set
If topic2.name exists but connection2.name and queue2.name does not exist, then it is OK to pass connection1.name and queue1.name values to create server config.
Similarly, if any combination of connection2.name, connection2.test, queue2.name, queue2.test, topic2.name, topic2.test exists but the others do not exist, then the value from xxx1.name or xxx1.test will be used in its place where xxx is connection or queue or topic.
This code will call the create server config function for each value of the dict:
a={
'set1':{'fileName': 'filename1',
'moduleName': 'modulename1',
'connection1.name': 'connection1',
'connection1.test':'connectiontest1',
'connection2.name': 'connection2',
'connection2.test':'connectiontest2',
'queue1.name': 'queue1',
'queue1.test':'queuetest1',
'topic1.name':'topic1',
'topic1.test':'topic1test',
'topic2.name':'topic2',
'topic2.test':'topic2test',
'ServerName': 'serverone',
'DeploymentName': 'deployment1'
},
'set2':{'fileName': 'filename2',
'moduleName': 'modulename2',
'connection1.name': 'connection1',
'connection1.test':'connectiontest1',
'queue1.name': 'queue1',
'queue1.test':'queuetest1',
'topic1.name':'topic1',
'topic1.test':'topic1test',
'topic2.name':'topic2',
'topic2.test':'topic2test',
'ServerName': 'servertwo',
'DeploymentName': 'deployment2'
}
}
for s,setkey in a.items():
fileName = setkey['fileName']
moduleName = setkey['moduleName']
connection = setkey['connection1.name']
connectiontest = setkey['connection1.test']
topic = setkey['topic1.name']
topictest = setkey['topic1.test']
queue = setkey['queue1.name']
queuetest = setkey['queue1.test']
servername = setkey['ServerName']
deploymentname = setkey['DeploymentName']
print (fileName,moduleName, \
connection,connectiontest, \
topic,topictest,queue, \
queuetest,servername,deploymentname)
create_server_config(fileName,moduleName, \
connection,connectiontest, \
topic,topictest,queue, \
queuetest,servername,deploymentname)
flag = False
if 'connection2.name' in setkey:
flag = True
connection = setkey['connection2.name']
if 'connection2.test' in setkey:
flag = True
connectiontest = setkey['connection2.test']
if 'queue2.name' in setkey:
flag = True
queue = setkey['queue2.name']
if 'queue2.test' in setkey:
flag = True
queuetest = setkey['queue2.test']
if 'topic2.name' in setkey:
flag = True
topic = setkey['topic2.name']
if 'topic2.test' in setkey:
flag = True
topictest = setkey['topic2.test']
if flag:
print (fileName,moduleName, \
connection,connectiontest, \
topic,topictest,queue, \
queuetest,servername,deploymentname)
create_server_config(fileName,moduleName, \
connection,connectiontest, \
topic,topictest,queue, \
queuetest,servername,deploymentname)
Related
i have a dictionary with entries that have the ip and ports displayed like this
{'source': '192.168.4.1:80', 'destination': '168.20.10.1:443'}
but i want it to display it like
{'src_ip': '192.168.4.1', 'src_port': 80, 'dest_ip': '168.20.10.1', 'dest_port': 443}
so i want to split the first two entries into 4 new ones and delete the two old ones.
my code currently looks like this:
log entry = {'source': '192.168.4.1:80', 'destination': '168.20.10.1:443'}
def split_ip_port(log_entry):
u_source = log_entry['source']
if ':' in u_source:
src_list = u_source.split(':')
src_ip = src_list[0]
src_port = src_list[1]
log_entry.update({'src_ip': src_ip})
log_entry.update({'src_port': src_port})
del log_entry['source']
u_dest = log_entry['destination']
if ':' in u_dest:
dest_list = u_dest.split(':')
dest_ip = dest_list[0]
dest_port = dest_list[1]
print(dest_list)
log_entry.update({'dest_ip': dest_ip})
log_entry.update({'dest_port': dest_port})
del log_entry['destination']
return log_entry
when i try to test the source it gives me keyerror :'destination' and when i try to test the destination it gives me keyerror source. what is happening here?
When you split value (e.g., log_entry['source'].split(":") ) it returns list ['192.168.4.1','80']. Then you have to return value by index from list, [0] index in list is '192.168.4.1'. Then you have to assign it to new key in your dict, log_entry['src_ip']
log_entry['src_ip'] = log_entry['source'].split(":")[0]
log_entry['src_port'] = log_entry['source'].split(":")[1]
log_entry['dest_ip'] = log_entry['destination'].split(":")[0]
log_entry['dest_port'] = log_entry['destination'].split(":")[1]
del log_entry['source']
del log_entry['destination']
Since the original code work. Here just an offer to simplify the original code - you could try to split the source/destination and ports then just create a new dictionary like this way:
orig_dc = {'source': '192.168.4.1:80', 'destination': '168.20.10.1:443'}
new_dc = {}
for k, v in orig_dc.items():
orig, port = v.split(':')
if k in 'source':
new_dc.setdefault('src_ip', orig)
new_dc.setdefault('src_port', int(port))
else:
new_dc.setdefault('dest_ip', orig)
new_dc.setdefault('dest_port', int(port))
expected = { 'src_ip': '192.168.4.1', 'src_port': 80,
'dest_ip': '168.20.10.1', 'dest_port': 443}
assert new_dc == expected
Is there a way to run boto3 module without intrinsically defining a region? I am trying to run a script to confirm tags have been added to cloudformation AWS stacks. I want it to run from whatever region would be currently selected inside of my default connection. I'm not sure if I need to write something for this. I am also curious if it needs to be written to just say the region. If that's the case is it possible to check ALL regions without it slowing down?
import boto3
cf = boto3.client('cloudformation')
def validate_stack(stack_id):
rsp = cf.describe_stacks(StackName=stack_id)
for stack in rsp['Stacks']:
has_product = False
has_service = False
has_team = False
has_owner = False
for tag in stack['Tags']:
if tag['Key'] == 'Product':
has_product = True
if tag['Key'] == 'Service':
has_service = True
if tag['Key'] == 'Team':
has_team = True
if tag['Key'] == 'Owner':
has_owner = True
last_time = stack.get('LastUpdatedTime', stack['CreationTime'])
if (has_product == False) or (has_service == False) or (has_team == False) or (has_owner == False):
print('last updated: {5}, has_product={1}, has_service={2}, has_team={3}, has_owner={4} {0}'.format(stack_id, has_product, has_service, has_team, has_owner, last_time))
def check_cloudformation(deployment_id):
list_rsp = cf.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE',
'UPDATE_COMPLETE'
]
)
deployment_substring = '-{0}-'.format(deployment_id)
while True:
for summary in list_rsp['StackSummaries']:
if deployment_substring in summary['StackName']:
validate_stack(summary['StackId'])
next_token = list_rsp.get('NextToken')
if next_token == None:
break
list_rsp = cf.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE',
'UPDATE_COMPLETE'
],
NextToken=next_token
)
I have a dictionary (d) like so:
d = {
'[primary_number]': '12345',
'[street_name]': 'Main',
'[street_predirection]': 'NW',
'[street_postdirection]': None,
'[street_suffix]': 'St',
'[secondary_number]': None,
'[secondary_designator]': None,
'[extra_secondary_number]': None,
'[zipcode]': '12345'
}
And I would like to create a new Key/Value pair called AddressStr that combines several (but not all) of the fields in d. I am trying to combine it with the following code:
d['AddressStr'] = d["[zipcode]"] + \
d["[street_name]"] + \
d["[primary_number]"] + \
d["[secondary_number]"] + \
d["[street_predirection]"] + \
d["[street_postdirection]"] + \
d["[street_suffix]"]
However, this produces the error:
can only concatenate str (not "NoneType") to str
Because [street_postdirection] = None. Ideally, I would like to just have a check/replace to change the None values in those 7 fields to '', but I'm not quite sure how to do that. I'd also like to do in place during the creation of d['AddressStr'], but if that's not possible I'm OK with that.
You can join the values that are not None:
keys = ["[zipcode]", "[street_name]", "[primary_number]",
"[secondary_number]", "[street_predirection]", "[street_postdirection]",
"[street_suffix]"]
d['AddressStr'] = ' '.join([d[key] for key in keys if d[key] is not None])
You could use the str() method and use the property that None behaves as false on a boolean check:
d['AddressStr'] = str(d["[zipcode]"] or '') + \
str(d["[street_name]"] or '') + \
str(d["[primary_number]"] or '') + \
str(d["[secondary_number]"] or '') + \
str(d["[street_predirection]"] or '') + \
str(d["[street_postdirection]"] or '') + \
str(d["[street_suffix]"] or '')
Trying to Nest no's and yes's with their respective applications and services.
That way when a request comes in for a specific zone to zone sequence, a check can be run against this logic to verify accepted requests.
I have tried calling Decision_List[Zone_Name][yes_no].update and i tried ,append when it was a list type and not a dict but there is no update method ?
Base_Sheet = range(5, sh.ncols)
Column_Rows = range(1, sh.nrows)
for colnum in Base_Sheet:
Zone_Name = sh.col_values(colnum)[0]
Zone_App_Header = {sh.col_values(4)[0]:{}}
Zone_Svc_Header = {sh.col_values(3)[0]:{}}
Zone_Proto_Header = {sh.col_values(2)[0]:{}}
Zone_DestPort_Header = {sh.col_values(1)[0]: {}}
Zone_SrcPort_Header = {sh.col_values(0)[0]: {}}
Decision_List = {Zone_Name:{}}
for rows in Column_Rows:
app_object = sh.col_values(4)[rows]
svc_object = sh.col_values(3)[rows]
proto_object = sh.col_values(3)[rows]
dst_object = sh.col_values(2)[rows]
src_object = sh.col_values(1)[rows]
yes_no = sh.col_values(colnum)[rows]
if yes_no not in Decision_List[Zone_Name]:
Decision_List[Zone_Name][yes_no] = [app_object]
else:
Decision_List[Zone_Name]=[yes_no].append(app_object)
I would like it present info as follows
Decision_List{Zone_Name:{yes:[ssh, ssl, soap], no:
[web-browsing,facebook]}}
I would still like to know why i couldnt call the append method on that specific yes_no key whos value was a list.
But in the mean time, i made a work around of sorts. I created a set as the key and gave the yes_no as the value. this will allow me to pair many no type values with the keys being a set of the application, port, service, etc.. and then i can search for yes values and create additional dicts out of them for logic.
Any better ideas out there i am all ears.
for rownum in range(0, sh.nrows):
#row_val is all the values in the row of cell.index[rownum] as determined by rownum
row_val = sh.row_values(rownum)
col_val = sh.col_values(rownum)
print rownum, col_val[0], col_val[1: CoR]
header.append({col_val[0]: col_val[1: CoR]})
print header[0]['Start Port']
dec_tree = {}
count = 1
Base_Sheet = range(5, sh.ncols)
Column_Rows = range(1, sh.nrows)
for colnum in Base_Sheet:
Zone_Name = sh.col_values(colnum)[0]
Zone_App_Header = {sh.col_values(4)[0]:{}}
Zone_Svc_Header = {sh.col_values(3)[0]:{}}
Zone_Proto_Header = {sh.col_values(2)[0]:{}}
Zone_DestPort_Header = {sh.col_values(1)[0]: {}}
Zone_SrcPort_Header = {sh.col_values(0)[0]: {}}
Decision_List = {Zone_Name:{}}
for rows in Column_Rows:
app_object = sh.col_values(4)[rows]
svc_object = sh.col_values(3)[rows]
proto_object = sh.col_values(3)[rows]
dst_object = sh.col_values(2)[rows]
src_object = sh.col_values(1)[rows]
yes_no = sh.col_values(colnum)[rows]
for rule_name in Decision_List.iterkeys():
Decision_List[Zone_Name][(app_object, svc_object, proto_object)]= yes_no
Thanks again.
I think still a better way is to use collections.defaultdict
In this manner it will ensure that i am able to append to the specific yes_no as i had originally intended.
for colnum in Base_Sheet:
Zone_Name = sh.col_values(colnum)[0]
Zone_App_Header = {sh.col_values(4)[0]:{}}
Zone_Svc_Header = {sh.col_values(3)[0]:{}}
Zone_Proto_Header = {sh.col_values(2)[0]:{}}
Zone_DestPort_Header = {sh.col_values(1)[0]: {}}
Zone_SrcPort_Header = {sh.col_values(0)[0]: {}}
Decision_List = {Zone_Name:defaultdict(list)}
for rows in Column_Rows:
app_object = sh.col_values(4)[rows]
svc_object = sh.col_values(3)[rows]
proto_object = sh.col_values(2)[rows]
dst_object = sh.col_values(1)[rows]
src_object = sh.col_values(0)[rows]
yes_no = sh.col_values(colnum)[rows]
if yes_no not in Decision_List[Zone_Name]:
Decision_List[Zone_Name][yes_no]= [app_object, svc_object, proto_object, dst_object, src_object]
else:
Decision_List[Zone_Name][yes_no].append([(app_object, svc_object, proto_object,dst_object, src_object)])
This allows me to then set the values as a set and append them as needed
Is there a way to assign a list based on a variable env and pass the result to function? I am passing a variable called env, which could be UPE, DEV, PRD for example. Based on the result, I want to assign the list respectively to the functions below. What would be the best approach?
UPE=['SERVER1','SERVER2','SERVER3','SERVER4']
DEV=['ServerA','ServerB']
PRD=['SERVER1','SERVER2','SERVER3','SERVER4']
if os.path.isfile('/myfile/' + configFile):
config_parser = ConfigParser()
config_parser.read('/myfile/' + configFile)
if actions == "start":
startOVD('start',UPE[3]) //I want pass the result of env setup variable
#ans = raw_input("Would you like to start OVD, MSAS,OAG : y | n : ")
if env == 'UPE':
startMSAS('start',UPE[0])
startOAG('start',UPE[1])
startOHS('start',UPE[2])
for section_name in sorted(config_parser.sections(), reverse=True):
adminURL = config_parser.get(section_name, 'ADMIN_URL')
adminUsername = config_parser.get(section_name, 'ADMIN_USER')
adminPassword = config_parser.get(section_name, 'ADMIN_PASS')
adminHost = config_parser.get(section_name, 'NM_HOST')
domainName = config_parser.get(section_name, 'DOMAIN_NAME')
domainDir = config_parser.get(section_name, 'DOMAIN_DIR')
admPort = adminURL[-4:]
printHeader('Initiating Starting Sequence')
startAdmin('start', adminHost, domainDir, domainName, admPort)
showServerStatus('start', adminUsername, adminPassword, adminURL)
if actions == "stop":
for section_name in (config_parser.sections()):
adminURL = config_parser.get(section_name, 'ADMIN_URL')
adminUsername = config_parser.get(section_name, 'ADMIN_USER')
adminPassword = config_parser.get(section_name, 'ADMIN_PASS')
adminHost = config_parser.get(section_name, 'NM_HOST')
domainName = config_parser.get(section_name, 'DOMAIN_NAME')
domainDir = config_parser.get(section_name, 'DOMAIN_DIR')
admPort = adminURL[-4:]
printHeader('Initiating Stopping Sequence')
showServerStatus('stop', adminUsername, adminPassword, adminURL)
stopAdmin(adminHost, domainDir, domainName, admPort)
if env == 'UPE':
stopMSAS('stop',UPE[0])
stopOAG('stop',UPE[1])
stopOHS('stop',UPE[2])
stopOVD('stop',UPE[3])
I would organize this by setting up a list of callbacks.
from functools import partial
start_funcs = [partial(startOVD 'start',UPE[3])
if env == 'UPE':
start_funcs.extend([partial(startMSAS, 'start', UPE[0]),
partial(startOAG, 'start', UPE[1])])
start_funcs.append(partial(startOHS, 'start', UPE[2]))
Add similar logic for the cases when env has a different value. In the end, you'll just iterate over start_funcs and call each function in order.
if actions == "start":
for f in start_funcs:
f()
# ...