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

resource/aws_efs_file_system: Add provisioned_throughput_in_mibps and throughput_mode arguments #5210

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 94 additions & 27 deletions aws/resource_aws_efs_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ func resourceAwsEfsFileSystem() *schema.Resource {
Computed: true,
},

"provisioned_throughput_in_mibps": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this name for some reason.

Type: schema.TypeFloat,
Optional: true,
},

"tags": tagsSchema(),

"throughput_mode": {
Type: schema.TypeString,
Optional: true,
Default: efs.ThroughputModeBursting,
ValidateFunc: validation.StringInSlice([]string{
efs.ThroughputModeBursting,
efs.ThroughputModeProvisioned,
}, false),
},
},
}
}
Expand All @@ -92,15 +107,21 @@ func resourceAwsEfsFileSystemCreate(d *schema.ResourceData, meta interface{}) er
creationToken = resource.UniqueId()
}
}
throughputMode := d.Get("throughput_mode").(string)

createOpts := &efs.CreateFileSystemInput{
CreationToken: aws.String(creationToken),
CreationToken: aws.String(creationToken),
ThroughputMode: aws.String(throughputMode),
}

if v, ok := d.GetOk("performance_mode"); ok {
createOpts.PerformanceMode = aws.String(v.(string))
}

if throughputMode == efs.ThroughputModeProvisioned {
createOpts.ProvisionedThroughputInMibps = aws.Float64(d.Get("provisioned_throughput_in_mibps").(float64))
}

encrypted, hasEncrypted := d.GetOk("encrypted")
kmsKeyId, hasKmsKeyId := d.GetOk("kms_key_id")

Expand All @@ -126,45 +147,69 @@ func resourceAwsEfsFileSystemCreate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] EFS file system ID: %s", d.Id())

stateConf := &resource.StateChangeConf{
Pending: []string{"creating"},
Target: []string{"available"},
Refresh: func() (interface{}, string, error) {
resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
FileSystemId: aws.String(d.Id()),
})
if err != nil {
return nil, "error", err
}

if hasEmptyFileSystems(resp) {
return nil, "not-found", fmt.Errorf("EFS file system %q could not be found.", d.Id())
}

fs := resp.FileSystems[0]
log.Printf("[DEBUG] current status of %q: %q", *fs.FileSystemId, *fs.LifeCycleState)
return fs, *fs.LifeCycleState, nil
},
Pending: []string{efs.LifeCycleStateCreating},
Target: []string{efs.LifeCycleStateAvailable},
Refresh: resourceEfsFileSystemCreateUpdateRefreshFunc(d.Id(), conn),
Timeout: 10 * time.Minute,
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for EFS file system (%q) to create: %s",
d.Id(), err.Error())
return fmt.Errorf("Error waiting for EFS file system (%q) to create: %s", d.Id(), err)
}
log.Printf("[DEBUG] EFS file system %q created.", d.Id())

return resourceAwsEfsFileSystemUpdate(d, meta)
err = setTagsEFS(conn, d)
if err != nil {
return fmt.Errorf("error setting tags for EFS file system (%q): %s", d.Id(), err)
}

return resourceAwsEfsFileSystemRead(d, meta)
}

func resourceAwsEfsFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).efsconn
err := setTagsEFS(conn, d)
if err != nil {
return fmt.Errorf("Error setting EC2 tags for EFS file system (%q): %s",
d.Id(), err.Error())

if d.HasChange("provisioned_throughput_in_mibps") || d.HasChange("throughput_mode") {
throughputMode := d.Get("throughput_mode").(string)

input := &efs.UpdateFileSystemInput{
FileSystemId: aws.String(d.Id()),
ThroughputMode: aws.String(throughputMode),
}

if throughputMode == efs.ThroughputModeProvisioned {
input.ProvisionedThroughputInMibps = aws.Float64(d.Get("provisioned_throughput_in_mibps").(float64))
}

_, err := conn.UpdateFileSystem(input)
if err != nil {
return fmt.Errorf("error updating EFS File System %q: %s", d.Id(), err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{efs.LifeCycleStateUpdating},
Target: []string{efs.LifeCycleStateAvailable},
Refresh: resourceEfsFileSystemCreateUpdateRefreshFunc(d.Id(), conn),
Timeout: 10 * time.Minute,
Delay: 2 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for EFS file system (%q) to update: %s", d.Id(), err)
}
}

if d.HasChange("tags") {
err := setTagsEFS(conn, d)
if err != nil {
return fmt.Errorf("Error setting EC2 tags for EFS file system (%q): %s",
d.Id(), err.Error())
}
}

return resourceAwsEfsFileSystemRead(d, meta)
Expand Down Expand Up @@ -235,9 +280,11 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro
}

d.Set("creation_token", fs.CreationToken)
d.Set("performance_mode", fs.PerformanceMode)
d.Set("encrypted", fs.Encrypted)
d.Set("kms_key_id", fs.KmsKeyId)
d.Set("performance_mode", fs.PerformanceMode)
d.Set("provisioned_throughput_in_mibps", fs.ProvisionedThroughputInMibps)
d.Set("throughput_mode", fs.ThroughputMode)

region := meta.(*AWSClient).region
err = d.Set("dns_name", resourceAwsEfsDnsName(*fs.FileSystemId, region))
Expand Down Expand Up @@ -314,3 +361,23 @@ func hasEmptyFileSystems(fs *efs.DescribeFileSystemsOutput) bool {
func resourceAwsEfsDnsName(fileSystemId, region string) string {
return fmt.Sprintf("%s.efs.%s.amazonaws.com", fileSystemId, region)
}

func resourceEfsFileSystemCreateUpdateRefreshFunc(id string, conn *efs.EFS) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
FileSystemId: aws.String(id),
})
if err != nil {
return nil, "error", err
}

