webspherejythonwsadmin

Modify name space bindings using wsadmin Jython


The task is to modify namespace binding using wsadmin (Jython). I have several string bindings for scope server. I plan to list existing bindings; add / modify target binding.

I can list them:

for ns in AdminConfig.list( 'NameSpaceBinding' ).splitlines() :
    name = AdminConfig.showAttribute( ns, 'name' )
    stringToBind = AdminConfig.showAttribute( ns, 'stringToBind' )
    print '%s="%s"' % ( name, stringToBind )

I can create a new one:

server1 = AdminConfig.list('Server')
print AdminConfig.create('StringNameSpaceBinding', server1, [['name', 'binding2'], ['nameInNameSpace', 'myBindings/myString'], ['stringToBind', "This is the String value"]])
AdminConfig.save()
  1. How to modify existing binding in case there is one?
  2. How to delete binding?

I guess it can be done with AdminConfig.modify and AdminConfig.remove. Links:

  1. Doc from IBM: Configuring namespace bindings using the wsadmin scripting tool
  2. Modify binding using jacl: Configuring namespace binding

Solution

  • I manage to do it myself. I'm not experienced in Jython, but it works. Script requires 4 parameters: target server for scope, binding name, binding value and name in name space:

    server = sys.argv[0]
    bindingName = sys.argv[1]
    bindingValue = sys.argv[2]
    bindingNameSpace = sys.argv[3]
    
    # Obtain the "simple" server name
    def getServerName(s):
        return AdminConfig.showAttribute(s, 'name')
    
    # Add binding
    def addBindingsToServer(s):
        for ns in AdminConfig.list( 'NameSpaceBinding' ).splitlines() :
            if bindingName == AdminConfig.showAttribute( ns, 'name' ):
                print "Removing existing binding from Server %s" % getServerName(s)
                AdminConfig.remove(ns)
    
        # Create binding
        print "Adding binding to Server %s" % getServerName(s)
        print AdminConfig.create('StringNameSpaceBinding', s, [['name', bindingName], ['nameInNameSpace', bindingNameSpace], ['stringToBind', bindingValue]])
    
    # Set server and call function
    server = AdminConfig.getid('/Server:'+server+'/')
    addBindingsToServer(server)
    
    # Save changes
    if (AdminConfig.hasChanges()):
        AdminConfig.save()