
    Vh=                     Z    d Z dZdZddlmZ ddlZ G d de      Zd Zed	k(  r e        yy)
a%  
---
module: server
short_description: Create/Delete Compute Instances from OpenStack
author: OpenStack Ansible SIG
description:
   - Create or Remove compute instances from OpenStack.
options:
    auto_ip:
      description:
        - Ensure instance has public ip however the cloud wants to do that.
        - For example, the cloud could add a floating ip for the server or
          attach the server to a public network.
        - Requires I(wait) to be C(True) during server creation.
        - Floating IP support is unstable in this module, use with caution.
        - Options I(auto_ip), I(floating_ip_pools) and I(floating_ips) interact
          in non-obvious ways and undocumentable depth. For explicit and safe
          attaching and detaching of floating ip addresses use module
          I(openstack.cloud.resource) instead.
      type: bool
      default: 'true'
      aliases: ['auto_floating_ip', 'public_ip']
    availability_zone:
      description:
        - Availability zone in which to create the server.
        - This server attribute cannot be updated.
      type: str
    boot_from_volume:
      description:
        - Should the instance boot from a persistent volume created based on
          the image given. Mutually exclusive with boot_volume.
        - This server attribute cannot be updated.
      type: bool
      default: 'false'
    boot_volume:
      description:
        - Volume name or id to use as the volume to boot from. Implies
          boot_from_volume. Mutually exclusive with image and boot_from_volume.
        - This server attribute cannot be updated.
      aliases: ['root_volume']
      type: str
    config_drive:
      description:
        - Whether to boot the server with config drive enabled.
        - This server attribute cannot be updated.
      type: bool
      default: 'false'
    delete_ips:
      description:
        - When I(state) is C(absent) and this option is true, any floating IP
          address associated with this server will be deleted along with it.
        - Floating IP support is unstable in this module, use with caution.
      type: bool
      aliases: ['delete_fip']
      default: 'false'
    description:
      description:
        - Description of the server.
      type: str
    flavor:
      description:
        - The name or id of the flavor in which the new instance has to be
          created.
        - Exactly one of I(flavor) and I(flavor_ram) must be defined when
          I(state=present).
        - This server attribute cannot be updated.
      type: str
    flavor_include:
      description:
        - Text to use to filter flavor names, for the case, such as Rackspace,
          where there are multiple flavors that have the same ram count.
          flavor_include is a positive match filter - it must exist in the
          flavor name.
        - This server attribute cannot be updated.
      type: str
    flavor_ram:
      description:
        - The minimum amount of ram in MB that the flavor in which the new
          instance has to be created must have.
        - Exactly one of I(flavor) and I(flavor_ram) must be defined when
          I(state=present).
        - This server attribute cannot be updated.
      type: int
    floating_ip_pools:
      description:
        - Name of floating IP pool from which to choose a floating IP.
        - Requires I(wait) to be C(True) during server creation.
        - Floating IP support is unstable in this module, use with caution.
        - Options I(auto_ip), I(floating_ip_pools) and I(floating_ips) interact
          in non-obvious ways and undocumentable depth. For explicit and safe
          attaching and detaching of floating ip addresses use module
          I(openstack.cloud.resource) instead.
      type: list
      elements: str
    floating_ips:
      description:
        - list of valid floating IPs that pre-exist to assign to this node.
        - Requires I(wait) to be C(True) during server creation.
        - Floating IP support is unstable in this module, use with caution.
        - Options I(auto_ip), I(floating_ip_pools) and I(floating_ips) interact
          in non-obvious ways and undocumentable depth. For explicit and safe
          attaching and detaching of floating ip addresses use module
          I(openstack.cloud.resource) instead.
      type: list
      elements: str
    image:
      description:
        - The name or id of the base image to boot.
        - Required when I(boot_from_volume=true).
        - This server attribute cannot be updated.
      type: str
    image_exclude:
      description:
        - Text to use to filter image names, for the case, such as HP, where
          there are multiple image names matching the common identifying
          portions. image_exclude is a negative match filter - it is text that
          may not exist in the image name.
        - This server attribute cannot be updated.
      type: str
      default: "(deprecated)"
    key_name:
      description:
        - The key pair name to be used when creating a instance.
        - This server attribute cannot be updated.
      type: str
    metadata:
      description:
        - 'A list of key value pairs that should be provided as a metadata to
          the new instance or a string containing a list of key-value pairs.
          Example:  metadata: "key1=value1,key2=value2"'
      aliases: ['meta']
      type: raw
    name:
      description:
        - Name that has to be given to the instance. It is also possible to
          specify the ID of the instance instead of its name if I(state) is
          I(absent).
        - This server attribute cannot be updated.
      required: true
      type: str
    network:
      description:
        - Name or ID of a network to attach this instance to. A simpler
          version of the I(nics) parameter, only one of I(network) or I(nics)
          should be supplied.
        - This server attribute cannot be updated.
      type: str
    nics:
      description:
        - A list of networks to which the instance's interface should
          be attached. Networks may be referenced by net-id/net-name/port-id
          or port-name.
        - 'Also this accepts a string containing a list of (net/port)-(id/name)
          Example: C(nics: "net-id=uuid-1,port-name=myport")'
        - Only one of I(network) or I(nics) should be supplied.
        - This server attribute cannot be updated.
      type: list
      elements: raw
      default: []
      suboptions:
        tag:
          description:
            - 'A I(tag) for the specific port to be passed via metadata.
              Eg: C(tag: test_tag)'
    reuse_ips:
      description:
        - When I(auto_ip) is true and this option is true, the I(auto_ip) code
          will attempt to re-use unassigned floating ips in the project before
          creating a new one. It is important to note that it is impossible
          to safely do this concurrently, so if your use case involves
          concurrent server creation, it is highly recommended to set this to
          false and to delete the floating ip associated with a server when
          the server is deleted using I(delete_ips).
        - Floating IP support is unstable in this module, use with caution.
        - This server attribute cannot be updated.
      type: bool
      default: 'true'
    scheduler_hints:
      description:
        - Arbitrary key/value pairs to the scheduler for custom use.
        - This server attribute cannot be updated.
      type: dict
    security_groups:
      description:
        - Names or IDs of the security groups to which the instance should be
          added.
        - On server creation, if I(security_groups) is omitted, the API creates
          the server in the default security group.
        - Requested security groups are not applied to pre-existing ports.
      type: list
      elements: str
      default: []
    state:
      description:
        - Should the resource be C(present) or C(absent).
      choices: [present, absent]
      default: present
      type: str
    tags:
      description:
        -  A list of tags should be added to instance
      type: list
      elements: str
      default: []
    terminate_volume:
      description:
        - If C(true), delete volume when deleting the instance and if it has
          been booted from volume(s).
        - This server attribute cannot be updated.
      type: bool
      default: 'false'
    timeout:
      description:
        - The amount of time the module should wait for the instance to get
          into active state.
      default: 180
      type: int
    userdata:
      description:
        - Opaque blob of data which is made available to the instance.
        - This server attribute cannot be updated.
      type: str
    volume_size:
      description:
        - The size of the volume to create in GB if booting from volume based
          on an image.
        - This server attribute cannot be updated.
      type: int
    volumes:
      description:
        - A list of preexisting volumes names or ids to attach to the instance
        - This server attribute cannot be updated.
      default: []
      type: list
      elements: str
    wait:
      description:
        - If the module should wait for the instance to be created.
      type: bool
      default: 'true'
