Skip to content

Commit af538dc

Browse files
authored
Added external Wiki service (#1003)
* Added external Wiki service minus the docs * Adding generated wiki doc file and committing linux version of external wiki files vs windows * Copy Pasta Error * Updated resource with computed fields and also updated the unit tests * Updating docs to reflect resource changes to some computed fields * Fixing acceptance test
1 parent 02f56f2 commit af538dc

File tree

5 files changed

+330
-0
lines changed

5 files changed

+330
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_service_external_wiki Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_service_external_wiki resource allows to manage the lifecycle of a project integration with External Wiki Service.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/integrations.html#external-wiki
8+
---
9+
10+
# gitlab_service_external_wiki (Resource)
11+
12+
The `gitlab_service_external_wiki` resource allows to manage the lifecycle of a project integration with External Wiki Service.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#external-wiki)
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "gitlab_project" "awesome_project" {
20+
name = "awesome_project"
21+
description = "My awesome project."
22+
visibility_level = "public"
23+
}
24+
25+
resource "gitlab_service_external_wiki" "wiki" {
26+
project = gitlab_project.awesome_project.id
27+
external_wiki_url = "https://MyAwesomeExternalWikiURL.com"
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `external_wiki_url` (String) The URL of the external wiki.
37+
- `project` (String) ID of the project you want to activate integration on.
38+
39+
### Optional
40+
41+
- `id` (String) The ID of this resource.
42+
43+
### Read-Only
44+
45+
- `active` (Boolean) Whether the integration is active.
46+
- `created_at` (String) The ISO8601 date/time that this integration was activated at in UTC.
47+
- `slug` (String) The name of the integration in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.
48+
- `title` (String) Title of the integration.
49+
- `updated_at` (String) The ISO8601 date/time that this integration was last updated at in UTC.
50+
51+
## Import
52+
53+
Import is supported using the following syntax:
54+
55+
```shell
56+
# You can import a gitlab_service_external_wiki state using the project ID, e.g.
57+
terraform import gitlab_service_external_wiki.wiki 1
58+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# You can import a gitlab_service_external_wiki state using the project ID, e.g.
2+
terraform import gitlab_service_external_wiki.wiki 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "gitlab_project" "awesome_project" {
2+
name = "awesome_project"
3+
description = "My awesome project."
4+
visibility_level = "public"
5+
}
6+
7+
resource "gitlab_service_external_wiki" "wiki" {
8+
project = gitlab_project.awesome_project.id
9+
external_wiki_url = "https://MyAwesomeExternalWikiURL.com"
10+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"log"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
gitlab "github.com/xanzy/go-gitlab"
12+
)
13+
14+
var _ = registerResource("gitlab_service_external_wiki", func() *schema.Resource {
15+
return &schema.Resource{
16+
Description: `The ` + "`gitlab_service_external_wiki`" + ` resource allows to manage the lifecycle of a project integration with External Wiki Service.
17+
18+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#external-wiki)`,
19+
20+
CreateContext: resourceGitlabServiceExternalWikiCreate,
21+
ReadContext: resourceGitlabServiceExternalWikiRead,
22+
UpdateContext: resourceGitlabServiceExternalWikiCreate,
23+
DeleteContext: resourceGitlabServiceExternalWikiDelete,
24+
Importer: &schema.ResourceImporter{
25+
StateContext: schema.ImportStatePassthroughContext,
26+
},
27+
28+
Schema: map[string]*schema.Schema{
29+
"project": {
30+
Description: "ID of the project you want to activate integration on.",
31+
Type: schema.TypeString,
32+
Required: true,
33+
ForceNew: true,
34+
ValidateFunc: validation.StringIsNotEmpty,
35+
},
36+
"external_wiki_url": {
37+
Description: "The URL of the external wiki.",
38+
Type: schema.TypeString,
39+
Required: true,
40+
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
41+
},
42+
"title": {
43+
Description: "Title of the integration.",
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"created_at": {
48+
Description: "The ISO8601 date/time that this integration was activated at in UTC.",
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"updated_at": {
53+
Description: "The ISO8601 date/time that this integration was last updated at in UTC.",
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
"slug": {
58+
Description: "The name of the integration in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.",
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"active": {
63+
Description: "Whether the integration is active.",
64+
Type: schema.TypeBool,
65+
Computed: true,
66+
},
67+
},
68+
}
69+
})
70+
71+
func resourceGitlabServiceExternalWikiCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
72+
client := meta.(*gitlab.Client)
73+
project := d.Get("project").(string)
74+
d.SetId(project)
75+
76+
options := &gitlab.SetExternalWikiServiceOptions{
77+
ExternalWikiURL: gitlab.String(d.Get("external_wiki_url").(string)),
78+
}
79+
80+
log.Printf("[DEBUG] create gitlab external wiki service for project %s", project)
81+
82+
_, err := client.Services.SetExternalWikiService(project, options, gitlab.WithContext(ctx))
83+
if err != nil {
84+
return diag.FromErr(err)
85+
}
86+
87+
return resourceGitlabServiceExternalWikiRead(ctx, d, meta)
88+
}
89+
90+
func resourceGitlabServiceExternalWikiRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
91+
client := meta.(*gitlab.Client)
92+
project := d.Id()
93+
94+
log.Printf("[DEBUG] read gitlab external wiki service for project %s", project)
95+
96+
service, _, err := client.Services.GetExternalWikiService(project, gitlab.WithContext(ctx))
97+
if err != nil {
98+
if is404(err) {
99+
log.Printf("[DEBUG] gitlab external wiki service not found for project %s", project)
100+
d.SetId("")
101+
return nil
102+
}
103+
return diag.FromErr(err)
104+
}
105+
106+
d.Set("project", project)
107+
d.Set("external_wiki_url", service.Properties.ExternalWikiURL)
108+
d.Set("active", service.Active)
109+
d.Set("slug", service.Slug)
110+
d.Set("title", service.Title)
111+
d.Set("created_at", service.CreatedAt.Format(time.RFC3339))
112+
if service.UpdatedAt != nil {
113+
d.Set("updated_at", service.UpdatedAt.Format(time.RFC3339))
114+
}
115+
116+
return nil
117+
}
118+
119+
func resourceGitlabServiceExternalWikiDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
120+
client := meta.(*gitlab.Client)
121+
project := d.Id()
122+
123+
log.Printf("[DEBUG] delete gitlab external wiki service for project %s", project)
124+
125+
_, err := client.Services.DeleteExternalWikiService(project, gitlab.WithContext(ctx))
126+
if err != nil {
127+
return diag.FromErr(err)
128+
}
129+
130+
return nil
131+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)