if hasEmptyFileSystems(resp) {
return nil, "not-found", fmt.Errorf("EFS file system %q could not be found.", id)
}

fs := resp.FileSystems[0]
state := aws.StringValue(fs.LifeCycleState)
log.Printf("[DEBUG] current status of %q: %q", id, state)
return fs, state, nil
}
}
89 changes: 89 additions & 0 deletions aws/resource_aws_efs_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func TestAccAWSEFSFileSystem_basic(t *testing.T) {
"aws_efs_file_system.foo",
"performance_mode",
"generalPurpose"),
resource.TestCheckResourceAttr(
"aws_efs_file_system.foo",
"throughput_mode",
efs.ThroughputModeBursting),
testAccCheckEfsFileSystem(
"aws_efs_file_system.foo",
),
Expand Down Expand Up @@ -183,6 +187,74 @@ func TestAccAWSEFSFileSystem_kmsConfigurationWithoutEncryption(t *testing.T) {
})
}

func TestAccAWSEFSFileSystem_ProvisionedThroughputInMibps(t *testing.T) {
resourceName := "aws_efs_file_system.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEfsFileSystemDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(1.0),
Check: resource.ComposeTestCheckFunc(
testAccCheckEfsFileSystem(resourceName),
resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "1"),
resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned),
),
},
{
Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(2.0),
Check: resource.ComposeTestCheckFunc(
testAccCheckEfsFileSystem(resourceName),
resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "2"),
resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"reference_name", "creation_token"},
},
},
})
}

func TestAccAWSEFSFileSystem_ThroughputMode(t *testing.T) {
resourceName := "aws_efs_file_system.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckEfsFileSystemDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(1.0),
Check: resource.ComposeTestCheckFunc(
testAccCheckEfsFileSystem(resourceName),
resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "1"),
resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeProvisioned),
),
},
{
Config: testAccAWSEFSFileSystemConfig_ThroughputMode(efs.ThroughputModeBursting),
Check: resource.ComposeTestCheckFunc(
testAccCheckEfsFileSystem(resourceName),
resource.TestCheckResourceAttr(resourceName, "provisioned_throughput_in_mibps", "0"),
resource.TestCheckResourceAttr(resourceName, "throughput_mode", efs.ThroughputModeBursting),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"reference_name", "creation_token"},
},
},
})
}

func testAccCheckEfsFileSystemDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).efsconn
for _, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -390,3 +462,20 @@ resource "aws_efs_file_system" "foo-with-kms" {
}
`, rInt)
}

func testAccAWSEFSFileSystemConfig_ThroughputMode(throughputMode string) string {
return fmt.Sprintf(`
resource "aws_efs_file_system" "test" {
throughput_mode = %q
}
`, throughputMode)
}

func testAccAWSEFSFileSystemConfig_ProvisionedThroughputInMibps(provisionedThroughputInMibps float64) string {
return fmt.Sprintf(`
resource "aws_efs_file_system" "test" {
provisioned_throughput_in_mibps = %f
throughput_mode = "provisioned"
}
`, provisionedThroughputInMibps)
}
8 changes: 4 additions & 4 deletions website/docs/r/efs_file_system.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ system creation. By default generated by Terraform. See [Elastic File System]
* `reference_name` - **DEPRECATED** (Optional) A reference name used when creating the
`Creation Token` which Amazon EFS uses to ensure idempotent file system creation. By
default generated by Terraform.
* `performance_mode` - (Optional) The file system performance mode. Can be either
`"generalPurpose"` or `"maxIO"` (Default: `"generalPurpose"`).
* `tags` - (Optional) A mapping of tags to assign to the file system.
* `encrypted` - (Optional) If true, the disk will be encrypted.
* `kms_key_id` - (Optional) The ARN for the KMS encryption key. When specifying kms_key_id, encrypted needs to be set to true.
* `performance_mode` - (Optional) The file system performance mode. Can be either `"generalPurpose"` or `"maxIO"` (Default: `"generalPurpose"`).
* `provisioned_throughput_in_mibps` - (Optional) The throughput, measured in MiB/s, that you want to provision for the file system. Only applicable with `throughput_mode` set to `provisioned`.
* `tags` - (Optional) A mapping of tags to assign to the file system.
* `throughput_mode` - (Optional) Throughput mode for the file system. Defaults to `bursting`. Valid values: `bursting`, `provisioned`. When using `provisioned`, also set `provisioned_throughput_in_mibps`.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The ID that identifies the file system (e.g. fs-ccfc0d65).
* `kms_key_id` - The ARN for the KMS encryption key.
* `dns_name` - The DNS name for the filesystem per [documented convention](http://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html).

## Import
Expand Down