os_namespace_resources_deletion.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 and the
  32. associated security groups
  33. author:
  34. - "Luis Tomas Bolivar <ltomasbo@redhat.com>"
  35. '''
  36. RETURN = '''
  37. '''
  38. def main():
  39. ''' Main module function '''
  40. module = AnsibleModule(
  41. argument_spec=dict(
  42. router_id=dict(default=False, type='str'),
  43. subnet_id=dict(default=False, type='str'),
  44. net_id=dict(default=False, type='str'),
  45. sg_id=dict(default=False, type='str'),
  46. ),
  47. supports_check_mode=True,
  48. )
  49. if not HAS_SHADE:
  50. module.fail_json(msg='shade is required for this module')
  51. try:
  52. cloud = shade.openstack_cloud()
  53. # pylint: disable=broad-except
  54. except Exception:
  55. module.fail_json(msg='Failed to connect to the cloud')
  56. try:
  57. adapter = keystoneauth1.adapter.Adapter(
  58. session=cloud.keystone_session,
  59. service_type=cloud.cloud_config.get_service_type('network'),
  60. interface=cloud.cloud_config.get_interface('network'),
  61. endpoint_override=cloud.cloud_config.get_endpoint('network'),
  62. version=cloud.cloud_config.get_api_version('network'))
  63. # pylint: disable=broad-except
  64. except Exception:
  65. module.fail_json(msg='Failed to get an adapter to talk to the Neutron '
  66. 'API')
  67. try:
  68. subnet_info = {"subnet_id": module.params['subnet_id'].encode('ascii')}
  69. data = {'data': str(subnet_info).replace('\'', '\"')}
  70. adapter.put('/routers/' + module.params['router_id'] + '/remove_router_interface', **data)
  71. # pylint: disable=broad-except
  72. except Exception:
  73. module.fail_json(msg='Failed to detach subnet from the router')
  74. try:
  75. adapter.delete('/networks/' + module.params['net_id'])
  76. # pylint: disable=broad-except
  77. except Exception:
  78. module.fail_json(msg='Failed to delete Neutron Network associated to the namespace')
  79. try:
  80. adapter.delete('/security-groups/' + module.params['sg_id'])
  81. # pylint: disable=broad-except
  82. except Exception:
  83. module.fail_json(msg='Failed to delete Security groups associated to the namespace')
  84. module.exit_json(
  85. changed=True)
  86. if __name__ == '__main__':
  87. main()