From 6c8cdd386b61501c1084ae2e750451b9e95b366e Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sat, 23 Mar 2013 16:30:02 +0100
Subject: [PATCH 1/6] Added module rhn_channel

---
 rhn_channel.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)
 create mode 100755 rhn_channel.py

diff --git a/rhn_channel.py b/rhn_channel.py
new file mode 100755
index 00000000000..ca24ee3e63f
--- /dev/null
+++ b/rhn_channel.py
@@ -0,0 +1,138 @@
+#!/usr/bin/python
+
+DOCUMENTATION = '''
+---
+module: rhn_channel
+short_description: Define Red Hat software channels
+description:
+    - Adds or removes Red Hat software channels on a system
+version_added: 1.0
+author: Vincent Van der Kussen
+notes:
+    - this module fetches the systemid from rhn. A function 
+      to use the local systemid is provided (get_localsystem)
+      but not integrated
+    - Username and password are currently hardcoded in the main
+      section
+requirements:
+    - none
+options:
+    name:
+        description
+            - name of the software channel
+        required: true
+        default: null
+    sysname:
+        description:
+            - name of the system as it is known in rhn/sattelite
+        required: true
+        default: null
+    url:
+        description: 
+            - The full url to the rhn/sattelite api
+        required: true
+examples:
+    - code: rhn_channel name=rhel-x86_64-server-v2vwin-6  sysname=server01
+      url=https://rhn.redhat.com/rpc/api
+      description: add software channel rhel-x86_64-server-v2vwin-6 
+                   to server01 in Red Hat Network
+'''
+
+import xmlrpclib
+from operator import itemgetter
+import re
+
+
+# ------------------------------------------------------- #
+
+def get_systemid(client, session, sysname):
+    systems = client.system.listUserSystems(session)
+    for system in systems:
+        if system.get('name') == sysname:
+            idres = system.get('id')
+            idd = int(idres)
+            return idd
+
+# ------------------------------------------------------- #
+
+def get_localsystemid():
+    f = open("/etc/sysconfig/rhn/systemid", "r")
+    content = f.read()
+    loc_id = re.search(r'\b(ID-)(\d{10})' ,content)
+    return loc_id.group(2)
+
+# ------------------------------------------------------- #
+
+def subscribe_channels(channels, client, session, sysname, sys_id):
+    c = base_channels(client, session, sys_id)
+    c.append(channels)
+    return client.channel.software.setSystemChannels(session, sys_id, c)
+
+# ------------------------------------------------------- #
+
+def unsubscribe_channels(channels, client, session, sysname, sys_id):
+    c = base_channels(client, session, sys_id)
+    c.remove(channels)
+    return client.channel.software.setSystemChannels(session, sys_id, c)
+
+# ------------------------------------------------------- #
+
+def base_channels(client, session, sys_id):
+    basechan = client.channel.software.listSystemChannels(session, sys_id)
+    chans = [item['channel_label'] for item in basechan]
+    return chans
+
+# ------------------------------------------------------- #
+
+
+def main():
+
+    module = AnsibleModule(
+        argument_spec = dict(
+            state = dict(default='present', choices=['present', 'absent']),
+            name = dict(required=True),
+            sysname = dict(required=True),
+            url = dict(required=True),
+        )
+#        supports_check_mode=True
+    )
+
+    state = module.params['state']
+    channelname = module.params['name']
+    systname = module.params['sysname']
+    saturl = module.params['url']
+    
+    #initialize connection
+    user = "kussenv"
+    pwd = "fubar2000"
+    client = xmlrpclib.Server(saturl, verbose=0)
+    session = client.auth.login(user, pwd)
+     
+    # get systemid
+    sys_id = get_systemid(client, session, systname)
+
+    # get channels for system
+    chans = base_channels(client, session, sys_id)
+    
+    
+    if state == 'present':
+        if channelname in chans:
+            module.exit_json(changed=False, msg="Channel %s already exists" % channelname)
+
+        else:
+            subscribe_channels(channelname, client, session, systname, sys_id)
+            module.exit_json(changed=True, msg="Channel %s added" % channelname)
+
+    if state == 'absent':
+        if not channelname in chans:
+            module.exit_json(changed=False, msg="System not subscribed to channel %s." % channelname)
+        else:
+            unsubscribe_channels(channelname, client, session, systname, sys_id)
+            module.exit_json(changed=True, msg="Channel %s removed" % channelname)
+
+    client.auth.logout(session)
+
+
+# include magic from lib/ansible/module_common.py
+#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
+main()

From 8b3b6c0c70b679500beaace2ae9eb69e126be91e Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sat, 23 Mar 2013 16:32:10 +0100
Subject: [PATCH 2/6] added rhn_channel module

