I'm trying to verify if a topic exists based on topic name.
Do you know if this is possible?
For example I want to verify if topic with name "test" already exist.
Below is what I'm trying but doesn't work because topicsList contains topicArns and not topicNames...
topics = sns.get_all_topics()
topicsList = topics['ListTopicsResponse']['ListTopicsResult'['Topics']
if "test" in topicsList:
print("true")
This code will work if you have more than 100 topics
def get_topic(token=None):
topics = self.sns.get_all_topics(token)
next_token = topics['ListTopicsResponse']['ListTopicsResult']['NextToken']
topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
for topic in topic_list:
if "your_topic_name" in topic['TopicArn'].split(':')[5]:
return topic['TopicArn']
else:
if next_token:
get_topic(next_token)
else:
return None
What if you try to catch An error occurred (NotFound) when calling the GetTopicAttributes operation: Topic does not exist exception?
from botocore.exceptions import ClientError
topic_arn = "arn:aws:sns:us-east-1:999999999:neverFound"
try:
response = client.get_topic_attributes(
TopicArn=topic_arn
)
print "Exists"
except ClientError as e:
# Validate if is this:
# An error occurred (NotFound) when calling the GetTopicAttributes operation: Topic does not exist
print "Does not exists"
This is kind of a hack but it should work:
topics = sns.get_all_topics()
topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
topic_names = [t['TopicArn'].split(':')[5] for t in topic_list]
if 'test' in topic_names:
print(True)
Related
I've looked at a few examples and responses, however, I'm stumped. It worked a few minutes ago and now it's just not returning a result. No code change happened.
The test code I have pulls the members from a LDAP role group and prints the member DN's. It worked a few minutes ago, and now it keeps returning empty results. I also printed the ldap_result and it's empty too.
def ldapgroupmembers():
try:
username = str(raw_input('Email: '))
password = str(getpass.getpass())
l = ldap.initialize("ldap://company.co.za")
l.simple_bind_s(username, password)
myfilter = "(&DN=GROUP)(cn=My-RG-Group))"
ldap_result = l.search_s("OU=MyGroups,OU=Role Groups,OU=Groups,OU=CompanyOU,DC=company,DC=co,DC=za", ldap.SCOPE_SUBTREE, myfilter, ['member'])
for dn in ldap_result[0][1]['member']:
print dn
except ldap.INVALID_CREDENTIALS:
print "Invalid credentials"
return "Invalid credentials"
except ldap.SERVER_DOWN:
print "Server down"
return "Server down"
except ldap.LDAPError, e:
if type(e.message) == dict and e.message.has_key('desc'):
return "Other LDAP error: " + e.message['desc']
else:
print "Other LDAP error: "
return "Other LDAP error: " + e
finally:
l.unbind_s()
return "Succesfully authenticated"
Your help would be greatly appreciated.
I found the problem. Turns out I never closed the myfilter bracket and I removed the first bracket set where I filter on dn=GROUP
I have the following lines in my python api which deletes a function created by user from postgres db upon request.
#func_app.route("/delete", methods=["POST"])
def delete_func():
try:
JSON = request.get_json()
user_func = function_table.query.filter_by(
created_by=token_payload["email"], functionid=JSON["funcId"]
).all()
functionid=JSON["funcId"]
func_detail = function_table.query.filter_by(functionid).first()
user = users_table.query.filter_by(email=token_payload["username"]).first()
if len(user_func) == 0:
log_metric(g.request_id + " " + "No functions found")
return make_response(
jsonify("User does not have any functions. Please try again later."),
204,
)
else:
function_table.query.filter_by(functionid).delete()
db.session.commit()
except Exception as err:
db.session.rollback()
log_metric(g.request_id + " " + str(err))
return make_response(
jsonify(
"Unable to process your request at the moment. Please try again later."
),
400,
)
finally:
db.session.close()
I have used filter_by similarly before but there I didn't find any issue. Can anyone help me figure out what went wrong?
Thanks!
You need to define the column name with the value in filter_by, it take **kwargs arguments not *args type of input.
you need to change
func_detail = function_table.query.filter_by(functionid).first()
to
func_detail = function_table.query.filter_by(id = functionid).first()
considering id is column name.
For more information, see the sqlalchemy documentation.
This is my test so far:
test_500(self):
client = ClientConfiguration(token=token, url=url)
client.url = 'https://localhost:1234/v1/' + bucket
keys = None
try:
get_bucket = json.loads(str(client.get_bucket(bucket)))
result = get_bucket['result']
except Exception as e:
expected_status_code = 500
failure_message = "Expected status code %s but got status code %s" % (expected_status_code, e)
self.assertEquals(e, expected_status_code, failure_message)
I need to write a mock that will return a 500 response when the 'https://localhost:1234/v1/' + bucket url is used. Can this be done with unittest and if so, how or where can I find some documentation on this? I've been through this site, the unittest documentation and Youtube and can't find anythingspecific to what I want to do.
I ended up using this to create my test.
The end result is:
#responses.activate
test_500(self):
responses.add(responses.GET, 'https://localhost:1234/v1/' + bucket,
json={'error': 'server error'}, status=500)
client = ClientConfiguration(token=token, url=url)
client.url = 'https://localhost:1234/v1/'
keys = None
try:
get_bucket = json.loads(str(client.get_bucket(bucket)))
result = get_bucket['result']
except Exception as e:
expected_status_code = 500
failure_message = "Expected status code %s but got status code %s" % (expected_status_code, e)
self.assertEquals(e, expected_status_code, failure_message)
I'm trying to trap the following error in a try/exception block, but as this is a custom module that is generating the error - not generating a standard error such as ValueError for example. What is the correct way to catch such errors?
Here is my code:
try:
obj = IPWhois(ip_address)
except Exception(IPDefinedError):
results = {}
else:
results = obj.lookup()
The most obvious way:
except IPDefinedError:
gives:
NameError: name 'IPDefinedError' is not defined
The error returned that I want to check for is:
ipwhois.exceptions.IPDefinedError
ipwhois.exceptions.IPDefinedError: IPv4 address '127.0.0.1' is already defined as 'Private-Use Networks' via 'RFC 1918'.
The issue here is the import!
I had the import as
from ipwhois import IPWhois
but I also needed
import ipwhois
So the following works:
try:
obj = IPWhois(ip_address)
except ipwhois.exceptions.IPDefinedError:
results = {}
else:
results = obj.lookup()
Here is a quick recap. Yes, the error in your question did look like it was likely related to an import issue (as per my comment ;) ).
from pprint import pprint as pp
class IPDefinedError(Exception):
"""Mock IPDefinedError implementation
"""
pass
class IPWhois(object):
"""Mock IPWhois implementation
"""
def __init__(self, ip_address):
if ip_address == "127.0.0.1":
raise IPDefinedError(
"IPv4 address '127.0.0.1' is already defined as 'Private-Use Networks' via 'RFC 1918'.")
self._ip_address = ip_address
def lookup(self):
return "RESULT"
def lookup(ip_address):
""" calculates IPWhois lookup result or None if unsuccessful
:param ip_address:
:return: IPWhois lookup result or None if unsuccessful
"""
result = None
try:
obj = IPWhois(ip_address)
result = obj.lookup()
except IPDefinedError as e:
msg = str(e)
print("Error received: {}".format(msg)) # do something with msg
return result
if __name__ == '__main__':
results = map(lookup, ["192.168.1.1", "127.0.0.1"])
pp(list(results)) # ['RESULT', None]
I know That it is not possible to pickle a pyramid request object, but I cant seem to find where I am sending the Request object.
Consider the following:
#task
def do_consignment_task(store, agent):
print "GOTHERE IN TASK"
s = sqlahelper.get_session()
consign = store.gen_consignment()
ca = Agents.by_id(store.consignment_agents_id)
consign.consignment_agents_id = ca.id
consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
consign.consignment_timestamp = func.now()
consign.created_by_agent_id = agent.id
consign.complete_stamp = func.now()
consign.sims = store.sims
consign.status = "SUCCESS"
print "GOT BEFORE LOOP "
for sim in store.sims:
if sim in consign.sims:
continue
else:
consign.sims.append(sim)
s.add(consign)
transaction.savepoint()
print "GOT AFTER SAVEPOINT"
for sim in consign.sims:
is_reconsign = sim.consignment_agent or sim.consignment_teamlead
if is_reconsign:
if not sim.consignment_history:
sim.consignment_history = []
sim.consignment_history.append(dict(
stamp=sim.consignment_timestamp,
consignment_agent_id=sim.consignment_agents_id,
consignment_teamleader_id=sim.consignment_teamleader_id,
by_agent_id=agent.id
))
s.query(
Sims
).filter(
Sims.iccid == sim.iccid
).update(
{
"consignment_agents_id": consign.consignment_agents_id,
"consignment_history": sim.consignment_history,
"consignment_teamleader_id": ca.ou[0].lead_agents_id,
"consignment_timestamp": func.now(),
"modify_stamp": func.now(),
"consignments_id": consign.id
},
synchronize_session=False
)
print "GOT BEFORE COMMIT"
transaction.savepoint()
print "THIS IS THE ID ID ID ID ID ID : ", consign.id
I call this function like:
if self.store.finalise:
try:
store = self.store
agent = self.agent
do_consignment_task.delay(store, agent)
transaction.commit()
self.check_and_purge()
return "Consignmnet is being processed"
except Exception, exc:
self.check_and_purge()
self.log.exception(exc)
exc_error = "CONSIGNERR:", exc.message
raise USSDFailure(exc_error)
else:
self.store.status = "CANCELLED"
if "fullconfirm" in self.session:
del self.session["fullconfirm"]
self.check_and_purge()
return "CONSIGNMENT Cancelled"
When I run this code I get the following error:
EncodeError: Can't pickle <class 'pyramid.util.Request'>: attribute lookup pyramid.util.Request failed
I am not sending self or request objects - at least not that I can see.
How can solve this problem? Am I sending a request object, because I can not see one?
The traceback can be seen here
EDIT:
okay So I have tried to change the data I send to the function - I am not passing a sqlalchemy object and I am making a copy of the store object, that changes my code to:
#task
def do_consignment_task(agent_id, **store):
print "GOTHERE IN TASK"
s = sqlahelper.get_session()
cObj = USSDConsignmentsObject()
consign = cObj.gen_consignment()
ca = Agents.by_id(store.consignment_agents_id)
consign.consignment_agents_id = ca.id
consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
consign.consignment_timestamp = func.now()
consign.created_by_agent_id = agent_id
# etc
and:
if self.store.finalise:
try:
# del self.service
store = self.store.__dict__.copy()
agent_id = self.agent.id
print store
print agent_id
# print help(store)
do_consignment_task.delay(agent_id, **store)
transaction.commit()
#etc
This however still gives me the same error :|
Try not to serialise a Pyramid request object. When you interact with a celery task you should think of it as an independent process.
Provide it all the information it needs to do it's work. Be aware that you need to serialise that information.
So self.store possibly contains attribute references that may be unrealistic to serialise.
Perhaps create a method on the store object that returns a clean dictionary object.
def serialize(self):
data = {}
data["element1"] = self.element1
data["element2"] = self.element2
data["element3"] = self.element3
return data
Then when you want to call the delay method make sure to use store.serialize() instead of store or the dict.