Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/storagegateway_gateway - add support for bandwidth values #13568

Merged
merged 9 commits into from
Aug 24, 2020
94 changes: 94 additions & 0 deletions aws/resource_aws_storagegateway_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ func resourceAwsStorageGatewayGateway() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice(storagegateway.SMBSecurityStrategy_Values(), false),
},
"average_download_rate_limit_in_bits_per_sec": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(102400),
},
"average_upload_rate_limit_in_bits_per_sec": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(51200),
},
},
}
}
Expand Down Expand Up @@ -314,6 +324,26 @@ func resourceAwsStorageGatewayGatewayCreate(d *schema.ResourceData, meta interfa
}
}

bandwidthInput := &storagegateway.UpdateBandwidthRateLimitInput{
GatewayARN: aws.String(d.Id()),
}

if v, ok := d.GetOk("average_download_rate_limit_in_bits_per_sec"); ok {
bandwidthInput.AverageDownloadRateLimitInBitsPerSec = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("average_upload_rate_limit_in_bits_per_sec"); ok {
bandwidthInput.AverageUploadRateLimitInBitsPerSec = aws.Int64(int64(v.(int)))
}

if bandwidthInput.AverageDownloadRateLimitInBitsPerSec != nil || bandwidthInput.AverageUploadRateLimitInBitsPerSec != nil {
log.Printf("[DEBUG] Storage Gateway Gateway %q setting Bandwidth Rate Limit: %#v", d.Id(), bandwidthInput)
_, err := conn.UpdateBandwidthRateLimit(bandwidthInput)
if err != nil {
return fmt.Errorf("error setting Bandwidth Rate Limit: %s", err)
}
}

return resourceAwsStorageGatewayGatewayRead(d, meta)
}

Expand Down Expand Up @@ -420,6 +450,20 @@ func resourceAwsStorageGatewayGatewayRead(d *schema.ResourceData, meta interface
d.Set("cloudwatch_log_group_arn", output.CloudWatchLogGroupARN)
d.Set("smb_security_strategy", smbSettingsOutput.SMBSecurityStrategy)

bandwidthInput := &storagegateway.DescribeBandwidthRateLimitInput{
GatewayARN: aws.String(d.Id()),
}

log.Printf("[DEBUG] Reading Storage Gateway Bandwidth rate limit: %s", bandwidthInput)
bandwidthOutput, err := conn.DescribeBandwidthRateLimit(bandwidthInput)
if err != nil && !isAWSErr(err, storagegateway.ErrCodeInvalidGatewayRequestException, "The specified operation is not supported") {
return fmt.Errorf("error reading Storage Gateway Bandwidth rate limit: %s", err)
}
if err == nil {
d.Set("average_download_rate_limit_in_bits_per_sec", aws.Int64Value(bandwidthOutput.AverageDownloadRateLimitInBitsPerSec))
d.Set("average_upload_rate_limit_in_bits_per_sec", aws.Int64Value(bandwidthOutput.AverageUploadRateLimitInBitsPerSec))
}

return nil
}

Expand Down Expand Up @@ -492,6 +536,56 @@ func resourceAwsStorageGatewayGatewayUpdate(d *schema.ResourceData, meta interfa
}
}

if d.HasChanges("average_download_rate_limit_in_bits_per_sec",
"average_upload_rate_limit_in_bits_per_sec") {

deleteInput := &storagegateway.DeleteBandwidthRateLimitInput{
GatewayARN: aws.String(d.Id()),
}
updateInput := &storagegateway.UpdateBandwidthRateLimitInput{
GatewayARN: aws.String(d.Id()),
}
needsDelete := false
needsUpdate := false

if v, ok := d.GetOk("average_download_rate_limit_in_bits_per_sec"); ok {
updateInput.AverageDownloadRateLimitInBitsPerSec = aws.Int64(int64(v.(int)))
needsUpdate = true
} else if d.HasChange("average_download_rate_limit_in_bits_per_sec") {
deleteInput.BandwidthType = aws.String("DOWNLOAD")
needsDelete = true
}

if v, ok := d.GetOk("average_upload_rate_limit_in_bits_per_sec"); ok {
updateInput.AverageUploadRateLimitInBitsPerSec = aws.Int64(int64(v.(int)))
needsUpdate = true
} else if d.HasChange("average_upload_rate_limit_in_bits_per_sec") {
if needsDelete {
deleteInput.BandwidthType = aws.String("ALL")
} else {
deleteInput.BandwidthType = aws.String("UPLOAD")
needsDelete = true
}
}

if needsUpdate {
log.Printf("[DEBUG] Storage Gateway Gateway (%q) updating Bandwidth Rate Limit: %#v", d.Id(), updateInput)
_, err := conn.UpdateBandwidthRateLimit(updateInput)
if err != nil {
return fmt.Errorf("error updating Bandwidth Rate Limit: %w", err)
}
}

if needsDelete {
log.Printf("[DEBUG] Storage Gateway Gateway (%q) unsetting Bandwidth Rate Limit: %#v", d.Id(), deleteInput)
_, err := conn.DeleteBandwidthRateLimit(deleteInput)
if err != nil {
return fmt.Errorf("error unsetting Bandwidth Rate Limit: %w", err)
}
}

}

return resourceAwsStorageGatewayGatewayRead(d, meta)
}

Expand Down
163 changes: 163 additions & 0 deletions aws/resource_aws_storagegateway_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,132 @@ func TestAccAWSStorageGatewayGateway_disappears(t *testing.T) {
})
}

