
    (                        d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 dd	l	mZ dd
lmZ ddlmZ ddlmZ e
j"                   e
j$                  e
j&                  j(                  e
j&                  j*                         G d de
j,                                      Z e
j$                  e
j&                  j0                         G d de             Zy)z"bigtable instances create command.    )absolute_import)division)unicode_literalsN)clusters)util)base)
exceptions)	arguments)log)	resourcesc                   v    e Zd ZdZdZd ej                  d      iZed        Z	d Z
d Zd Zd	 Zedd
       Zy)CreateInstanceCreate a new Bigtable instance.FEXAMPLESa            To create an instance with id `my-instance-id` with a cluster located
          in `us-east1-c`, run:

            $ {command} my-instance-id --display-name="My Instance" --cluster-config=id=my-cluster-id,zone=us-east1-c

          To create an instance with multiple clusters, run:

            $ {command} my-instance-id --display-name="My Instance" --cluster-config=id=my-cluster-id-1,zone=us-east1-c --cluster-config=id=my-cluster-id-2,zone=us-west1-c,nodes=3

          To create an instance with `HDD` storage and `10` nodes, run:

            $ {command} my-hdd-instance --display-name="HDD Instance" --cluster-storage-type=HDD --cluster-config=id=my-cluster-id,zone=us-east1-c,nodes=10

          c                    t        j                  |      j                  d      j                         j	                         j                         j                         j                         j                         j                          | j                  r#t        j                  |      j                          t        j                  |dd       |j                  j                  t         j                         y)z Register flags for this command.T)requiredz	to create)
positionalN)r
   ArgAdderAddInstanceDisplayNameAddClusterConfigAddDeprecatedClusterAddDeprecatedClusterZoneAddDeprecatedClusterNodesAddClusterStorageAddAsyncAddDeprecatedInstanceType_support_tagsAddTagsAddInstanceResourceArgdisplay_infoAddCacheUpdaterInstanceCompleter)clsparsers     (lib/surface/bigtable/instances/create.pyArgszCreateInstance.Args9   s     	6"				.							!	!	#	"	"	$					"	"	$  ((*$$V[TJ
''	(C(CD    c                 $    | j                  |      S )zExecutes the instances create command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    )_Run)selfargss     r%   RunzCreateInstance.RunN   s     99T?r'   c                 b   t        j                         }|j                  j                  j	                         }t
        j                  j                  d|j                        }t        j                         }|j                  j                  |j                        }| j                  |      }g }t        |j                               D ];  \  }	}
|j!                  |j"                  j$                  j'                  |	|
             = |j#                  |j)                         |j+                         |j                  |j,                  || j/                  |            |j"                  j%                  |            }|j0                  j                  |      }t        j2                  |      }|j4                  rEt7        j8                  |j+                         dj;                  |j)                               d	       |S t        j<                  |d
j;                  |j)                                     S )z8Implements Run() with different possible features flags.zbigtableadmin.projects)	projectIdkeyvalue)displayNametypetagsadditionalProperties)
instanceIdparentinstancer   zbigtable instance {0}T)kindis_asynczCreating bigtable instance {0})r   GetAdminClientCONCEPTSr9   Parser   REGISTRYCreate
projectsIdGetAdminMessagesInstanceTypeValueValuesEnuminstance_type	_ClusterssorteditemsappendCreateInstanceRequestClustersValueAdditionalPropertyNameRelativeNamedisplay_name_Tagsprojects_instancesGetOperationRefasync_r   CreatedResourceformatAwaitInstance)r*   r+   cliref
parent_refmsgsrE   new_clustersclusters_properties
cluster_idclustermsgresultoperation_refs                 r%   r)   zCreateInstance._Run[   s    


C
--
 
 
&
&
(C##** CNN + <J  "DMM55d6H6HIM>>$'L%l&8&8&:;
G  

$
$
2
2
E
EG F -.  <
 
$
$88:&&())D!  

 ++99!4 : 
 % C ##**3/F((0M{{	

$
$
&&--chhj9 m7>>sxxzJL Lr'   c           
         t        j                         }|j                  j                  |j                  j                               }|j                  \|j                  |j                  |j                  t        j                  dd      | j                  |j                         i }|j                  D ]  }|j                  dd      }|j                  d|j                  j                  |j                  j                  j                              }|j                  |||t        j                   |d               }d	|v r|j#                  |d	   
      |_        d|v sd|v sd|v r:d|v r|d   }	nd}	t'        j(                  |d   |d   |d   |	      |_        d|_        |||d   <    |S |j                  |j                  t        j                  dd      |j                  t/        j0                  |      ||j                  j                  |j                  j                  j                        t        j                   |j                              }|j                  |iS t        j                  dd      )zGet the clusters configs from command arguments.

    Args:
      args: the argparse namespace from Run().

    Returns:
      A dict mapping from cluster id to msg.Cluster.
    Nz=--cluster-config --cluster --cluster-zone --cluster-num-nodesz}Use --cluster-config or the combination of --cluster, --cluster-zone and --cluster-num-nodes to specify cluster(s), not both.nodes   znode-scaling-factorzone)
