Skip to content

Commit

Permalink
Fix generated import formats for resources that allow in their names
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
slevenick authored and modular-magician committed Dec 16, 2019
1 parent 9afcffe commit 6eec31d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
15 changes: 12 additions & 3 deletions google/iam_source_repo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package google

import (
"fmt"
"regexp"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -32,10 +33,18 @@ var SourceRepoRepositoryIamSchema = map[string]*schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
DiffSuppressFunc: SourceRepoRepositoryDiffSuppress,
},
}

func SourceRepoRepositoryDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
oldParts := regexp.MustCompile("projects/[^/]+/repos/").Split(old, -1)
if len(oldParts) == 2 {
return oldParts[1] == new
}
return new == old
}

type SourceRepoRepositoryIamUpdater struct {
project string
repository string
Expand All @@ -56,7 +65,7 @@ func SourceRepoRepositoryIamUpdaterProducer(d *schema.ResourceData, config *Conf
}

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/repos/(?P<repository>[^/]+)", "(?P<project>[^/]+)/(?P<repository>[^/]+)", "(?P<repository>[^/]+)"}, d, config, d.Get("repository").(string))
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/repos/(?P<repository>.+)", "(?P<repository>.+)"}, d, config, d.Get("repository").(string))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +96,7 @@ func SourceRepoRepositoryIdParseFunc(d *schema.ResourceData, config *Config) err
}
values["project"] = project

m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/repos/(?P<repository>[^/]+)", "(?P<project>[^/]+)/(?P<repository>[^/]+)", "(?P<repository>[^/]+)"}, d, config, d.Id())
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/repos/(?P<repository>.+)", "(?P<repository>.+)"}, d, config, d.Id())
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions google/iam_source_repo_repository_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAccSourceRepoRepositoryIamBindingGenerated(t *testing.T) {
},
{
ResourceName: "google_sourcerepo_repository_iam_binding.foo",
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-repository%s", context["random_suffix"])),
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-repository/%s", context["random_suffix"])),
ImportState: true,
ImportStateVerify: true,
},
Expand All @@ -49,7 +49,7 @@ func TestAccSourceRepoRepositoryIamBindingGenerated(t *testing.T) {
},
{
ResourceName: "google_sourcerepo_repository_iam_binding.foo",
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-repository%s", context["random_suffix"])),
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer", getTestProjectFromEnv(), fmt.Sprintf("my-repository/%s", context["random_suffix"])),
ImportState: true,
ImportStateVerify: true,
},
Expand All @@ -75,7 +75,7 @@ func TestAccSourceRepoRepositoryIamMemberGenerated(t *testing.T) {
},
{
ResourceName: "google_sourcerepo_repository_iam_member.foo",
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer user:[email protected]", getTestProjectFromEnv(), fmt.Sprintf("my-repository%s", context["random_suffix"])),
ImportStateId: fmt.Sprintf("projects/%s/repos/%s roles/viewer user:[email protected]", getTestProjectFromEnv(), fmt.Sprintf("my-repository/%s", context["random_suffix"])),
ImportState: true,
ImportStateVerify: true,
},
Expand All @@ -100,7 +100,7 @@ func TestAccSourceRepoRepositoryIamPolicyGenerated(t *testing.T) {
},
{
ResourceName: "google_sourcerepo_repository_iam_policy.foo",
ImportStateId: fmt.Sprintf("projects/%s/repos/%s", getTestProjectFromEnv(), fmt.Sprintf("my-repository%s", context["random_suffix"])),
ImportStateId: fmt.Sprintf("projects/%s/repos/%s", getTestProjectFromEnv(), fmt.Sprintf("my-repository/%s", context["random_suffix"])),
ImportState: true,
ImportStateVerify: true,
},
Expand All @@ -111,7 +111,7 @@ func TestAccSourceRepoRepositoryIamPolicyGenerated(t *testing.T) {
func testAccSourceRepoRepositoryIamMember_basicGenerated(context map[string]interface{}) string {
return Nprintf(`
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository%{random_suffix}"
name = "my-repository/%{random_suffix}"
}
resource "google_sourcerepo_repository_iam_member" "foo" {
Expand All @@ -126,7 +126,7 @@ resource "google_sourcerepo_repository_iam_member" "foo" {
func testAccSourceRepoRepositoryIamPolicy_basicGenerated(context map[string]interface{}) string {
return Nprintf(`
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository%{random_suffix}"
name = "my-repository/%{random_suffix}"
}
data "google_iam_policy" "foo" {
Expand All @@ -147,7 +147,7 @@ resource "google_sourcerepo_repository_iam_policy" "foo" {
func testAccSourceRepoRepositoryIamBinding_basicGenerated(context map[string]interface{}) string {
return Nprintf(`
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository%{random_suffix}"
name = "my-repository/%{random_suffix}"
}
resource "google_sourcerepo_repository_iam_binding" "foo" {
Expand All @@ -162,7 +162,7 @@ resource "google_sourcerepo_repository_iam_binding" "foo" {
func testAccSourceRepoRepositoryIamBinding_updateGenerated(context map[string]interface{}) string {
return Nprintf(`
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository%{random_suffix}"
name = "my-repository/%{random_suffix}"
}
resource "google_sourcerepo_repository_iam_binding" "foo" {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_source_repo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ func resourceSourceRepoRepositoryDelete(d *schema.ResourceData, meta interface{}
func resourceSourceRepoRepositoryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/repos/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<name>[^/]+)",
"(?P<name>[^/]+)",
"projects/(?P<project>[^/]+)/repos/(?P<name>.+)",
"(?P<name>.+)",
}, d, config); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion google/resource_source_repo_repository_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAccSourceRepoRepository_sourcerepoRepositoryBasicExample(t *testing.T)
func testAccSourceRepoRepository_sourcerepoRepositoryBasicExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository%{random_suffix}"
name = "my-repository/%{random_suffix}"
}
`, context)
}
Expand Down
3 changes: 1 addition & 2 deletions website/docs/r/sourcerepo_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To get more information about Repository, see:

```hcl
resource "google_sourcerepo_repository" "my-repo" {
name = "my-repository"
name = "my-repository/"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
Expand Down Expand Up @@ -139,7 +139,6 @@ Repository can be imported using any of these accepted formats:

```
$ terraform import google_sourcerepo_repository.default projects/{{project}}/repos/{{name}}
$ terraform import google_sourcerepo_repository.default {{project}}/{{name}}
$ terraform import google_sourcerepo_repository.default {{name}}
```

Expand Down
1 change: 0 additions & 1 deletion website/docs/r/sourcerepo_repository_iam.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ exported:
For all import syntaxes, the "resource in question" can take any of the following forms:

* projects/{{project}}/repos/{{name}}
* {{project}}/{{name}}
* {{name}}

Any variables not passed in the import command will be taken from the provider configuration.
Expand Down

0 comments on commit 6eec31d

Please sign in to comment.