---
 rhn_channel.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rhn_channel.py b/rhn_channel.py
index ca24ee3e63f..e7a8e225880 100755
--- a/rhn_channel.py
+++ b/rhn_channel.py
@@ -103,8 +103,8 @@ def main():
     saturl = module.params['url']
     
     #initialize connection
-    user = "kussenv"
-    pwd = "fubar2000"
+    user = ""
+    pwd = ""
     client = xmlrpclib.Server(saturl, verbose=0)
     session = client.auth.login(user, pwd)
      

From 65c2dd279d6b9bfa629eacee6aaed49d90789975 Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sat, 23 Mar 2013 16:33:14 +0100
Subject: [PATCH 3/6] renamed module

---
 rhn_channel.py => rhn_channel | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename rhn_channel.py => rhn_channel (100%)

diff --git a/rhn_channel.py b/rhn_channel
similarity index 100%
rename from rhn_channel.py
rename to rhn_channel

From f2cf88001308a7a72eb6b119e3c3119b8ea6981e Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sat, 23 Mar 2013 22:04:48 +0100
Subject: [PATCH 4/6] Made user and password options that can be passed to the
 module

---
 rhn_channel | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/rhn_channel b/rhn_channel
index e7a8e225880..1c09c662767 100755
--- a/rhn_channel
+++ b/rhn_channel
@@ -31,9 +31,18 @@ options:
         description: 
             - The full url to the rhn/sattelite api
         required: true
+        default: https://rhn.redhat.com/rpc/api
+    user:
+        description:
+            - the user to login to rhn/sattelite
+        required: true
+    pwd:
+        description:
+            - the user's password
+        required: true
 examples:
     - code: rhn_channel name=rhel-x86_64-server-v2vwin-6  sysname=server01
-      url=https://rhn.redhat.com/rpc/api
+      url=https://rhn.redhat.com/rpc/api user=rhnuser pwd=guessme
       description: add software channel rhel-x86_64-server-v2vwin-6 
                    to server01 in Red Hat Network
 '''
@@ -93,6 +102,8 @@ def main():
             name = dict(required=True),
             sysname = dict(required=True),
             url = dict(required=True),
+            user = dict(required=True),
+            pwd = dict(required=True),
         )
 #        supports_check_mode=True
     )
@@ -101,10 +112,10 @@ def main():
     channelname = module.params['name']
     systname = module.params['sysname']
     saturl = module.params['url']
+    user = module.params['user']
+    pwd = module.params['pwd']
     
     #initialize connection
-    user = ""
-    pwd = ""
     client = xmlrpclib.Server(saturl, verbose=0)
     session = client.auth.login(user, pwd)
      
@@ -125,7 +136,7 @@ def main():
 
     if state == 'absent':
         if not channelname in chans:
-            module.exit_json(changed=False, msg="System not subscribed to channel %s." % channelname)
+            module.exit_json(changed=False, msg="Not subscribed to channel %s." % channelname)
         else:
             unsubscribe_channels(channelname, client, session, systname, sys_id)
             module.exit_json(changed=True, msg="Channel %s removed" % channelname)

From c99d78290b3f518ce1d927ffeba472710e6b131e Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sun, 24 Mar 2013 09:16:58 +0100
Subject: [PATCH 5/6] Removed info about hard coded user/pwd in the
 documentation part

---
 rhn_channel | 2 --
 1 file changed, 2 deletions(-)

diff --git a/rhn_channel b/rhn_channel
index 1c09c662767..5f40120124b 100755
--- a/rhn_channel
+++ b/rhn_channel
@@ -12,8 +12,6 @@ notes:
     - this module fetches the systemid from rhn. A function 
       to use the local systemid is provided (get_localsystem)
       but not integrated
-    - Username and password are currently hardcoded in the main
-      section
 requirements:
     - none
 options:

From 231233d9619bdb5c270435b2b4572ae67551d9fb Mon Sep 17 00:00:00 2001
From: Vincent Van der Kussen <vincent@vanderkussen.org>
Date: Sun, 24 Mar 2013 19:45:39 +0100
Subject: [PATCH 6/6] Removed false info

---
 rhn_channel | 1 -
 1 file changed, 1 deletion(-)

diff --git a/rhn_channel b/rhn_channel
index 5f40120124b..fa5df9d4535 100755
--- a/rhn_channel
+++ b/rhn_channel
@@ -29,7 +29,6 @@ options:
         description: 
             - The full url to the rhn/sattelite api
         required: true
-        default: https://rhn.redhat.com/rpc/api
     user:
         description:
             - the user to login to rhn/sattelite