serveNodesnodeScalingFactordefaultStorageTypelocationzkms-key)
kmsKeyNameautoscaling-min-nodesautoscaling-max-nodesautoscaling-cpu-targetautoscaling-storage-target)autoscaling_minautoscaling_maxautoscaling_cpu_targetautoscaling_storage_targetidz--cluster-zonez!--cluster-zone must be specified.)rf   rh   rg   ri   z--cluster --cluster-configz+Use --cluster-config to specify cluster(s).)r   rB   Cluster!DefaultStorageTypeValueValuesEnumcluster_storage_typeuppercluster_configr^   cluster_zonecluster_num_nodesr	   InvalidArgumentException_ValidateClusterConfigArgsget NodeScalingFactorValueValuesEnumNODE_SCALING_FACTOR_1XLocationUrlEncryptionConfigencryptionConfigr   BuildClusterConfigclusterConfigrf   r
   ProcessInstanceTypeAndNodes)
r*   r+   rZ   storage_typer[   cluster_dictrc   node_scaling_factorr^   storage_targets
             r%   rF   zCreateInstance._Clusters   s      "D<<AA!!'')+L &
,,
"*##/11K 	 %%d&9&9:l--,  !,*..!LL99==TT
 ,,1+ %%l6&:;  = $%)%:%:%i0 &; &2'
" $|3#|3$4)\9)*FGN!N"*"="=*+BC*+BC%12J%K)7	#9'
  $'
+2\$'(K .L 		!				"11AC 	C::4@) LLIIll;;RR
 ##D$5$56  	8g llG$$//
&
7 r'   c                     |D ]O  }d|v sd|v s	d|v sd|v sd|v rt        j                  dd      d|vs	d|vsd|vs;t        j                  dd	       y
)z9Validates arguments in cluster-config as a repeated dict.rk   rl   rm   rn   rc   ze--autoscaling-min-nodes --autoscaling-max-nodes --autoscaling-cpu-target --autoscaling-storage-targetzAt most one of nodes | autoscaling-min-nodes autoscaling-max-nodes autoscaling-cpu-target autoscaling-storage-target may be specified in --cluster-configzH--autoscaling-min-nodes --autoscaling-max-nodes --autoscaling-cpu-targetzrAll of --autoscaling-min-nodes --autoscaling-max-nodes --autoscaling-cpu-target must be set to enable Autoscaling.N)r	   r{   )r*   rx   r   s      r%   r|   z)CreateInstance._ValidateClusterConfigArgs   s     '
!\
1
!\
1
"l
2
&,
6l"33FPQ Q $<7#<7$L833)+$% %# 'r'   c           
         | j                   syt        ||      }|syt        j                         j                  j
                  } |t        |j                               D cg c]  \  }}|j                  ||       c}}      S c c}}w )zGet the tags from command arguments.

    Args:
      args: the argparse namespace from Run().
      tags_arg_name: the name of the tags argument.

    Returns:
      A dict mapping from tag key to tag value.
    Nr/   r5   )	r   getattrr   rB   rC   	TagsValuerG   rH   rL   )r#   r+   tags_arg_namer4   tags_messager0   r1   s          r%   rP   zCreateInstance._Tags   s     4'D((*33==L %TZZ\2
2
U ++5+A2
 
s   #B	
N)r4   )__name__
__module____qualname____doc__r   textwrapdedentdetailed_helpclassmethodr&   r,   r)   rF   r|   rP    r'   r%   r   r       sl     (- /(// # -$ E E()LVTl%4  r'   r   c                       e Zd ZdZdZy)CreateInstanceAlphar   TN)r   r   r   r   r   r   r'   r%   r   r     s
    '-r'   r   )r   
__future__r   r   r   r   googlecloudsdk.api_lib.bigtabler   r   googlecloudsdk.callioper   r	   #googlecloudsdk.command_lib.bigtabler
   googlecloudsdk.corer   r   UniverseCompatibleReleaseTracksReleaseTrackGABETACreateCommandr   ALPHAr   r   r'   r%   <module>r      s    ) &  '  4 0 ( . 9 # ) D%%(($*;*;*@*@AnT'' n B nb D%%++,.  -r'   