
    Vh?                         d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZ dZ	 d dlZd
Zd Zd Zedk(  r e        yy# e$ r d	ZY w xY w)    )absolute_importdivisionprint_functiona  
module: packet_project

short_description: Create/delete a project in Packet host

description:
  - Create/delete a project in Packet host.
  - API is documented at U(https://www.packet.com/developers/api/#projects).
version_added: '0.2.0'

author:
  - Tomas Karasek (@t0mk) <tom.to.the.k@gmail.com>
  - Nurfet Becirevic (@nurfet-becirevic) <nurfet.becirevic@gmail.com>

extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  state:
    description:
      - Indicate desired state of the target.
    default: present
    choices: ['present', 'absent']
    type: str

  payment_method:
    description:
      - Payment method is name of one of the payment methods available to your user.
      - When blank, the API assumes the default payment method.
    type: str

  auth_token:
    description:
      - Packet API token. You can also supply it in environment variable E(PACKET_API_TOKEN).
    type: str

  name:
    description:
      - Name for/of the project.
    type: str

  org_id:
    description:
      - UUID of the organization to create a project for.
      - When blank, the API assumes the default organization.
    type: str

  id:
    description:
      - UUID of the project which you want to remove.
    type: str

  custom_data:
    description:
      - Custom data about the project to create.
    type: str

requirements:
  - "packet-python >= 1.40"
aD  
# All the examples assume that you have your Packet API token in env var PACKET_API_TOKEN.
# You can also pass the api token in module param auth_token.

- name: Create new project
  hosts: localhost
  tasks:
    community.general.packet_project:
      name: "new project"

- name: Create new project within non-default organization
  hosts: localhost
  tasks:
    community.general.packet_project:
      name: "my org project"
      org_id: a4cc87f9-e00f-48c2-9460-74aa60beb6b0

- name: Remove project by id
  hosts: localhost
  tasks:
    community.general.packet_project:
      state: absent
      id: eef49903-7a09-4ca1-af67-4087c29ab5b6

- name: Create new project with non-default billing method
  hosts: localhost
  tasks:
    community.general.packet_project:
      name: "newer project"
      payment_method: "the other visa"
a  
changed:
  description: True if a project was created or removed.
  type: bool
  sample: true
  returned: success

name:
  description: Name of addressed project.
  type: str
  returned: success

id:
  description: UUID of addressed project.
  type: str
  returned: success
)AnsibleModuleenv_fallback)	to_nativeTNFPACKET_API_TOKENc                    ddi}|j                   j                  d      }|j                   j                  d      }|r.|j                         D cg c]  }||j                  k(  s| }}n-|j                         D cg c]  }||j                  k(  s| }}| dk(  rt        |      dk(  r|j                   j                  d      }|j                   j                  d      }	|j                   j                  d	      }
|s0||
|	d
}|j                  dd|      }t        j                  ||      }n|j                  |||
|	      }d|d<   |j                  |       |d   j                  |d<   |d   j                  |d<   |S t        |      dkD  r,dj                  t        |            }|j                  |       t        |      dk(  r;|d   }|j                  |d<   |j                  |d<   d|d<   	 |j                          |S |S c c}w c c}w # t        $ rM}dj                  |j                  |j                  t        |            }|j                  |       Y d }~|S d }~ww xY w)NchangedFidnamepresentr   org_idcustom_datapayment_method)r   payment_method_id
customdataprojectsPOST)r   r   r   r   T   zCMore than projects matched for module call with state = absent: {0}msgz:while trying to remove project {0}, id {1}, got error: {2})paramsgetlist_projectsr   r   lencall_apipacketProjectcreate_organization_projectappendformatr   	fail_jsondelete	Exception)target_statemodulepacket_connresult_dictgiven_id
given_namepmatching_projectsr   r   r   r   new_project_datanew_project_msges                   t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/packet_project.pyact_on_projectr3      s   e$K}}  &H""6*J"002Hh!$$6FAH H #002LjAFF6JAL L y  !Q&]]&&x0F --++M:K#]]../?@N&)7"-
 $/#7#7
FF#S $nn-={K)EE!#&4*	 F  &*K	"$$[1/277F-a033D$ !  !A% &+<!=> & !Q&!!$A"#&&K !K%)K	"+

 ;eHLV  +T[[il4  T **	+s1   H$H=H	H	0H 	I$AII$c                     t        t        t        ddgd      t        dt        t        gfd      t        d      t        d      t        d      t        d      t        d            dd	gd	g
      } t        s| j                  d       | j                  j                  d      s'dj                  t              }| j                  |       | j                  j                  d      }t        j                  |      }| j                  j                  d      }|dv r=| j                  r| j                  d       	  | j                  di t        || |       y | j                  dj                  |             y # t        $ r5}| j                  dj                  |t        |                   Y d }~y d }~ww xY w)Nr   absent)choicesdefaultstrT)typefallbackno_log)r9   )state
auth_tokenr   r   r   r   r   )r   r   )argument_specsupports_check_moderequired_one_ofmutually_exclusivezpacket required for this moduler   r=   z\if Packet API token is not in environment variable {0}, the auth_token parameter is required)r=   r<   )r   r5   F)r   z$failed to set project state {0}: {1}z({0} is not a valid state for this module )r   dictr   PACKET_API_TOKEN_ENV_VARHAS_PACKET_SDKr#   r   r   r"   r   Manager
check_mode	exit_jsonr3   r%   r   )r'   	_fail_msgr=   r(   r<   r1   s         r2   mainrJ      s   	84iH&)A(BC
 5!U#U+%(
 !()
!F( >?==\*<<BFC[<\ 	Y'""<0J..J7KMMg&E%%U+	XFJ~eV[IJ
 	GNNuUV	  	X:AA%STV  X X	Xs   F 	F?
+F::F?__main__)
__future__r   r   r   r9   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rE   r   ImportErrorrD   r3   rJ   __name__rB       r2   <module>rV      s    A @AF@
$ C A . 7t-W` zF a  Ns   > AA