/*******************************************************************************
* 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.S3Control;
using Amazon.S3Control.Model;
namespace Amazon.PowerShell.Cmdlets.S3C
{
///
///
/// This action creates an Amazon S3 on Outposts bucket. To create an S3 bucket, see Create
/// Bucket in the Amazon S3 API Reference.
///
/// Creates a new Outposts bucket. By creating the bucket, you become the bucket owner.
/// To create an Outposts bucket, you must have S3 on Outposts. For more information,
/// see Using
/// Amazon S3 on Outposts in Amazon S3 User Guide.
///
/// Not every string is an acceptable bucket name. For information on bucket naming restrictions,
/// see Working
/// with Amazon S3 Buckets.
///
/// S3 on Outposts buckets support:
/// -
/// Tags
///
-
/// LifecycleConfigurations for deleting expired objects
///
/// For a complete list of restrictions and Amazon S3 feature limitations on S3 on Outposts,
/// see
/// Amazon S3 on Outposts Restrictions and Limitations.
///
/// For an example of the request syntax for Amazon S3 on Outposts that uses the S3 on
/// Outposts endpoint hostname prefix and x-amz-outpost-id
in your API request,
/// see the Examples
/// section.
///
/// The following actions are related to CreateBucket
for Amazon S3 on Outposts:
///
///
[Cmdlet("New", "S3CBucket", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("Amazon.S3Control.Model.CreateBucketResponse")]
[AWSCmdlet("Calls the Amazon S3 Control CreateBucket API operation.", Operation = new[] {"CreateBucket"}, SelectReturnType = typeof(Amazon.S3Control.Model.CreateBucketResponse))]
[AWSCmdletOutput("Amazon.S3Control.Model.CreateBucketResponse",
"This cmdlet returns an Amazon.S3Control.Model.CreateBucketResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class NewS3CBucketCmdlet : AmazonS3ControlClientCmdlet, IExecutor
{
#region Parameter ACL
///
///
/// The canned ACL to apply to the bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.S3Control.BucketCannedACL")]
public Amazon.S3Control.BucketCannedACL ACL { get; set; }
#endregion
#region Parameter Bucket
///
///
/// The name of the bucket.
///
///
#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 Bucket { get; set; }
#endregion
#region Parameter GrantFullControl
///
///
/// Allows grantee the read, write, read ACP, and write ACP permissions on the bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String GrantFullControl { get; set; }
#endregion
#region Parameter GrantRead
///
///
/// Allows grantee to list the objects in the bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String GrantRead { get; set; }
#endregion
#region Parameter GrantReadACP
///
///
/// Allows grantee to read the bucket ACL.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String GrantReadACP { get; set; }
#endregion
#region Parameter GrantWrite
///
///
/// Allows grantee to create, overwrite, and delete any object in the bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String GrantWrite { get; set; }
#endregion
#region Parameter GrantWriteACP
///
///
/// Allows grantee to write the ACL for the applicable bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String GrantWriteACP { get; set; }
#endregion
#region Parameter CreateBucketConfiguration_LocationConstraint
///
///
/// Specifies the Region where the bucket will be created. If you are creating a bucket
/// on the US East (N. Virginia) Region (us-east-1), you do not need to specify the location.
/// This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.S3Control.BucketLocationConstraint")]
public Amazon.S3Control.BucketLocationConstraint CreateBucketConfiguration_LocationConstraint { get; set; }
#endregion
#region Parameter ObjectLockEnabledForBucket
///
///
/// Specifies whether you want S3 Object Lock to be enabled for the new bucket.This is not supported by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.Boolean? ObjectLockEnabledForBucket { get; set; }
#endregion
#region Parameter OutpostId
///
///
/// The ID of the Outposts where the bucket is being created.This ID is required by Amazon S3 on Outposts buckets.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String OutpostId { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is '*'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.S3Control.Model.CreateBucketResponse).
/// Specifying the name of a property of type Amazon.S3Control.Model.CreateBucketResponse 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; } = "*";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the Bucket parameter.
/// The -PassThru parameter is deprecated, use -Select '^Bucket' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^Bucket' 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 = "s3v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.OutpostId), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "New-S3CBucket (CreateBucket)"))
{
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.Bucket;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ACL = this.ACL;
context.Bucket = this.Bucket;
#if MODULAR
if (this.Bucket == null && ParameterWasBound(nameof(this.Bucket)))
{
WriteWarning("You are passing $null as a value for parameter Bucket 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.CreateBucketConfiguration_LocationConstraint = this.CreateBucketConfiguration_LocationConstraint;
context.GrantFullControl = this.GrantFullControl;
context.GrantRead = this.GrantRead;
context.GrantReadACP = this.GrantReadACP;
context.GrantWrite = this.GrantWrite;
context.GrantWriteACP = this.GrantWriteACP;
context.ObjectLockEnabledForBucket = this.ObjectLockEnabledForBucket;
context.OutpostId = this.OutpostId;
// 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.S3Control.Model.CreateBucketRequest();
if (cmdletContext.ACL != null)
{
request.ACL = cmdletContext.ACL;
}
if (cmdletContext.Bucket != null)
{
request.Bucket = cmdletContext.Bucket;
}
// populate CreateBucketConfiguration
var requestCreateBucketConfigurationIsNull = true;
request.CreateBucketConfiguration = new Amazon.S3Control.Model.CreateBucketConfiguration();
Amazon.S3Control.BucketLocationConstraint requestCreateBucketConfiguration_createBucketConfiguration_LocationConstraint = null;
if (cmdletContext.CreateBucketConfiguration_LocationConstraint != null)
{
requestCreateBucketConfiguration_createBucketConfiguration_LocationConstraint = cmdletContext.CreateBucketConfiguration_LocationConstraint;
}
if (requestCreateBucketConfiguration_createBucketConfiguration_LocationConstraint != null)
{
request.CreateBucketConfiguration.LocationConstraint = requestCreateBucketConfiguration_createBucketConfiguration_LocationConstraint;
requestCreateBucketConfigurationIsNull = false;
}
// determine if request.CreateBucketConfiguration should be set to null
if (requestCreateBucketConfigurationIsNull)
{
request.CreateBucketConfiguration = null;
}
if (cmdletContext.GrantFullControl != null)
{
request.GrantFullControl = cmdletContext.GrantFullControl;
}
if (cmdletContext.GrantRead != null)
{
request.GrantRead = cmdletContext.GrantRead;
}
if (cmdletContext.GrantReadACP != null)
{
request.GrantReadACP = cmdletContext.GrantReadACP;
}
if (cmdletContext.GrantWrite != null)
{
request.GrantWrite = cmdletContext.GrantWrite;
}
if (cmdletContext.GrantWriteACP != null)
{
request.GrantWriteACP = cmdletContext.GrantWriteACP;
}
if (cmdletContext.ObjectLockEnabledForBucket != null)
{
request.ObjectLockEnabledForBucket = cmdletContext.ObjectLockEnabledForBucket.Value;
}
if (cmdletContext.OutpostId != null)
{
request.OutpostId = cmdletContext.OutpostId;
}
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.S3Control.Model.CreateBucketResponse CallAWSServiceOperation(IAmazonS3Control client, Amazon.S3Control.Model.CreateBucketRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon S3 Control", "CreateBucket");
try
{
#if DESKTOP
return client.CreateBucket(request);
#elif CORECLR
return client.CreateBucketAsync(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 Amazon.S3Control.BucketCannedACL ACL { get; set; }
public System.String Bucket { get; set; }
public Amazon.S3Control.BucketLocationConstraint CreateBucketConfiguration_LocationConstraint { get; set; }
public System.String GrantFullControl { get; set; }
public System.String GrantRead { get; set; }
public System.String GrantReadACP { get; set; }
public System.String GrantWrite { get; set; }
public System.String GrantWriteACP { get; set; }
public System.Boolean? ObjectLockEnabledForBucket { get; set; }
public System.String OutpostId { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}