Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recovery Services Vault #210 #254

Merged
merged 37 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
217f8c2
Resolved #234
Sep 7, 2020
dd0129f
Merge branch 'dev' into dev
suyeb786 Sep 7, 2020
f1ce08e
Adjusted code as per review comments
Sep 8, 2020
4ae121b
Updated the main.yaml file extension and registered module in pr-pipe…
Sep 8, 2020
0b4dece
Removed azure_rm_backupazurevm_info from pr-pipelines.yaml
Sep 8, 2020
85c4d8b
Updated meta/main.yaml to meta/main.yml
Sep 8, 2020
b162337
removed meta/main.yaml
Sep 8, 2020
48b6d8d
Updated module with latest review comments
Sep 8, 2020
7c8d8e5
Adjusted task/main.yml as per the recommendation
Sep 8, 2020
6e9a707
Adjusted long string with column size 80
Sep 8, 2020
77608ee
Update azure_rm_backupazurevm_info.py
suyeb786 Sep 9, 2020
75e3fc3
Update azure_rm_backupazurevm_info.py
suyeb786 Sep 9, 2020
065b703
Updated azure_rm_backupazurevm_info with latest review comments
Sep 9, 2020
e12918e
Merge branch 'dev' into dev
suyeb786 Sep 9, 2020
ad48576
Updated tests/sanity/ignore-2.xx
Sep 9, 2020
27b74e9
Merge branch 'dev' of https://github.com/suyeb786/azure-234 into dev
Sep 9, 2020
05599af
Udpated tests/sanity/ignore-2.xx
Sep 9, 2020
34fe106
Updated ignore-2.9.txt(removed lines 186-189) and tasks/main.yml(remo…
Sep 9, 2020
85917b2
Resolved #210 Recovery Service Vault
Sep 9, 2020
d00dbc2
Resolved Merge Conflict
Sep 9, 2020
25507f8
Adjusted the state and added provision for deleting RSV
Sep 10, 2020
1ec5760
Updated Class Name
Sep 10, 2020
610ba57
Adjusted the code with latest review comments
Sep 11, 2020
7e9519b
Merge branch 'dev' into dev
suyeb786 Sep 11, 2020
a671491
Added new line at end of aliases main.yml files
Sep 11, 2020
3c26d89
Merge branch 'dev' of https://github.com/suyeb786/azure-234 into dev
Sep 11, 2020
6230f42
Adjusted tasks/main.yml
Sep 14, 2020
ab9833c
removed line no 3 from tasks/main.yml
Sep 14, 2020
b9d7bf7
Read Recovery Vault Details Pre Creation Logic Integrated
Sep 14, 2020
4fd850e
Added idempotent test and adjusted module return type
Sep 16, 2020
289a668
Merge branch 'dev' into dev
Fred-sun Sep 16, 2020
7d5cf97
Adjusted code as per review comments
Sep 16, 2020
efcdd10
Merge branch 'dev' of https://github.com/suyeb786/azure-234 into dev
Sep 16, 2020
3285928
'2016-06-01' is required for create, delete as per official doc
Sep 16, 2020
1469625
Adjusted code as per latest review comments
Sep 16, 2020
4285de0
Adjusted indentation
Sep 16, 2020
e7c5412
Removed whitespace from line 175
Sep 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 319 additions & 0 deletions plugins/modules/azure_rm_recoveryservicesvault.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
#!/usr/bin/python
#
# Copyright (c) 2020 Suyeb Ansari (@suyeb786)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'],
'supported_by': 'community'}

DOCUMENTATION = \
'''
---
module: azure_rm_recoveryservicesvault
version_added: '1.1.0'
short_description: Create and Delete Azure Recovery Services vault
description:
- Create or Delete Azure Recovery Services vault.
options:
resource_group:
description:
- The name of the resource group.
required: true
type: str
name:
description:
- The name of the Azure Recovery Service Vault.
required: true
type: str
location:
description:
- Azure Resource location.
required: true
type: str
state:
suyeb786 marked this conversation as resolved.
Show resolved Hide resolved
description:
- Assert the state of the protection item.
- Use C(present) for Creating Azure Recovery Service Vault.
- Use C(absent) for Deleting Azure Recovery Service Vault.
default: present
type: str
choices:
- present
- absent
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Suyeb Ansari (@suyeb786)
'''

EXAMPLES = '''
- name: Create/Update Azure Recovery Service vault
azure_rm_recoveryservicesvault:
resource_group: 'myResourceGroup'
name: 'testVault'
location: 'westeurope'
state: 'present'
- name: Delete Recovery Service Vault
azure_rm_recoveryservicesvault:
resource_group: 'myResourceGroup'
name: 'testVault'
location: 'westeurope'
state: 'absent'
'''

