/*******************************************************************************
* 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 GetItem
operation returns a set of attributes for the item with the
/// given primary key. If there is no matching item, GetItem
does not return
/// any data and there will be no Item
element in the response.
///
///
/// GetItem
provides an eventually consistent read by default. If your application
/// requires a strongly consistent read, set ConsistentRead
to true
.
/// Although a strongly consistent read might take more time than an eventually consistent
/// read, it always returns the last updated value.
///
///
[Cmdlet("Get", "DDBItem")]
[OutputType("System.String")]
[AWSCmdlet("Calls the Amazon DynamoDB GetItem API operation.", Operation = new[] {"GetItem"}, SelectReturnType = typeof(Amazon.DynamoDBv2.Model.GetItemResponse))]
[AWSCmdletOutput("System.String or Amazon.DynamoDBv2.Model.GetItemResponse",
"This cmdlet returns a collection of System.String objects.",
"The service call response (type Amazon.DynamoDBv2.Model.GetItemResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class GetDDBItemCmdlet : AmazonDynamoDBClientCmdlet, IExecutor
{
#region Parameter AttributesToGet
///
///
/// This is a legacy parameter. Use ProjectionExpression
instead. For more
/// information, see AttributesToGet
/// in the Amazon DynamoDB Developer Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String[] AttributesToGet { get; set; }
#endregion
#region Parameter ConsistentRead
///
///
/// Determines the read consistency model: If set to true
, then the operation
/// uses strongly consistent reads; otherwise, the operation uses eventually consistent
/// reads.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? ConsistentRead { get; set; }
#endregion
#region Parameter ExpressionAttributeName
///
///
/// One or more substitution tokens for attribute names in an expression. The following
/// are some use cases for using ExpressionAttributeNames
:- To access an attribute whose name conflicts with a DynamoDB reserved word.
- To create a placeholder for repeating occurrences of an attribute name in an expression.
- To prevent special characters in an attribute name from being misinterpreted in an
/// expression.
Use the # character in an expression to dereference an attribute name. For
/// example, consider the following attribute name:The name of this attribute conflicts with a reserved word, so it cannot be used directly
/// in an expression. (For the complete list of reserved words, see Reserved
/// Words in the Amazon DynamoDB Developer Guide). To work around this, you
/// could specify the following for ExpressionAttributeNames
:You could then use this substitution in an expression, as in this example:Tokens that begin with the : character are expression attribute values,
/// which are placeholders for the actual value at runtime.For more information on expression attribute names, see Specifying
/// Item Attributes in the Amazon DynamoDB Developer Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("ExpressionAttributeNames")]
public System.Collections.Hashtable ExpressionAttributeName { get; set; }
#endregion
#region Parameter Key
///
///
/// A map of attribute names to AttributeValue
objects, representing the
/// primary key of the item to retrieve.For the primary key, you must provide all of the attributes. For example, with a simple
/// primary key, you only need to provide a value for the partition key. For a composite
/// primary key, you must provide values for both the partition key and the sort key.
///
///
#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]
public System.Collections.Hashtable Key { get; set; }
#endregion
#region Parameter ProjectionExpression
///
///
/// A string that identifies one or more attributes to retrieve from the table. These
/// attributes can include scalars, sets, or elements of a JSON document. The attributes
/// in the expression must be separated by commas.If no attribute names are specified, then all attributes are returned. If any of the
/// requested attributes are not found, they do not appear in the result.For more information, see Specifying
/// Item Attributes in the Amazon DynamoDB Developer Guide.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ProjectionExpression { 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 TableName
///
///
/// The name of the table containing the requested item.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String TableName { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'Item'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.DynamoDBv2.Model.GetItemResponse).
/// Specifying the name of a property of type Amazon.DynamoDBv2.Model.GetItemResponse 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; } = "Item";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the Key parameter.
/// The -PassThru parameter is deprecated, use -Select '^Key' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^Key' 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.Key;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (this.AttributesToGet != null)
{
context.AttributesToGet = new List(this.AttributesToGet);
}
context.ConsistentRead = this.ConsistentRead;
if (this.ExpressionAttributeName != null)
{
context.ExpressionAttributeName = new Dictionary(StringComparer.Ordinal);
foreach (var hashKey in this.ExpressionAttributeName.Keys)
{
context.ExpressionAttributeName.Add((String)hashKey, (String)(this.ExpressionAttributeName[hashKey]));
}
}
if (this.Key != null)
{
context.Key = new Dictionary(StringComparer.Ordinal);
foreach (var hashKey in this.Key.Keys)
{
context.Key.Add((String)hashKey, (AttributeValue)(this.Key[hashKey]));
}
}
#if MODULAR
if (this.Key == null && ParameterWasBound(nameof(this.Key)))
{
WriteWarning("You are passing $null as a value for parameter Key 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.ProjectionExpression = this.ProjectionExpression;
context.ReturnConsumedCapacity = this.ReturnConsumedCapacity;
context.TableName = this.TableName;
#if MODULAR
if (this.TableName == null && ParameterWasBound(nameof(this.TableName)))
{
WriteWarning("You are passing $null as a value for parameter TableName 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
// 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.GetItemRequest();
if (cmdletContext.AttributesToGet != null)
{
request.AttributesToGet = cmdletContext.AttributesToGet;
}
if (cmdletContext.ConsistentRead != null)
{
request.ConsistentRead = cmdletContext.ConsistentRead.Value;
}
if (cmdletContext.ExpressionAttributeName != null)
{
request.ExpressionAttributeNames = cmdletContext.ExpressionAttributeName;
}
if (cmdletContext.Key != null)
{
request.Key = cmdletContext.Key;
}
if (cmdletContext.ProjectionExpression != null)
{
request.ProjectionExpression = cmdletContext.ProjectionExpression;
}
if (cmdletContext.ReturnConsumedCapacity != null)
{
request.ReturnConsumedCapacity = cmdletContext.ReturnConsumedCapacity;
}
if (cmdletContext.TableName != null)
{
request.TableName = cmdletContext.TableName;
}
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.GetItemResponse CallAWSServiceOperation(IAmazonDynamoDB client, Amazon.DynamoDBv2.Model.GetItemRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon DynamoDB", "GetItem");
try
{
#if DESKTOP
return client.GetItem(request);
#elif CORECLR
return client.GetItemAsync(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 List AttributesToGet { get; set; }
public System.Boolean? ConsistentRead { get; set; }
public Dictionary ExpressionAttributeName { get; set; }
public Dictionary Key { get; set; }
public System.String ProjectionExpression { get; set; }
public Amazon.DynamoDBv2.ReturnConsumedCapacity ReturnConsumedCapacity { get; set; }
public System.String TableName { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.Item;
}
}
}