def Run(self, args): """This is what gets called when the user runs this 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. """ cli = util.GetAdminClient() ref = resources.REGISTRY.Parse( args.instance, collection='bigtableadmin.projects.instances') msg = util.GetAdminMessages().BigtableadminProjectsInstancesGetRequest( projectsId=ref.projectsId, instancesId=ref.Name()) instance = cli.projects_instances.Get(msg) instance.state = None # must be unset when calling Update if args.description: instance.displayName = args.description msg = util.GetAdminMessages( ).BigtableadminProjectsInstancesUpdateRequest( projectsId=ref.projectsId, instancesId=ref.Name(), instance=instance) instance = cli.projects_instances.Update(msg) log.UpdatedResource(instance.name, kind='instance') return instance
def GetIamPolicy(instance_ref): """Get IAM policy for a given instance.""" client = util.GetAdminClient() msgs = util.GetAdminMessages() req = msgs.BigtableadminProjectsInstancesGetIamPolicyRequest( resource=instance_ref.RelativeName()) return client.projects_instances.GetIamPolicy(req)
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Some value that we want to have printed later. """ cli = util.GetAdminClient() instance_ref = args.CONCEPTS.instance.Parse() cluster_ref = args.CONCEPTS.cluster.Parse() if cluster_ref: cluster_str = cluster_ref.RelativeName() elif instance_ref: if args.IsSpecified('cluster'): cluster_str = instance_ref.RelativeName( ) + '/clusters/' + args.cluster else: cluster_str = instance_ref.RelativeName() + '/clusters/-' else: raise exceptions.InvalidArgumentException( '--instance', '--instance must be specified') msg = (util.GetAdminMessages(). BigtableadminProjectsInstancesClustersBackupsListRequest( parent=cluster_str)) for backup in list_pager.YieldFromList( cli.projects_instances_clusters_backups, msg, field='backups', batch_size_attribute=None): yield backup
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Some value that we want to have printed later. """ cli = util.GetAdminClient() # cluster_ref: A resource reference to the cluster to search for hottablets. cluster_ref = args.CONCEPTS.cluster.Parse() # Create a ListHotTablets Request and send through Admin Client. msg = (util.GetAdminMessages( ).BigtableadminProjectsInstancesClustersHotTabletsListRequest( parent=cluster_ref.RelativeName(), startTime=args.start_time and times.FormatDateTime(args.start_time), endTime=args.end_time and times.FormatDateTime(args.end_time))) for hot_tablet in list_pager.YieldFromList( cli.projects_instances_clusters_hotTablets, msg, field='hotTablets', batch_size_attribute=None): yield hot_tablet
def Run(self, args): cli = util.GetAdminClient() msgs = util.GetAdminMessages() instances = args.instances results = [] for instance in instances: instance_ref = resources.REGISTRY.Parse( instance, params={ 'projectsId': properties.VALUES.core.project.GetOrFail }, collection='bigtableadmin.projects.instances') request = msgs.BigtableadminProjectsInstancesTablesListRequest( parent=instance_ref.RelativeName(), ) for table in list_pager.YieldFromList( cli.projects_instances_tables, request, field='tables', batch_size_attribute=None): results.append(table) return results
def Run(self, args): """This is what gets called when the user runs this 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. """ cli = bigtable_util.GetAdminClient() ref = resources.REGISTRY.Parse( args.instance, params={ 'projectsId': properties.VALUES.core.project.GetOrFail, }, collection='bigtableadmin.projects.instances') msgs = bigtable_util.GetAdminMessages() instance = cli.projects_instances.Get( msgs.BigtableadminProjectsInstancesGetRequest( name=ref.RelativeName())) instance.state = None # must be unset when calling Update if args.description: instance.displayName = args.description if args.instance_type: instance.type = msgs.Instance.TypeValueValuesEnum( args.instance_type) instance = cli.projects_instances.Update(instance) log.UpdatedResource(instance.name, kind='instance') return instance
def Create(cluster_ref, zone, serve_nodes=3): """Create a cluster. Args: cluster_ref: A resource reference to the cluster to create. zone: string, The zone this cluster should be inside. serve_nodes: int, The number of nodes in this cluster. Returns: Long running operation. """ client = util.GetAdminClient() msgs = util.GetAdminMessages() storage_type = ( msgs.Cluster.DefaultStorageTypeValueValuesEnum.STORAGE_TYPE_UNSPECIFIED) cluster_msg = msgs.Cluster( serveNodes=serve_nodes, location=util.LocationUrl(zone), defaultStorageType=storage_type) msg = msgs.BigtableadminProjectsInstancesClustersCreateRequest( cluster=cluster_msg, clusterId=cluster_ref.Name(), parent=cluster_ref.Parent().RelativeName()) return client.projects_instances_clusters.Create(msg)
def Run(self, args): """This is what gets called when the user runs this 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. """ cli = util.GetAdminClient() msgs = util.GetAdminMessages() for instance in args.instance: should_continue = console_io.PromptContinue( message='Delete instance {}. Are you sure?'.format(instance)) if should_continue: ref = resources.REGISTRY.Parse( instance, params={ 'projectsId': properties.VALUES.core.project.GetOrFail, }, collection='bigtableadmin.projects.instances') msg = msgs.BigtableadminProjectsInstancesDeleteRequest( name=ref.RelativeName()) cli.projects_instances.Delete(msg) return None
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Some value that we want to have printed later. """ cli = util.GetAdminClient() instance_refs = args.CONCEPTS.instances.Parse() if not args.IsSpecified('instances'): instance_refs = [util.GetInstanceRef('-')] for instance_ref in instance_refs: msg = ( util.GetAdminMessages() .BigtableadminProjectsInstancesClustersListRequest( parent=instance_ref.RelativeName())) for cluster in list_pager.YieldFromList( cli.projects_instances_clusters, msg, field='clusters', batch_size_attribute=None): yield cluster
def ProcessInstanceTypeAndNodes(args, instance_type): """Ensure that --instance-type and --num-nodes are consistent. If --instance-type is DEVELOPMENT, then no --cluster-num-nodes can be specified. If --instance-type is PRODUCTION, then --cluster-num-nodes must be at least 3 if specified and defaults to 3 if not specified. Args: args: an argparse namespace. instance_type: string, The instance type; PRODUCTION or DEVELOPMENT. Raises: exceptions.InvalidArgumentException: If --cluster-num-nodes is specified when --instance-type is DEVELOPMENT, or if --instance-type is PRODUCTION and --cluster-num-nodes is less than 3. Returns: Number of nodes or None if DEVELOPMENT instance-type. """ msgs = util.GetAdminMessages() num_nodes = args.cluster_num_nodes if not args.IsSpecified('cluster_num_nodes'): if instance_type == msgs.Instance.TypeValueValuesEnum.PRODUCTION: num_nodes = 3 else: if instance_type == msgs.Instance.TypeValueValuesEnum.DEVELOPMENT: raise exceptions.InvalidArgumentException( '--cluster-num-nodes', 'Cannot set --cluster-num-nodes for DEVELOPMENT instances.') elif num_nodes < 3: raise exceptions.InvalidArgumentException( '--cluster-num-nodes', 'Clusters of PRODUCTION instances must have at least 3 nodes.') return num_nodes
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Some value that we want to have printed later. """ cli = util.GetAdminClient() instances = args.instances or ['-'] for instance in instances: ref = resources.REGISTRY.Parse( instance, params={ 'projectsId': properties.VALUES.core.project.GetOrFail, }, collection='bigtableadmin.projects.instances') msg = (util.GetAdminMessages(). BigtableadminProjectsInstancesClustersListRequest( parent=ref.RelativeName())) for cluster in list_pager.YieldFromList( cli.projects_instances_clusters, msg, field='clusters', batch_size_attribute=None): yield cluster
def Run(self, args): """This is what gets called when the user runs this 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. """ cli = util.GetAdminClient() msgs = util.GetAdminMessages() ref = resources.REGISTRY.Parse( args.cluster, params={'instancesId': args.instance}, collection='bigtableadmin.projects.instances.clusters') msg = msgs.BigtableadminProjectsInstancesClustersUpdateRequest( projectsId=ref.projectsId, instancesId=ref.instancesId, clustersId=ref.Name(), cluster=msgs.Cluster(name=ref.Name(), serveNodes=args.num_nodes)) result = cli.projects_instances_clusters.Update(msg) if not args.async: # TODO(user): enable this line when b/29563942 is fixed in apitools pass # util.WaitForOpV2(result, 'Updating cluster') log.UpdatedResource(args.cluster, kind='cluster', async=args.async)
def _Cluster(self, args): msgs = util.GetAdminMessages() storage_type = (msgs.Cluster.DefaultStorageTypeValueValuesEnum. STORAGE_TYPE_UNSPECIFIED) cluster = msgs.Cluster(serveNodes=args.num_nodes, location=util.LocationUrl(args.zone), defaultStorageType=storage_type) kms_key = arguments.GetAndValidateKmsKeyName(args) if kms_key: cluster.encryptionConfig = msgs.EncryptionConfig( kmsKeyName=kms_key) if (args.autoscaling_min_nodes is not None or args.autoscaling_max_nodes is not None or args.autoscaling_cpu_target is not None): cluster.clusterConfig = clusters.BuildClusterConfig( autoscaling_min=args.autoscaling_min_nodes, autoscaling_max=args.autoscaling_max_nodes, autoscaling_cpu_target=args.autoscaling_cpu_target) # serveNodes must be set to None or 0 to enable Autoscaling. # go/cbt-autoscaler-api cluster.serveNodes = None return cluster
def Run(self, args): """This is what gets called when the user runs this 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. """ cli = util.GetAdminClient() ref_name = 'operations/' + resources.REGISTRY.Parse( properties.VALUES.core.project.Get(required=True), collection='bigtableadmin.projects').RelativeName() if args.IsSpecified('instance'): ref_name = ref_name + '/instances/' + args.instance msg = (util.GetAdminMessages( ).BigtableadminOperationsProjectsOperationsListRequest(name=ref_name)) return list_pager.YieldFromList(cli.operations_projects_operations, msg, field='operations', batch_size_attribute=None)
def ProcessInstanceTypeAndNodes(args): """Ensure that --instance-type and --num-nodes are consistent. If --instance-type is DEVELOPMENT, then no --cluster-num-nodes can be specified. If --instance-type is PRODUCTION, then --cluster-num-nodes defaults to 3 if not specified, but can be any positive value. Args: args: an argparse namespace. Raises: exceptions.InvalidArgumentException: If --cluster-num-nodes is specified when --instance-type is DEVELOPMENT, or --cluster-num-nodes is not positive. Returns: Number of nodes or None if DEVELOPMENT instance-type. """ msgs = util.GetAdminMessages() num_nodes = args.cluster_num_nodes instance_type = msgs.Instance.TypeValueValuesEnum(args.instance_type) if not args.IsSpecified('cluster_num_nodes'): if instance_type == msgs.Instance.TypeValueValuesEnum.PRODUCTION: num_nodes = 3 else: if instance_type == msgs.Instance.TypeValueValuesEnum.DEVELOPMENT: raise exceptions.InvalidArgumentException( '--cluster-num-nodes', 'Cannot set --cluster-num-nodes for DEVELOPMENT instances.') elif num_nodes < 1: raise exceptions.InvalidArgumentException( '--cluster-num-nodes', 'Clusters of PRODUCTION instances must have at least 1 node.') return num_nodes
def SetPolicy(instance_ref, policy): """Sets the given policy on the instance, overwriting what exists.""" client = util.GetAdminClient() msgs = util.GetAdminMessages() req = msgs.BigtableadminProjectsInstancesSetIamPolicyRequest( resource=instance_ref.RelativeName(), setIamPolicyRequest=msgs.SetIamPolicyRequest(policy=policy)) return client.projects_instances.SetIamPolicy(req)
def _Cluster(self, args): msgs = util.GetAdminMessages() storage_type = (msgs.Cluster.DefaultStorageTypeValueValuesEnum. STORAGE_TYPE_UNSPECIFIED) cluster = msgs.Cluster(serveNodes=args.num_nodes, location=util.LocationUrl(args.zone), defaultStorageType=storage_type) return cluster
def BuildClusterConfig(autoscaling_min=None, autoscaling_max=None, autoscaling_cpu_target=None): msgs = util.GetAdminMessages() return msgs.ClusterConfig( clusterAutoscalingConfig=BuildClusterAutoscalingConfig( min_nodes=autoscaling_min, max_nodes=autoscaling_max, cpu_target=autoscaling_cpu_target))
def Run(self, args): """This is what gets called when the user runs this 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. """ # TODO(b/73365914) remove after deprecation period display_name = args.display_name or args.description cli = bigtable_util.GetAdminClient() ref = resources.REGISTRY.Parse( args.instance, params={ 'projectsId': properties.VALUES.core.project.GetOrFail, }, collection='bigtableadmin.projects.instances') parent_ref = resources.REGISTRY.Create('bigtableadmin.projects', projectId=ref.projectsId) msgs = bigtable_util.GetAdminMessages() msg = msgs.CreateInstanceRequest( instanceId=ref.Name(), parent=parent_ref.RelativeName(), instance=msgs.Instance( # TODO(b/73365914) replace with args.display_name after deprecation displayName=display_name, type=msgs.Instance.TypeValueValuesEnum(args.instance_type)), clusters=msgs.CreateInstanceRequest. ClustersValue(additionalProperties=[ msgs.CreateInstanceRequest.ClustersValue.AdditionalProperty( key=args.cluster, value=msgs.Cluster( serveNodes=args.cluster_num_nodes, defaultStorageType=( msgs.Cluster.DefaultStorageTypeValueValuesEnum( args.cluster_storage_type.upper())), # TODO(b/36056455): switch location to resource # when b/29566669 is fixed on API location=bigtable_util.LocationUrl(args.cluster_zone))) ])) result = cli.projects_instances.Create(msg) operation_ref = resources.REGISTRY.ParseRelativeName( result.name, 'bigtableadmin.operations') if args. async: log.CreatedResource(operation_ref, kind='bigtable instance {0}'.format( ref.Name()), async=True) return result return bigtable_util.WaitForInstance( cli, operation_ref, 'Creating bigtable instance {0}'.format(ref.Name()))
def SetIamPolicy(instance_ref, policy): """Sets the given policy on the instance, overwriting what exists.""" client = util.GetAdminClient() msgs = util.GetAdminMessages() policy.version = iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION req = msgs.BigtableadminProjectsInstancesSetIamPolicyRequest( resource=instance_ref.RelativeName(), setIamPolicyRequest=msgs.SetIamPolicyRequest(policy=policy)) return client.projects_instances.SetIamPolicy(req)
def BuildClusterAutoscalingConfig(min_nodes=None, max_nodes=None, cpu_target=None): msgs = util.GetAdminMessages() limits = msgs.AutoscalingLimits( minServeNodes=min_nodes, maxServeNodes=max_nodes) targets = msgs.AutoscalingTargets(cpuUtilizationPercent=cpu_target) return msgs.ClusterAutoscalingConfig( autoscalingLimits=limits, autoscalingTargets=targets)
def GetIamPolicy(instance_ref): """Get IAM policy for a given instance.""" client = util.GetAdminClient() msgs = util.GetAdminMessages() req = msgs.BigtableadminProjectsInstancesGetIamPolicyRequest( resource=instance_ref.RelativeName(), getIamPolicyRequest=msgs.GetIamPolicyRequest( options=msgs.GetPolicyOptions(requestedPolicyVersion=iam_util. MAX_LIBRARY_IAM_SUPPORTED_VERSION))) return client.projects_instances.GetIamPolicy(req)
def Delete(cluster_ref): """Delete a cluster. Args: cluster_ref: A resource reference to the cluster to delete. """ client = util.GetAdminClient() msgs = util.GetAdminMessages() msg = msgs.BigtableadminProjectsInstancesClustersDeleteRequest( name=cluster_ref.RelativeName()) client.projects_instances_clusters.Delete(msg)
def _Cluster(self, args): msgs = util.GetAdminMessages() num_nodes = arguments.ProcessInstanceTypeAndNodes(args) storage_type = msgs.Cluster.DefaultStorageTypeValueValuesEnum( args.cluster_storage_type.upper()) return msgs.Cluster( serveNodes=num_nodes, defaultStorageType=storage_type, # TODO(b/36049938): switch location to resource # when b/29566669 is fixed on API location=util.LocationUrl(args.cluster_zone))
def _Cluster(self, args): msgs = util.GetAdminMessages() storage_type = (msgs.Cluster.DefaultStorageTypeValueValuesEnum. STORAGE_TYPE_UNSPECIFIED) cluster = msgs.Cluster(serveNodes=args.num_nodes, location=util.LocationUrl(args.zone), defaultStorageType=storage_type) kms_key = arguments.GetAndValidateKmsKeyName(args) if kms_key: cluster.encryptionConfig = msgs.EncryptionConfig( kmsKeyName=kms_key) return cluster
def Update(app_profile_ref, cluster=None, description='', multi_cluster=False, transactional_writes=False, force=False): """Update an app profile. Args: app_profile_ref: A resource reference of the app profile to update. cluster: string, The cluster id for the app profile to route to using single cluster routing. description: string, A description of the app profile. multi_cluster: bool, Whether this app profile should route to multiple clusters, instead of single cluster. transactional_writes: bool, Whether this app profile has transactional writes enabled. This is only possible when using single cluster routing. force: bool, Whether to ignore API warnings and create forcibly. Raises: ValueError: Cannot specify both cluster and multi_cluster. Returns: Long running operation. """ if cluster and multi_cluster: raise ValueError('Cannot update both --route-to and --route-any') client = util.GetAdminClient() msgs = util.GetAdminMessages() changed_fields = [] app_profile = msgs.AppProfile() if cluster: changed_fields.append('singleClusterRouting') app_profile.singleClusterRouting = msgs.SingleClusterRouting( clusterId=cluster, allowTransactionalWrites=transactional_writes) elif multi_cluster: changed_fields.append('multiClusterRoutingUseAny') app_profile.multiClusterRoutingUseAny = msgs.MultiClusterRoutingUseAny( ) if description: changed_fields.append('description') app_profile.description = description msg = msgs.BigtableadminProjectsInstancesAppProfilesPatchRequest( appProfile=app_profile, name=app_profile_ref.RelativeName(), updateMask=','.join(changed_fields), ignoreWarnings=force) return client.projects_instances_appProfiles.Patch(msg)
def _Clusters(self, args): """Get the clusters configs from command arguments. Args: args: the argparse namespace from Run(). Returns: A dict mapping from cluster id to msg.Cluster. """ msgs = util.GetAdminMessages() storage_type = msgs.Cluster.DefaultStorageTypeValueValuesEnum( args.cluster_storage_type.upper()) if args.cluster_config is not None: if (args.cluster is not None or args.cluster_zone is not None or args.cluster_num_nodes is not None): raise exceptions.InvalidArgumentException( '--cluster-config --cluster --cluster-zone --cluster-num-nodes', 'Use --cluster-config or the combination of --cluster, ' '--cluster-zone and --cluster-num-nodes to specify cluster(s), not ' 'both.') clusters = {} for cluster_dict in args.cluster_config: nodes = cluster_dict['nodes'] if 'nodes' in cluster_dict else 1 cluster = msgs.Cluster( serveNodes=nodes, defaultStorageType=storage_type, # TODO(b/36049938): switch location to resource # when b/29566669 is fixed on API location=util.LocationUrl(cluster_dict['zone'])) if 'kms-key' in cluster_dict: cluster.encryptionConfig = msgs.EncryptionConfig( kmsKeyName=cluster_dict['kms-key']) clusters[cluster_dict['id']] = cluster return clusters elif args.cluster is not None: if args.cluster_zone is None: raise exceptions.InvalidArgumentException( '--cluster-zone', '--cluster-zone must be specified.') cluster = msgs.Cluster( serveNodes=arguments.ProcessInstanceTypeAndNodes(args), defaultStorageType=storage_type, # TODO(b/36049938): switch location to resource # when b/29566669 is fixed on API location=util.LocationUrl(args.cluster_zone)) return {args.cluster: cluster} else: raise exceptions.InvalidArgumentException( '--cluster --cluster-config', 'Use --cluster-config to specify cluster(s).')
def CopyBackup(source_backup_ref, destination_backup_ref, args): """Copy a backup.""" client = util.GetAdminClient() msgs = util.GetAdminMessages() copy_backup_request = msgs.CopyBackupRequest( backupId=destination_backup_ref.Name(), sourceBackup=source_backup_ref.RelativeName()) copy_backup_request.expireTime = GetExpireTime(args) req = msgs.BigtableadminProjectsInstancesClustersBackupsCopyRequest( parent=destination_backup_ref.Parent().RelativeName(), copyBackupRequest=copy_backup_request) return client.projects_instances_clusters_backups.Copy(req)
def Run(self, args): """This is what gets called when the user runs this 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. """ cli = bigtable_util.GetAdminClient() ref = bigtable_util.GetInstanceRef(args.instance) parent_ref = resources.REGISTRY.Create('bigtableadmin.projects', projectId=ref.projectsId) msgs = bigtable_util.GetAdminMessages() instance_type = msgs.Instance.TypeValueValuesEnum(args.instance_type) num_nodes = arguments.ProcessInstanceTypeAndNodes(args, instance_type) msg = msgs.CreateInstanceRequest( instanceId=ref.Name(), parent=parent_ref.RelativeName(), instance=msgs.Instance(displayName=args.display_name, type=msgs.Instance.TypeValueValuesEnum( args.instance_type)), clusters=msgs.CreateInstanceRequest. ClustersValue(additionalProperties=[ msgs.CreateInstanceRequest.ClustersValue.AdditionalProperty( key=args.cluster, value=msgs.Cluster( serveNodes=num_nodes, defaultStorageType=( msgs.Cluster.DefaultStorageTypeValueValuesEnum( args.cluster_storage_type.upper())), # TODO(b/36056455): switch location to resource # when b/29566669 is fixed on API location=bigtable_util.LocationUrl(args.cluster_zone))) ])) result = cli.projects_instances.Create(msg) operation_ref = bigtable_util.GetOperationRef(result) if args.async_: log.CreatedResource(operation_ref, kind='bigtable instance {0}'.format( ref.Name()), is_async=True) return result return bigtable_util.AwaitInstance( operation_ref, 'Creating bigtable instance {0}'.format(ref.Name()))
def Describe(app_profile_ref): """Describe an app profile. Args: app_profile_ref: A resource reference to the app profile to describe. Returns: App profile resource object. """ client = util.GetAdminClient() msg = (util.GetAdminMessages(). BigtableadminProjectsInstancesAppProfilesGetRequest( name=app_profile_ref.RelativeName())) return client.projects_instances_appProfiles.Get(msg)