diff --git a/builtin/providers/aws/resource_aws_codecommit_repository.go b/builtin/providers/aws/resource_aws_codecommit_repository.go index 67d8ea280..f1830864e 100644 --- a/builtin/providers/aws/resource_aws_codecommit_repository.go +++ b/builtin/providers/aws/resource_aws_codecommit_repository.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "log" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/codecommit" @@ -108,7 +109,7 @@ func resourceAwsCodeCommitRepositoryUpdate(d *schema.ResourceData, meta interfac } } - return nil + return resourceAwsCodeCommitRepositoryRead(d, meta) } func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error { @@ -127,12 +128,24 @@ func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{ d.Set("arn", *out.RepositoryMetadata.Arn) d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp) d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh) - d.Set("default_branch", *out.RepositoryMetadata.DefaultBranch) + if out.RepositoryMetadata.DefaultBranch != nil { + d.Set("default_branch", *out.RepositoryMetadata.DefaultBranch) + } return nil } func resourceAwsCodeCommitRepositoryDelete(d *schema.ResourceData, meta interface{}) error { + codecommitconn := meta.(*AWSClient).codecommitconn + + log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id()) + _, err := codecommitconn.DeleteRepository(&codecommit.DeleteRepositoryInput{ + RepositoryName: aws.String(d.Id()), + }) + if err != nil { + return fmt.Errorf("Error deleting CodeCommit Repository: %s", err.Error()) + } + return nil } diff --git a/builtin/providers/aws/resource_aws_codecommit_repository_test.go b/builtin/providers/aws/resource_aws_codecommit_repository_test.go new file mode 100644 index 000000000..7859e0d82 --- /dev/null +++ b/builtin/providers/aws/resource_aws_codecommit_repository_test.go @@ -0,0 +1,76 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSCodeCommitRepository_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCodeCommitRepositoryDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCodeCommitRepository_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"), + ), + }, + }, + }) +} + +func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn + out, err := codecommitconn.GetRepository(&codecommit.GetRepositoryInput{ + RepositoryName: aws.String(rs.Primary.ID), + }) + + if err != nil { + return err + } + + if out.RepositoryMetadata.Arn == nil { + return fmt.Errorf("No CodeCommit Repository Vault Found") + } + + if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID { + return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q", + *out.RepositoryMetadata.RepositoryName, rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error { + if len(s.RootModule().Resources) > 0 { + return fmt.Errorf("Expected all resources to be gone, but found: %#v", + s.RootModule().Resources) + } + + return nil +} + +const testAccCodeCommitRepository_basic = ` +resource "aws_codecommit_repository" "test" { + repository_name = "my_test_repository" + description = "This is a test description" +} +`