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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
HAS_SF_SDK = False
try:
import solidfire.common
HAS_SF_SDK = True
except:
HAS_SF_SDK = False
def has_sf_sdk():
return HAS_SF_SDK
class NaElementSWModule(object):
def __init__(self, elem):
self.elem_connect = elem
self.parameters = dict()
def get_volume(self, volume_id):
"""
Return volume details if volume exists for given volume_id
:param volume_id: volume ID
:type volume_id: int
:return: Volume dict if found, None if not found
:rtype: dict
"""
volume_list = self.elem_connect.list_volumes(volume_ids=[volume_id])
for volume in volume_list.volumes:
if volume.volume_id == volume_id:
if str(volume.delete_time) == "":
return volume
return None
def get_volume_id(self, vol_name, account_id):
"""
Return volume id from the given (valid) account_id if found
Return None if not found
:param vol_name: Name of the volume
:type vol_name: str
:param account_id: Account ID
:type account_id: int
:return: Volume ID of the first matching volume if found. None if not found.
:rtype: int
"""
volume_list = self.elem_connect.list_volumes_for_account(account_id=account_id)
for volume in volume_list.volumes:
if volume.name == vol_name:
# return volume_id
if str(volume.delete_time) == "":
return volume.volume_id
return None
def volume_id_exists(self, volume_id):
"""
Return volume_id if volume exists for given volume_id
:param volume_id: volume ID
:type volume_id: int
:return: Volume ID if found, None if not found
:rtype: int
"""
volume_list = self.elem_connect.list_volumes(volume_ids=[volume_id])
for volume in volume_list.volumes:
if volume.volume_id == volume_id:
if str(volume.delete_time) == "":
return volume.volume_id
return None
def volume_exists(self, volume, account_id):
"""
Return volume_id if exists, None if not found
:param volume: Volume ID or Name
:type volume: str
:param account_id: Account ID (valid)
:type account_id: int
:return: Volume ID if found, None if not found
"""
# If volume is an integer, get_by_id
if str(volume).isdigit():
volume_id = int(volume)
try:
if self.volume_id_exists(volume_id):
return volume_id
except solidfire.common.ApiServerError:
# don't fail, continue and try get_by_name
pass
# get volume by name
volume_id = self.get_volume_id(volume, account_id)
return volume_id
def get_snapshot(self, snapshot_id, volume_id):
"""
Return snapshot details if found
:param snapshot_id: Snapshot ID or Name
:type snapshot_id: str
:param volume_id: Account ID (valid)
:type volume_id: int
:return: Snapshot dict if found, None if not found
:rtype: dict
"""
# mandate src_volume_id although not needed by sdk
snapshot_list = self.elem_connect.list_snapshots(
volume_id=volume_id)
for snapshot in snapshot_list.snapshots:
# if actual id is provided
if str(snapshot_id).isdigit() and snapshot.snapshot_id == int(snapshot_id):
return snapshot
# if snapshot name is provided
elif snapshot.name == snapshot_id:
return snapshot
return None
def account_exists(self, account):
"""
Return account_id if account exists for given account id or name
Raises an exception if account does not exist
:param account: Account ID or Name
:type account: str
:return: Account ID if found, None if not found
"""
# If account is an integer, get_by_id
if account.isdigit():
account_id = int(account)
try:
result = self.elem_connect.get_account_by_id(account_id=account_id)
if result.account.account_id == account_id:
return account_id
except solidfire.common.ApiServerError:
# don't fail, continue and try get_by_name
pass
# get account by name, the method returns an Exception if account doesn't exist
result = self.elem_connect.get_account_by_name(username=account)
return result.account.account_id
def set_element_attributes(self, source):
"""
Return telemetry attributes for the current execution
:param source: name of the module
:type source: str
:return: a dict containing telemetry attributes
"""
attributes = {}
attributes['config-mgmt'] = 'ansible'
attributes['event-source'] = source
return(attributes)
|