how to enable connectiondraining with python boto modify_lb_attribute - python

I have been trying to enable ELB connection draining using the modify_lb_attribute method in the python boto module; however I haven't been able to get it working. According to the documentation here http://boto.readthedocs.org/en/latest/ref/elb.html I should be able to call it like his:
modify_lb_attribute(load_balancer_name, attribute, value)
Here is an example:
modify_lb_attribute('my-elb', 'connectionDraining', 120)
When I do this however I receive the following error:
File "/Library/Python/2.7/site-packages/boto/ec2/elb/init.py", line 421, in modify_lb_attribute
value.enabled and 'true' or 'false'
AttributeError: 'NoneType' object has no attribute 'enabled'
I have been able to get it to work successfully with crossZoneLoadBalancing.
For example this works:
modify_lb_attribute('my-elb', 'crossZoneLoadBalancing', 'true')
Any help or suggestions would be appreciated.
Thanks

Working syntax for instantiating a ConnectionDrainingAttribute and passing it a to a load balancer:
from boto.ec2.elb.attributes import ConnectionDrainingAttribute
import boto.ec2.elb
connection = boto.ec2.elb.connect_to_region("region")
cda = ConnectionDrainingAttribute(connection)
cda.enabled = True
cda.timeout = 120
connection.modify_lb_attribute(
load_balancer_name='my-elb',
attribute='connectionDraining',
value=cda
)
More information about the ConnectionDrainingAttribute class can be found here in the boto docs.

When you modify the connectionDraining attribute of a load balancer, there are actually two values you can supply. The first is a boolean indicating whether you are enabling or disabling the connection draining feature. The second is an integer indicating the timeout which obviously applies only if connection draining is being enabled.
To allow you to specify both of these values, boto defines a ConnectionDrainingAttribute class in boto.ec2.elb.attributes. You must pass an instance of this class as the value to modify_elb_attribute, e.g.:
from boto.ec2.elb.attributes import ConnectionDrainingAttribute
cda = ConnectionDrainingAttribute()
cda.enabled = True
cda.timeout = 120
...
modify_lb_attribute('my-elb', cda)

Related

Python Zeep - List Object has no Attribute _xsd_name

I'm trying to call a SOAP endpoint using the Zeep library.
The endpoint has the following signature:
ExecuteStaticSQLQuery(queryType: ns0:StaticQueries, xmlParameters: {_value_1: ANY}, iRowsAffected: xsd:int) -> ExecuteStaticSQLQueryResult: xsd:int, iRowsAffected: xsd:int
I built a function that contains the following logic:
INSERT_AUTO_FORWARD_IMPORT = 'INSERT_AUTO_FORWARD_IMPORT_1'
query_parameter = self.client.get_type('ns0:QueryParameter')
db_type = self.client.get_type('ns0:DbType')
table_parameter = query_parameter(ParameterType=db_type('String'),
ParameterName='#Auto_Forward_Table_Name',
ParameterValue=auto_forward_table)
any_object = AnyObject(query_parameter, table_parameter)
xml_parameters = {
'_value_1': any_object
}
soap_envelope = self.service.ExecuteStaticSQLQuery(queryType=INSERT_AUTO_FORWARD_IMPORT,
xmlParameters=xml_parameters,
iRowsAffected=rows_affected)
When I call the function I get the following error:
'list' object has no attribute '_xsd_name'
I suspect the issue is coming from the any_object I build, because I can send a request to the endpoint without the xmlParameters argument and I get a response back.
Based on the Zeep Any Objects Docs, I would think I'm correctly constructing the AnyObject and implementing the call to the SOAP endpoint.
I'm not really sure where to go from here.
I'm not sure you are correctly creating the AnyObject.
According to the Zeep docs you link to, you should be calling get_element, not get_type, to construct the AnyObject.
In other words, try replacing the line
query_parameter = self.client.get_type('ns0:QueryParameter')
with
query_parameter = self.client.get_element('ns0:QueryParameter')

Get elasticloadbalancers names with boto3

When I try to print the load balancers from aws I get a huge dictionary with a lot of keys, but when I'm trying to print only the 'LoadBalancerName' value I get: None, I want to print all the load balancers names in our environment how I can do it? thanks!
What I tried:
import boto3
client = boto3.client('elbv2')
elb = client.describe_load_balancers()
Name = elb.get('LoadBalancerName')
print(Name)
The way in which you were handling the response object was incorrect, and you'll need to put it in a loop if you want all the Names and not just one. What you'll you'll need is this :
import boto3
client = boto3.client('elbv2')
elb = client.describe_load_balancers()
for i in elb['LoadBalancers']:
print(i['LoadBalancerArn'])
print(i['LoadBalancerName'])
However if your still getting none as a value it would be worth double checking what region the load balancers are in as well as if you need to pass in the use of a profile too.

