
    Vh$                     z    d dl mZmZmZ eZdZdZdZd dl	m
Z
 ddlmZmZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: subnet
short_description: Manages subnets on the cloudscale.ch IaaS service
description:
  - Create, update and remove subnets.
author:
  - René Moser (@resmo)
version_added: "1.3.0"
options:
  uuid:
    description:
      - UUID of the subnet.
    type: str
  cidr:
    description:
      - The cidr of the subnet.
      - Required if I(state=present).
    type: str
  network:
    description:
      - The name of the network the subnet is related to.
      - Required if I(state=present).
    type: dict
    suboptions:
      uuid:
        description:
          - The uuid of the network.
        type: str
      name:
        description:
          - The uuid of the network.
        type: str
      zone:
        description:
          - The zone the network allocated in.
        type: str
  gateway_address:
    description:
      - The gateway address of the subnet. If not set, no gateway is used.
      - Cannot be within the DHCP range, which is the lowest .101-.254 in the subnet.
    type: str
  dns_servers:
    description:
      - A list of DNS resolver IP addresses, that act as DNS servers.
      - If not set, the cloudscale.ch default resolvers are used.
    type: list
    elements: str
  reset:
    description:
      - Resets I(gateway_address) and I(dns_servers) to default values by the API.
      - "Note: Idempotency is not given."
    type: bool
    default: false
  state:
    description:
      - State of the subnet.
    choices: [ present, absent ]
    default: present
    type: str
  tags:
    description:
      - Tags associated with the subnet. Set this to C({}) to clear any tags.
    type: dict
extends_documentation_fragment: cloudscale_ch.cloud.api_parameters
aX  
---
- name: Ensure subnet exists
  cloudscale_ch.cloud.subnet:
    cidr: 172.16.0.0/24
    network:
      uuid: 2db69ba3-1864-4608-853a-0771b6885a3a
    api_token: xxxxxx

- name: Ensure subnet exists
  cloudscale_ch.cloud.subnet:
    cidr: 192.168.1.0/24
    gateway_address: 192.168.1.1
    dns_servers:
      - 192.168.1.10
      - 192.168.1.11
    network:
      name: private
      zone: lpg1
    api_token: xxxxxx

- name: Ensure a subnet is absent
  cloudscale_ch.cloud.subnet:
    cidr: 172.16.0.0/24
    network:
      name: private
      zone: lpg1
    state: absent
    api_token: xxxxxx