extends_documentation_fragment:
- openstack.cloud.openstack
a/  
- name: Create a new instance with metadata and attaches it to a network
  openstack.cloud.server:
       state: present
       auth:
         auth_url: https://identity.example.com
         username: admin
         password: admin
         project_name: admin
       name: vm1
       image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
       key_name: ansible_key
       timeout: 200
       flavor: 4
       nics:
         - net-id: 34605f38-e52a-25d2-b6ec-754a13ffb723
         - net-name: another_network
       meta:
         hostname: test1
         group: uge_master

# Create a new instance in HP Cloud AE1 region availability zone az2 and
# automatically assigns a floating IP
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        state: present
        auth:
          auth_url: https://identity.example.com
          username: username
          password: Equality7-2521
          project_name: username-project1
        name: vm1
        region_name: region-b.geo-1
        availability_zone: az2
        image: 9302692b-b787-4b52-a3a6-daebb79cb498
        key_name: test
        timeout: 200
        flavor: 101
        security_groups:
        - default
        auto_ip: true

# Create a new instance in named cloud mordred availability zone az2
# and assigns a pre-known floating IP
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        state: present
        cloud: mordred
        name: vm1
        availability_zone: az2
        image: 9302692b-b787-4b52-a3a6-daebb79cb498
        key_name: test
        timeout: 200
        flavor: 101
        floating_ips:
          - 12.34.56.79