func TestAccAWSStorageGatewayGateway_bandwidthUpload(t *testing.T) {
var gateway storagegateway.DescribeGatewayInformationOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_storagegateway_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSStorageGatewayGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigUpload(rName, 102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "102400"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"activation_key", "gateway_ip_address"},
},
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigUpload(rName, 2*102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "204800"),
),
},
{
Config: testAccAWSStorageGatewayGatewayConfig_GatewayType_Cached(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "0"),
),
},
},
})
}

func TestAccAWSStorageGatewayGateway_bandwidthDownload(t *testing.T) {
var gateway storagegateway.DescribeGatewayInformationOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_storagegateway_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSStorageGatewayGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigDownload(rName, 102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_download_rate_limit_in_bits_per_sec", "102400"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"activation_key", "gateway_ip_address"},
},
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigDownload(rName, 2*102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_download_rate_limit_in_bits_per_sec", "204800"),
),
},
{
Config: testAccAWSStorageGatewayGatewayConfig_GatewayType_Cached(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "0"),
),
},
},
})
}

func TestAccAWSStorageGatewayGateway_bandwidthAll(t *testing.T) {
var gateway storagegateway.DescribeGatewayInformationOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_storagegateway_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSStorageGatewayGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigAll(rName, 102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "102400"),
resource.TestCheckResourceAttr(resourceName, "average_download_rate_limit_in_bits_per_sec", "102400"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"activation_key", "gateway_ip_address"},
},
{
Config: testAccAWSStorageGatewayGatewayBandwidthConfigAll(rName, 2*102400),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "204800"),
resource.TestCheckResourceAttr(resourceName, "average_download_rate_limit_in_bits_per_sec", "204800"),
),
},
{
Config: testAccAWSStorageGatewayGatewayConfig_GatewayType_Cached(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSStorageGatewayGatewayExists(resourceName, &gateway),
resource.TestCheckResourceAttr(resourceName, "average_upload_rate_limit_in_bits_per_sec", "0"),
resource.TestCheckResourceAttr(resourceName, "average_download_rate_limit_in_bits_per_sec", "0"),
),
},
},
})
}

func testAccCheckAWSStorageGatewayGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).storagegatewayconn

Expand Down Expand Up @@ -943,3 +1069,40 @@ resource "aws_storagegateway_gateway" "test" {
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccAWSStorageGatewayGatewayBandwidthConfigUpload(rName string, rate int) string {
return testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName) + fmt.Sprintf(`
resource "aws_storagegateway_gateway" "test" {
gateway_ip_address = aws_instance.test.public_ip
gateway_name = %[1]q
gateway_timezone = "GMT"
gateway_type = "CACHED"
average_upload_rate_limit_in_bits_per_sec = %[2]d
}
`, rName, rate)
}

func testAccAWSStorageGatewayGatewayBandwidthConfigDownload(rName string, rate int) string {
return testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName) + fmt.Sprintf(`
resource "aws_storagegateway_gateway" "test" {
gateway_ip_address = aws_instance.test.public_ip
gateway_name = %[1]q
gateway_timezone = "GMT"
gateway_type = "CACHED"
average_download_rate_limit_in_bits_per_sec = %[2]d
}
`, rName, rate)
}

func testAccAWSStorageGatewayGatewayBandwidthConfigAll(rName string, rate int) string {
return testAccAWSStorageGateway_TapeAndVolumeGatewayBase(rName) + fmt.Sprintf(`
resource "aws_storagegateway_gateway" "test" {
gateway_ip_address = aws_instance.test.public_ip
gateway_name = %[1]q
gateway_timezone = "GMT"
gateway_type = "CACHED"
average_upload_rate_limit_in_bits_per_sec = %[2]d
average_download_rate_limit_in_bits_per_sec = %[2]d
}
`, rName, rate)
}
2 changes: 2 additions & 0 deletions website/docs/r/storagegateway_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ The following arguments are supported:
* `gateway_name` - (Required) Name of the gateway.
* `gateway_timezone` - (Required) Time zone for the gateway. The time zone is of the format "GMT", "GMT-hr:mm", or "GMT+hr:mm". For example, `GMT-4:00` indicates the time is 4 hours behind GMT. The time zone is used, for example, for scheduling snapshots and your gateway's maintenance schedule.
* `activation_key` - (Optional) Gateway activation key during resource creation. Conflicts with `gateway_ip_address`. Additional information is available in the [Storage Gateway User Guide](https://docs.aws.amazon.com/storagegateway/latest/userguide/get-activation-key.html).
* `average_download_rate_limit_in_bits_per_sec` - (Optional) The average download bandwidth rate limit in bits per second. This is supported for the `CACHED`, `STORED`, and `VTL` gateway types.
* `average_upload_rate_limit_in_bits_per_sec` - (Optional) The average upload bandwidth rate limit in bits per second. This is supported for the `CACHED`, `STORED`, and `VTL` gateway types.
* `gateway_ip_address` - (Optional) Gateway IP address to retrieve activation key during resource creation. Conflicts with `activation_key`. Gateway must be accessible on port 80 from where Terraform is running. Additional information is available in the [Storage Gateway User Guide](https://docs.aws.amazon.com/storagegateway/latest/userguide/get-activation-key.html).
* `gateway_type` - (Optional) Type of the gateway. The default value is `STORED`. Valid values: `CACHED`, `FILE_S3`, `STORED`, `VTL`.
* `gateway_vpc_endpoint` - (Optional) VPC endpoint address to be used when activating your gateway. This should be used when your instance is in a private subnet. Requires HTTP access from client computer running terraform. More info on what ports are required by your VPC Endpoint Security group in [Activating a Gateway in a Virtual Private Cloud](https://docs.aws.amazon.com/storagegateway/latest/userguide/gateway-private-link.html).
Expand Down