Skip to content

Commit 3291f33

Browse files
joepvdarmsnyder
authored andcommitted
Add import_url attribute to project resource
This allows importing a repository from a url Add test for project: import_url Wait for import to complete before read
1 parent d9818d1 commit 3291f33

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
113113
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
114114
Default: "private",
115115
},
116+
"import_url": {
117+
Type: schema.TypeString,
118+
Optional: true,
119+
},
116120
"merge_method": {
117121
Type: schema.TypeString,
118122
Optional: true,
@@ -309,6 +313,10 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
309313
setProperties = append(setProperties, "initialize_with_readme")
310314
}
311315

316+
if v, ok := d.GetOk("import_url"); ok {
317+
options.ImportURL = gitlab.String(v.(string))
318+
}
319+
312320
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
313321

314322
project, _, err := client.Projects.CreateProject(options)
@@ -352,6 +360,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
352360
}
353361

354362
func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
363+
var project *gitlab.Project
355364
client := meta.(*gitlab.Client)
356365
log.Printf("[DEBUG] read gitlab project %s", d.Id())
357366

@@ -365,6 +374,13 @@ func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
365374
return nil
366375
}
367376

377+
for project.ImportStatus == "started" {
378+
project, _, err = client.Projects.GetProject(d.Id(), nil)
379+
if err != nil {
380+
return err
381+
}
382+
}
383+
368384
resourceGitlabProjectSetToState(d, project)
369385
return nil
370386
}

gitlab/resource_gitlab_project_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ func TestAccGitlabProject_transfer(t *testing.T) {
352352
})
353353
}
354354

355+
func TestAccImportURL(t *testing.T) {
356+
var received gitlab.Project
357+
resource.Test(t, resource.TestCase{
358+
PreCheck: func() { testAccPreCheck(t) },
359+
Providers: testAccProviders,
360+
CheckDestroy: testAccCheckGitlabProjectDestroy,
361+
Steps: []resource.TestStep{
362+
{
363+
Config: testImportURLOptions(),
364+
Check: resource.ComposeTestCheckFunc(
365+
testAccCheckGitlabProjectExists("gitlab_project.import_url", &received),
366+
testAccCheckImportURL("gitlab/resource_gitlab_project.go", &received),
367+
),
368+
},
369+
},
370+
})
371+
}
372+
355373
func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource.TestCheckFunc {
356374
return func(s *terraform.State) error {
357375
var err error
@@ -667,3 +685,49 @@ resource "gitlab_project" "foo" {
667685
}
668686
`, rInt, rInt)
669687
}
688+
689+
func testImportURLOptions() string {
690+
return fmt.Sprintf(`
691+
resource "gitlab_project" "import_url" {
692+
name = "import"
693+
path = "import"
694+
description = "Terraform Import URL Acceptance test"
695+
696+
# So that acceptance tests can be run in a gitlab organization
697+
# with no billing
698+
visibility_level = "public"
699+
merge_method = "ff"
700+
only_allow_merge_if_pipeline_succeeds = true
701+
only_allow_merge_if_all_discussions_are_resolved = true
702+
703+
issues_enabled = false
704+
merge_requests_enabled = false
705+
approvals_before_merge = 0
706+
wiki_enabled = false
707+
snippets_enabled = false
708+
container_registry_enabled = false
709+
shared_runners_enabled = false
710+
archived = false
711+
import_url = "https://github.com/terraform-providers/terraform-provider-gitlab.git"
712+
default_branch = "master"
713+
}
714+
`)
715+
}
716+
717+
func testAccCheckImportURL(fp string, project *gitlab.Project) resource.TestCheckFunc {
718+
return func(s *terraform.State) error {
719+
conn := testAccProvider.Meta().(*gitlab.Client)
720+
ref := &gitlab.GetFileOptions{
721+
Ref: gitlab.String("master"),
722+
}
723+
f, _, err := conn.RepositoryFiles.GetFile(project.ID, fp, ref, nil)
724+
if err != nil {
725+
return fmt.Errorf("Cannot find file %s, error %s\n", fp, err)
726+
}
727+
if f == nil {
728+
return fmt.Errorf("Did not find file\n")
729+
}
730+
731+
return nil
732+
}
733+
}

0 commit comments

Comments
 (0)