/******************************************************************************* * Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). You may not use * this file except in compliance with the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * ***************************************************************************** * * AWS Tools for Windows (TM) PowerShell (TM) * */ using System; using System.Collections.Generic; using System.Linq; using System.Management.Automation; using System.Text; using Amazon.PowerShell.Common; using Amazon.Runtime; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; namespace Amazon.PowerShell.Cmdlets.DDB { /// /// The BatchGetItem operation returns the attributes of one or more items /// from one or more tables. You identify requested items by primary key. /// /// /// /// A single operation can retrieve up to 16 MB of data, which can contain as many as /// 100 items. BatchGetItem returns a partial result if the response size /// limit is exceeded, the table's provisioned throughput is exceeded, more than 1MB per /// partition is requested, or an internal processing failure occurs. If a partial result /// is returned, the operation returns a value for UnprocessedKeys. You can /// use this value to retry the operation starting with the next item to get. /// /// If you request more than 100 items, BatchGetItem returns a ValidationException /// with the message "Too many items requested for the BatchGetItem call." /// /// For example, if you ask to retrieve 100 items, but each individual item is 300 KB /// in size, the system returns 52 items (so as not to exceed the 16 MB limit). It also /// returns an appropriate UnprocessedKeys value so you can get the next /// page of results. If desired, your application can include its own logic to assemble /// the pages of results into one dataset. /// /// If none of the items can be processed due to insufficient provisioned throughput /// on all of the tables in the request, then BatchGetItem returns a ProvisionedThroughputExceededException. /// If at least one of the items is successfully processed, then BatchGetItem /// completes successfully, while returning the keys of the unread items in UnprocessedKeys. /// /// If DynamoDB returns any unprocessed items, you should retry the batch operation on /// those items. However, we strongly recommend that you use an exponential backoff /// algorithm. If you retry the batch operation immediately, the underlying read or /// write requests can still fail due to throttling on the individual tables. If you delay /// the batch operation using exponential backoff, the individual requests in the batch /// are much more likely to succeed. /// /// For more information, see Batch /// Operations and Error Handling in the Amazon DynamoDB Developer Guide. /// /// By default, BatchGetItem performs eventually consistent reads on every /// table in the request. If you want strongly consistent reads instead, you can set ConsistentRead /// to true for any or all tables. /// /// In order to minimize response latency, BatchGetItem may retrieve items /// in parallel. /// /// When designing your application, keep in mind that DynamoDB does not return items /// in any particular order. To help parse the response by item, include the primary key /// values for the items in your request in the ProjectionExpression parameter. /// /// If a requested item does not exist, it is not returned in the result. Requests for /// nonexistent items consume the minimum read capacity units according to the type of /// read. For more information, see Working /// with Tables in the Amazon DynamoDB Developer Guide. /// /// [Cmdlet("Get", "DDBBatchItem")] [OutputType("System.String")] [AWSCmdlet("Calls the Amazon DynamoDB BatchGetItem API operation.", Operation = new[] {"BatchGetItem"}, SelectReturnType = typeof(Amazon.DynamoDBv2.Model.BatchGetItemResponse))] [AWSCmdletOutput("System.String or Amazon.DynamoDBv2.Model.BatchGetItemResponse", "This cmdlet returns a collection of System.String objects.", "The service call response (type Amazon.DynamoDBv2.Model.BatchGetItemResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack." )] public partial class GetDDBBatchItemCmdlet : AmazonDynamoDBClientCmdlet, IExecutor { #region Parameter RequestItem /// /// /// A map of one or more table names and, for each table, a map that describes one or /// more items to retrieve from that table. Each table name can be used only once per /// BatchGetItem request.Each element in the map of items to retrieve consists of the following: /// /// #if !MODULAR [System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)] #else [System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)] [System.Management.Automation.AllowEmptyCollection] [System.Management.Automation.AllowNull] #endif [Amazon.PowerShell.Common.AWSRequiredParameter] [Alias("RequestItems")] public System.Collections.Hashtable RequestItem { get; set; } #endregion #region Parameter ReturnConsumedCapacity /// /// /// The service has not provided documentation for this parameter; please refer to the service's API reference documentation for the latest available information. /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.DynamoDBv2.ReturnConsumedCapacity")] public Amazon.DynamoDBv2.ReturnConsumedCapacity ReturnConsumedCapacity { get; set; } #endregion #region Parameter Select /// /// Use the -Select parameter to control the cmdlet output. The default value is 'Responses'. /// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.DynamoDBv2.Model.BatchGetItemResponse). /// Specifying the name of a property of type Amazon.DynamoDBv2.Model.BatchGetItemResponse will result in that property being returned. /// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value. /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public string Select { get; set; } = "Responses"; #endregion #region Parameter PassThru /// /// Changes the cmdlet behavior to return the value passed to the RequestItem parameter. /// The -PassThru parameter is deprecated, use -Select '^RequestItem' instead. This parameter will be removed in a future version. /// [System.Obsolete("The -PassThru parameter is deprecated, use -Select '^RequestItem' instead. This parameter will be removed in a future version.")] [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public SwitchParameter PassThru { get; set; } #endregion protected override void ProcessRecord() { this._AWSSignerType = "v4"; base.ProcessRecord(); var context = new CmdletContext(); // allow for manipulation of parameters prior to loading into context PreExecutionContextLoad(context); #pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute if (ParameterWasBound(nameof(this.Select))) { context.Select = CreateSelectDelegate(Select) ?? throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select)); if (this.PassThru.IsPresent) { throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select)); } } else if (this.PassThru.IsPresent) { context.Select = (response, cmdlet) => this.RequestItem; } #pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute if (this.RequestItem != null) { context.RequestItem = new Dictionary(StringComparer.Ordinal); foreach (var hashKey in this.RequestItem.Keys) { context.RequestItem.Add((String)hashKey, (KeysAndAttributes)(this.RequestItem[hashKey])); } } #if MODULAR if (this.RequestItem == null && ParameterWasBound(nameof(this.RequestItem))) { WriteWarning("You are passing $null as a value for parameter RequestItem which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues."); } #endif context.ReturnConsumedCapacity = this.ReturnConsumedCapacity; // allow further manipulation of loaded context prior to processing PostExecutionContextLoad(context); var output = Execute(context) as CmdletOutput; ProcessOutput(output); } #region IExecutor Members public object Execute(ExecutorContext context) { var cmdletContext = context as CmdletContext; // create request var request = new Amazon.DynamoDBv2.Model.BatchGetItemRequest(); if (cmdletContext.RequestItem != null) { request.RequestItems = cmdletContext.RequestItem; } if (cmdletContext.ReturnConsumedCapacity != null) { request.ReturnConsumedCapacity = cmdletContext.ReturnConsumedCapacity; } CmdletOutput output; // issue call var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint); try { var response = CallAWSServiceOperation(client, request); object pipelineOutput = null; pipelineOutput = cmdletContext.Select(response, this); output = new CmdletOutput { PipelineOutput = pipelineOutput, ServiceResponse = response }; } catch (Exception e) { output = new CmdletOutput { ErrorResponse = e }; } return output; } public ExecutorContext CreateContext() { return new CmdletContext(); } #endregion #region AWS Service Operation Call private Amazon.DynamoDBv2.Model.BatchGetItemResponse CallAWSServiceOperation(IAmazonDynamoDB client, Amazon.DynamoDBv2.Model.BatchGetItemRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon DynamoDB", "BatchGetItem"); try { #if DESKTOP return client.BatchGetItem(request); #elif CORECLR return client.BatchGetItemAsync(request).GetAwaiter().GetResult(); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } } #endregion internal partial class CmdletContext : ExecutorContext { public Dictionary RequestItem { get; set; } public Amazon.DynamoDBv2.ReturnConsumedCapacity ReturnConsumedCapacity { get; set; } public System.Func Select { get; set; } = (response, cmdlet) => response.Responses; } } }