# Create a new instance with 4G of RAM on Ubuntu Trusty, ignoring
# deprecated images
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        state: present
        cloud: mordred
        region_name: region-b.geo-1
        image: Ubuntu Server 14.04
        image_exclude: deprecated
        flavor_ram: 4096

# Create a new instance with 4G of RAM on Ubuntu Trusty on a Performance node
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        cloud: rax-dfw
        state: present
        image: Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)
        flavor_ram: 4096
        flavor_include: Performance

# Creates a new instance and attaches to multiple network
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance with a string
      openstack.cloud.server:
        auth:
           auth_url: https://identity.example.com
           username: admin
           password: admin
           project_name: admin
        name: vm1
        image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
        key_name: ansible_key
        timeout: 200
        flavor: 4
        nics: >-
            net-id=4cb08b20-62fe-11e5-9d70-feff819cdc9f,
            net-id=542f0430-62fe-11e5-9d70-feff819cdc9f

- name: Creates a new instance with metadata and attaches it to a network
  openstack.cloud.server:
       state: present
       auth:
         auth_url: https://identity.example.com
         username: admin
         password: admin
         project_name: admin
       name: vm1
       image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
       key_name: ansible_key
       timeout: 200
       flavor: 4
       nics:
         - net-id: 34605f38-e52a-25d2-b6ec-754a13ffb723
         - net-name: another_network
       meta: "hostname=test1,group=uge_master"

- name:  Creates a new instance and attaches to a specific network
  openstack.cloud.server:
    state: present
    auth:
      auth_url: https://identity.example.com
      username: admin
      password: admin
      project_name: admin
    name: vm1
    image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
    key_name: ansible_key
    timeout: 200
    flavor: 4
    network: another_network

# Create a new instance with 4G of RAM on a 75G Ubuntu Trusty volume
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        state: present
        cloud: mordred
        region_name: ams01
        image: Ubuntu Server 14.04
        flavor_ram: 4096
        boot_from_volume: True
        volume_size: 75

# Creates a new instance with 2 volumes attached
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        state: present
        cloud: mordred
        region_name: ams01
        image: Ubuntu Server 14.04
        flavor_ram: 4096
        volumes:
        - photos
        - music

# Creates a new instance with provisioning userdata using Cloud-Init
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        state: present
        image: "Ubuntu Server 14.04"
        flavor: "P-1"
        network: "Production"
        userdata: |
          #cloud-config
          chpasswd:
            list: |
              ubuntu:{{ default_password }}
            expire: False
          packages:
            - ansible
          package_upgrade: true

# Creates a new instance with provisioning userdata using Bash Scripts
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        name: vm1
        state: present
        image: "Ubuntu Server 22.04"
        flavor: "P-1"
        network: "Production"
        userdata: |
          #!/bin/sh
          apt update
          apt -y full-upgrade

# Create a new instance with server group for (anti-)affinity
# server group ID is returned from openstack.cloud.server_group module.
- name: launch a compute instance
  hosts: localhost
  tasks:
    - name: launch an instance
      openstack.cloud.server:
        state: present
        name: vm1
        image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
        flavor: 4
        scheduler_hints:
          group: f5c8c61a-9230-400a-8ed2-3b023c190a7f

# Create an instance with "tags" for the nic
- name: Create instance with nics "tags"
  openstack.cloud.server:
    state: present
    auth:
        auth_url: https://identity.example.com
        username: admin
        password: admin
        project_name: admin
    name: vm1
    image: 4f905f38-e52a-43d2-b6ec-754a13ffb529
    key_name: ansible_key
    flavor: 4
    nics:
      - port-name: net1_port1
        tag: test_tag
      - net-name: another_network

# Deletes an instance via its ID
- name: remove an instance
  hosts: localhost
  tasks:
    - name: remove an instance
      openstack.cloud.server:
        name: abcdef01-2345-6789-0abc-def0123456789
        state: absent

