Skip to content

Commit

Permalink
resource/gitlab_application_settings: New experimental resource
Browse files Browse the repository at this point in the history
This change set implements the new `gitlab_application_settings`
resource which provides a way to change the GitLab Application Settings
using the API at
https://docs.gitlab.com/ee/api/settings.html#change-application-settings.

Even though I suggest that we can merge this first experimental version
of the resource there are still quite some open questions for me.

I'd like to tackle them over at
https://github.com/gitlabhq/terraform-provider-gitlab/issues/957 and
give the community the change to take part in the discussion on how to
move forward with this.

Refs: #957 #1187
  • Loading branch information
timofurrer committed Aug 17, 2022
1 parent 630a88e commit 6432168
Show file tree
Hide file tree
Showing 6 changed files with 3,388 additions and 0 deletions.
299 changes: 299 additions & 0 deletions docs/resources/application_settings.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions examples/resources/gitlab_application_settings/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set the default branch
resource "gitlab_application_settings" "this" {
default_branch_name = "main"
}

# Set the 2FA settings
resource "gitlab_application_settings" "this" {
require_two_factor_authentication = true
two_factor_grace_period = 24
}
78 changes: 78 additions & 0 deletions internal/provider/resource_gitlab_application_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package provider

import (
"context"
"log"

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

const applicationSettingsID = "gitlab"

var _ = registerResource("gitlab_application_settings", func() *schema.Resource {
return &schema.Resource{
Description: `The ` + "`" + `gitlab_application_settings` + "`" + ` resource allows to manage the GitLabLab application settings.
~> This is an **experimental resource**. By nature it doesn't properly fit into how Terraform resources are meant to work.
Feel free to join the [discussion](https://github.com/gitlabhq/terraform-provider-gitlab/issues/957) if you have any
ideas or questions regarding this resource.
~> All ` + "`" + `gitlab_application_settings` + "`" + ` use the same ID ` + "`" + `gitlab` + "`" + `.
!> This resource does not implement any destroy logic, it's a no-op at this point.
It's also not possible to revert to the previous settings.
-> Requires at administrative privileges on GitLab.
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/settings.html)`,

CreateContext: resourceGitlabApplicationSettingsSet,
ReadContext: resourceGitlabApplicationSettingsRead,
UpdateContext: resourceGitlabApplicationSettingsSet,
DeleteContext: resourceGitlabApplicationSettingsDelete,

Schema: gitlabApplicationSettingsSchema(),
}
})

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

log.Printf("[DEBUG] update GitLab Application Settings")
options := gitlabApplicationSettingsToUpdateOptions(d)
if (gitlab.UpdateSettingsOptions{}) != *options {
_, _, err := client.Settings.UpdateSettings(options, gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}

d.SetId(applicationSettingsID)
return resourceGitlabApplicationSettingsRead(ctx, d, meta)
}

func resourceGitlabApplicationSettingsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.Id() != applicationSettingsID {
return diag.Errorf("The `gitlab_application_settings` resource can only exist once and requires the id to be `gitlab`")
}

client := meta.(*gitlab.Client)
log.Printf("[DEBUG] read GitLab Application settings")
settings, _, err := client.Settings.GetSettings(gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

stateMap := gitlabApplicationSettingsToStateMap(settings)
if err = setStateMapInResourceData(stateMap, d); err != nil {
return diag.FromErr(err)
}
return nil
}

func resourceGitlabApplicationSettingsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] destroying the application settings does not yet do anything.")
return nil
}
33 changes: 33 additions & 0 deletions internal/provider/resource_gitlab_application_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build acceptance
// +build acceptance

package provider

import (
"testing"

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

func TestAccGitlabApplicationSettings_basic(t *testing.T) {
// lintignore:AT001
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
// Verify empty application settings
{
Config: `
resource "gitlab_application_settings" "this" {}
`,
},
// Verify changing some application settings
{
Config: `
resource "gitlab_application_settings" "this" {
after_sign_up_text = "Welcome to GitLab!"
}
`,
},
},
})
}

0 comments on commit 6432168

Please sign in to comment.