%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/states/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/states/boto_dynamodb.pyc

�
���^c@@s|dZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
mZddlj
jZejdejdddej�ej�Zd	efd
��YZd�Zddddddddddddddddd
d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"ddddd�Z#dS(u�
Manage DynamoDB Tables
======================

.. versionadded:: 2015.5.0

Create and destroy DynamoDB tables. Be aware that this interacts with Amazon's
services, and so may incur charges.

This module uses ``boto``, which can be installed via package, or pip.

This module accepts explicit DynamoDB credentials but can also utilize
IAM roles assigned to the instance through Instance Profiles. Dynamic
credentials are then automatically obtained from AWS API and no further
configuration is necessary. More information available `here
<http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html>`_.

If IAM roles are not used you need to specify them either in a pillar file or
in the minion's config file:

.. code-block:: yaml

    keyid: GKTADJGHEIQSXMKKRBJ08H
    key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
    region: us-east-1

It's also possible to specify ``key``, ``keyid`` and ``region`` via a
profile, either passed in as a dict, or as a string to pull from
pillars or minion config:

.. code-block:: yaml

    myprofile:
        keyid: GKTADJGHEIQSXMKKRBJ08H
        key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
            region: us-east-1

.. code-block:: yaml

    Ensure DynamoDB table does not exist:
      boto_dynamodb.absent:
        - table_name: new_table
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
        - region: us-east-1

    Ensure DynamoDB table exists:
      boto_dynamodb.present:
        - table_name: new_table
        - read_capacity_units: 1
        - write_capacity_units: 2
        - hash_key: primary_id
        - hash_key_data_type: N
        - range_key: start_timestamp
        - range_key_data_type: N
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
        - region: us-east-1
        - local_indexes:
            - index:
                - name: "primary_id_end_timestamp_index"
                - hash_key: primary_id
                - hash_key_data_type: N
                - range_key: end_timestamp
                - range_key_data_type: N
        - global_indexes:
            - index:
                - name: "name_end_timestamp_index"
                - hash_key: name
                - hash_key_data_type: S
                - range_key: end_timestamp
                - range_key_data_type: N
                - read_capacity_units: 3
                - write_capacity_units: 4

It's possible to specify cloudwatch alarms that will be setup along with the
DynamoDB table. Note the alarm name will be defined by the name attribute
provided, plus the DynamoDB resource name.

.. code-block:: yaml

    Ensure DynamoDB table exists:
      boto_dynamodb.present:
        - name: new_table
        - read_capacity_units: 1
        - write_capacity_units: 2
        - hash_key: primary_id
        - hash_key_data_type: N
        - range_key: start_timestamp
        - range_key_data_type: N
        - alarms:
             ConsumedWriteCapacityUnits:
                name: 'DynamoDB ConsumedWriteCapacityUnits **MANAGED BY SALT**'
                attributes:
                  metric: ConsumedWriteCapacityUnits
                  namespace: AWS/DynamoDB
                  statistic: Sum
                  comparison: '>='
                  # threshold_percent is used to calculate the actual threshold
                  # based on the provisioned capacity for the table.
                  threshold_percent: 0.75
                  period: 300
                  evaluation_periods: 2
                  unit: Count
                  description: 'DynamoDB ConsumedWriteCapacityUnits'
                  alarm_actions: [ 'arn:aws:sns:us-east-1:1234:my-alarm' ]
                  insufficient_data_actions: []
                  ok_actions: [ 'arn:aws:sns:us-east-1:1234:my-alarm' ]
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
        - region: us-east-1

You can also use alarms from pillars, and override values from the pillar
alarms by setting overrides on the resource. Note that 'boto_dynamodb_alarms'
will be used as a default value for all resources, if defined and can be
used to ensure alarms are always set for a resource.

Setting the alarms in a pillar:

.. code-block:: yaml

    boto_dynamodb_alarms:
      ConsumedWriteCapacityUnits:
        name: 'DynamoDB ConsumedWriteCapacityUnits **MANAGED BY SALT**'
        attributes:
          metric: ConsumedWriteCapacityUnits
          namespace: AWS/DynamoDB
          statistic: Sum
          comparison: '>='
          # threshold_percent is used to calculate the actual threshold
          # based on the provisioned capacity for the table.
          threshold_percent: 0.75
          period: 300
          evaluation_periods: 2
          unit: Count
          description: 'DynamoDB ConsumedWriteCapacityUnits'
          alarm_actions: [ 'arn:aws:sns:us-east-1:1234:my-alarm' ]
          insufficient_data_actions: []
          ok_actions: [ 'arn:aws:sns:us-east-1:1234:my-alarm' ]

    Ensure DynamoDB table exists:
      boto_dynamodb.present:
        - name: new_table
        - read_capacity_units: 1
        - write_capacity_units: 2
        - hash_key: primary_id
        - hash_key_data_type: N
        - range_key: start_timestamp
        - range_key_data_type: N
        - alarms:
             ConsumedWriteCapacityUnits:
                attributes:
                  threshold_percent: 0.90
                  period: 900