a+  
server:
    description: Dictionary describing the server.
    type: dict
    returned: On success when I(state) is 'present'.
    contains:
        access_ipv4:
            description: |
                IPv4 address that should be used to access this server.
                May be automatically set by the provider.
            returned: success
            type: str
        access_ipv6:
            description: |
                IPv6 address that should be used to access this
                server. May be automatically set by the provider.
            returned: success
            type: str
        addresses:
            description: |
                A dictionary of addresses this server can be accessed through.
                The dictionary contains keys such as 'private' and 'public',
                each containing a list of dictionaries for addresses of that
                type. The addresses are contained in a dictionary with keys
                'addr' and 'version', which is either 4 or 6 depending on the
                protocol of the IP address.
            returned: success
            type: dict
        admin_password:
            description: |
                When a server is first created, it provides the administrator
                password.
            returned: success
            type: str
        attached_volumes:
            description: |
                A list of an attached volumes. Each item in the list contains
                at least an 'id' key to identify the specific volumes.
            returned: success
            type: list
        availability_zone:
            description: |
                The name of the availability zone this server is a part of.
            returned: success
            type: str
        block_device_mapping:
            description: |
                Enables fine grained control of the block device mapping for an
                instance. This is typically used for booting servers from
                volumes.
            returned: success
            type: str
        compute_host:
            description: |
                The name of the compute host on which this instance is running.
                Appears in the response for administrative users only.
            returned: success
            type: str
        config_drive:
            description: |
                Indicates whether or not a config drive was used for this
                server.
            returned: success
            type: str
        created_at:
            description: Timestamp of when the server was created.
            returned: success
            type: str
        description:
            description: |
                The description of the server. Before microversion
                2.19 this was set to the server name.
            returned: success
            type: str
        disk_config:
            description: The disk configuration. Either AUTO or MANUAL.
            returned: success
            type: str
        flavor:
            description: The flavor property as returned from server.
            returned: success
            type: dict
        flavor_id:
            description: |
                The flavor reference, as a ID or full URL, for the flavor to
                use for this server.
            returned: success
            type: str
        has_config_drive:
            description: |
                Indicates whether a configuration drive enables metadata
                injection. Not all cloud providers enable this feature.
            returned: success
            type: str
        host_id:
            description: An ID representing the host of this server.
            returned: success
            type: str
        host_status:
            description: The host status.
            returned: success
            type: str
        hostname:
            description: |
                The hostname set on the instance when it is booted.
                By default, it appears in the response for administrative users
                only.
            returned: success
            type: str
        hypervisor_hostname:
            description: |
                The hypervisor host name. Appears in the response for
                administrative users only.
            returned: success
            type: str
        id:
            description: ID of the server.
            returned: success
            type: str
        image:
            description: The image property as returned from server.
            returned: success
            type: dict
        image_id:
            description: |
                The image reference, as a ID or full URL, for the image to use
                for this server.
            returned: success
            type: str
        instance_name:
            description: |
                The instance name. The Compute API generates the instance name
                from the instance name template. Appears in the response for
                administrative users only.
            returned: success
            type: str
        is_locked:
            description: The locked status of the server
            returned: success
            type: bool
        kernel_id:
            description: |
                The UUID of the kernel image when using an AMI. Will be null if
                not. By default, it appears in the response for administrative
                users only.
            returned: success
            type: str
        key_name:
            description: The name of an associated keypair.
            returned: success
            type: str
        launch_index:
            description: |
                When servers are launched via multiple create, this is the
                sequence in which the servers were launched. By default, it
                appears in the response for administrative users only.
            returned: success
            type: int
        launched_at:
            description: The timestamp when the server was launched.
            returned: success
            type: str
        links:
            description: |
                A list of dictionaries holding links relevant to this server.
            returned: success
            type: str
        max_count:
            description: The maximum number of servers to create.
            returned: success
            type: str
        metadata:
            description: List of tag strings.
            returned: success
            type: dict
        min_count:
            description: The minimum number of servers to create.
            returned: success
            type: str
        name:
            description: Name of the server
            returned: success
            type: str
        networks:
            description: |
                A networks object. Required parameter when there are multiple
                networks defined for the tenant. When you do not specify the
                networks parameter, the server attaches to the only network
                created for the current tenant.
            returned: success
            type: str
        power_state:
            description: The power state of this server.
            returned: success
            type: str
        progress:
            description: |
                While the server is building, this value represents the
                percentage of completion. Once it is completed, it will be 100.
            returned: success
            type: int
        project_id:
            description: The ID of the project this server is associated with.
            returned: success
            type: str
        ramdisk_id:
            description: |
                The UUID of the ramdisk image when using an AMI. Will be null
                if not. By default, it appears in the response for
                administrative users only.
            returned: success
            type: str
        reservation_id:
            description: |
                The reservation id for the server. This is an id that can be
                useful in tracking groups of servers created with multiple
                create, that will all have the same reservation_id. By default,
                it appears in the response for administrative users only.
            returned: success
            type: str
        root_device_name:
            description: |
                The root device name for the instance By default, it appears in
                the response for administrative users only.
            returned: success
            type: str
        scheduler_hints:
            description: The dictionary of data to send to the scheduler.
            returned: success
            type: dict
        security_groups:
            description: |
                A list of applicable security groups. Each group contains keys
                for: description, name, id, and rules.
            returned: success
            type: list
            elements: dict
        server_groups:
            description: |
                The UUIDs of the server groups to which the server belongs.
                Currently this can contain at most one entry.
            returned: success
            type: list
        status:
            description: |
                The state this server is in. Valid values include 'ACTIVE',
                'BUILDING', 'DELETED', 'ERROR', 'HARD_REBOOT', 'PASSWORD',
                'PAUSED', 'REBOOT', 'REBUILD', 'RESCUED', 'RESIZED',
                'REVERT_RESIZE', 'SHUTOFF', 'SOFT_DELETED', 'STOPPED',
                'SUSPENDED', 'UNKNOWN', or 'VERIFY_RESIZE'.
            returned: success
            type: str
        tags:
            description:  A list of associated tags.
            returned: success
            type: list
            elements: str
        task_state:
            description: The task state of this server.
            returned: success
            type: str
        terminated_at:
            description: |
                The timestamp when the server was terminated (if it has been).
            returned: success
            type: str
        trusted_image_certificates:
            description: |
                A list of trusted certificate IDs, that were used during image
                signature verification to verify the signing certificate.
            returned: success
            type: list
        updated_at:
            description: Timestamp of when this server was last updated.
            returned: success
            type: str
        user_data:
            description: |
                Configuration information or scripts to use upon launch.
                Base64 encoded.
            returned: success
            type: str
        user_id:
            description: The ID of the owners of this server.
            returned: success
            type: str
        vm_state:
            description: The VM state of this server.
            returned: success
            type: str
        volumes:
            description: Same as attached_volumes.
            returned: success
            type: list
    )OpenStackModuleNc                      e Zd Z edMi d eddddg      d e       d ed	d
      d edg      d ed	d
      d ed	ddg      d e       d e       d e       d ed      d edd      d edd      d e       d ed      d  e       d! ed"d#g$      d% ed&      d' e       d( eg dd")      d* edd
      d+ ed,      d- eg dd)      d. ed/d0d/g1      d2 edg d3      d4 ed	d
      d5 e       d6 ed      d7 eg dd)      Z eg d8ddgddgddgd(d'ggddd6dgfd9d:gd;      Zd< Zd= Zd> Zd? Z	d@ Z
dA ZdB ZdC ZdD ZdE ZdF ZdG ZdH ZdI ZdJ ZdK ZyL)NServerModuleauto_ipTboolauto_floating_ip	public_ip)defaulttypealiasesavailability_zoneboot_from_volumeF)r
   r   boot_volumeroot_volume)r   config_drive