a{  
---
href:
  description: API URL to get details about the subnet.
  returned: success
  type: str
  sample: https://api.cloudscale.ch/v1/subnets/33333333-1864-4608-853a-0771b6885a3
uuid:
  description: The unique identifier for the subnet.
  returned: success
  type: str
  sample: 33333333-1864-4608-853a-0771b6885a3
cidr:
  description: The CIDR of the subnet.
  returned: success
  type: str
  sample: 172.16.0.0/24
network:
  description: The network object of the subnet.
  returned: success
  type: complex
  contains:
    href:
      description: API URL to get details about the network.
      returned: success
      type: str
      sample: https://api.cloudscale.ch/v1/networks/33333333-1864-4608-853a-0771b6885a3
    uuid:
      description: The unique identifier for the network.
      returned: success
      type: str
      sample: 33333333-1864-4608-853a-0771b6885a3
    name:
      description: The name of the network.
      returned: success
      type: str
      sample: my network
    zone:
        description: The zone the network is allocated in.
        returned: success
        type: dict
        sample: { 'slug': 'rma1' }
        version_added: 1.4.0
gateway_address:
  description: The gateway address of the subnet.
  returned: success
  type: str
  sample: "192.168.42.1"
dns_servers:
  description: List of DNS resolver IP addresses.
  returned: success
  type: list
  sample: ["9.9.9.9", "149.112.112.112"]
state:
  description: State of the subnet.
  returned: success
  type: str
  sample: present
tags:
  description: Tags associated with the subnet.
  returned: success
  type: dict
  sample: { 'project': 'my project' }
)AnsibleModule   )AnsibleCloudscaleBasecloudscale_argument_specc                   D     e Zd Z fdZddZ fdZ fdZ fdZ xZS )AnsibleCloudscaleSubnetc                 L    t         t        |   |ddg dg d       d | _        y )Nsubnetscidr)r   gateway_addressdns_serverstags)r   r   r   )moduleresource_nameresource_key_nameresource_create_param_keysresource_update_param_keys)superr   __init___network)selfr   	__class__s     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cloudscale_ch/cloud/plugins/modules/subnet.pyr   z AnsibleCloudscaleSubnet.__init__   s4    %t5#$(( 	6 	
      c                 ,   | j                   | j                   S | j                  j                  d   }|xs |d   }|6| j                  d|z        }|s| j                  j	                  d|z         n|d   g }| j                  d      }|xs g D ]?  }|d   |d   d	   |d   k7  r|j                  d      |d   k(  s/|j                  |       A |s%d
|d   z  }| j                  j	                  |       nUt        |      dk(  r|d   }nAd|d   z  }| j                  j	                  |       n| j                  j	                  d       t               | _         j                         D ]  \  }}	|dv s|	| j                   |<    | j                   S )Nnetworkuuidznetworks/%sz!Network with 'uuid' not found: %smsgnamenetworkszoneslugz!Network with 'name' not found: %s   r   zxMultiple networks with 'name' not found: %s.Add the 'zone' to distinguish or use 'uuid' argument to specify the network.z(Either Network UUID or name is required.)r#   r    hrefr%   )
r   _moduleparams_get	fail_jsongetappendlendictitems)
r   r    	net_paramnet_uuidr   networks_foundr$   r"   kvs
             r   query_networkz%AnsibleCloudscaleSubnet.query_network   s   ==$== LL''	2	,9V,ii 89G&&+NQY+Y&Zv*Nyy,H#>r 3V$0WV_V5LPYZ`Pa5a;;v&)F*;;"))'23 "9If<MM&&3&/^$)(+fhqrxhyz&&3&/ LL""'Q"R MMO 	%DAq44#$a 	% }}r   c                 f    | j                         |d<   d|d   d   i}t        t        |   ||      S )Nr   r    )r7   r   r   create)r   resourcedatar   s      r   r9   zAnsibleCloudscaleSubnet.create   sE    "002 x	*62
 ,d:8TJJr   c                    | j                   j                  j                  d      rdD ]  }| j                   j                  j                  |      d| j                  d<   |d i}| j                   j                  rR|j                  d      }|s| j                   j                  d|z         | j                  |d	        | j                   j                  s| j                         }t        t        | +  |      S )
Nreset)r   r   Tchangedr(   z#Unable to update %s, no href found.r!   F)filter_none)r)   r*   r-   _result
check_moder,   _patchqueryr   r   update)r   r:   key
patch_datar(   r   s        r   rD   zAnsibleCloudscaleSubnet.update  s    <<""7+: E<<&&**3/7.2DLL+T"J ||..#<</D..3X[^3^._KKjeKDE <<**::<,d:8DDr   c                 l    |rd|v r| j                  |d   d         |d<   t        t        |   |      S )Nr   r    )r    )r7   r   r   
get_result)r   r:   r   s     r   rH   z"AnsibleCloudscaleSubnet.get_result  sB    	X-"&"4"4(9:Mf:U"4"VHY,d>xHHr   )N)	__name__
__module____qualname__r   r7   r9   rD   rH   __classcell__)r   s   @r   r   r      s&    &-^KE*I Ir   r   c                     t               } | j                  t        t        d      t        d      t        dt        t        d      t        d      t        d                  t        d      t        ddd       t        d      t        dd	
      t        dddg                   t        | dddd      }t	        |      }|j
                  d   dk(  r|j                         }n|j                         } |j                  di | y )Nstr)typer0   )r    r#   r%   )rO   optionslist)rO   elementsdefaultboolF)rO   rS   presentabsent)rS   choices)r    r   r   r   r   r   r=   state))r   r    )r   r   ))rX   rU   rY   T)argument_specrequired_one_ofrequired_togetherrequired_ifsupports_check_moderX    )	r	   rD   r0   r   r   r*   rV   rU   	exit_json)rZ   r   cloudscale_subnetresults       r   mainrc     s    ,.Muuu%u%u%
 %(fudCv.9x.CD $ #,1B F 07}}W)"))+"**,Fvr   __main__N)
__future__r   r   r   rO   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.apir   r	   r   rc   rI   r_   r   r   <module>rl      sa    A @AF>?
B 5dI3 dIN"J zF r   