diff --git a/aws/resource_aws_opsworks_application.go b/aws/resource_aws_opsworks_application.go index d1dad14242f..1e5ba805801 100644 --- a/aws/resource_aws_opsworks_application.go +++ b/aws/resource_aws_opsworks_application.go @@ -444,12 +444,16 @@ func resourceAwsOpsworksSetApplicationSource(d *schema.ResourceData, v *opsworks if v.Username != nil { m["username"] = *v.Username } - if v.Password != nil { - m["password"] = *v.Password - } if v.Revision != nil { m["revision"] = *v.Revision } + + // v.Password and v.SshKey will, on read, contain the placeholder string + // "*****FILTERED*****", so we ignore it on read and let persist + // the value already in the state. + m["password"] = d.Get("app_source.0.password").(string) + m["ssh_key"] = d.Get("app_source.0.ssh_key").(string) + nv = append(nv, m) } diff --git a/aws/resource_aws_opsworks_stack.go b/aws/resource_aws_opsworks_stack.go index afa406828e7..177a4a93453 100644 --- a/aws/resource_aws_opsworks_stack.go +++ b/aws/resource_aws_opsworks_stack.go @@ -250,9 +250,13 @@ func resourceAwsOpsworksSetStackCustomCookbooksSource(d *schema.ResourceData, v if v.Revision != nil { m["revision"] = *v.Revision } - // v.Password will, on read, contain the placeholder string + + // v.Password and v.SshKey will, on read, contain the placeholder string // "*****FILTERED*****", so we ignore it on read and let persist // the value already in the state. + m["password"] = d.Get("custom_cookbooks_source.0.password").(string) + m["ssh_key"] = d.Get("custom_cookbooks_source.0.ssh_key").(string) + nv = append(nv, m) } diff --git a/aws/resource_aws_opsworks_stack_test.go b/aws/resource_aws_opsworks_stack_test.go index fdcb7e7a54e..4b22e44114a 100644 --- a/aws/resource_aws_opsworks_stack_test.go +++ b/aws/resource_aws_opsworks_stack_test.go @@ -147,6 +147,37 @@ func TestAccAWSOpsworksStack_noVpcCreateTags(t *testing.T) { }) } +///////////////////////////// +// Tests for Custom Cookbooks +///////////////////////////// + +func TestAccAWSOpsworksStack_CustomCookbooks_SetPrivateProperties(t *testing.T) { + stackName := fmt.Sprintf("tf-opsworks-acc-%d", acctest.RandInt()) + var opsstack opsworks.Stack + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsOpsworksStackDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSOpsworksStackConfig_CustomCookbooks_Set(stackName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSOpsworksStackExists("aws_opsworks_stack.tf-acc", true, &opsstack), + testAccCheckAWSOpsworksCreateStackAttributesWithCookbooks(&opsstack, "us-west-2a", stackName), + resource.TestCheckResourceAttr( + "aws_opsworks_stack.tf-acc", + "custom_cookbooks_source.0.password", + "password"), + resource.TestCheckResourceAttr( + "aws_opsworks_stack.tf-acc", + "custom_cookbooks_source.0.ssh_key", + sshKey), + ), + }, + }, + }) +} + // Tests the addition of regional endpoints and supporting the classic link used // to create Stack's prior to v0.9.0. // See https://github.com/hashicorp/terraform/issues/12842 @@ -550,6 +581,61 @@ func testAccCheckAWSOpsworksCreateStackAttributes( } } +func testAccCheckAWSOpsworksCreateStackAttributesWithCookbooks( + opsstack *opsworks.Stack, zone, stackName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if *opsstack.Name != stackName { + return fmt.Errorf("Unnexpected stackName: %s", *opsstack.Name) + } + + if *opsstack.DefaultAvailabilityZone != zone { + return fmt.Errorf("Unnexpected DefaultAvailabilityZone: %s", *opsstack.DefaultAvailabilityZone) + } + + if *opsstack.DefaultOs != "Amazon Linux 2016.09" { + return fmt.Errorf("Unnexpected defaultOs: %s", *opsstack.DefaultOs) + } + + if *opsstack.DefaultRootDeviceType != "ebs" { + return fmt.Errorf("Unnexpected DefaultRootDeviceType: %s", *opsstack.DefaultRootDeviceType) + } + + if *opsstack.CustomJson != `{"key": "value"}` { + return fmt.Errorf("Unnexpected CustomJson: %s", *opsstack.CustomJson) + } + + if *opsstack.ConfigurationManager.Version != "11.10" { + return fmt.Errorf("Unnexpected Version: %s", *opsstack.ConfigurationManager.Version) + } + + if *opsstack.UseOpsworksSecurityGroups { + return fmt.Errorf("Unnexpected UseOpsworksSecurityGroups: %t", *opsstack.UseOpsworksSecurityGroups) + } + + if !*opsstack.UseCustomCookbooks { + return fmt.Errorf("Unnexpected UseCustomCookbooks: %t", *opsstack.UseCustomCookbooks) + } + + if *opsstack.CustomCookbooksSource.Type != "git" { + return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Type) + } + + if *opsstack.CustomCookbooksSource.Revision != "master" { + return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Revision) + } + + if *opsstack.CustomCookbooksSource.Url != "https://github.com/aws/opsworks-example-cookbooks.git" { + return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Url) + } + + if *opsstack.CustomCookbooksSource.Username != "username" { + return fmt.Errorf("Unnexpected *opsstack.CustomCookbooksSource.Type: %s", *opsstack.CustomCookbooksSource.Username) + } + + return nil + } +} + func testAccCheckAWSOpsworksUpdateStackAttributes( opsstack *opsworks.Stack, zone, stackName string) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -1250,3 +1336,151 @@ resource "aws_iam_instance_profile" "opsworks_instance" { } `, name, name, name, name, name) } + +///////////////////////////////////////// +// Helpers for Custom Cookbook properties +///////////////////////////////////////// + +func testAccAWSOpsworksStackConfig_CustomCookbooks_Set(name string) string { + return fmt.Sprintf(` +resource "aws_vpc" "tf-acc" { + cidr_block = "10.3.5.0/24" + + tags = { + Name = "terraform-testacc-opsworks-stack-vpc-update" + } +} + +resource "aws_subnet" "tf-acc" { + vpc_id = "${aws_vpc.tf-acc.id}" + cidr_block = "${aws_vpc.tf-acc.cidr_block}" + availability_zone = "us-west-2a" + + tags = { + Name = "tf-acc-opsworks-stack-vpc-update" + } +} + +resource "aws_opsworks_stack" "tf-acc" { + name = "%s" + region = "us-west-2" + vpc_id = "${aws_vpc.tf-acc.id}" + default_subnet_id = "${aws_subnet.tf-acc.id}" + service_role_arn = "${aws_iam_role.opsworks_service.arn}" + default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}" + default_os = "Amazon Linux 2016.09" + default_root_device_type = "ebs" + custom_json = "{\"key\": \"value\"}" + configuration_manager_version = "11.10" + use_opsworks_security_groups = false + use_custom_cookbooks = true + manage_berkshelf = true + + custom_cookbooks_source { + type = "git" + revision = "master" + url = "https://github.com/aws/opsworks-example-cookbooks.git" + username = "username" + password = "password" + ssh_key = "%s" + } +} + +resource "aws_iam_role" "opsworks_service" { + name = "%s_opsworks_service" + + assume_role_policy = <