Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Adding a new module 'vcd_resources' to add/delete/update
Browse files Browse the repository at this point in the history
infra resources with VCD.

Work towards: #175

Signed-off-by: mukultaneja <mtaneja@vmware.com>
  • Loading branch information
mukultaneja committed May 5, 2021
1 parent 921139f commit e7c9ed7
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 1 deletion.
1 change: 1 addition & 0 deletions module_utils/vcd_resources_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NSXT_MANAGER_ENDPOINT = "/api/admin/extension/nsxtManagers"
205 changes: 205 additions & 0 deletions modules/vcd_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Copyright © 2018 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause OR GPL-3.0-only

# !/usr/bin/python

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

DOCUMENTATION = '''
---
module: vcd_resources
short_description: Add/Delete/Update VCD Infrastructure resources
version_added: "2.4"
description:
- Add/Delete/Update VCD Infrastructure resources
options:
user:
description:
- vCloud Director user name
required: false
password:
description:
- vCloud Director user password
required: false
host:
description:
- vCloud Director host address
required: false
org:
description:
- Organization name on vCloud Director to access
required: false
api_version:
description:
- Pyvcloud API version
required: false
verify_ssl_certs:
description:
- whether to use secure connection to vCloud Director host
required: false
nsxts:
description:
- List of network type of Infrastructure resource
required: true
state:
description:
- state of vcd resource ('present'/'absent'/'update').
required: false
operation:
description:
- operations on vcd resource ('read').
required: false
author:
- mtaneja@vmware.com
'''

EXAMPLES = '''
- name: add vcd resources
vcd_resources
nsxt:
url: ""
username: ""
password: ""
networkProviderScope: ""
state: "present"
register: output
'''

RETURN = '''
msg: success/failure message corresponding to resource state
changed: true if resource has been changed
'''

import requests
from ansible.module_utils.vcd import VcdAnsibleModule
from pyvcloud.vcd.nsxt_extension import NsxtExtension
from pyvcloud.vcd.exceptions import EntityNotFoundException, BadRequestException

VCD_RESOURCES_STATES = ['present', 'absent', 'update']
VCD_RESOURCES_OPERATIONS = ['list']


def vcd_resources_argument_spec():
return dict(
nsxts=dict(type='list', required=False),
state=dict(choices=VCD_RESOURCES_STATES, required=True),
)


class VcdResources(VcdAnsibleModule):
def __init__(self, **kwargs):
super(VcdResources, self).__init__(**kwargs)
self.nsxt_extension = NsxtExtension(self.client)

def manage_states(self):
state = self.params.get('state')
if state == "present":
return self.add()

if state == "absent":
return self.delete()

if state == "update":
return self.update()

def get(self, name):
return self.nsxt_extension.get(name)

def add(self):
nsxts = self.params.get('nsxts')
response = dict()
response['changed'] = False
response['msg'] = list()

for nsxt in nsxts:
try:
name = nsxt.get('name')
self.get(name)
response['msg'].append('Nsx-T manager {0} is already exists'.format(name))
except EntityNotFoundException:
url = nsxt.get('url')
username = nsxt.get('username')
password = nsxt.get('password')
self.nsxt_extension.add(name, url, username, password)
response['msg'].append('NSX-T Manager {0} is added'.format(name))
response['changed'] = True

return response

def delete(self):
nsxts = self.params.get('nsxts')
response = dict()
response['changed'] = False
response['msg'] = list()

for nsxt in nsxts:
try:
name = nsxt.get('name')
self.get(name)
self.nsxt_extension.delete(name)
response['msg'].append('NSX-T Manager {0} is deleted'.format(name))
response['changed'] = True
except EntityNotFoundException:
response['msg'].append('NSX-T Manager {0} is not present'.format(name))


return response

def update(self):
nsxts = self.params.get('nsxts')
response = dict()
response['changed'] = False
response['msg'] = list()

for nsxt in nsxts:
try:
name = nsxt.get('name')
self.get(name)
new_name = nsxt.get('new_name')
url = nsxt.get('url')
username = nsxt.get('username')
password = nsxt.get('password')
self.nsxt_extension.update(name, new_name, url, username, password)
response['msg'].append('NSX-T Manager {0} is updated'.format(name))
response['changed'] = True
except EntityNotFoundException:
response['msg'].append('NSX-T Manager {0} is not present'.format(name))

return response

def read(self):
pass


def main():
argument_spec = vcd_resources_argument_spec()
response = dict(msg=dict(type='str'))
module = VcdResources(argument_spec=argument_spec, supports_check_mode=True)

try:
if module.check_mode:
response = dict()
response['changed'] = False
response['msg'] = "skipped, running in check mode"
response['skipped'] = True
elif module.params.get('state'):
response = module.manage_states()
elif module.params.get('operation'):
response = module.manage_operations()
else:
raise Exception('Please provide state/operation for resource')
except Exception as error:
response['msg'] = error.__str__()
module.fail_json(**response)
else:
module.exit_json(**response)


if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pyvcloud
lxml
lxml
requests

0 comments on commit e7c9ed7

Please sign in to comment.