-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbigv-disc
executable file
·140 lines (124 loc) · 4.73 KB
/
bigv-disc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python
#encoding: utf-8 -*-
import json
import sys
sys.path.append(".")
import bigv
DOCUMENTATION = '''
---
module: bigv-disc
version_added: "1.4.3"
short_description: Create/Delete Discs from BigV
description:
- Create or Remove discs attached to machines in BigV.
options:
login_username:
description:
- login username to authenticate to BigV
required: true
default: admin
login_password:
description:
- Password for user to login with
required: true
default: 'yes'
login_yubikey:
description:
- Yubikey OTP
required: false
vm_name:
description:
- Full name of the VM to operate on, e.g. host.group.account.uk0.bigv.io
required: true
disc_label:
description:
- Label for the disc
required: true
disc_grade:
description:
- Grade for the disc, must be one of 'sata', 'ssd' or 'archive'
required: false
default: 'sata'
disc_size:
description:
- Size for the disc, in GiB, as a number eg 25
required: false
default: 25
purge:
description:
- Whether or not to purge the disc when deleting.
default: false
required: false
requirements: ["bigv","requests"]
'''
EXAMPLES = '''
# Creates a disc if it doesn't exist
- bigv-disc: state: present
login_username: alice
login_password: test123
vm_name: status.testing.mycompany.uk0.bigv.io
disc_name: backups
disc_grade: archive
disc_size: 500
'''
def main():
module = AnsibleModule(
argument_spec = dict(
login_username = dict(required=True),
login_password = dict(required=True),
login_yubikey = dict(default=None),
vm_name = dict(required=True),
disc_label = dict(required=True),
disc_size = dict(required=False, default=25),
disc_grade = dict(default='sata', choices=['sata','ssd','archive']),
state = dict(default='present', choices=['absent', 'present']),
purge = dict(default=False, choices=BOOLEANS)
),
)
try:
vm_name, vm_group, vm_account, vm_location = module.params['vm_name'].split(".", 3)
except ValueError:
module.fail_json(msg="vm_name must be a full BigV host name, e.g. hostname.group.account.uk0.bigv.io")
vm_location = "https://"+vm_location
try:
account = bigv.BigVAccount(username=module.params['login_username'],
password=module.params['login_password'],
yubikey=module.params['login_yubikey'],
account=vm_account,
location=vm_location)
group = account.group(vm_group)
if group == None:
module.fail_json(msg="Group " + vm_group + " doesn't exist and group_create is false")
target = group.machine(vm_name)
if target == None:
# machine doesn't exist
module.fail_json(msg="No such vm %s" % ngrp)
else:
# machine does exist
disc = target.disc(module.params['disc_label'])
if disc == None:
if module.params['state'] == 'absent':
module.exit_json(changed=False)
else:
# disc=None but state=Present
newdisc = target.create_disc(label=module.params['disc_label'],
grade=module.params['disc_grade'],
size=module.params['disc_size'])
module.exit_json(changed=True,disc=newdisc.info())
else:
# disc exists
if module.params['state'] == 'absent':
if module.boolean(module.params['purge']):
target.disc(module.params['disc_label']).purge()
else:
target.disc(module.params['disc_label']).delete()
module.exit_json(changed=True,msg="Deleted disc %s from %s" % (module.params['disc_label'],
ngrp))
else:
# is it the same size/grade?
module.exit_json(changed=False, disc=disc.info())
module.fail_json(msg="Shouldn't reach here")
except bigv.BigVProblem as e:
module.fail_json(msg=e.msg, http_status=e.http_status, http_method=e.http_method, url=e.url)
from ansible.module_utils.basic import *
main()