def main(): argument_spec = openstack_full_argument_spec( peering=dict(type='str', required=True, aliases=['peering_id', "nexthop"]), state=dict(default='accept', choices=['present', 'absent']), destination=dict(type='str', required=True), vpc=dict(type='str', required=True, aliases=['vpc_id']), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) state = module.params['state'] peering_name = module.params['peering'] vpc_name = module.params['vpc'] cloud = connect_from_ansible(module) try: cloud.add_service(vpc_service.VpcService("vpc", aliases=['vpc1'])) # need to use the artificial endpoint without project-id cloud.add_service(vpc_service.VpcService("peervpc")) peering = cloud.peervpc.find_peering(peering_name, ignore_missing=False) vpc = cloud.vpc.find_vpc(vpc_name, ignore_missing=False) routes = cloud.peervpc.routes(nexthop=peering.id, destination=module.params['destination'], vpc_id=vpc.id) if state == 'present': try: route = next(routes) changed = False except StopIteration: route = cloud.peervpc.create_route( nexthop=peering.id, destination=module.params['destination'], vpc_id=vpc.id) changed = True module.exit_json(changed=changed, route=route.copy(), id=route.id) elif state == 'absent': try: cloud.peervpc.delete_route(next(routes).id) module.exit_json(changed=True) except StopIteration: module.exit_json(changed=False) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))
def main(): argument_spec = openstack_full_argument_spec( name=dict(type='str', required=True), cidr=dict(type='str'), enable_shared_snat=dict(default=False, type='bool'), state=dict(default='present', choices=['absent', 'present']), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) state = module.params['state'] name = module.params['name'] cloud = connect_from_ansible(module) try: cloud.add_service( vpc_service.VpcService("vpc", aliases=['vpc1'] )) cloud.add_service( vpc_service.VpcService("vpc2.0", aliases=['vpc2'] )) v = cloud.vpc.find_vpc(name) if state == 'present': if not v: v = cloud.vpc.create_vpc(name=name, cidr=module.params['cidr'], enable_shared_snat=module.params['enable_shared_snat']) changed = True elif _needs_update(module, cloud, v): v = cloud.vpc.update_vpc(vpc=v, name=name, cidr=module.params['cidr'], enable_shared_snat=module.params['enable_shared_snat']) changed = True else: changed = False module.exit_json(changed=changed, vpc=v.copy(), id=v.id ) elif state == 'absent': if not v: module.exit_json(changed=False) else: cloud.vpc.delete_vpc(v) module.exit_json(changed=True) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))
def main(): argument_spec = openstack_full_argument_spec( peering=dict(type='str', required=True, aliases=['peering_id']), state=dict(default='accept', choices=['accept', 'reject']), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) state = module.params['state'] peering_name = module.params['peering'] cloud = connect_from_ansible(module) try: cloud.add_service(vpc_service.VpcService("vpc", aliases=['vpc1'])) # need to use the artificial endpoint without project-id cloud.add_service(vpc_service.VpcService("peervpc")) v = cloud.peervpc.find_peering(peering_name, ignore_missing=False) if state == 'accept': if v.status == "PENDING_ACCEPTANCE": cloud.peervpc.accept_peering(v) if module.params['wait']: v = cloud.peervpc.wait_for_status(v, "ACTIVE") changed = True elif v.status == "ACTIVE": changed = False else: module.fail_json(msg="Peering in wrong state " + v.status) elif state == 'reject': if v.status == "PENDING_ACCEPTANCE": cloud.peervpc.reject_peering(v) if module.params['wait']: v = cloud.peervpc.wait_for_status(v, "REJECTED") changed = True elif v.status == "REJECTED": changed = False else: module.fail_json(msg="Peering in wrong state " + v.status) else: changed = False module.exit_json(changed=changed, peering=v.copy(), id=v.id) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))
def main(): argument_spec = openstack_full_argument_spec(name=dict(required=False, default=None, aliases=['subnet']), filters=dict(required=False, type='dict', default={})) module = AnsibleModule(argument_spec) cloud = connect_from_ansible(module) try: cloud.add_service(vpc_service.VpcService("vpc", aliases=['vpc1'])) cloud.add_service(vpc_service.VpcService("vpc2.0", aliases=['vpc2'])) subnets = cloud.vpc.find_subnet(name_or_id=module.params['name'], **module.params['filters']) if subnets: module.exit_json(changed=False, subnet=subnets.copy()) else: module.fail_json(msg="Subnet %s not found" % module.params['name']) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))
def main(): argument_spec = openstack_full_argument_spec( state=dict(type='str', default='present', choices=['absent', 'present']), name=dict(type='str', required=True), cidr=dict(type='str'), gateway_ip=dict(type='str'), vpc_id=dict(type='str'), dhcp_enable=dict(type='bool', default=True), dns_nameservers=dict(type='list', default=None), availability_zone=dict(type='str'), extra_dhcp_opts=dict(type='dict', default=dict()), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, supports_check_mode=False, **module_kwargs) state = module.params['state'] name = module.params['name'] gateway_ip = module.params['gateway_ip'] cloud = connect_from_ansible(module) try: cloud.add_service(vpc_service.VpcService("vpc", aliases=['vpc'])) cloud.add_service(vpc_service.VpcService("vpc2.0", aliases=['vpc2'])) subnet = cloud.vpc.find_subnet(name) # FIXME # if module.check_mode: # module.exit_json(changed=_system_state_change(module, subnet, # cloud)) if state == 'present': if not subnet: dhcp_extras = _dict_to_opts(module.params['extra_dhcp_opts']) if dhcp_extras: subnet = cloud.vpc.create_subnet( name=name, cidr=module.params['cidr'], dhcp_enable=module.params['dhcp_enable'], gateway_ip=gateway_ip, vpc=module.params['vpc_id'], dnsList=module.params['dns_nameservers'], extra_dhcp_opts=dhcp_extras) subnet.extra_dhcp_opts = module.params['extra_dhcp_opts'] else: subnet = cloud.vpc.create_subnet( name=name, cidr=module.params['cidr'], dhcp_enable=module.params['dhcp_enable'], gateway_ip=gateway_ip, vpc=module.params['vpc_id'], dnsList=module.params['dns_nameservers']) changed = True elif (_can_update(module, cloud, subnet) and _needs_update(module, cloud, subnet)): cloud.vpc.update_subnet( subnet['id'], name=name, cidr=module.params['cidr'], dhcp_enable=module.params['dhcp_enable'], dnsList=module.params['dns_nameservers'], extra_dhcp_opts=_dict_to_opts( module.params['extra_dhcp_opts'])) changed = True else: changed = False if changed and module.params['wait']: cloud.vpc.wait_for_status(subnet) module.exit_json(changed=changed, id=subnet.id, subnet=subnet.copy()) elif state == 'absent': if not subnet: changed = False else: changed = True cloud.vpc.delete_subnet(subnet) if module.params['wait']: cloud.vpc.wait_for_delete(subnet) module.exit_json(changed=changed) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))
def main(): argument_spec = openstack_full_argument_spec( name=dict(type='str', required=True), vpc=dict(type='str', aliases=['vpc_id']), peervpc=dict(type='str', aliases=['peervpc_id']), peerproject_id=dict(type='str'), description=dict(type='str'), state=dict(default='present', choices=['absent', 'present']), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) state = module.params['state'] name = module.params['name'] description = module.params['description'] cloud = connect_from_ansible(module) try: cloud.add_service(vpc_service.VpcService("vpc", aliases=['vpc1'])) # need to use the artificial endpoint without project-id cloud.add_service(vpc_service.VpcService("peervpc")) peering = cloud.peervpc.find_peering(name) if state == 'present': if not peering: vpc = cloud.vpc.find_vpc(module.params['vpc'], ignore_missing=False) peerproject_id = module.params['peerproject_id'] if not peerproject_id: peervpc = cloud.vpc.find_vpc(module.params['peervpc'], ignore_missing=False) else: # FIXME: remote peerings work only with ids at the moment # so we build an empty resource to use the same followup code peervpc = Vpc(id=module.params['peervpc']) new_peer = Peering( name=name, request_vpc_info=VpcInfoSpec(vpc_id=vpc.id, ), accept_vpc_info=VpcInfoSpec(vpc_id=peervpc.id, )) if peerproject_id: new_peer.accept_vpc_info.tenant_id = peerproject_id if description: new_peer.description = description peering = cloud.peervpc.create_peering(**new_peer.to_dict( ignore_none=True)) changed = True elif _needs_update(module, cloud, peering): if description: peering.description = description peering = cloud.peervpc.update_peering(**peering.to_dict( ignore_none=True)) changed = True else: changed = False module.exit_json(changed=changed, vpc=peering.copy(), id=peering.id) elif state == 'absent': if not peering: module.exit_json(changed=False) else: cloud.peervpc.delete_peering(peering) module.exit_json(changed=True) except exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e))