From f6ac200acae872d4a0ff243a5ed947638f7468a9 Mon Sep 17 00:00:00 2001 From: Clint Date: Wed, 8 Mar 2017 09:29:54 -0600 Subject: [PATCH] provider/aws: Rename 'timeout' to 'build_timeout' for Codebuild (#12503) --- .../aws/resource_aws_codebuild_project.go | 14 +++-- .../resource_aws_codebuild_project_migrate.go | 36 +++++++++++++ ...urce_aws_codebuild_project_migrate_test.go | 53 +++++++++++++++++++ .../resource_aws_codebuild_project_test.go | 4 +- .../aws/r/codebuild_project.html.markdown | 4 +- 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_codebuild_project_migrate.go create mode 100644 builtin/providers/aws/resource_aws_codebuild_project_migrate_test.go diff --git a/builtin/providers/aws/resource_aws_codebuild_project.go b/builtin/providers/aws/resource_aws_codebuild_project.go index b655f6b9c..8639e3c3e 100644 --- a/builtin/providers/aws/resource_aws_codebuild_project.go +++ b/builtin/providers/aws/resource_aws_codebuild_project.go @@ -164,6 +164,12 @@ func resourceAwsCodeBuildProject() *schema.Resource { Type: schema.TypeInt, Optional: true, ValidateFunc: validateAwsCodeBuildTimeout, + Removed: "This field has been removed. Please use build_timeout instead", + }, + "build_timeout": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validateAwsCodeBuildTimeout, }, "tags": tagsSchema(), }, @@ -196,7 +202,7 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{}) params.ServiceRole = aws.String(v.(string)) } - if v, ok := d.GetOk("timeout"); ok { + if v, ok := d.GetOk("build_timeout"); ok { params.TimeoutInMinutes = aws.Int64(int64(v.(int))) } @@ -373,7 +379,7 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e d.Set("encryption_key", project.EncryptionKey) d.Set("name", project.Name) d.Set("service_role", project.ServiceRole) - d.Set("timeout", project.TimeoutInMinutes) + d.Set("build_timeout", project.TimeoutInMinutes) if err := d.Set("tags", tagsToMapCodeBuild(project.Tags)); err != nil { return err @@ -416,8 +422,8 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{}) params.ServiceRole = aws.String(d.Get("service_role").(string)) } - if d.HasChange("timeout") { - params.TimeoutInMinutes = aws.Int64(int64(d.Get("timeout").(int))) + if d.HasChange("build_timeout") { + params.TimeoutInMinutes = aws.Int64(int64(d.Get("build_timeout").(int))) } // The documentation clearly says "The replacement set of tags for this build project." diff --git a/builtin/providers/aws/resource_aws_codebuild_project_migrate.go b/builtin/providers/aws/resource_aws_codebuild_project_migrate.go new file mode 100644 index 000000000..97d7a9ff2 --- /dev/null +++ b/builtin/providers/aws/resource_aws_codebuild_project_migrate.go @@ -0,0 +1,36 @@ +package aws + +import ( + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform/terraform" +) + +func resourceAwsCodebuildMigrateState( + v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { + switch v { + case 0: + log.Println("[INFO] Found AWS Codebuild State v0; migrating to v1") + return migrateCodebuildStateV0toV1(is) + default: + return is, fmt.Errorf("Unexpected schema version: %d", v) + } +} + +func migrateCodebuildStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { + if is.Empty() { + log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") + return is, nil + } + + log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) + + if is.Attributes["timeout"] != "" { + is.Attributes["build_timeout"] = strings.TrimSpace(is.Attributes["timeout"]) + } + + log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) + return is, nil +} diff --git a/builtin/providers/aws/resource_aws_codebuild_project_migrate_test.go b/builtin/providers/aws/resource_aws_codebuild_project_migrate_test.go new file mode 100644 index 000000000..2ae6b4e53 --- /dev/null +++ b/builtin/providers/aws/resource_aws_codebuild_project_migrate_test.go @@ -0,0 +1,53 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/terraform" +) + +func TestAWSCodebuildMigrateState(t *testing.T) { + cases := map[string]struct { + StateVersion int + ID string + Attributes map[string]string + Expected string + Meta interface{} + }{ + "v0_1": { + StateVersion: 0, + ID: "tf-testing-file", + Attributes: map[string]string{ + "description": "some description", + "timeout": "5", + }, + Expected: "5", + }, + "v0_2": { + StateVersion: 0, + ID: "tf-testing-file", + Attributes: map[string]string{ + "description": "some description", + "build_timeout": "5", + }, + Expected: "5", + }, + } + + for tn, tc := range cases { + is := &terraform.InstanceState{ + ID: tc.ID, + Attributes: tc.Attributes, + } + is, err := resourceAwsCodebuildMigrateState( + tc.StateVersion, is, tc.Meta) + + if err != nil { + t.Fatalf("bad: %s, err: %#v", tn, err) + } + + if is.Attributes["build_timeout"] != tc.Expected { + t.Fatalf("Bad build_timeout migration: %s\n\n expected: %s", is.Attributes["build_timeout"], tc.Expected) + } + } +} diff --git a/builtin/providers/aws/resource_aws_codebuild_project_test.go b/builtin/providers/aws/resource_aws_codebuild_project_test.go index 2657a8c34..fbd9f9cfb 100644 --- a/builtin/providers/aws/resource_aws_codebuild_project_test.go +++ b/builtin/providers/aws/resource_aws_codebuild_project_test.go @@ -342,7 +342,7 @@ resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { resource "aws_codebuild_project" "foo" { name = "test-project-%s" description = "test_codebuild_project" - timeout = "5" + build_timeout = "5" service_role = "${aws_iam_role.codebuild_role.arn}" artifacts { @@ -429,7 +429,7 @@ resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { resource "aws_codebuild_project" "foo" { name = "test-project-%s" description = "test_codebuild_project" - timeout = "5" + build_timeout = "5" service_role = "${aws_iam_role.codebuild_role.arn}" artifacts { diff --git a/website/source/docs/providers/aws/r/codebuild_project.html.markdown b/website/source/docs/providers/aws/r/codebuild_project.html.markdown index 333b5f7ad..b469815b6 100644 --- a/website/source/docs/providers/aws/r/codebuild_project.html.markdown +++ b/website/source/docs/providers/aws/r/codebuild_project.html.markdown @@ -66,7 +66,7 @@ resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { resource "aws_codebuild_project" "foo" { name = "test-project" description = "test_codebuild_project" - timeout = "5" + build_timeout = "5" service_role = "${aws_iam_role.codebuild_role.arn}" artifacts { @@ -108,7 +108,7 @@ The following arguments are supported: * `description` - (Optional) A short description of the project. * `encryption_key` - (Optional) The AWS Key Management Service (AWS KMS) customer master key (CMK) to be used for encrypting the build project's build output artifacts. * `service_role` - (Optional) The Amazon Resource Name (ARN) of the AWS Identity and Access Management (IAM) role that enables AWS CodeBuild to interact with dependent AWS services on behalf of the AWS account. -* `timeout` - (Optional) How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed. The default is 60 minutes. +* `build_timeout` - (Optional) How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed. The default is 60 minutes. * `tags` - (Optional) A mapping of tags to assign to the resource. * `artifacts` - (Required) Information about the project's build output artifacts. Artifact blocks are documented below. * `environment` - (Required) Information about the project's build environment. Environment blocks are documented below.