os_namespace_resources_deletion.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2018 Red Hat, Inc. and/or its affiliates
  4. # and other contributors as indicated by the @author tags.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # pylint: disable=unused-wildcard-import,wildcard-import,unused-import,redefined-builtin
  18. ''' os_namespace_resources_deletion '''
  19. import keystoneauth1
  20. from ansible.module_utils.basic import AnsibleModule
  21. try:
  22. import shade
  23. HAS_SHADE = True
  24. except ImportError:
  25. HAS_SHADE = False
  26. DOCUMENTATION = '''
  27. ---
  28. module: os_namespace_resources_deletion
  29. short_description: Delete network resources associated to the namespace
  30. description:
  31. - Detach namespace's subnet from the router and delete the network
  32. author:
  33. - "Luis Tomas Bolivar <ltomasbo@redhat.com>"
  34. '''
  35. RETURN = '''
  36. '''
  37. def main():
  38. ''' Main module function '''
  39. module = AnsibleModule(
  40. argument_spec=dict(
  41. router_id=dict(default=False, type='str'),
  42. subnet_id=dict(default=False, type='str'),
  43. net_id=dict(default=False, type='str'),
  44. ),
  45. supports_check_mode=True,
  46. )
  47. if not HAS_SHADE:
  48. module.fail_json(msg='shade is required for this module')
  49. try:
  50. cloud = shade.openstack_cloud()
  51. # pylint: disable=broad-except
  52. except Exception:
  53. module.fail_json(msg='Failed to connect to the cloud')
  54. try:
  55. adapter = keystoneauth1.adapter.Adapter(
  56. session=cloud.keystone_session,
  57. service_type=cloud.cloud_config.get_service_type('network'),
  58. interface=cloud.cloud_config.get_interface('network'),
  59. endpoint_override=cloud.cloud_config.get_endpoint('network'),
  60. version=cloud.cloud_config.get_api_version('network'))
  61. # pylint: disable=broad-except
  62. except Exception:
  63. module.fail_json(msg='Failed to get an adapter to talk to the Neutron '
  64. 'API')
  65. try:
  66. subnet_info = {"subnet_id": module.params['subnet_id'].encode('ascii')}
  67. data = {'data': str(subnet_info).replace('\'', '\"')}
  68. adapter.put('/routers/' + module.params['router_id'] + '/remove_router_interface', **data)
  69. # pylint: disable=broad-except
  70. except Exception:
  71. module.fail_json(msg='Failed to detach subnet from the router')
  72. try:
  73. adapter.delete('/networks/' + module.params['net_id'])
  74. # pylint: disable=broad-except
  75. except Exception:
  76. module.fail_json(msg='Failed to delete Neutron Network associated to the namespace')
  77. module.exit_json(
  78. changed=True)
  79. if __name__ == '__main__':
  80. main()