Skip to content
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

Added external Wiki service #1003

Merged
merged 7 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/resources/service_external_wiki.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitlab_service_external_wiki Resource - terraform-provider-gitlab"
subcategory: ""
description: |-
The gitlab_service_external_wiki resource allows to manage the lifecycle of a project integration with External Wiki Service.
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/integrations.html#external-wiki
---

# gitlab_service_external_wiki (Resource)

The `gitlab_service_external_wiki` resource allows to manage the lifecycle of a project integration with External Wiki Service.

**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#external-wiki)

## Example Usage

```terraform
resource "gitlab_project" "awesome_project" {
name = "awesome_project"
description = "My awesome project."
visibility_level = "public"
}

resource "gitlab_service_external_wiki" "wiki" {
project = gitlab_project.awesome_project.id
external_wiki_url = "https://MyAwesomeExternalWikiURL.com"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `external_wiki_url` (String) The URL of the external wiki.
- `project` (String) ID of the project you want to activate integration on.

### Optional

- `id` (String) The ID of this resource.

### Read-Only

- `active` (Boolean) Whether the integration is active.
- `created_at` (String) The ISO8601 date/time that this integration was activated at in UTC.
- `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.
- `title` (String) Title of the integration.
- `updated_at` (String) The ISO8601 date/time that this integration was last updated at in UTC.

## Import

Import is supported using the following syntax:

```shell
# You can import a gitlab_service_external_wiki state using the project ID, e.g.
terraform import gitlab_service_external_wiki.wiki 1
```
2 changes: 2 additions & 0 deletions examples/resources/gitlab_service_external_wiki/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# You can import a gitlab_service_external_wiki state using the project ID, e.g.
terraform import gitlab_service_external_wiki.wiki 1
10 changes: 10 additions & 0 deletions examples/resources/gitlab_service_external_wiki/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "gitlab_project" "awesome_project" {
name = "awesome_project"
description = "My awesome project."
visibility_level = "public"
}

resource "gitlab_service_external_wiki" "wiki" {
project = gitlab_project.awesome_project.id
external_wiki_url = "https://MyAwesomeExternalWikiURL.com"
}
131 changes: 131 additions & 0 deletions internal/provider/resource_gitlab_service_external_wiki.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package provider

import (
"context"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
gitlab "github.com/xanzy/go-gitlab"
)

var _ = registerResource("gitlab_service_external_wiki", func() *schema.Resource {
return &schema.Resource{
Description: `The ` + "`gitlab_service_external_wiki`" + ` resource allows to manage the lifecycle of a project integration with External Wiki Service.

**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#external-wiki)`,

CreateContext: resourceGitlabServiceExternalWikiCreate,
ReadContext: resourceGitlabServiceExternalWikiRead,
UpdateContext: resourceGitlabServiceExternalWikiCreate,
DeleteContext: resourceGitlabServiceExternalWikiDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"project": {
Description: "ID of the project you want to activate integration on.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"external_wiki_url": {
Description: "The URL of the external wiki.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},
"title": {
Description: "Title of the integration.",
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Description: "The ISO8601 date/time that this integration was activated at in UTC.",
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Description: "The ISO8601 date/time that this integration was last updated at in UTC.",
Type: schema.TypeString,
Computed: true,
},
"slug": {
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.",
Type: schema.TypeString,
Computed: true,
},
"active": {
Description: "Whether the integration is active.",
Type: schema.TypeBool,
Computed: true,
},
},
}
})

func resourceGitlabServiceExternalWikiCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
d.SetId(project)

options := &gitlab.SetExternalWikiServiceOptions{
ExternalWikiURL: gitlab.String(d.Get("external_wiki_url").(string)),
}

log.Printf("[DEBUG] create gitlab external wiki service for project %s", project)

_, err := client.Services.SetExternalWikiService(project, options, gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

return resourceGitlabServiceExternalWikiRead(ctx, d, meta)
}

func resourceGitlabServiceExternalWikiRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
project := d.Id()

log.Printf("[DEBUG] read gitlab external wiki service for project %s", project)

service, _, err := client.Services.GetExternalWikiService(project, gitlab.WithContext(ctx))
if err != nil {
if is404(err) {
log.Printf("[DEBUG] gitlab external wiki service not found for project %s", project)
d.SetId("")
return nil
}
return diag.FromErr(err)
}

