/*******************************************************************************
* 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.XRay;
using Amazon.XRay.Model;
namespace Amazon.PowerShell.Cmdlets.XR
{
///
/// Sets the resource policy to grant one or more Amazon Web Services services and accounts
/// permissions to access X-Ray. Each resource policy will be associated with a specific
/// Amazon Web Services account. Each Amazon Web Services account can have a maximum of
/// 5 resource policies, and each policy name must be unique within that account. The
/// maximum size of each resource policy is 5KB.
///
[Cmdlet("Write", "XRResourcePolicy", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("Amazon.XRay.Model.ResourcePolicy")]
[AWSCmdlet("Calls the AWS X-Ray PutResourcePolicy API operation.", Operation = new[] {"PutResourcePolicy"}, SelectReturnType = typeof(Amazon.XRay.Model.PutResourcePolicyResponse))]
[AWSCmdletOutput("Amazon.XRay.Model.ResourcePolicy or Amazon.XRay.Model.PutResourcePolicyResponse",
"This cmdlet returns an Amazon.XRay.Model.ResourcePolicy object.",
"The service call response (type Amazon.XRay.Model.PutResourcePolicyResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class WriteXRResourcePolicyCmdlet : AmazonXRayClientCmdlet, IExecutor
{
#region Parameter BypassPolicyLockoutCheck
///
///
/// A flag to indicate whether to bypass the resource policy lockout safety check.Setting this value to true increases the risk that the policy becomes unmanageable.
/// Do not set this value to true indiscriminately.Use this parameter only when you include a policy in the request and you intend to
/// prevent the principal that is making the request from making a subsequent PutResourcePolicy
/// request.The default value is false.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? BypassPolicyLockoutCheck { get; set; }
#endregion
#region Parameter PolicyDocument
///
///
/// The resource policy document, which can be up to 5kb in size.
///
///
#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 PolicyDocument { get; set; }
#endregion
#region Parameter PolicyName
///
///
/// The name of the resource policy. Must be unique within a specific Amazon Web Services
/// account.
///
///
#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.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String PolicyName { get; set; }
#endregion
#region Parameter PolicyRevisionId
///
///
/// Specifies a specific policy revision, to ensure an atomic create operation. By default
/// the resource policy is created if it does not exist, or updated with an incremented
/// revision id. The revision id is unique to each policy in the account.If the policy revision id does not match the latest revision id, the operation will
/// fail with an InvalidPolicyRevisionIdException
exception. You can also
/// provide a PolicyRevisionId
of 0. In this case, the operation will fail
/// with an InvalidPolicyRevisionIdException
exception if a resource policy
/// with the same name already exists.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String PolicyRevisionId { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'ResourcePolicy'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.XRay.Model.PutResourcePolicyResponse).
/// Specifying the name of a property of type Amazon.XRay.Model.PutResourcePolicyResponse 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; } = "ResourcePolicy";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the PolicyName parameter.
/// The -PassThru parameter is deprecated, use -Select '^PolicyName' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^PolicyName' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter Force
///
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.PolicyName), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Write-XRResourcePolicy (PutResourcePolicy)"))
{
return;
}
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.PolicyName;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.BypassPolicyLockoutCheck = this.BypassPolicyLockoutCheck;
context.PolicyDocument = this.PolicyDocument;
#if MODULAR
if (this.PolicyDocument == null && ParameterWasBound(nameof(this.PolicyDocument)))
{
WriteWarning("You are passing $null as a value for parameter PolicyDocument 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.PolicyName = this.PolicyName;
#if MODULAR
if (this.PolicyName == null && ParameterWasBound(nameof(this.PolicyName)))
{
WriteWarning("You are passing $null as a value for parameter PolicyName 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.PolicyRevisionId = this.PolicyRevisionId;
// 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.XRay.Model.PutResourcePolicyRequest();
if (cmdletContext.BypassPolicyLockoutCheck != null)
{
request.BypassPolicyLockoutCheck = cmdletContext.BypassPolicyLockoutCheck.Value;
}
if (cmdletContext.PolicyDocument != null)
{
request.PolicyDocument = cmdletContext.PolicyDocument;
}
if (cmdletContext.PolicyName != null)
{
request.PolicyName = cmdletContext.PolicyName;
}
if (cmdletContext.PolicyRevisionId != null)
{
request.PolicyRevisionId = cmdletContext.PolicyRevisionId;
}
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.XRay.Model.PutResourcePolicyResponse CallAWSServiceOperation(IAmazonXRay client, Amazon.XRay.Model.PutResourcePolicyRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS X-Ray", "PutResourcePolicy");
try
{
#if DESKTOP
return client.PutResourcePolicy(request);
#elif CORECLR
return client.PutResourcePolicyAsync(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 System.Boolean? BypassPolicyLockoutCheck { get; set; }
public System.String PolicyDocument { get; set; }
public System.String PolicyName { get; set; }
public System.String PolicyRevisionId { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.ResourcePolicy;
}
}
}