RETURN = '''
response:
description:
- The response about the current state of the recovery services vault.
returned: always
type: complex
contains:
etag:
description:
- A unique read-only string that changes whenever the resource create.
returned: always
type: str
sample: "datetime'2020-09-16T02%3A44%3A27.834293Z'"
id:
description:
- Resource ID.
returned: always
type: str
sample: "/subscriptions/xxxxxxx/resourceGroups/resourcegroup_name/ \
providers/Microsoft.RecoveryServices/vaults/rev_name"
location:
description:
- The location of the resource.
returned: always
type: str
sample: "eastus"
name:
description:
- Name of the recovery services vault name.
returned: always
type: str
sample: revault_name
properties:
description:
- The recovery service vault properties.
returned: always
type: dict
sample: {
"privateEndpointStateForBackup": "None",
"privateEndpointStateForSiteRecovery": "None",
"provisioningState": "Succeeded"
}
sku:
description:
- The sku type of the recovery service vault.
returned: always
type: str
sample: Standard
type:
description:
- The type of the recovery service vault.
returned: always
type: str
sample: "Microsoft.RecoveryServices/vaults"
'''

Fred-sun marked this conversation as resolved.
Show resolved Hide resolved
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
import re
import json
import time

try:
from msrestazure.azure_exceptions import CloudError
except ImportError:
# This is handled in azure_rm_common
pass

suyeb786 marked this conversation as resolved.
Show resolved Hide resolved

class AzureRMRecoveryServicesVault(AzureRMModuleBaseExt):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
name=dict(
type='str',
required=True
),
location=dict(
type='str',
required=True
),
state=dict(
type='str',
default='present',
choices=['present', 'absent']
)
)

self.resource_group = None
self.name = None
self.location = None
self.state = None

self.results = dict(changed=False)
self.mgmt_client = None
self.url = None
self.status_code = [200, 201, 202, 204]

self.body = {}
self.query_parameters = {}
self.query_parameters['api-version'] = None
self.header_parameters = {}
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8'

suyeb786 marked this conversation as resolved.
Show resolved Hide resolved
super(AzureRMRecoveryServicesVault, self).__init__(derived_arg_spec=self.module_arg_spec,
Fred-sun marked this conversation as resolved.
Show resolved Hide resolved
supports_check_mode=True,
supports_tags=True
)

def get_api_version(self):
return '2016-06-01'

def get_url(self):
if self.state == 'present' or self.state == 'absent':
return '/subscriptions/' \
+ self.subscription_id \
+ '/resourceGroups/' \
+ self.resource_group \
+ '/providers/Microsoft.RecoveryServices' \
+ '/vaults' + '/' \
+ self.name

def get_body(self):
if self.state == 'present':
return {
"properties": {},
"sku": {
"name": "Standard"
},
"location": self.location
}
else:
return {}

def exec_module(self, **kwargs):
for key in list(self.module_arg_spec.keys()):
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
self.body[key] = kwargs[key]

self.inflate_parameters(self.module_arg_spec, self.body, 0)

self.query_parameters['api-version'] = self.get_api_version()
self.url = self.get_url()
self.body = self.get_body()
old_response = None
response = None

self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
base_url=self._cloud_environment.endpoints.resource_manager)

old_response = self.get_resource()

changed = False
if self.state == 'present':
if old_response is False:
changed = True
response = self.create_recovery_service_vault()
else:
changed = False
response = old_response
if self.state == 'absent':
changed = True
response = self.delete_recovery_service_vault()

self.results['response'] = response
self.results['changed'] = changed

return self.results

def create_recovery_service_vault(self):
# self.log('Creating Recovery Service Vault Name {0}'.format(self.))
try:
response = self.mgmt_client.query(
self.url,
'PUT',
self.query_parameters,
self.header_parameters,
self.body,
self.status_code,
600,
30,
)
except CloudError as e:
self.log('Error in creating Azure Recovery Service Vault.')
self.fail('Error in creating Azure Recovery Service Vault {0}'.format(str(e)))

try:
response = json.loads(response.text)
except Exception:
response = {'text': response.text}

return response

def delete_recovery_service_vault(self):
# self.log('Deleting Recovery Service Vault {0}'.format(self.))
try:
response = self.mgmt_client.query(
self.url,
'DELETE',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30,
)
except CloudError as e:
self.log('Error attempting to delete Azure Recovery Service Vault.')
self.fail('Error while deleting Azure Recovery Service Vault: {0}'.format(str(e)))

try:
response = json.loads(response.text)
except Exception:
response = {'text': response.text}
return response

def get_resource(self):
# self.log('Get Recovery Service Vault Name {0}'.format(self.))
found = False
try:
response = self.mgmt_client.query(
self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30,
)
found = True
except CloudError as e:
self.log('Recovery Service Vault Does not exist.')
if found is True:
response = json.loads(response.text)
return response
else:
return False

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines


def main():
AzureRMRecoveryServicesVault()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines


if __name__ == '__main__':
main()
Loading