4
4
package provider
5
5
6
6
import (
7
- "errors"
8
7
"fmt"
9
8
"testing"
10
9
11
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
12
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13
11
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
14
- "github.com/xanzy/go-gitlab"
15
12
)
16
13
17
14
func TestAccGitlabGroupSamlLink_basic (t * testing.T ) {
18
- rInt := acctest .RandInt ()
19
- resourceName := "gitlab_group_saml_link.foo"
15
+ testAccCheckEE (t )
20
16
21
- // PreCheck runs after Config so load test data here
22
- var samlLink gitlab.SAMLGroupLink
23
- testSamlLink := gitlab.SAMLGroupLink {
24
- Name : "test_saml_group" ,
25
- }
17
+ testGroup := testAccCreateGroups (t , 1 )[0 ]
26
18
27
19
resource .ParallelTest (t , resource.TestCase {
28
20
ProviderFactories : providerFactories ,
29
21
CheckDestroy : testAccCheckGitlabGroupSamlLinkDestroy ,
30
22
Steps : []resource.TestStep {
31
23
32
- // Create a group SAML link as a developer (uses testAccGitlabGroupLdapSamlCreateConfig for Config)
24
+ // Create a group SAML link as a developer
33
25
{
34
- SkipFunc : isRunningInCE ,
35
26
Config : fmt .Sprintf (`
36
- resource "gitlab_group_saml_link" "foo" {
37
- group_id = "%d"
38
- access_level = "Developer"
39
- saml_group_name = "%s"
40
-
41
- }` , rInt , rInt , testSamlLink .Name ),
42
- Check : resource .ComposeTestCheckFunc (
43
- testAccCheckGitlabGroupSamlLinkExists (resourceName , & samlLink )),
44
- },
27
+ resource "gitlab_group_saml_link" "this" {
28
+ group = "%d"
29
+ access_level = "Developer"
30
+ saml_group_name = "test_saml_group"
45
31
46
- // Import the group SAML link (re-uses testAccGitlabGroupSamlLinkCreateConfig for Config)
32
+ }
33
+ ` , testGroup .ID ),
34
+ },
35
+ // Verify Import
47
36
{
48
- SkipFunc : isRunningInCE ,
49
- ResourceName : resourceName ,
50
- ImportStateIdFunc : getGitlabGroupSamlLinkImportID (resourceName ),
37
+ ResourceName : "gitlab_group_saml_link.this" ,
51
38
ImportState : true ,
52
39
ImportStateVerify : true ,
53
40
},
54
-
55
- // Update the group SAML link to change the access level (uses testAccGitlabGroupSamlLinkUpdateConfig for Config)
41
+ // Update the group SAML link to change the access level
56
42
{
57
- SkipFunc : isRunningInCE ,
58
43
Config : fmt .Sprintf (`
59
- resource "gitlab_group_saml_link" "foo " {
60
- group_id = "%d"
61
- access_level = "Maintainer"
62
- saml_group_name = "%s "
63
- }` , rInt , rInt , testSamlLink . Name ),
64
- Check : resource . ComposeTestCheckFunc (
65
- testAccCheckGitlabGroupSamlLinkExists ( resourceName , & samlLink ) ),
44
+ resource "gitlab_group_saml_link" "this " {
45
+ group = "%d"
46
+ access_level = "Maintainer"
47
+ saml_group_name = "test_saml_group "
48
+
49
+ }
50
+ ` , testGroup . ID ),
66
51
},
67
52
},
68
53
})
69
54
}
70
55
71
- func getGitlabGroupSamlLinkImportID (resourceName string ) resource.ImportStateIdFunc {
72
- return func (s * terraform.State ) (string , error ) {
73
- rs , ok := s .RootModule ().Resources [resourceName ]
74
- if ! ok {
75
- return "" , fmt .Errorf ("Not Found: %s" , resourceName )
76
- }
77
-
78
- groupID := rs .Primary .Attributes ["group_id" ]
79
- if groupID == "" {
80
- return "" , fmt .Errorf ("No group ID is set" )
81
- }
82
- samlGroupName := rs .Primary .Attributes ["saml_group_name" ]
83
- if samlGroupName == "" {
84
- return "" , fmt .Errorf ("No SAML group name is set" )
85
- }
86
-
87
- return fmt .Sprintf ("%s:%s" , groupID , samlGroupName ), nil
88
- }
89
- }
90
-
91
- func testAccCheckGitlabGroupSamlLinkExists (resourceName string , samlLink * gitlab.SAMLGroupLink ) resource.TestCheckFunc {
92
- return func (s * terraform.State ) error {
93
- // Clear the "found" SAML link before checking for existence
94
- * samlLink = gitlab.SAMLGroupLink {}
95
-
96
- resourceState , ok := s .RootModule ().Resources [resourceName ]
97
- if ! ok {
98
- return fmt .Errorf ("Not found: %s" , resourceName )
56
+ func testAccCheckGitlabGroupSamlLinkDestroy (s * terraform.State ) error {
57
+ for _ , resourceState := range s .RootModule ().Resources {
58
+ if resourceState .Type != "gitlab_group_saml_link" {
59
+ continue
99
60
}
100
61
101
- err := testAccGetGitlabGroupSamlLink ( samlLink , resourceState )
62
+ group , samlGroupName , err := parseTwoPartID ( resourceState . Primary . ID )
102
63
if err != nil {
103
64
return err
104
65
}
105
66
106
- return nil
107
- }
108
- }
109
-
110
- func testAccCheckGitlabGroupSamlLinkDestroy (s * terraform.State ) error {
111
- // Can't check for links if the group is destroyed so make sure all groups are destroyed instead
112
- for _ , resourceState := range s .RootModule ().Resources {
113
- if resourceState .Type != "gitlab_group" {
114
- continue
115
- }
116
-
117
- group , _ , err := testGitlabClient .Groups .GetGroup (resourceState .Primary .ID , nil )
67
+ samlGroupLink , _ , err := testGitlabClient .Groups .GetGroupSAMLLink (group , samlGroupName )
118
68
if err == nil {
119
- if group != nil && fmt .Sprintf ("%d" , group .ID ) == resourceState .Primary .ID {
120
- if group .MarkedForDeletionOn == nil {
121
- return fmt .Errorf ("Group still exists" )
122
- }
69
+ if samlGroupLink != nil {
70
+ return fmt .Errorf ("SAML Group Link still exists" )
123
71
}
124
72
}
125
73
if ! is404 (err ) {
@@ -129,41 +77,3 @@ func testAccCheckGitlabGroupSamlLinkDestroy(s *terraform.State) error {
129
77
}
130
78
return nil
131
79
}
132
-
133
- func testAccGetGitlabGroupSamlLink (samlLink * gitlab.SAMLGroupLink , resourceState * terraform.ResourceState ) error {
134
- groupId := resourceState .Primary .Attributes ["group_id" ]
135
- if groupId == "" {
136
- return fmt .Errorf ("No group ID is set" )
137
- }
138
-
139
- // Construct our desired SAML Link from the config values
140
- desiredSamlLink := gitlab.SAMLGroupLink {
141
- AccessLevel : resourceState .Primary .Attributes ["access_level" ],
142
- Name : resourceState .Primary .Attributes ["saml_group_name" ],
143
- }
144
-
145
- desiredSamlLinkId := buildTwoPartID (& groupId , & desiredSamlLink .Name )
146
-
147
- // Try to fetch all group links from GitLab
148
- currentSamlLinks , _ , err := testGitlabClient .Groups .ListGroupSamlLinks (groupId , nil )
149
- if err != nil {
150
- return err
151
- }
152
-
153
- found := false
154
-
155
- // Check if the SAML link exists in the returned list of links
156
- for _ , currentSamlLink := range currentSamlLinks {
157
- if buildTwoPartID (& groupId , & currentSamlLink .Name ) == desiredSamlLinkId {
158
- found = true
159
- * samlLink = * currentSamlLink
160
- break
161
- }
162
- }
163
-
164
- if ! found {
165
- return errors .New (fmt .Sprintf ("SamlLink %s does not exist." , desiredSamlLinkId )) // nolint // TODO: Resolve this golangci-lint issue: S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
166
- }
167
-
168
- return nil
169
- }
0 commit comments