delete_ips
delete_fipdescriptionflavorflavor_include
flavor_ramint)r   floating_ip_poolsliststr)r   elementsfloating_ipsimageimage_excludez(deprecated))r
   key_namemetadatarawmeta)r   r   name)requirednetworknics)r
   r   r   	reuse_ipsscheduler_hintsdictsecurity_groupsstatepresentabsent)r
   choicestags)r   r
   r   terminate_volumeuserdatavolume_sizevolumesr   r   r   )r,   r-   )r   r   T)r,   r-   )r   r   T)mutually_exclusiverequired_ifsupports_check_modec                    | j                   d   }| j                  j                  j                  | j                   d         }|r%| j                  j                  j	                  |      }| j
                  j                  r"| j                  | j                  ||             |dk(  r6|s4| j                         }| j                  d|j                  d             y |dk(  rT|rR| j                  |      }|r| j                  ||      }| j                  t        |      |j                  d             y |d	k(  r&|r$| j                  |       | j                  d       y |d	k(  r|s| j                  d       y y y )
Nr,   r$   )changedr-   TF)computed)r:   serverr.   )paramsconncomputefind_server
get_serveransible
check_mode	exit_json_will_change_createto_dict_build_update_updater   _delete)selfr,   r<   updates       j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/server.pyrunzServerModule.runZ  sJ   G$""..t{{6/BCYY&&11&9F<<""NN4#4#4UF#CNDIf\\^FNN4"(..%."@  B iF''/Fff5NN4<"(..%."@  B h6LL NN4N(hvNN5N) (.    c                     |j                   dvr+| j                  dj                  |j                                i | j                  |      | j	                  |      | j                  |      | j                  |      S )N)ACTIVESHUTOFFPAUSED	SUSPENDEDz3The instance is available but not active state: {0}msg)status	fail_jsonformat_build_update_ips_build_update_security_groups_build_update_server_build_update_tags)rK   r<   s     rM   rH   zServerModule._build_update}  s    == LLNN  3396&--3H  J/$$V,/008/ ''// %%f-	/ 	/rO   c           	         | j                   d   }| j                   d   }| j                   d   }|s|s|si S |d   j                         D cg c]#  }|D ]  }|j                  dd       dk(  r|d    % }}}|r|r|s|si S |st        t        |||      	      S |s|si S i }|D 	cg c]	  }	|	|vs|	 }
}	|
r|
|d
<   |D 	cg c]	  }	|	|vs|	 }}	|r||d<   y y c c}}w c c}	w c c}	w )Nr   r   r   	addresseszOS-EXT-IPS:typefloatingaddr)r   ipsip_pool)rb   add_ips
remove_ips)r=   valuesgetr*   )rK   r<   r   r   r   vinterface_specrb   rL   iprd   re   s               rM   rZ   zServerModule._build_update_ips  sK   ++i({{>2 KK(;<<+< I {+224M%&M! $$%6=K f% M% M M
 $5l I D )+ , ,
 , I  ,>"#2>> 'F9#&AR"L*@bA
A#-F<  CM6 ?
 Bs   (C'	C!1C!	C&C&c           
          i }t        d  j                  d   D cg c])  } j                  j                  j	                  |d      + c}D              } j                  j
                  j                  |      }t         fd|j                  D              }|j                         D cg c]  \  }}||vr| }}}|r||d<   |j                         D cg c]  \  }}||vr| }	}}|	r|	|d<   |S c c}w c c}}w c c}}w )Nc              3   *   K   | ]  }|d    |f  ywidN ).0sgs     rM   	<genexpr>z=ServerModule._build_update_security_groups.<locals>.<genexpr>  s      (!RXrN(s   r+   Fignore_missingc              3   x   K   | ]1  }|d    j                   j                  j                  |d          f 3 ywrm   )r>   r&   get_security_group)rp   rq   rK   s     rM   rr   z=ServerModule._build_update_security_groups.<locals>.<genexpr>  s;      (. Xtyy((;;BtHEF(.s   7:add_security_groupsremove_security_groups)	r*   r=   r>   r&   find_security_groupr?   fetch_server_security_groupsr+   items)
rK   r<   rL   security_group_name_or_idrequired_security_groupsassigned_security_groupssg_idrq   rw   rx   s
   `         rM   r[   z*ServerModule._build_update_security_groups  s;   #' ( 26=N1O& . 		!!55-e 6 E&( $  ""??G#' (.,,(. $.  #;"@"@"B6E244 6 6 ,?F() #;"@"@"B"6E244 "6 "6 "/EF+,9&6"6s   .C5
,C:D c                     i } j                   j                  d         }j                  }t               }|j	                         D ]  \  }}||vs	||   |k7  s|||<    |r||d<   t               }|j	                         D ]  \  }}||vs	||   |k7  s|||<    |r||d<   t         fddD              }	|	r|	|d<   |S )Nr!   add_metadataremove_metadatac              3      K   | ]G  }|j                   v r7j                   |   (j                   |   |   k7  r|j                   |   f I y wNr=   )rp   krK   r<   s     rM   rr   z4ServerModule._build_update_server.<locals>.<genexpr>  sS      !-DKKDKKN$>A&)+	 A!-s   AA)access_ipv4access_ipv6hostnamedisk_configr   server_attributes)_parse_metadatar=   r!   r*   r{   )
rK   r<   rL   required_metadataassigned_metadatar   r   rh   r   r   s
   ``        rM   r\   z!ServerModule._build_update_server  s    !00Z1HI"OOv'--/ 	$FQ))->q-AQ-F"#Q	$ %1F>"&'--/ 	'FQ))->q-AQ-F%&"	' (7F$%. ! !-%!- - *;F&'rO   c                     | j                   j                  d      }t        |d         t        |      k(  ri S t        |      }|S )Nr0   )r0   )r=   rg   setr*   )rK   r<   required_tagsrL   s       rM   r]   zServerModule._build_update_tags  s>    /vf~#m"44I=)rO   c           
      8   dD ]E  }| j                   |   s| j                   d   du s%| j                  dj                  |             G | j                   d   }d }| j                   d   st| j                  j	                  | j                   d   | j                   d	         }|s<| j                  d
j                  | j                   d   | j                   d	                |r(| j                  j
                  j                  |d      }nJ| j                  j                  | j                   d   | j                   d         }|s| j                  d       t        |j                  || j                   d   | j                   d   | j                  | j                   d         | j                               }dD ]$  }| j                   |   | j                   |   ||<   &  | j                  j                  di |}| j                  j
                  j                  |      S )Nr5   waitFz"Option '{0}' requires 'wait: true'rU   r   r   r   r   z)Could not find image {0} with exclude {1}rs   r   r   z"Could not find any matching flavorr   r   r!   )r   r   rc   rb   r#   r'   )r   r   r   r   r   r   r    r$   r&   r(   r)   r+   r0   r1   timeoutr2   r3   r4   r   ro   )r=   rX   rY   r>   get_image_idr?   find_flavorget_flavor_by_ramr*   rn   r   _parse_nicscreate_serverrA   )rK   r   flavor_name_or_idimage_idr   argsr<   s          rM   rF   zServerModule._create  s   A 	HA{{1~;;v&%/ <CCAF  H	H !KK1{{=)yy--G$dkk/&BDHCJJG,dkk/.JL  M YY&&223DBG 3 IF YY00\1J15=M1NPF#GH99KK 34N+%%dkk*&=>!!#
@ 	)A
 {{1~)++a.Q	) )((040 yy  ++F33rO   c                 l      j                   j                  |j                  fi t         fddD                j                  d   rk j
                  j                  j                   j                  d   d      D ]3  } j                   j                  j                  |j                        3 y  y y )Nc              3   @   K   | ]  }|j                   |   f  y wr   r   rp   r   rK   s     rM   rr   z'ServerModule._delete.<locals>.<genexpr>T  s&      ? t{{1~& ?   )r   r   r   r   r   z'Timeout waiting for server to be absent)r   message)
r>   delete_serverrn   r*   r=   sdkutilsiterate_timeoutr?   r@   )rK   r<   counts   `  rM   rJ   zServerModule._deleteQ  s    		II	@ ?=? ?	@ ;;v77I.A 8   99$$00;C rO   c                     | j                  ||      }| j                  ||      }| j                  ||      }| j                  ||      }| j                  j
                  j                  |      S r   )_update_ips_update_security_groups_update_tags_update_serverr>   r?   rA   )rK   r<   rL   s      rM   rI   zServerModule._update`  sg    !!&&1--ff=""662$$VV4
 yy  ++F33rO   c                     t         fddD              }|j                  d      }|r  j                  j                  |fi ||}|j                  d      }|r  j                  j                  ||fi |}|j                  d      }|r_|D ]Z  } j                  j
                  j                  |d      j                  } j                  j                  |j                  |       \ |S )	Nc              3   @   K   | ]  }|j                   |   f  y wr   r   r   s     rM   rr   z+ServerModule._update_ips.<locals>.<genexpr>l  s     EAQA'Er   )r   r   rb   rd   re   F)
name_or_idrt   )	server_idfloating_ip_id)	r*   rg   r>   add_ips_to_serveradd_ip_listr&   find_iprn   detach_ip_from_server)	rK   r<   rL   r   rb   rd   re   rj   ip_ids	   `        rM   r   zServerModule._update_ipsk  s    E1DEEjj0TYY00G3G$GF**Y'*TYY**67CdCFZZ-
  F		))11RAF 2 HHJ 
 		//&))?D 0 FF rO   c                    |j                  d      }|r-|D ](  }| j                  j                  j                  ||       * |j                  d      }|r-|D ](  }| j                  j                  j	                  ||       * |S )Nrw   rx   )rg   r>   r?   add_security_group_to_server!remove_security_group_from_server)rK   r<   rL   rw   rq   rx   s         rM   r   z$ServerModule._update_security_groups  s    $jj)>?) K		!!>>vrJK "(,D!E!, P		!!CCFBOP rO   c                    |j                  d      }|r1 | j                  j                  j                  |j                  fi | |j                  d      }|r>| j                  j                  j                  |j                  |j                                |j                  d      }|r* | j                  j                  j                  |d   fi |}|S )Nr   r   r   rn   )rg   r>   r?   set_server_metadatarn   delete_server_metadatakeysupdate_server)rK   r<   rL   r   r   r   s         rM   r   zServerModule._update_server  s    zz.11DII11&)) B4@B !**%67II44VYY5D5I5I5KM #JJ':;
 5TYY&&44VD\ J7HJF rO   c                     |j                  d      }| j                  j                  j                  dj	                  |d         d|id       |S )Nr0   z/servers/{server_id}/tagsrn   )r   z2.26)jsonmicroversion)rg   r>   r?   putrY   )rK   r<   rL   r0   s       rM   r   zServerModule._update_tags  sS    zz&!		'...F$ 	 	

 rO   c                     |si S t        |t              r3i }|j                  d      D ]  }|j                  d      \  }}|||<    |S |S )N,=)