Odoo - How to acess recordsets on web controller

I am using web controller in odoo 8 to make a REST API that will get some data and return values from the database. The problem is that I am not able to get the database from the builtin ORM.
I tried to call osv.pool.get() but gave me the error:
AttributeError: type object 'Model' has no attribute 'pool'
Odoo 8 apparently uses recordsets, but I can't use it too, and couldn't find anything usefull on docs.
How can I browse database data on web controller?
My code:
class TestWebService(http.Controller):
#http.route('/test', type='http', auth='none')
def test(self):
objects = osv.osv.pool.get("some_table")
# I need to get the objects from some_table and search them
return "Hello World"
Try Following
myobj = request.env['some.table']

What is md5_hash property of BlobInfo object intended for?

If I call
blobsotre.BlobInfo.properties()
the function return
set(['filename', 'creation', 'content_type', 'md5_hash', 'size'])
but if I call
a = blobstore.BlobInfo.all()
obj = a.fetch(1)[0]
print obj.md5_hash
the function raise exception
AttributeError(name) AttributeError: md5_hash
What is md5_hash property of BlobInfo object intended for?
P.S. I want to check what uploaded file is not exist into Blobstore.
A cryptographic hash function can be used for many things:
to provide an integrity check value for the file/blob to detect changes
to provide a unique identifier for a file/blob used to refer to the contents
to enable fast lookup of the contents of a hash table
to enable fast searching for duplicate files
etc
The "intended" use of course depends on what application the blobstore is supporting - are you building a shopping cart, or a data cache, or a map-reduce processing application, or what?
The code you show works fine for me, on shell.appspot.com:
>>> from google.appengine.ext import blobstore
>>> blobstore.BlobInfo.properties()
set(['filename', 'creation', 'content_type', 'md5_hash', 'size'])
>>> o = blobstore.BlobInfo.all().get()
>>> o.md5_hash
u'5d41402abc4b2a76b9719d911017c592'
You must be doing something different to what's in your sample code. Can you paste your exact code, and the complete stacktrace?
You probably have BlobInfo objects that don't have an md5_hash written including the first result returned by blobstore.BlobInfo.all()
You can check easily in your dev server's interactive console:
from google.appengine.ext import blobstore
query1 = blobstore.BlobInfo.all()
query2 = blobstore.BlobInfo.gql("WHERE md5_hash != ''")
print query1.count(), query2.count()
# for me this returns '100 85'

Need help using M2Crypto.Engine to access USB Token

