-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add Support for Google source repos #256
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4071e85
govendor add google.golang.org/api/sourcerepo/v1
alex1x 4a7aaf6
Add Source Repos resource, add tests
alex1x 54cb11c
Add Source Repos website documentation
alex1x 8249c81
Add fixes suggested by rosbo
alex1x d12a694
Fix acceptance test by using new resource name.
rosbo 00d4401
Add fix suggested by danawillow
alex1x 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
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,107 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"google.golang.org/api/sourcerepo/v1" | ||
) | ||
|
||
func resourceSourceRepoRepository() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceSourceRepoRepositoryCreate, | ||
Read: resourceSourceRepoRepositoryRead, | ||
Delete: resourceSourceRepoRepositoryDelete, | ||
//Update: not supported, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"size": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSourceRepoRepositoryCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repoName := d.Get("name").(string) | ||
name := buildRepositoryName(project, repoName) | ||
|
||
repo := &sourcerepo.Repo{ | ||
Name: name, | ||
} | ||
|
||
parent := "projects/" + project | ||
|
||
op, err := config.clientSourceRepo.Projects.Repos.Create(parent, repo).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error creating the Source Repo: %s", err) | ||
} | ||
d.SetId(op.Name) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSourceRepoRepositoryRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repoName := d.Get("name").(string) | ||
name := buildRepositoryName(project, repoName) | ||
|
||
repo, err := config.clientSourceRepo.Projects.Repos.Get(name).Do() | ||
|
||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Source Repo %q", d.Id())) | ||
} | ||
|
||
d.Set("size", repo.Size) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSourceRepoRepositoryDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repoName := d.Get("name").(string) | ||
name := buildRepositoryName(project, repoName) | ||
|
||
_, err = config.clientSourceRepo.Projects.Repos.Delete(name).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting the Source Repo: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildRepositoryName(project, name string) string { | ||
repositoryName := "projects/" + project + "/repos/" + name | ||
return repositoryName | ||
} |
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,81 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccSourceRepoRepository_basic(t *testing.T) { | ||
repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(10)) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckSourceRepoRepositoryDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccSourceRepoRepository_basic(repositoryName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSourceRepoRepositoryExists( | ||
"google_sourcerepo_repository.acceptance", repositoryName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckSourceRepoRepositoryDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type == "google_sourcerepo_repository" { | ||
repositoryName := buildRepositoryName(config.Project, rs.Primary.Attributes["name"]) | ||
|
||
_, err := config.clientSourceRepo.Projects.Repos.Get(repositoryName).Do() | ||
if err == nil { | ||
return fmt.Errorf(repositoryName + "Source Repository still exists") | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckSourceRepoRepositoryExists(resourceType, resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceType] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
repositoryName := buildRepositoryName(config.Project, resourceName) | ||
|
||
resp, err := config.clientSourceRepo.Projects.Repos.Get(repositoryName).Do() | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error confirming Source Repository existence: %#v", err) | ||
} | ||
|
||
if resp.Name != repositoryName { | ||
return fmt.Errorf("Failed to verify Source Repository by name") | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccSourceRepoRepository_basic(repositoryName string) string { | ||
return fmt.Sprintf(` | ||
resource "google_sourcerepo_repository" "acceptance" { | ||
name = "%s" | ||
} | ||
`, repositoryName) | ||
} |
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.
Consider adding the
size
as a Computed field. I can see the value for a user who might want to add an output block to their Terraform config to print out the current repo size.