isinstancer   split)rK   r!   metaskv_strr   rh   s         rM   r   zServerModule._parse_metadata  sX    Ih$E"..- ||C(1a LrO   c                    g }| j                   d   }t        |t              s| j                  d       |D cg c],  }t        |t              rd |j                  d      D        n|. }}|D ]d  }t        |t              s| j                  d       |j                  d      r|j                  |       n
|j                  d      rc| j                  j                  j                  |d   d	
      j                  }t        j                  |      }|d= ||d<   |j                  |       n|j                  d      r|j                  |       ns|j                  d      rb| j                  j                  j                  |d   d	
      j                  }t        j                  |      }|d= ||d<   |j                  |       d|v sZ|d   |d   d<   g |S c c}w )Nr'   z$The 'nics' parameter must be a list.rU   c              3   R   K   | ]  }t        |j                  d       f       ! yw)r   N)r*   r   )rp   
nested_nets     rM   rr   z+ServerModule._parse_nics.<locals>.<genexpr>  s+      2 z'',./ 2s   %'r   z2Each entry in the 'nics' parameter must be a dict.znet-idznet-nameFrs   zport-idz	port-nametag)r=   r   r   rX   r   r   r*   rg   appendr>   r&   find_networkrn   copydeepcopy	find_port)rK   r'   stringified_netsnetnets
network_idport_ids          rM   r   zServerModule._parse_nics  s   ;;v.*D1NNENF
 ,-  c3'2"%))C.2-01 - -
  #	-Cc4(L  N wwx C $!YY..;;
