
    Vho%                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZmZ d dlmZ dZ	 d dlmZmZ d	Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r  e	j,                         Zd
ZY 7w xY w)    )absolute_importdivisionprint_functiona}  
module: linode_v4
short_description: Manage instances on the Linode cloud
description: Manage instances on the Linode cloud.
requirements:
  - linode_api4 >= 2.0.0
author:
  - Luke Murphy (@decentral1se)
notes:
  - No Linode resizing is currently implemented. This module will, in time, replace the current Linode module which uses deprecated
    API bindings on the Linode side.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  region:
    description:
      - The region of the instance. This is a required parameter only when creating Linode instances. See U(https://www.linode.com/docs/api/regions/).
    type: str
  image:
    description:
      - The image of the instance. This is a required parameter only when creating Linode instances.
      - See U(https://www.linode.com/docs/api/images/).
    type: str
  type:
    description:
      - The type of the instance. This is a required parameter only when creating Linode instances.
      - See U(https://www.linode.com/docs/api/linode-types/).
    type: str
  label:
    description:
      - The instance label. This label is used as the main determiner for idempotence for the module and is therefore mandatory.
    type: str
    required: true
  group:
    description:
      - The group that the instance should be marked under. Please note, that group labelling is deprecated but still supported.
        The encouraged method for marking instances is to use tags.
    type: str
  private_ip:
    description:
      - If V(true), the created Linode will have private networking enabled and assigned a private IPv4 address.
    type: bool
    default: false
    version_added: 3.0.0
  tags:
    description:
      - The tags that the instance should be marked under.
      - See U(https://www.linode.com/docs/api/tags/).
    type: list
    elements: str
  root_pass:
    description:
      - The password for the root user. If not specified, one will be generated. This generated password will be available
        in the task success JSON.
    type: str
  authorized_keys:
    description:
      - A list of SSH public key parts to deploy for the root user.
    type: list
    elements: str
  state:
    description:
      - The desired instance state.
    type: str
    choices:
      - present
      - absent
    required: true
  access_token:
    description:
      - The Linode API v4 access token. It may also be specified by exposing the E(LINODE_ACCESS_TOKEN) environment variable.
      - See U(https://www.linode.com/docs/api#access-and-authentication).
    required: true
    type: str
  stackscript_id:
    description:
      - The numeric ID of the StackScript to use when creating the instance.
      - See U(https://www.linode.com/docs/api/stackscripts/).
    type: int
    version_added: 1.3.0
  stackscript_data:
    description:
      - An object containing arguments to any User Defined Fields present in the StackScript used when creating the instance.
        Only valid when a O(stackscript_id) is provided.
      - See U(https://www.linode.com/docs/api/stackscripts/).
    type: dict
    version_added: 1.3.0
a  
- name: Create a new Linode.
  community.general.linode_v4:
    label: new-linode
    type: g6-nanode-1
    region: eu-west
    image: linode/debian9
    root_pass: passw0rd
    authorized_keys:
      - "ssh-rsa ..."
    stackscript_id: 1337
    stackscript_data:
      variable: value
    state: present

- name: Delete that new Linode.
  community.general.linode_v4:
    label: new-linode
    state: absent
a  
instance:
  description: The instance description in JSON serialized form.
  returned: Always.
  type: dict
  sample: {
    "root_pass": "foobar",  # if auto-generated
    "alerts": {
      "cpu": 90,
      "io": 10000,
      "network_in": 10,
      "network_out": 10,
      "transfer_quota": 80
    },
    "backups": {
      "enabled": false,
      "schedule": {
        "day": null,
        "window": null
      }
    },
    "created": "2018-09-26T08:12:33",
    "group": "Foobar Group",
    "hypervisor": "kvm",
    "id": 10480444,
    "image": "linode/centos7",
    "ipv4": [
      "130.132.285.233"
    ],
    "ipv6": "2a82:7e00::h03c:46ff:fe04:5cd2/64",
    "label": "lin-foo",
    "region": "eu-west",
    "specs": {
      "disk": 25600,
      "memory": 1024,
      "transfer": 1000,
      "vcpus": 1
    },
    "status": "running",
    "tags": [],
    "type": "g6-nanode-1",
    "updated": "2018-09-26T10:10:14",
    "watchdog_enabled": true
  }
N)AnsibleModuleenv_fallbackmissing_required_lib)get_user_agent)InstanceLinodeClientTFc                    |d   |j                  d       	  |j                  j                  di |}	 t        t              r&|\  }}|j                  }|j                  d|i       |S |j                  S # t        $ r}| j	                  d|z         Y d}~fd}~ww xY w# t        $ r | j	                  d       Y yw xY w)z4Creates a Linode instance and handles return format.	root_passN'Unable to query the Linode API. Saw: %smsgzUnable to parse Linode instance creation response. Please raise a bug against this module on https://github.com/ansible-collections/community.general/issues )
poplinodeinstance_create	Exception	fail_json
isinstancetuple	_raw_jsonupdate	TypeError)moduleclientkwargsresponse	exceptioninstancer   instance_jsons           o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/linode_v4.pycreate_linoder$      s    k"

