Skip to content

Commit 9114485

Browse files
authored
Merge pull request #771 from hanks/fix/add-import-to-ldap-link
fix(group_ldap_link): add missing terraform import feature for group LDAP link
2 parents b68da4b + 22fda1f commit 9114485

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

docs/resources/group_ldap_link.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ 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+
## Import
4041

42+
Import is supported using the following syntax:
43+
44+
```shell
45+
# GitLab group ldap links can be imported using an id made up of `group_id:ldap_provider:cn`, e.g.
46+
terraform import gitlab_group_ldap_link.test "12345:ldapmain:testuser"
47+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GitLab group ldap links can be imported using an id made up of `group_id:ldap_provider:cn`, e.g.
2+
terraform import gitlab_group_ldap_link.test "12345:ldapmain:testuser"

internal/provider/resource_gitlab_group_ldap_link.go

Lines changed: 21 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+
StateContext: resourceGitlabGroupLdapLinkImporter,
25+
},
2326

2427
Schema: map[string]*schema.Schema{
2528
"group_id": {
@@ -180,3 +183,21 @@ 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, error) {
188+
parts := strings.SplitN(d.Id(), ":", 3)
189+
if len(parts) != 3 {
190+
return nil, 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+
if diag.HasError() {
200+
return nil, fmt.Errorf("%s", diag[0].Summary)
201+
}
202+
return []*schema.ResourceData{d}, nil
203+
}

internal/provider/resource_gitlab_group_ldap_link_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
1515
rInt := acctest.RandInt()
16+
resourceName := "gitlab_group_ldap_link.foo"
1617

1718
// PreCheck runs after Config so load test data here
1819
var ldapLink gitlab.LDAPGroupLink
@@ -31,18 +32,27 @@ func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
3132
SkipFunc: isRunningInCE,
3233
Config: testAccGitlabGroupLdapLinkCreateConfig(rInt, &testLdapLink),
3334
Check: resource.ComposeTestCheckFunc(
34-
testAccCheckGitlabGroupLdapLinkExists("gitlab_group_ldap_link.foo", &ldapLink),
35+
testAccCheckGitlabGroupLdapLinkExists(resourceName, &ldapLink),
3536
testAccCheckGitlabGroupLdapLinkAttributes(&ldapLink, &testAccGitlabGroupLdapLinkExpectedAttributes{
3637
accessLevel: fmt.Sprintf("developer"), // nolint // TODO: Resolve this golangci-lint issue: S1039: unnecessary use of fmt.Sprintf (gosimple)
3738
})),
3839
},
3940

41+
// Import the group LDAP link (re-uses testAccGitlabGroupLdapLinkCreateConfig for Config)
42+
{
43+
SkipFunc: isRunningInCE,
44+
ResourceName: resourceName,
45+
ImportStateIdFunc: getGitlabGroupLdapLinkImportID(resourceName),
46+
ImportState: true,
47+
ImportStateVerify: true,
48+
},
49+
4050
// Update the group LDAP link to change the access level (uses testAccGitlabGroupLdapLinkUpdateConfig for Config)
4151
{
4252
SkipFunc: isRunningInCE,
4353
Config: testAccGitlabGroupLdapLinkUpdateConfig(rInt, &testLdapLink),
4454
Check: resource.ComposeTestCheckFunc(
45-
testAccCheckGitlabGroupLdapLinkExists("gitlab_group_ldap_link.foo", &ldapLink),
55+
testAccCheckGitlabGroupLdapLinkExists(resourceName, &ldapLink),
4656
testAccCheckGitlabGroupLdapLinkAttributes(&ldapLink, &testAccGitlabGroupLdapLinkExpectedAttributes{
4757
accessLevel: fmt.Sprintf("maintainer"), // nolint // TODO: Resolve this golangci-lint issue: S1039: unnecessary use of fmt.Sprintf (gosimple)
4858
})),
@@ -51,6 +61,30 @@ func TestAccGitlabGroupLdapLink_basic(t *testing.T) {
5161
})
5262
}
5363

64+
func getGitlabGroupLdapLinkImportID(resourceName string) resource.ImportStateIdFunc {
65+
return func(s *terraform.State) (string, error) {
66+
rs, ok := s.RootModule().Resources[resourceName]
67+
if !ok {
68+
return "", fmt.Errorf("Not Found: %s", resourceName)
69+
}
70+
71+
groupID := rs.Primary.Attributes["group_id"]
72+
if groupID == "" {
73+
return "", fmt.Errorf("No group ID is set")
74+
}
75+
ldapProvider := rs.Primary.Attributes["ldap_provider"]
76+
if ldapProvider == "" {
77+
return "", fmt.Errorf("No LDAP provider is set")
78+
}
79+
ldapCN := rs.Primary.Attributes["cn"]
80+
if ldapCN == "" {
81+
return "", fmt.Errorf("No LDAP CN is set")
82+
}
83+
84+
return fmt.Sprintf("%s:%s:%s", groupID, ldapProvider, ldapCN), nil
85+
}
86+
}
87+
5488
func testAccCheckGitlabGroupLdapLinkExists(resourceName string, ldapLink *gitlab.LDAPGroupLink) resource.TestCheckFunc {
5589
return func(s *terraform.State) error {
5690
// Clear the "found" LDAP link before checking for existence

0 commit comments

Comments
 (0)