| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_cloudmanager_info |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
|
| 15 |
module: na_cloudmanager_info |
| 16 |
short_description: NetApp Cloud Manager info |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.cloudmanager.netapp.cloudmanager |
| 19 |
version_added: '21.4.0' |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
|
| 22 |
description: |
| 23 |
- This module allows you to gather various information about cloudmanager using REST APIs. |
| 24 |
|
| 25 |
options: |
| 26 |
client_id: |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- The connector ID of the Cloud Manager Connector. |
| 31 |
|
| 32 |
gather_subsets: |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
description: |
| 36 |
- When supplied, this argument will restrict the information collected to a given subset. |
| 37 |
- Possible values for this argument include |
| 38 |
- 'working_environments_info' |
| 39 |
- 'aggregates_info' |
| 40 |
- 'accounts_info' |
| 41 |
- 'account_info' |
| 42 |
- 'agents_info' |
| 43 |
- 'active_agents_info' |
| 44 |
default: 'all' |
| 45 |
|
| 46 |
notes: |
| 47 |
- Support check_mode |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = """ |
| 51 |
- name: Get all available subsets |
| 52 |
netapp.cloudmanager.na_cloudmanager_info: |
| 53 |
client_id: "{{ client_id }}" |
| 54 |
refresh_token: "{{ refresh_token }}" |
| 55 |
gather_subsets: |
| 56 |
- all |
| 57 |
|
| 58 |
- name: Collect data for cloud manager with indicated subsets |
| 59 |
netapp.cloudmanager.na_cloudmanager_info: |
| 60 |
client_id: "{{ client_id }}" |
| 61 |
refresh_token: "{{ refresh_token }}" |
| 62 |
gather_subsets: |
| 63 |
- aggregates_info |
| 64 |
- working_environments_info |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = """ |
| 68 |
info: |
| 69 |
description: |
| 70 |
- a dictionary of collected subsets |
| 71 |
- each subset if in JSON format |
| 72 |
returned: success |
| 73 |
type: dict |
| 74 |
sample: '{ |
| 75 |
"info": { |
| 76 |
"working_environments_info": [ |
| 77 |
{ |
| 78 |
"azureVsaWorkingEnvironments": [], |
| 79 |
"gcpVsaWorkingEnvironments": [], |
| 80 |
"onPremWorkingEnvironments": [], |
| 81 |
"vsaWorkingEnvironments": [ |
| 82 |
{ |
| 83 |
"actionsRequired": null, |
| 84 |
"activeActions": null, |
| 85 |
"awsProperties": null, |
| 86 |
"capacityFeatures": null, |
| 87 |
"cbsProperties": null, |
| 88 |
"cloudProviderName": "Amazon", |
| 89 |
"cloudSyncProperties": null, |
| 90 |
"clusterProperties": null, |
| 91 |
"complianceProperties": null, |
| 92 |
"creatorUserEmail": "samlp|NetAppSAML|test_user", |
| 93 |
"cronJobSchedules": null, |
| 94 |
"encryptionProperties": null, |
| 95 |
"fpolicyProperties": null, |
| 96 |
"haProperties": null, |
| 97 |
"interClusterLifs": null, |
| 98 |
"isHA": false, |
| 99 |
"k8sProperties": null, |
| 100 |
"monitoringProperties": null, |
| 101 |
"name": "testAWS", |
| 102 |
"ontapClusterProperties": null, |
| 103 |
"publicId": "VsaWorkingEnvironment-3txYJOsX", |
| 104 |
"replicationProperties": null, |
| 105 |
"reservedSize": null, |
| 106 |
"saasProperties": null, |
| 107 |
"schedules": null, |
| 108 |
"snapshotPolicies": null, |
| 109 |
"status": null, |
| 110 |
"supportRegistrationInformation": [], |
| 111 |
"supportRegistrationProperties": null, |
| 112 |
"supportedFeatures": null, |
| 113 |
"svmName": "svm_testAWS", |
| 114 |
"svms": null, |
| 115 |
"tenantId": "Tenant-2345", |
| 116 |
"workingEnvironmentType": "VSA" |
| 117 |
} |
| 118 |
] |
| 119 |
}, |
| 120 |
null |
| 121 |
] |
| 122 |
} |
| 123 |
}' |
| 124 |
""" |
| 125 |
|
| 126 |
from ansible.module_utils.basic import AnsibleModule |
| 127 |
import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils |
| 128 |
from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import NetAppModule |
| 129 |
from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp import CloudManagerRestAPI |
| 130 |
|
| 131 |
|
| 132 |
class NetAppCloudmanagerInfo(object): |
| 133 |
''' |
| 134 |
Contains methods to parse arguments, |
| 135 |
derive details of CloudmanagerInfo objects |
| 136 |
and send requests to CloudmanagerInfo via |
| 137 |
the restApi |
| 138 |
''' |
| 139 |
|
| 140 |
def __init__(self): |
| 141 |
self.argument_spec = netapp_utils.cloudmanager_host_argument_spec() |
| 142 |
self.argument_spec.update(dict( |
| 143 |
gather_subsets=dict(type='list', elements='str', default='all'), |
| 144 |
client_id=dict(required=True, type='str'), |
| 145 |
)) |
| 146 |
|
| 147 |
self.module = AnsibleModule( |
| 148 |
argument_spec=self.argument_spec, |
| 149 |
required_one_of=[['refresh_token', 'sa_client_id']], |
| 150 |
required_together=[['sa_client_id', 'sa_secret_key']], |
| 151 |
supports_check_mode=True |
| 152 |
) |
| 153 |
|
| 154 |
self.na_helper = NetAppModule() |
| 155 |
# set up state variables |
| 156 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 157 |
# Calling generic rest_api class |
| 158 |
self.rest_api = CloudManagerRestAPI(self.module) |
| 159 |
self.rest_api.url += self.rest_api.environment_data['CLOUD_MANAGER_HOST'] |
| 160 |
self.rest_api.api_root_path = None |
| 161 |
self.methods = dict( |
| 162 |
working_environments_info=self.na_helper.get_working_environments_info, |
| 163 |
aggregates_info=self.get_aggregates_info, |
| 164 |
accounts_info=self.na_helper.get_accounts_info, |
| 165 |
account_info=self.na_helper.get_account_info, |
| 166 |
agents_info=self.na_helper.get_agents_info, |
| 167 |
active_agents_info=self.na_helper.get_active_agents_info, |
| 168 |
) |
| 169 |
self.headers = {} |
| 170 |
if 'client_id' in self.parameters: |
| 171 |
self.headers['X-Agent-Id'] = self.rest_api.format_client_id(self.parameters['client_id']) |
| 172 |
|
| 173 |
def get_aggregates_info(self, rest_api, headers): |
| 174 |
''' |
| 175 |
Get aggregates info: there are 4 types of working environments. |
| 176 |
Each of the aggregates will be categorized by working environment type and working environment id |
| 177 |
''' |
| 178 |
aggregates = {} |
| 179 |
# get list of working environments |
| 180 |
working_environments, error = self.na_helper.get_working_environments_info(rest_api, headers) |
| 181 |
if error is not None: |
| 182 |
self.module.fail_json(msg="Error: Failed to get working environments: %s" % str(error)) |
| 183 |
# Four types of working environments: |
| 184 |
# azureVsaWorkingEnvironments, gcpVsaWorkingEnvironments, onPremWorkingEnvironments, vsaWorkingEnvironments |
| 185 |
for working_env_type in working_environments: |
| 186 |
we_aggregates = {} |
| 187 |
# get aggregates for each working environment |
| 188 |
for we in working_environments[working_env_type]: |
| 189 |
provider = we['cloudProviderName'] |
| 190 |
working_environment_id = we['publicId'] |
| 191 |
self.na_helper.set_api_root_path(we, rest_api) |
| 192 |
if provider != "Amazon": |
| 193 |
api = '%s/aggregates/%s' % (rest_api.api_root_path, working_environment_id) |
| 194 |
else: |
| 195 |
api = '%s/aggregates?workingEnvironmentId=%s' % (rest_api.api_root_path, working_environment_id) |
| 196 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 197 |
if error: |
| 198 |
self.module.fail_json(msg="Error: Failed to get aggregate list: %s" % str(error)) |
| 199 |
we_aggregates[working_environment_id] = response |
| 200 |
aggregates[working_env_type] = we_aggregates |
| 201 |
return aggregates |
| 202 |
|
| 203 |
def get_info(self, func, rest_api): |
| 204 |
''' |
| 205 |
Main get info function |
| 206 |
''' |
| 207 |
return self.methods[func](rest_api, self.headers) |
| 208 |
|
| 209 |
def apply(self): |
| 210 |
''' |
| 211 |
Apply action to the Cloud Manager |
| 212 |
:return: None |
| 213 |
''' |
| 214 |
info = {} |
| 215 |
if 'all' in self.parameters['gather_subsets']: |
| 216 |
self.parameters['gather_subsets'] = self.methods.keys() |
| 217 |
for func in self.parameters['gather_subsets']: |
| 218 |
if func in self.methods: |
| 219 |
info[func] = self.get_info(func, self.rest_api) |
| 220 |
else: |
| 221 |
msg = '%s is not a valid gather_subset. Only %s are allowed' % (func, self.methods.keys()) |
| 222 |
self.module.fail_json(msg=msg) |
| 223 |
self.module.exit_json(changed=False, info=info) |
| 224 |
|
| 225 |
|
| 226 |
def main(): |
| 227 |
''' |
| 228 |
Main function |
| 229 |
''' |
| 230 |
na_cloudmanager_info = NetAppCloudmanagerInfo() |
| 231 |
na_cloudmanager_info.apply() |
| 232 |
|
| 233 |
|
| 234 |
if __name__ == '__main__': |
| 235 |
main() |