d.Set("project", project)
d.Set("external_wiki_url", service.Properties.ExternalWikiURL)
d.Set("active", service.Active)
d.Set("slug", service.Slug)
d.Set("title", service.Title)
d.Set("created_at", service.CreatedAt.Format(time.RFC3339))
if service.UpdatedAt != nil {
d.Set("updated_at", service.UpdatedAt.Format(time.RFC3339))
}

return nil
}

func resourceGitlabServiceExternalWikiDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
project := d.Id()

log.Printf("[DEBUG] delete gitlab external wiki service for project %s", project)

_, err := client.Services.DeleteExternalWikiService(project, gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

return nil
}
129 changes: 129 additions & 0 deletions internal/provider/resource_gitlab_service_external_wiki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package provider

import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
gitlab "github.com/xanzy/go-gitlab"
)

func TestAccGitlabServiceExternalWiki_basic(t *testing.T) {
testAccCheck(t)

testProject := testAccCreateProject(t)

var externalWikiService gitlab.ExternalWikiService

var externalWikiURL1 = "http://mynumberonewiki.com"
var externalWikiURL2 = "http://mynumbertwowiki.com"
var externalWikiResourceName = "gitlab_service_external_wiki.this"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabServiceExternalWikiDestroy,
Steps: []resource.TestStep{
// Create an External Wiki service
{
Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL1),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService),
resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1),
resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1),
resource.TestCheckResourceAttr(externalWikiResourceName, "active", "true"),
testCheckResourceAttrLazy(externalWikiResourceName, "created_at", func() string { return externalWikiService.CreatedAt.Format(time.RFC3339) }),
),
},
// Verify import
{
ResourceName: "gitlab_service_external_wiki.this",
ImportState: true,
ImportStateVerify: true,
},
// Update the External Wiki service
{
Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL2),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService),
resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL2),
testCheckResourceAttrLazy(externalWikiResourceName, "created_at", func() string { return externalWikiService.CreatedAt.Format(time.RFC3339) }),
testCheckResourceAttrLazy(externalWikiResourceName, "updated_at", func() string { return externalWikiService.UpdatedAt.Format(time.RFC3339) }),
),
},
// Verify import
{
ResourceName: "gitlab_service_external_wiki.this",
ImportState: true,
ImportStateVerify: true,
},
// Update the External Wiki service to get back to previous settings
{
Config: testAccGitlabServiceExternalWikiConfig(testProject.ID, externalWikiURL1),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabServiceExternalWikiExists(externalWikiResourceName, &externalWikiService),
resource.TestCheckResourceAttr(externalWikiResourceName, "external_wiki_url", externalWikiURL1),
),
},
// Verify import
{
ResourceName: "gitlab_service_external_wiki.this",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckGitlabServiceExternalWikiExists(resourceIdentifier string, service *gitlab.ExternalWikiService) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceIdentifier]
if !ok {
return fmt.Errorf("Not Found: %s", resourceIdentifier)
}

project := rs.Primary.Attributes["project"]
if project == "" {
return fmt.Errorf("No project ID is set")
}

externalWikiService, _, err := testGitlabClient.Services.GetExternalWikiService(project)
if err != nil {
return fmt.Errorf("External Wiki service does not exist in project %s: %v", project, err)
}
*service = *externalWikiService

return nil
}
}

func testAccCheckGitlabServiceExternalWikiDestroy(s *terraform.State) error {
var project string

for _, rs := range s.RootModule().Resources {
if rs.Type == "gitlab_service_external_wiki" {
project = rs.Primary.ID

externalWikiService, _, err := testGitlabClient.Services.GetExternalWikiService(project)
if err == nil {
if externalWikiService != nil && externalWikiService.Active != false {
return fmt.Errorf("[ERROR] External Wiki Service %v still exists", project)
}
} else {
return err
}
}
}
return nil
}

func testAccGitlabServiceExternalWikiConfig(projectID int, externalWikiURL string) string {
return fmt.Sprintf(`
resource "gitlab_service_external_wiki" "this" {
project = %[1]d
external_wiki_url = "%[2]s"
}
`, projectID, externalWikiURL)
}