-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: CodeCommit - Currently only in us-east-1 #3274
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89ce6f7
Started the work for the AWS CodeCommit Repository resource
stack72 d9fd77c
Finishing the last of the CodeCommit Repository resource. Also starte…
stack72 2ad006a
Currently, AWS CodeCommit is only available in us-east-1, therefore we
stack72 14604e4
Added the documentation for the CodeCommit repository
stack72 63d7f59
Changing the AWS CodeCommit Connection Initiation to use the new sess…
stack72 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
builtin/providers/aws/resource_aws_codecommit_repository.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/codecommit" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsCodeCommitRepository() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsCodeCommitRepositoryCreate, | ||
Update: resourceAwsCodeCommitRepositoryUpdate, | ||
Read: resourceAwsCodeCommitRepositoryRead, | ||
Delete: resourceAwsCodeCommitRepositoryDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
if len(value) > 100 { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot be longer than 100 characters", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
|
||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
if len(value) > 1000 { | ||
errors = append(errors, fmt.Errorf( | ||
"%q cannot be longer than 1000 characters", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
|
||
"arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"repository_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"clone_url_http": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"clone_url_ssh": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_branch": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsCodeCommitRepositoryCreate(d *schema.ResourceData, meta interface{}) error { | ||
codecommitconn := meta.(*AWSClient).codecommitconn | ||
region := meta.(*AWSClient).region | ||
|
||
// This is a temporary thing - we need to ensure that CodeCommit is only being run against us-east-1 | ||
// As this is the only place that AWS currently supports it | ||
if region != "us-east-1" { | ||
return fmt.Errorf("CodeCommit can only be used with us-east-1. You are trying to use it on %s", region) | ||
} | ||
|
||
input := &codecommit.CreateRepositoryInput{ | ||
RepositoryName: aws.String(d.Get("repository_name").(string)), | ||
RepositoryDescription: aws.String(d.Get("description").(string)), | ||
} | ||
|
||
out, err := codecommitconn.CreateRepository(input) | ||
if err != nil { | ||
return fmt.Errorf("Error creating CodeCommit Repository: %s", err) | ||
} | ||
|
||
d.SetId(d.Get("repository_name").(string)) | ||
d.Set("repository_id", *out.RepositoryMetadata.RepositoryId) | ||
d.Set("arn", *out.RepositoryMetadata.Arn) | ||
d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp) | ||
d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh) | ||
|
||
return resourceAwsCodeCommitRepositoryUpdate(d, meta) | ||
} | ||
|
||
func resourceAwsCodeCommitRepositoryUpdate(d *schema.ResourceData, meta interface{}) error { | ||
codecommitconn := meta.(*AWSClient).codecommitconn | ||
|
||
if d.HasChange("default_branch") { | ||
if err := resourceAwsCodeCommitUpdateDefaultBranch(codecommitconn, d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if d.HasChange("description") { | ||
if err := resourceAwsCodeCommitUpdateDescription(codecommitconn, d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceAwsCodeCommitRepositoryRead(d, meta) | ||
} | ||
|
||
func resourceAwsCodeCommitRepositoryRead(d *schema.ResourceData, meta interface{}) error { | ||
codecommitconn := meta.(*AWSClient).codecommitconn | ||
|
||
input := &codecommit.GetRepositoryInput{ | ||
RepositoryName: aws.String(d.Id()), | ||
} | ||
|
||
out, err := codecommitconn.GetRepository(input) | ||
if err != nil { | ||
return fmt.Errorf("Error reading CodeCommit Repository: %s", err.Error()) | ||
} | ||
|
||
d.Set("repository_id", *out.RepositoryMetadata.RepositoryId) | ||
d.Set("arn", *out.RepositoryMetadata.Arn) | ||
d.Set("clone_url_http", *out.RepositoryMetadata.CloneUrlHttp) | ||
d.Set("clone_url_ssh", *out.RepositoryMetadata.CloneUrlSsh) | ||
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 | ||
} | ||
|
||
func resourceAwsCodeCommitUpdateDescription(codecommitconn *codecommit.CodeCommit, d *schema.ResourceData) error { | ||
branchInput := &codecommit.UpdateRepositoryDescriptionInput{ | ||
RepositoryName: aws.String(d.Id()), | ||
RepositoryDescription: aws.String(d.Get("description").(string)), | ||
} | ||
|
||
_, err := codecommitconn.UpdateRepositoryDescription(branchInput) | ||
if err != nil { | ||
return fmt.Errorf("Error Updating Repository Description for CodeCommit Repository: %s", err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsCodeCommitUpdateDefaultBranch(codecommitconn *codecommit.CodeCommit, d *schema.ResourceData) error { | ||
branchInput := &codecommit.UpdateDefaultBranchInput{ | ||
RepositoryName: aws.String(d.Id()), | ||
DefaultBranchName: aws.String(d.Get("default_branch").(string)), | ||
} | ||
|
||
_, err := codecommitconn.UpdateDefaultBranch(branchInput) | ||
if err != nil { | ||
return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error()) | ||
} | ||
|
||
return nil | ||
} |
109 changes: 109 additions & 0 deletions
109
builtin/providers/aws/resource_aws_codecommit_repository_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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 TestAccAWSCodeCommitRepository_withChanges(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"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_repository.test", "description", "This is a test description"), | ||
), | ||
}, | ||
resource.TestStep{ | ||
Config: testAccCodeCommitRepository_withChanges, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCodeCommitRepositoryExists("aws_codecommit_repository.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_repository.test", "description", "This is a test description - with changes"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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" | ||
} | ||
` | ||
|
||
const testAccCodeCommitRepository_withChanges = ` | ||
resource "aws_codecommit_repository" "test" { | ||
repository_name = "my_test_repository" | ||
description = "This is a test description - with changes" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
website/source/docs/providers/aws/r/code_commit_repository.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_codecommit_repository" | ||
sidebar_current: "docs-aws-resource-codecommit-repository" | ||
description: |- | ||
Provides a CodeCommit Repository Resource. | ||
--- | ||
|
||
# aws\_codecommit\_repository | ||
|
||
Provides a CodeCommit Repository Resource. | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_codecommit_repository" "test" { | ||
repository_name = "MyTestRepository" | ||
description = "This is the Sample App Repository" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. | ||
* `description` - (Optional) The description of the repository. This needs to be less than 1000 characters | ||
* `default_branch` - (Optional) The default branch of the repository. The branch specified here needs to exist. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `repository_id` - The ID of the repository | ||
* `arn` - The ARN of the repository | ||
* `clone_url_http` - The URL to use for cloning the repository over HTTPS. | ||
* `clone_url_ssh` - The URL to use for cloning the repository over SSH. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stylistically we've tended to call the client just
conn
when it's a local variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Unless of course you're juggling two in a given function.)