;T06==00:6:h&"*Hi$..M  +y!9:  %%%  TFRSST   j 	s.   A7 5B" +B" 7	B BB"C ?C c                     	 | j                   d   }|j                  j                  t        j                  |k(        }|d   S # t
        $ r Y yt        $ r}| j                  d|z         Y d}~yd}~ww xY w)z-Try to retrieve an instance based on a label.labelr   Nr   r   )paramsr   	instancesr
   r&   
IndexErrorr   r   )r   r   r&   resultr    s        r#   maybe_instance_from_labelr+      st    Tg&((5)@Aay  TFRSSTs   ?A 	A4A4A//A4c                     t        t        t        dd      t        ddddg      t        dddt        dgf      t        d	dd
      t        d      t        d      t        dd
      t        d      t        dd      t        d	d      t        d      t        d      t        d            d
ddgfg df      S )z.Initialise the module parameter specification.strT)typerequiredpresentabsent)r.   r/   choicesLINODE_ACCESS_TOKEN)r.   r/   no_logfallbacklistF)r.   elementsr4   )r.   bool)r.   default)r.   r4   )r.   r7   intdict)r&   stateaccess_tokenauthorized_keysgroupimage
private_ipregionr   tagsr.   stackscript_idstackscript_datar<   r&   )rB   r@   r.   )argument_specsupports_check_moderequired_one_ofrequired_together)r   r;   r   r       r#   initialise_modulerK      s    ED1"H-
 &)>(?@	 !fuUKE"E"7U#d36E25!U+!v.-
0 "g
 (
;   rJ   c                 H    t        | j                  d   t        d            S )zBuild a LinodeClient.r=   linode_v4_module)
user_agent)r   r'   r	   )r   s    r#   build_clientrO     s$    n%!"45 rJ   c                  j   t               } t        s | j                  t        d      t               t        |       }t        | |      }| j                  d   dk(  r || j                  d|j                         y| j                  d   dk(  r|t        | || j                  d   | j                  d	   | j                  d
   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d   | j                  d         }| j                  d|       y| j                  d   dk(  r0|.|j                          | j                  d|j                         y| j                  d   dk(  r|| j                  di        yyy)zModule entrypoint.zlinode-api4)r   r    r<   r0   NF)changedr!   r>   r?   r@   r&   rA   rB   r   rC   r.   rD   rE   )r>   r?   r@   r&   rA   rB   r   rC   ltypestackscriptrE   Tr1   )rK   HAS_LINODE_DEPENDENCYr   r   LINODE_IMP_ERRrO   r+   r'   	exit_jsonr   r$   delete)r   r   r!   r"   s       r#   mainrX     s    F 1-@N[&!F(8H}}W*x/C1C1CD	w	9	,1A%F"MM*;<--(--(--(}}\2==*mmK0v&--'&67#]]+=>
 	>	w	8	+0D0B0BC	w	8	+0@4 1A	+rJ   __main__)
__future__r   r   r   r.   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackansible.module_utils.basicr   r   r   Aansible_collections.community.general.plugins.module_utils.linoder	   rU   linode_api4r
   r   rT   ImportError
format_excr$   r+   rK   rO   rX   __name__r   rJ   r#   <module>rf      s    A @\|*,
\  X X \"2 0	T"J#5L zF y  ")Y))+N!"s   
A A,+A,