OE < ;;=2  mmC(
O *HC #C %))++55$U 6 <<>B  mmC($!(IC |"%e*RG#	-H S-s   1Gc                 f    |dk(  r|sy|dk(  r|rt        | j                  |            S |dk(  r|ryy)Nr-   Tr.   F)r   rH   )rK   r,   r<   s      rM   rE   zServerModule._will_change  sA    IfiF**6233h6 rO   Nro   )__name__
__module____qualname__r*   argument_specmodule_kwargsrN   rH   rZ   r[   r\   r]   rF   rJ   rI   r   r   r   r   r   r   rE   ro   rO   rM   r   r   (  s~    T0+>@ & e&9	
 -1 %f5 F\NK F v v U# FU; v6 f >2  !" 56(3#$ 4 %& '( "6E:)* t&1+, &)-. RfuE/0 9x.CD12 vrE:34 e&956 78 e$9: Rfu=;M@ <|$m$/Y
  w'?@@@

 !M !*F	/..` D6p64p	40 20d	rO   r   c                  &    t               }  |         y r   )r   )modules    rM   mainr     s    ^F
HrO   __main__)	DOCUMENTATIONEXAMPLESRETURNBansible_collections.openstack.cloud.plugins.module_utils.openstackr   r   r   r   r   ro   rO   rM   <module>r      sQ   rh}~f
N	 _ V? Vr
 zF rO   