I am using M2Crypto-0.20.2. I want to use engine_pkcs11 from the OpenSC project and the Aladdin PKI client for token based authentication making xmlrpc calls over ssl.
from M2Crypto import Engine
Engine.load_dynamic()
dynamic = Engine.Engine('dynamic')
# Load the engine_pkcs from the OpenSC project
dynamic.ctrl_cmd_string("SO_PATH", "/usr/local/ssl/lib/engines/engine_pkcs11.so")
Engine.cleanup()
Engine.load_dynamic()
# Load the Aladdin PKI Client
aladdin = Engine.Engine('dynamic')
aladdin.ctrl_cmd_string("SO_PATH", "/usr/lib/libeTPkcs11.so")
key = aladdin.load_private_key("PIN","password")
This is the error I receive:
key = pkcs.load_private_key("PIN","eT0ken")
File "/usr/local/lib/python2.4/site-packages/M2Crypto/Engine.py", line 70, in load_private_key
return self._engine_load_key(m2.engine_load_private_key, name, pin)
File "/usr/local/lib/python2.4/site-packages/M2Crypto/Engine.py", line 60, in _engine_load_key
raise EngineError(Err.get_error())
M2Crypto.Engine.EngineError: 23730:error:26096075:engine routines:ENGINE_load_private_key:not initialised:eng_pkey.c:112:
For load_private_key(), what should be passed as the first argument? The M2Crypto documentation does not explain it.
I don't get any errors loading the engines, but I'm not sure if I'm loading them correctly. It seems like the engine ID has to be a specific name but I don't find that list anywhere. 'dynamic' is working for me.
Any help would be appreciated!
Found !!!!
Yes, exactly the way where I came from.
So, actually the ENGINE_init() is not implemented in M2Crypto.Engine. So, only one solution: patching!!! (very small...) so I've created a new Engine method (in Engine.py)
def engine_initz(self):
"""Return engine name"""
return m2.engine_initz(self._ptr)
Why engine_initz ? because engine_init is already define in SWIG/_engine.i,:
void engine_init(PyObject *engine_err) {
Py_INCREF(engine_err);
_engine_err = engine_err;
}
I don't really know what is done, so I've prefered creating a new one... So I've just added the following to SWIG/_engine.i:
%rename(engine_initz) ENGINE_init;
extern int ENGINE_init(ENGINE *);
And recompile the __m2crypto.so, now just add a "pkcs11.engine_initz()" before launching the private key, and it works.....
I don't know what and why the engine_init code present in current M2Crypto is supposed to do. Exposing ENGINE_init() as engine_init2 with the following patch to M2Crypto helps:
Index: SWIG/_engine.i
===================================================================
--- SWIG/_engine.i (revision 719)
+++ SWIG/_engine.i (working copy)
## -44,6 +44,9 ##
%rename(engine_free) ENGINE_free;
extern int ENGINE_free(ENGINE *);
+%rename(engine_init2) ENGINE_init;
+extern int ENGINE_init(ENGINE *);
+
/*
* Engine id/name functions
*/
After this, the following code takes me further (but urllib does not fully work for me currently):
import sys, os, time, cgi, urllib, urlparse
from M2Crypto import m2urllib2 as urllib2
from M2Crypto import m2, SSL, Engine
# load dynamic engine
e = Engine.load_dynamic_engine("pkcs11", "/Users/martin/prefix/lib/engines/engine_pkcs11.so")
pk = Engine.Engine("pkcs11")
pk.ctrl_cmd_string("MODULE_PATH", "/Library/OpenSC/lib/opensc-pkcs11.so")
m2.engine_init2(m2.engine_by_id("pkcs11")) # This makes the trick
cert = e.load_certificate("slot_01-id_01")
key = e.load_private_key("slot_01-id_01", sys.argv[1])
ctx = SSL.Context("sslv23")
ctx.set_cipher_list("HIGH:!aNULL:!eNULL:#STRENGTH")
ctx.set_session_id_ctx("foobar")
m2.ssl_ctx_use_x509(ctx.ctx, cert.x509)
m2.ssl_ctx_use_pkey_privkey(ctx.ctx, key.pkey)
opener = urllib2.build_opener(ctx)
urllib2.install_opener(opener)
Looking at the pastebin link Becky provided, I believe it translates to something like this in the new API:
from M2Crypto import Engine, m2
dynamic = Engine.load_dynamic_engine("pkcs11", "/Users/martin/prefix/lib/engines/engine_pkcs11.so")
pkcs11 = Engine.Engine("pkcs11")
pkcs11.ctrl_cmd_string("MODULE_PATH", "/Library/OpenSC/lib/opensc-pkcs11.so")
r = pkcs11.ctrl_cmd_string("PIN", sys.argv[1])
key = pkcs11.load_private_key("id_01")
So I am betting that if you substitute "/Users/martin/prefix/lib/engines/engine_pkcs11.so" with "/usr/local/ssl/lib/engines/engine_pkcs11.so" and "/Library/OpenSC/lib/opensc-pkcs11.so" with "/usr/lib/libeTPkcs11.so" you might get it to work with Aladdin.
That is exactly the code I've tried. But It ended with the following error:
Traceback (most recent call last):
File "prog9.py", line 13, in <module>
key = pkcs11.load_private_key("id_45")
File "/usr/lib/pymodules/python2.5/M2Crypto/Engine.py", line 70, in load_private_key
return self._engine_load_key(m2.engine_load_private_key, name, pin)
File "/usr/lib/pymodules/python2.5/M2Crypto/Engine.py", line 60, in _engine_load_key
raise EngineError(Err.get_error())
M2Crypto.Engine.EngineError: 11814:error:26096075:engine outines:ENGINE_load_private_key:not initialised:eng_pkey.c:112:
I'm using OpenSC PKCS11 lib, not aladdin lib. But I don't think the problem is closed.
I tried the code that Heikki suggested (minus one line) and got the same error as Erlo. For load_private_key(), how do I know what to put in for the argument?
dynamic = Engine.load_dynamic_engine("pkcs11", "/usr/local/ssl/lib/engines/engine_pkcs11.so")
# m2.engine_free(dynamic) this line gave me an error TypeError: in method 'engine_free', argument 1 of type 'ENGINE *'
pkcs11 = Engine.Engine("pkcs11")
pkcs11.ctrl_cmd_string("MODULE_PATH", "/usr/lib/libeTPkcs11.so")
r = pkcs11.ctrl_cmd_string("PIN", "password")
key = pkcs11.load_private_key("id_01")
I think the problem is not really the "load_private_key()". It's like something is missing between "MODULE_PATH" definition and the load_private_key() call. What happen if you remplace "/usr/lib/libeTPkcs11.so" by a wrong path ? In my case I have no error related to this.
I've run "pcscd" in foreground with high debug level, there is no call to smartcard during the python execution... So definitly, I don't understand what's wrong...
The equivalent in "openssl" is using "-pre" command. The "-pre" (by opposite to the "-post") are command sent to the engine before loading. Perhaps we need to call a methode which "load" the engine after all "ctrl_cmd_string" calls ?? ...
Lost :-/

Categories