|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 10 | + gitlab "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGitlabServiceExternalWiki_basic(t *testing.T) { |
| 14 | + testAccCheck(t) |
| 15 | + |
| 16 | + testProject := testAccCreateProject(t) |
| 17 | + |
| 18 | + var externalWikiService gitlab.ExternalWikiService |
| 19 | + |
| 20 | + var externalWikiURL1 = "http://mynumberonewiki.com" |
| 21 | + var externalWikiURL2 = "http://mynumbertwowiki.com" |
| 22 | + var externalWikiResourceName = "gitlab_service_external_wiki.this" |
| 23 | + |
| 24 | + resource.Test(t, resource.TestCase{ |
| 25 | + PreCheck: func() { testAccPreCheck(t) }, |
| 26 | + ProviderFactories: providerFactories, |
| 27 | + CheckDestroy: testAccCheckGitlabServiceExternalWikiDestroy, |
| 28 | + Steps: []resource.TestStep{ |
| 29 | + // Create an External Wiki service |
| 30 | + { |
| 31 | + Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL1), |
| 32 | + Check: resource.ComposeTestCheckFunc( |
| 33 | + testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService), |
| 34 | + resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1), |
| 35 | + resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1), |
| 36 | + resource.TestCheckResourceAttr(externalWikiResourceName, "active", "true"), |
| 37 | + testCheckResourceAttrLazy(externalWikiResourceName, "created_at", func() string { return externalWikiService.CreatedAt.Format(time.RFC3339) }), |
| 38 | + ), |
| 39 | + }, |
| 40 | + // Verify import |
| 41 | + { |
| 42 | + ResourceName: "gitlab_service_external_wiki.this", |
| 43 | + ImportState: true, |
| 44 | + ImportStateVerify: true, |
| 45 | + }, |
| 46 | + // Update the External Wiki service |
| 47 | + { |
| 48 | + Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL2), |
| 49 | + Check: resource.ComposeTestCheckFunc( |
| 50 | + testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService), |
| 51 | + resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL2), |
| 52 | + testCheckResourceAttrLazy(externalWikiResourceName, "created_at", func() string { return externalWikiService.CreatedAt.Format(time.RFC3339) }), |
| 53 | + testCheckResourceAttrLazy(externalWikiResourceName, "updated_at", func() string { return externalWikiService.UpdatedAt.Format(time.RFC3339) }), |
| 54 | + ), |
| 55 | + }, |
| 56 | + // Verify import |
| 57 | + { |
| 58 | + ResourceName: "gitlab_service_external_wiki.this", |
| 59 | + ImportState: true, |
| 60 | + ImportStateVerify: true, |
| 61 | + }, |
| 62 | + // Update the External Wiki service to get back to previous settings |
| 63 | + { |
| 64 | + Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL1), |
| 65 | + Check: resource.ComposeTestCheckFunc( |
| 66 | + testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService), |
| 67 | + resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1), |
| 68 | + ), |
| 69 | + }, |
| 70 | + // Verify import |
| 71 | + { |
| 72 | + ResourceName: "gitlab_service_external_wiki.this", |
| 73 | + ImportState: true, |
| 74 | + ImportStateVerify: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func testAccCheckGitlabServiceExternalWikiExists(resourceIdentifier string, service *gitlab.ExternalWikiService) resource.TestCheckFunc { |
| 81 | + return func(s *terraform.State) error { |
| 82 | + rs, ok := s.RootModule().Resources[resourceIdentifier] |
| 83 | + if !ok { |
| 84 | + return fmt.Errorf("Not Found: %s", resourceIdentifier) |
| 85 | + } |
| 86 | + |
| 87 | + project := rs.Primary.Attributes["project"] |
| 88 | + if project == "" { |
| 89 | + return fmt.Errorf("No project ID is set") |
| 90 | + } |
| 91 | + |
| 92 | + externalWikiService, _, err := testGitlabClient.Services.GetExternalWikiService(project) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("External Wiki service does not exist in project %s: %v", project, err) |
| 95 | + } |
| 96 | + *service = *externalWikiService |
| 97 | + |
| 98 | + return nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func testAccCheckGitlabServiceExternalWikiDestroy(s *terraform.State) error { |
| 103 | + var project string |
| 104 | + |
| 105 | + for _, rs := range s.RootModule().Resources { |
| 106 | + if rs.Type == "gitlab_service_external_wiki" { |
| 107 | + project = rs.Primary.ID |
| 108 | + |
| 109 | + externalWikiService, _, err := testGitlabClient.Services.GetExternalWikiService(project) |
| 110 | + if err == nil { |
| 111 | + if externalWikiService != nil && externalWikiService.Active != false { |
| 112 | + return fmt.Errorf("[ERROR] External Wiki Service %v still exists", project) |
| 113 | + } |
| 114 | + } else { |
| 115 | + return err |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func testAccGitlabServiceExternalWikiConfig(projectID int, externalWikiURL string) string { |
| 123 | + return fmt.Sprintf(` |
| 124 | +resource "gitlab_service_external_wiki" "this" { |
| 125 | + project = %[1]d |
| 126 | + external_wiki_url = "%[2]s" |
| 127 | +} |
| 128 | +`, projectID, externalWikiURL) |
| 129 | +} |
0 commit comments