1# This code is part of Ansible, but is an independent component.
2# This particular file snippet, and this file snippet only, is BSD licensed.
3
4HAS_SF_SDK = False
5try:
6    import solidfire.common
7
8    HAS_SF_SDK = True
9except Exception:
10    HAS_SF_SDK = False
11
12
13def has_sf_sdk():
14    return HAS_SF_SDK
15
16
17class NaElementSWModule(object):
18
19    def __init__(self, elem):
20        self.elem_connect = elem
21        self.parameters = dict()
22
23    def get_volume(self, volume_id):
24        """
25            Return volume details if volume exists for given volume_id
26
27            :param volume_id: volume ID
28            :type volume_id: int
29            :return: Volume dict if found, None if not found
30            :rtype: dict
31        """
32        volume_list = self.elem_connect.list_volumes(volume_ids=[volume_id])
33        for volume in volume_list.volumes:
34            if volume.volume_id == volume_id:
35                if str(volume.delete_time) == "":
36                    return volume
37        return None
38
39    def get_volume_id(self, vol_name, account_id):
40        """
41            Return volume id from the given (valid) account_id if found
42            Return None if not found
43
44            :param vol_name: Name of the volume
45            :type vol_name: str
46            :param account_id: Account ID
47            :type account_id: int
48
49            :return: Volume ID of the first matching volume if found. None if not found.
50            :rtype: int
51        """
52        volume_list = self.elem_connect.list_volumes_for_account(account_id=account_id)
53        for volume in volume_list.volumes:
54            if volume.name == vol_name:
55                # return volume_id
56                if str(volume.delete_time) == "":
57                    return volume.volume_id
58        return None
59
60    def volume_id_exists(self, volume_id):
61        """
62            Return volume_id if volume exists for given volume_id
63
64            :param volume_id: volume ID
65            :type volume_id: int
66            :return: Volume ID if found, None if not found
67            :rtype: int
68        """
69        volume_list = self.elem_connect.list_volumes(volume_ids=[volume_id])
70        for volume in volume_list.volumes:
71            if volume.volume_id == volume_id:
72                if str(volume.delete_time) == "":
73                    return volume.volume_id
74        return None
75
76    def volume_exists(self, volume, account_id):
77        """
78            Return volume_id if exists, None if not found
79
80            :param volume: Volume ID or Name
81            :type volume: str
82            :param account_id: Account ID (valid)
83            :type account_id: int
84            :return: Volume ID if found, None if not found
85        """
86        # If volume is an integer, get_by_id
87        if str(volume).isdigit():
88            volume_id = int(volume)
89            try:
90                if self.volume_id_exists(volume_id):
91                    return volume_id
92            except solidfire.common.ApiServerError:
93                # don't fail, continue and try get_by_name
94                pass
95        # get volume by name
96        volume_id = self.get_volume_id(volume, account_id)
97        return volume_id
98
99    def get_snapshot(self, snapshot_id, volume_id):
100        """
101            Return snapshot details if found
102
103            :param snapshot_id: Snapshot ID or Name
104            :type snapshot_id: str
105            :param volume_id: Account ID (valid)
106            :type volume_id: int
107            :return: Snapshot dict if found, None if not found
108            :rtype: dict
109        """
110        # mandate src_volume_id although not needed by sdk
111        snapshot_list = self.elem_connect.list_snapshots(
112            volume_id=volume_id)
113        for snapshot in snapshot_list.snapshots:
114            # if actual id is provided
115            if str(snapshot_id).isdigit() and snapshot.snapshot_id == int(snapshot_id):
116                return snapshot
117            # if snapshot name is provided
118            elif snapshot.name == snapshot_id:
119                return snapshot
120        return None
121
122    def account_exists(self, account):
123        """
124            Return account_id if account exists for given account id or name
125            Raises an exception if account does not exist
126
127            :param account: Account ID or Name
128            :type account: str
129            :return: Account ID if found, None if not found
130        """
131        # If account is an integer, get_by_id
132        if account.isdigit():
133            account_id = int(account)
134            try:
135                result = self.elem_connect.get_account_by_id(account_id=account_id)
136                if result.account.account_id == account_id:
137                    return account_id
138            except solidfire.common.ApiServerError:
139                # don't fail, continue and try get_by_name
140                pass
141        # get account by name, the method returns an Exception if account doesn't exist
142        result = self.elem_connect.get_account_by_name(username=account)
143        return result.account.account_id
144
145    def set_element_attributes(self, source):
146        """
147            Return telemetry attributes for the current execution
148
149            :param source: name of the module
150            :type source: str
151            :return: a dict containing telemetry attributes
152        """
153        attributes = {}
154        attributes['config-mgmt'] = 'ansible'
155        attributes['event-source'] = source
156        return(attributes)
157