@@ -4,14 +4,21 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "strings"
8
7
9
8
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12
11
gitlab "github.com/xanzy/go-gitlab"
13
12
)
14
13
14
+ var validGroupSamlLinkAccessLevelNames = []string {
15
+ "Guest" ,
16
+ "Reporter" ,
17
+ "Developer" ,
18
+ "Maintainer" ,
19
+ "Owner" ,
20
+ }
21
+
15
22
var _ = registerResource ("gitlab_group_saml_link" , func () * schema.Resource {
16
23
return & schema.Resource {
17
24
Description : `The ` + "`gitlab_group_saml_link`" + ` resource allows to manage the lifecycle of an SAML integration with a group.
@@ -22,12 +29,12 @@ var _ = registerResource("gitlab_group_saml_link", func() *schema.Resource {
22
29
ReadContext : resourceGitlabGroupSamlLinkRead ,
23
30
DeleteContext : resourceGitlabGroupSamlLinkDelete ,
24
31
Importer : & schema.ResourceImporter {
25
- StateContext : resourceGitlabGroupSamlLinkImporter ,
32
+ StateContext : schema . ImportStatePassthroughContext ,
26
33
},
27
34
28
35
Schema : map [string ]* schema.Schema {
29
- "group_id " : {
30
- Description : "The id of the GitLab group." ,
36
+ "group " : {
37
+ Description : "The ID or path of the group to add the SAML Group Link to ." ,
31
38
Type : schema .TypeString ,
32
39
Required : true ,
33
40
ForceNew : true ,
@@ -45,65 +52,53 @@ var _ = registerResource("gitlab_group_saml_link", func() *schema.Resource {
45
52
Required : true ,
46
53
ForceNew : true ,
47
54
},
48
- "force" : {
49
- Description : "If true, then delete and replace an existing SAML link if one exists." ,
50
- Type : schema .TypeBool ,
51
- Optional : true ,
52
- Default : false ,
53
- ForceNew : true ,
54
- },
55
55
},
56
56
}
57
57
})
58
58
59
59
func resourceGitlabGroupSamlLinkCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
60
60
client := meta .(* gitlab.Client )
61
61
62
- groupId := d .Get ("group_id " ).(string )
62
+ group := d .Get ("group " ).(string )
63
63
accessLevel := d .Get ("access_level" ).(string )
64
64
samlGroupName := d .Get ("saml_group_name" ).(string )
65
- force := d .Get ("force" ).(bool )
66
65
67
66
options := & gitlab.AddGroupSAMLLinkOptions {
68
- AccessLevel : & accessLevel ,
69
- SamlGroupName : & samlGroupName ,
70
- }
71
-
72
- if force {
73
- if err := resourceGitlabGroupSamlLinkDelete (ctx , d , meta ); err != nil {
74
- return err
75
- }
67
+ AccessLevel : gitlab .String (accessLevel ),
68
+ SamlGroupName : gitlab .String (samlGroupName ),
76
69
}
77
70
78
71
log .Printf ("[DEBUG] Create GitLab group SamlLink %s" , d .Id ())
79
- SamlLink , _ , err := client .Groups .AddGroupSAMLLink (groupId , options , gitlab .WithContext (ctx ))
72
+ SamlLink , _ , err := client .Groups .AddGroupSAMLLink (group , options , gitlab .WithContext (ctx ))
80
73
if err != nil {
81
74
return diag .FromErr (err )
82
75
}
83
76
84
- d .SetId (buildTwoPartID (& groupId , & SamlLink .Name ))
77
+ d .SetId (buildTwoPartID (& group , & SamlLink .Name ))
85
78
86
79
return resourceGitlabGroupSamlLinkRead (ctx , d , meta )
87
80
}
88
81
89
82
func resourceGitlabGroupSamlLinkRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
90
83
client := meta .(* gitlab.Client )
91
- groupId := d .Get ("group_id" ).(string )
84
+ group , samlGroupName , parse_err := parseTwoPartID (d .Id ())
85
+ if parse_err != nil {
86
+ return diag .FromErr (parse_err )
87
+ }
92
88
93
89
// Try to fetch all group links from GitLab
94
- log .Printf ("[DEBUG] Read GitLab group SamlLinks %s" , groupId )
95
- samlLinks , _ , err := client .Groups .ListGroupSAMLLinks (groupId , nil , gitlab .WithContext (ctx ))
90
+ log .Printf ("[DEBUG] Read GitLab group SamlLinks %s" , group )
91
+ samlLinks , _ , err := client .Groups .ListGroupSAMLLinks (group , nil , gitlab .WithContext (ctx ))
96
92
if err != nil {
97
93
return diag .FromErr (err )
98
94
}
99
95
100
- // If we got here and don't have links, assume GitLab is below version 12.8 and skip the check
101
96
if samlLinks != nil {
102
- // Check if the LDAP link exists in the returned list of links
97
+ // Check if the SAML link exists in the returned list of links
103
98
found := false
104
99
for _ , samlLink := range samlLinks {
105
- if buildTwoPartID ( & groupId , & samlLink .Name ) == d . Id () {
106
- d .Set ("group_id " , groupId )
100
+ if samlLink .Name == samlGroupName {
101
+ d .Set ("group " , group )
107
102
d .Set ("access_level" , samlLink .AccessLevel )
108
103
d .Set ("saml_group_name" , samlLink .Name )
109
104
found = true
@@ -112,8 +107,9 @@ func resourceGitlabGroupSamlLinkRead(ctx context.Context, d *schema.ResourceData
112
107
}
113
108
114
109
if ! found {
110
+ log .Printf ("[DEBUG] GitLab SAML Group Link %d, group ID %s not found, removing from state" , samlGroupName , group )
115
111
d .SetId ("" )
116
- return diag . Errorf ( "SamlLink %s does not exist." , d . Id ())
112
+ return nil
117
113
}
118
114
}
119
115
@@ -122,42 +118,20 @@ func resourceGitlabGroupSamlLinkRead(ctx context.Context, d *schema.ResourceData
122
118
123
119
func resourceGitlabGroupSamlLinkDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
124
120
client := meta .(* gitlab.Client )
125
- groupId := d .Get ("group_id" ).(string )
126
- samlGroupName := d .Get ("saml_group_name" ).(string )
121
+ group , samlGroupName , parse_err := parseTwoPartID (d .Id ())
122
+ if parse_err != nil {
123
+ return diag .FromErr (parse_err )
124
+ }
127
125
128
126
log .Printf ("[DEBUG] Delete GitLab group SamlLink %s" , d .Id ())
129
- _ , err := client .Groups .DeleteGroupSAMLLink (groupId , samlGroupName , cn , gitlab .WithContext (ctx ))
127
+ _ , err := client .Groups .DeleteGroupSAMLLink (group , samlGroupName , gitlab .WithContext (ctx ))
130
128
if err != nil {
131
- switch err .(type ) { // nolint // TODO: Resolve this golangci-lint issue: S1034: assigning the result of this type assertion to a variable (switch err := err.(type)) could eliminate type assertions in switch cases (gosimple)
132
- case * gitlab.ErrorResponse :
133
- // Ignore SAML links that don't exist
134
- if strings .Contains (string (err .(* gitlab.ErrorResponse ).Message ), "Linked SAML group not found" ) { // nolint // TODO: Resolve this golangci-lint issue: S1034(related information): could eliminate this type assertion (gosimple)
135
- log .Printf ("[WARNING] %s" , err )
136
- } else {
137
- return diag .FromErr (err )
138
- }
139
- default :
129
+ if is404 (err ) {
130
+ log .Printf ("[WARNING] %s" , err )
131
+ } else {
140
132
return diag .FromErr (err )
141
133
}
142
134
}
143
135
144
136
return nil
145
137
}
146
-
147
- func resourceGitlabGroupSamlLinkImporter (ctx context.Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
148
- parts := strings .SplitN (d .Id (), ":" , 2 )
149
- if len (parts ) != 2 {
150
- return nil , fmt .Errorf ("invalid saml link import id (should be <group id>:<saml group name>): %s" , d .Id ())
151
- }
152
-
153
- groupId , samlGroupName := parts [0 ], parts [1 ]
154
- d .SetId (buildTwoPartID (& groupId , & samlGroupName ))
155
- d .Set ("group_id" , groupId )
156
- d .Set ("force" , false )
157
-
158
- diag := resourceGitlabGroupSamlLinkRead (ctx , d , meta )
159
- if diag .HasError () {
160
- return nil , fmt .Errorf ("%s" , diag [0 ].Summary )
161
- }
162
- return []* schema.ResourceData {d }, nil
163
- }
0 commit comments