Skip to content

Commit 3545901

Browse files
author
Han Zhou
committed
fix(group_ldap_link): add missing terraform import feature for group LDAP link
**Why** There is a bug that [the doc](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs/resources/group_ldap_link#import) says we can use `terraform import`, but actually the function is not implemented **Code Changes** * Added the missing import function * Fixed a bug to set `access_level` instead of `group_level`, because `access_level` is the one defined in schema * Set `force = false` as default when importing, because we can not get any force value from the Gitlab API * Updated the doc about import part **Design Changes** * Added `group_id` in the import id string to change it from `ldap_provider:cn` to `group_id:ldap_provider:cn`, because we need the `group_id` to identify the group **Close** * https://github.com/gitlabhq/terraform-provider-gitlab/issues/448
1 parent e3a911c commit 3545901

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

docs/resources/group_ldap_link.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ resource "gitlab_group_ldap_link" "test" {
3737
- **group_access** (String) Minimum access level for members of the LDAP group. Valid values are: `no one`, `minimal`, `guest`, `reporter`, `developer`, `maintainer`, `owner`, `master`
3838
- **id** (String) The ID of this resource.
3939

40+
GitLab group ldap links can be imported using an id made up of `group_id:ldap_provider:cn`, e.g.
4041

42+
```shell
43+
terraform import gitlab_group_ldap_link.test "12345:ldapmain:testuser"
44+
```

internal/provider/resource_gitlab_group_ldap_link.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var _ = registerResource("gitlab_group_ldap_link", func() *schema.Resource {
2020
CreateContext: resourceGitlabGroupLdapLinkCreate,
2121
ReadContext: resourceGitlabGroupLdapLinkRead,
2222
DeleteContext: resourceGitlabGroupLdapLinkDelete,
23+
Importer: &schema.ResourceImporter{
24+
State: resourceGitlabGroupLdapLinkImporter,
25+
},
2326

2427
Schema: map[string]*schema.Schema{
2528
"group_id": {
@@ -180,3 +183,18 @@ func resourceGitlabGroupLdapLinkDelete(ctx context.Context, d *schema.ResourceDa
180183

181184
return nil
182185
}
186+
187+
func resourceGitlabGroupLdapLinkImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, diag.Diagnostics) {
188+
parts := strings.SplitN(d.Id(), ":", 3)
189+
if len(parts) != 3 {
190+
return nil, diag.FromErr(fmt.Errorf("invalid ldap link import id (should be <group id>:<ldap provider>:<ladp cn>): %s", d.Id()))
191+
}
192+
193+
groupId, ldapProvider, ldapCN := parts[0], parts[1], parts[2]
194+
d.SetId(buildTwoPartID(&ldapProvider, &ldapCN))
195+
d.Set("group_id", groupId)
196+
d.Set("force", false)
197+
198+
diag := resourceGitlabGroupLdapLinkRead(ctx, d, meta)
199+
return []*schema.ResourceData{d}, diag
200+
}

0 commit comments

Comments
 (0)