-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource/gitlab_application_settings: New experimental resource
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
1 parent
630a88e
commit 6432168
Showing
6 changed files
with
3,388 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
examples/resources/gitlab_application_settings/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
internal/provider/resource_gitlab_application_settings_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
} | ||
`, | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.