#!/usr/bin/python

DOCUMENTATION = '''
---
module: pvol
author: "Travis Dixon (@thetrav)"
version_added: "0.1"
short_description: Manage LVM physical volumes
requirements: [ lvm2 ]
description:
    - Manage LVM physical volumes
options:
    device:
        required: true
        description:
            - Device to select (eg: /dev/sdb1)
    state:
        default: present
        choices: [ present, absent ]
        description:
            - Device to select (eg: /dev/sdb1)
'''

EXAMPLES = '''
# add a physical volume
- partition: device=/dev/sdb1
# remove a physical volume
- partition: device=/dev/sdb1 state=absent
'''

def read_fixed_width_table(table):
    titles = table[0].split()

    column_starts = [table[0].index(title) for title in titles]
    column_ends = [num - 1 for num in column_starts[1:]] + [len(table[0])]
    columns = zip(column_starts, column_ends)

    values = [[line[column[0]:column[1]].strip() for column in columns] for line in table[1:]]
    return [dict(zip([title.strip() for title in titles], line)) for line in values]


class Lvm(object):
    def __init__(self, run_command):
        self.command_runner = run_command

    def run_command(self, cmd):
        rc, stdout, stderr = self.command_runner(cmd)
        if rc != 0:
            raise ValueError("{} {} {} {}".format(rc, stdout, stderr, cmd))
        return (stdout, stderr)

    def refresh(self):
        stdout, stderr = self.run_command("pvs")
        rows = read_fixed_width_table(stdout.split("\n"))
        self.volumes = [row['PV'] for row in rows]

    def set_volume(self, device):
        self.refresh()
        if device in self.volumes:
            return False
        stdout, stderr = self.run_command("pvcreate {}".format(device))
        self.refresh()
        return True


    def remove_volume(self, device):
        self.refresh()
        if device not in self.volumes:
            return False
        stdout, stderr = self.run_command("pvremove {}".format(device))
        self.refresh()
        return True

def main():
    module = AnsibleModule(
        argument_spec = dict(
            device=dict(required=True, type='str'),
            state=dict(default='present', choices=['present', 'absent'], type='str')
        ),
        supports_check_mode=True)
    def param(key):
        return module.params[key]

    result = {}

    lvm = Lvm(module.run_command)
    state = param('state')

    if state == 'present':
        result['changed'] = lvm.set_volume(param('device'))
    if state == 'absent':
        result['changed'] = lvm.remove_volume(param('device'))
    
    result['pvols'] = lvm.volumes
    module.exit_json(**result)


# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
    main()

@@ -670,16 +670,44 @@
 titles =
+ %5Btitle.strip() for title in
  table%5B0
@@ -715,17 +715,22 @@
 .split()
-%0A
+%5D%0A    
 %0A    col
@@ -907,17 +907,42 @@
 n_ends)%0A
-%0A
+    def record(line):%0A    
     valu
@@ -946,17 +946,16 @@
 alues = 
-%5B
 %5Bline%5Bco
@@ -1007,32 +1007,13 @@
 mns%5D
- for line in table%5B1:%5D%5D%0A
+%0A    
     
@@ -1019,17 +1019,16 @@
  return 
-%5B
 dict(zip
@@ -1032,51 +1032,50 @@
 zip(
-%5B
 title
-.strip() for title in titles%5D, 
+s, values))%0A    return %5B record(
 line)
-)
  for
@@ -1083,22 +1083,26 @@
 line in 
-values
+table%5B1:%5D 
 %5D%0A%0A%0Aclas

improve line reading code for readability