i(tabsolute_importtprint_functiontunicode_literalsN(tsixtleveltformatu.%(asctime)s %(name)s %(levelname)s %(message)ststreamtGsiNotUpdatableErrorcB@seZdZRS(u7Raised when a global secondary index cannot be updated.(t__name__t
__module__t__doc__(((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR�scC@sdtkrdnt}|S(u2
    Only load if boto_dynamodb is available.
    uboto_dynamodb.existsu
boto_dynamodb(t__salt__tFalse(tret((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyt__virtual__�suboto_dynamodb_alarmsuboto_dynamodb_backup_configsc"C@s�i|d6td6dd6id6}|rHdg|d<||d<|}ng}i}i}td|||||�}|s�td	r�d5|d<d
j|�|d<|Std||||||||
|||
||�
}|st|d<dj|�|d<t|||�|S|jd
j|��||d<||d<||d<|
|d<||d<||d<|
|d<||d<||d<n|jdj|��td|||||�}|jdi�jdi�}|jd�}|jd�}||ko||k}|s�td	r>d5|d<|jdj|��qtd|di|d 6|d!6d"|d#|d$|d%|�}|s�t|d<d&j|�|d<t|||�|S|jd'j|��|f|d<|f|d<|f|d<|f|d<n|jd(j|��|jdi�jd)g�}t	||||||||||�
}|ds�|j|d�|d|d<|dtkr�d*j
|�|d<t|||�|Snt|||	||||||�	}tj
|d|d�|d<|j|d�|dsa|d|d<|dtkrad*j
|�|d<t|||�|Sntjtd+|g��}x�|D]�} td,|d-| dd.| d/d0| d1d2| d3�}!|!dtd5gkr2|!d|d<|j|!d�|!jd�rg|!jd�f|dd4j| d�<qgq�d*j
|d|!dg�|d<t|||�|Sq�Wd*j
|�|d<t|||�|S(6u�
    Ensure the DynamoDB table exists. Table throughput can be updated after
    table creation.

    Global secondary indexes (GSIs) are managed with some exceptions:

    - If a GSI deletion is detected, a failure will occur (deletes should be
      done manually in the AWS console).

    - If multiple GSIs are added in a single Salt call, a failure will occur
      (boto supports one creation at a time). Note that this only applies after
      table creation; multiple GSIs can be created during table creation.

    - Updates to existing GSIs are limited to read/write capacity only
      (DynamoDB limitation).

    name
        Name of the DynamoDB table

    table_name
        Name of the DynamoDB table (deprecated)

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.

    read_capacity_units
        The read throughput for this table

    write_capacity_units
        The write throughput for this table

    hash_key
        The name of the attribute that will be used as the hash key
        for this table

    hash_key_data_type
        The DynamoDB datatype of the hash key

    range_key
        The name of the attribute that will be used as the range key
        for this table

    range_key_data_type
        The DynamoDB datatype of the range key

    local_indexes
        The local indexes you would like to create

    global_indexes
        The global indexes you would like to create

    backup_configs_from_pillars
        Pillars to use to configure DataPipeline backups
    unameuresultuucommentuchangesuMboto_dynamodb.present: `table_name` is deprecated. Please use `name` instead.uwarningsuboto_dynamodb.existsutestu$DynamoDB table {0} would be created.uboto_dynamodb.create_tableuFailed to create table {0}u+DynamoDB table {0} was successfully createdutableuread_capacity_unitsuwrite_capacity_unitsuhash_keyuhash_key_data_typeu	range_keyurange_key_data_typeu
local_indexesuglobal_indexesuDynamoDB table {0} existsuboto_dynamodb.describeuTableuProvisionedThroughputuWriteCapacityUnitsuReadCapacityUnitsu(DynamoDB table {0} is set to be updated.uboto_dynamodb.updatet
throughputureaduwritetregiontkeytkeyidtprofileuFailed to update table {0}u+DynamoDB table {0} was successfully updatedu%DynamoDB table {0} throughput matchesuGlobalSecondaryIndexesu,
u
pillar.gettnamet
schedule_nametperioduperiodtutc_houruutc_hourts3_base_locationus3_base_locationubackup_datapipeline_{0}N(tTrueRt__opts__tNoneRRt_add_changestappendtgett_global_indexes_presenttjoint_alarms_presentt
dictupdatetupdatetcopytdeepcopyt#_ensure_backup_datapipeline_present("Rt
table_nameRRRRtread_capacity_unitstwrite_capacity_unitstalarmstalarms_from_pillarthash_keythash_key_data_typet	range_keytrange_key_data_typet
local_indexestglobal_indexestbackup_configs_from_pillarsR
tcommentstchanges_oldtchanges_newttable_existst
is_createdtdescriptiontprovisioned_throughputtcurrent_write_capacity_unitstcurrent_read_capacity_unitstthroughput_matchest
is_updatedtprovisioned_indexest_rettdatapipeline_configstconfigtdatapipeline_ret((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pytpresent�s�Q"

		

	









	


	



	






-!cC@s2|r||dd<n|r.||dd<ndS(Nuchangesuoldunew((R
R4R5((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR�sc

C@s*itd6}
|r,td�|D��}ni}t|j��}i}
|r�x�|D]�}|j�d}d
}x6|D].}|j�dgkrz|j�d}qzqzW|s�t|
d<dj|�|
d<|
S||
|<qWWnt|t|
j���\}}}|r8t|
d<djdj	|��|
d<|
St
|�d	krkt|
d<d
j|�|
d<|
S|r�tt|��}t
|
||||||
||||	�|
ds�|
Sn|r�t|
||||||
|||||	�|
ds�|
Snd|kr&d|kr&|jd�n|
S(u;Handles global secondary index for the table present state.uresultcs@s|]}|d|fVqdS(u	IndexNameN((t.0tindex((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pys	<genexpr>�siunameu"Index name not found for table {0}ucommentuTDeletion of GSIs ({0}) is not supported! Please do this manually in the AWS console.u, iujCreation of multiple GSIs ({0}) is not supported due to API limitations. Please create them one at a time.uglobal_indexesu"All global secondary indexes matchN(RtdicttsettkeystvaluesRRRt_partition_index_namesR tlentnexttitert_add_global_secondary_indext _update_global_secondary_indexesR(R>R1R4R5R3RRRRRR
tprovisioned_gsi_configtprovisioned_index_namest
gsi_configREtindex_configt
index_nametentrytexisting_index_namestnew_index_namestindex_names_to_be_deleted((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR�sZ



!



	
cC@sct�}t�}x7|D]/}||kr;|j|�q|j|�qW||}|||fS(uOReturns 3 disjoint sets of indexes: existing, to be created, and to be deleted.(RGtadd(RQtindex_namesRVRWRRX((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyRJ�s		

cC@s�tdr.d|d<dj||�|d<dS|jdi�td|td||d	t�d
|d|d|	d
|
�}|r�|jdj|��|||d|<nt|d<dj|�|d<dS(u4Updates ret iff there was a failure or in test mode.utesturesultu+Dynamo table {0} will have a GSI added: {1}ucommentNuglobal_indexesu+boto_dynamodb.create_global_secondary_indexuboto_dynamodb.extract_indextglobal_indexRRRRuCreated GSI {0}uFailed to create GSI {0}(RRRt
setdefaultRRRR(R
RRTR4R5R3RRRRRRtsuccess((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyRN�s&

	
cC@sVyt|||�\}}
Wn1tk
rO}t|d<tj|�|d<dSX|
rRtdr�d|d<dj|dj|
j	���|d<dS|j
di�|j
di�td||
d	|d
|	d|
d|�}|r2|jd
j|
��xR|
D]*}|||d|<|
||d|<qWqRt|d<dj|
�|d<ndS(u4Updates ret iff there was a failure or in test mode.uresultucommentNutestu,Dynamo table {0} will have GSIs updated: {1}u, uglobal_indexesu+boto_dynamodb.update_global_secondary_indexRRRRu%Updated GSIs with new throughputs {0}u$Failed to update GSI throughputs {0}(
t_determine_gsi_updatesRRRt	text_typeRRRR RHR\RR(R
R4R5R3RVRPRRRRRRRtprovisioned_throughputst
index_updatesteR]RT((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyROs<


	

cC@s�i}i}x�|D]�}||}td||dt�j�}x�|D]�}||krJ|dkrJ||}	||}
|dkr�|	d|
dkr�td��qt|	jdg��t|
jdg��krtd��qq|	|
krtd	j||���qqJqJW|jd�}|jd
�}|jd�}
i|d6|
d
6||<|jd�}|jd
�}|jd�}||ks�|
|kri|d6|d
6||<qqW||fS(Nuboto_dynamodb.extract_indexR[uProvisionedThroughputu
ProjectionuProjectionTypeu!GSI projection types do not matchuNonKeyAttributesu0NonKeyAttributes do not match for GSI projectionu0GSI property {0} cannot be updated for index {1}uReadCapacityUnitsuWriteCapacityUnitsureaduwrite(RRtschemaRRGRR(RVRPRRR`RaRTtcurrent_configt
new_configRt	new_valuet
current_valuetcurrent_throughputtcurrent_readt
current_writetnew_throughputtnew_readt	new_write((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR^Bs8




0c	C@sbtjtd|i��}	|r7tj|	|�}	ni|d6td6dd6id6}
xtj|	�D]�\}}|d|d|d<|d|dd	|dd	<i|gd
6|dd<|ddd
kr2d|dkr2tj	||dd�|dd<|dd=|ddc|dd9<n|dddkr�d|dkr�tj	||dd�|dd<|dd=|ddc|dd9<ni|dd6|dd6|d6|d6|d6|d6}
t
d|
�}|ds|d|
d<n|jdi�ikr7|d|
d|d<nd|kri|
dc|d7<qiqiW|
S(uAhelper method for present.  ensure that cloudwatch_alarms are setu
config.optionunameuresultuucommentuchangesu u
attributesudescriptionu	TableNameu
dimensionsumetricuConsumedWriteCapacityUnitsu	thresholduthreshold_percentuperioduConsumedReadCapacityUnitsuregionukeyukeyiduprofileuboto_cloudwatch_alarm.present(R$R%RR"R#RRt	iteritemstmathtceilt
__states__R(RR*R+R)R(RRRRttmptmerged_return_valuet_tinfotkwargstresults((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR!isD"##

cC@s�idj||�d6ii|d6i|d6dd6t||�j�d6d6d6d	6i|d
6dj||�d6d
6}td|�S(Nu{0}-{1}-backupunameuperioduScheduleutypeu
startDateTimeufieldsuDefaultScheduleupipeline_objectsumyDDBTableNameu{0}/{1}/u
myOutputS3Locuparameter_valuesuboto_datapipeline.present(Rt_next_datetime_with_utc_hourt	isoformatRq(RRRRRRv((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyR&�s"cC@st|�|S(uV
    For a given table_name, returns hash of the table_name limited by max_value.
    (thash(R't	max_value((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyt'_get_deterministic_value_for_table_name�scC@s�tjj�}tjd|jd|jd|jd|dt|d�dt|d��}|tjj�kr�tjdd	�}||7}n|S(
ux
    Datapipeline API is throttling us, as all the pipelines are started at the same time.
    We would like to uniformly distribute the startTime over a 60 minute window.

    Return the next future utc datetime where
        hour == utc_hour
        minute = A value between 0-59 (depending on table name)
        second = A value between 0-59 (depending on table name)
    tyeartmonthtdaythourtminutei<tsecondtdaysi(	tdatetimetdatettodayR}R~RR|tutcnowt	timedelta(R'RR�tstart_date_timetone_day((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyRx�s
				
cC@si|d6td6dd6id6}td|||||�}|s[dj|�|d<|Stdr�d	j|�|d<d|d<|Std
|||||�}|r�dj|�|d<|djdd
j|��|djddj|��ndj|�|d<t|d<|S(ug
    Ensure the DynamoDB table does not exist.

    name
        Name of the DynamoDB table.

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.
    unameuresultuucommentuchangesuboto_dynamodb.existsu!DynamoDB table {0} does not existutestu'DynamoDB table {0} is set to be deleteduboto_dynamodb.deleteuDeleted DynamoDB table {0}uolduTable {0} existsunewuTable {0} deletedu#Failed to delete DynamoDB table {0}N(RRRRRR\R(RRRRRR
texistst
is_deleted((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pytabsent�s,"	

 
($R
t
__future__RRRR�RotsystloggingR$tsalt.extRtsalt.utils.dictupdatetutilsR"tbasicConfigtINFOtstdoutt	getLoggertlogt	ExceptionRRRRCRRRJRNROR^R!R&R|RxR�(((s=/usr/lib/python2.7/site-packages/salt/states/boto_dynamodb.pyt<module>�s\		
	�		?	
		(	'	6			

Zerion Mini Shell 1.0