@@ -2,6 +2,7 @@ package sentry
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"net/http"
6
7
7
8
"github.com/hashicorp/go-multierror"
@@ -31,19 +32,20 @@ func resourceSentryProject() *schema.Resource {
31
32
Required : true ,
32
33
},
33
34
"team" : {
34
- Description : "The slug of the team to create the project for. One of 'team' or 'teams' must be set." ,
35
- Type : schema .TypeString ,
36
- Deprecated : "To be replaced by 'teams' in a future release." ,
37
- Optional : true ,
35
+ Description : "The slug of the team to create the project for. **Deprecated** Use `teams` instead." ,
36
+ Type : schema .TypeString ,
37
+ Deprecated : "Use `teams` instead." ,
38
+ ConflictsWith : []string {"teams" },
39
+ Optional : true ,
38
40
},
39
41
"teams" : {
40
- Description : "The slugs of the teams to create the project for. One of 'team' or 'teams' must be set. " ,
42
+ Description : "The slugs of the teams to create the project for." ,
41
43
Type : schema .TypeSet ,
42
44
Elem : & schema.Schema {
43
45
Type : schema .TypeString ,
44
46
},
45
- ExactlyOneOf : []string {"team" },
46
- Optional : true ,
47
+ ConflictsWith : []string {"team" },
48
+ Optional : true ,
47
49
},
48
50
"name" : {
49
51
Description : "The name for the project." ,
@@ -124,32 +126,42 @@ func resourceSentryProjectCreate(ctx context.Context, d *schema.ResourceData, me
124
126
client := meta .(* sentry.Client )
125
127
126
128
org := d .Get ("organization" ).(string )
127
- team := d .Get ("team" ).(string )
128
- var teams []interface {}
129
- if team == "" {
129
+
130
+ team , teamOk := d .GetOk ("team" )
131
+ teams , teamsOk := d .GetOk ("teams" )
132
+ if ! teamOk && ! teamsOk {
133
+ return diag .FromErr (errors .New ("one of team or teams must be configured" ))
134
+ }
135
+
136
+ var initialTeam string
137
+ if teamOk {
138
+ initialTeam = team .(string )
139
+ } else {
130
140
// Since `Set.List()` produces deterministic ordering, `teams[0]` should always
131
141
// resolve to the same value given the same `teams`.
132
- teams = d . Get ( "teams" ).( * schema. Set ). List ()
133
- team = teams [0 ].(string )
142
+ // Pick the first team when creating the project.
143
+ initialTeam = teams .( * schema. Set ). List () [0 ].(string )
134
144
}
145
+
135
146
params := & sentry.CreateProjectParams {
136
147
Name : d .Get ("name" ).(string ),
137
148
Slug : d .Get ("slug" ).(string ),
138
149
}
139
150
140
151
tflog .Debug (ctx , "Creating Sentry project" , map [string ]interface {}{
141
- "team" : team ,
142
- "teams" : teams ,
143
- "org" : org ,
152
+ "team" : team ,
153
+ "teams" : teams ,
154
+ "org" : org ,
155
+ "initialTeam" : initialTeam ,
144
156
})
145
- proj , _ , err := client .Projects .Create (ctx , org , team , params )
157
+ proj , _ , err := client .Projects .Create (ctx , org , initialTeam , params )
146
158
if err != nil {
147
159
return diag .FromErr (err )
148
160
}
149
161
tflog .Debug (ctx , "Created Sentry project" , map [string ]interface {}{
150
162
"projectSlug" : proj .Slug ,
151
163
"projectID" : proj .ID ,
152
- "team" : team ,
164
+ "team" : initialTeam ,
153
165
"org" : org ,
154
166
})
155
167
@@ -177,31 +189,11 @@ func resourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta
177
189
"org" : org ,
178
190
})
179
191
180
- setTeams := func () error {
181
- if len (proj .Teams ) <= 1 {
182
- return multierror .Append (
183
- d .Set ("team" , proj .Team .Slug ),
184
- d .Set ("teams" , nil ),
185
- )
186
- }
187
-
188
- teams := make ([]string , len (proj .Teams ))
189
- for i , team := range proj .Teams {
190
- teams [i ] = * team .Slug
191
- }
192
-
193
- return multierror .Append (
194
- d .Set ("team" , nil ),
195
- d .Set ("teams" , teams ),
196
- )
197
- }
198
-
199
192
d .SetId (proj .Slug )
200
193
retErr := multierror .Append (
201
194
d .Set ("organization" , proj .Organization .Slug ),
202
195
d .Set ("name" , proj .Name ),
203
196
d .Set ("slug" , proj .Slug ),
204
- setTeams (),
205
197
d .Set ("platform" , proj .Platform ),
206
198
d .Set ("internal_id" , proj .ID ),
207
199
d .Set ("is_public" , proj .IsPublic ),
@@ -213,6 +205,15 @@ func resourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta
213
205
d .Set ("resolve_age" , proj .ResolveAge ),
214
206
d .Set ("project_id" , proj .ID ), // Deprecated
215
207
)
208
+ if _ , ok := d .GetOk ("team" ); ok {
209
+ retErr = multierror .Append (retErr , d .Set ("team" , proj .Team .Slug ))
210
+ } else {
211
+ teams := make ([]string , 0 , len (proj .Teams ))
212
+ for _ , team := range proj .Teams {
213
+ teams = append (teams , * team .Slug )
214
+ }
215
+ retErr = multierror .Append (retErr , d .Set ("teams" , flattenStringSet (teams )))
216
+ }
216
217
217
218
// TODO: Project options
218
219
@@ -285,35 +286,37 @@ func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, me
285
286
286
287
// Ensure old teams and new teams do not overlap.
287
288
for newTeam := range newTeams {
288
- if _ , exists := oldTeams [newTeam ]; exists {
289
- delete (oldTeams , newTeam )
290
- }
289
+ delete (oldTeams , newTeam )
291
290
}
292
291
293
- tflog .Debug (ctx , "Adding teams to project" , map [string ]interface {}{
294
- "org" : org ,
295
- "project" : project ,
296
- "teamsToAdd" : newTeams ,
297
- })
292
+ if len (newTeams ) > 0 {
293
+ tflog .Debug (ctx , "Adding teams to project" , map [string ]interface {}{
294
+ "org" : org ,
295
+ "project" : project ,
296
+ "teamsToAdd" : newTeams ,
297
+ })
298
298
299
- for newTeam := range newTeams {
300
- _ , _ , err = client .Projects .AddTeam (ctx , org , project , newTeam )
301
- if err != nil {
302
- return diag .FromErr (err )
299
+ for newTeam := range newTeams {
300
+ _ , _ , err = client .Projects .AddTeam (ctx , org , project , newTeam )
301
+ if err != nil {
302
+ return diag .FromErr (err )
303
+ }
303
304
}
304
305
}
305
306
306
- tflog .Debug (ctx , "Removing teams from project" , map [string ]interface {}{
307
- "org" : org ,
308
- "project" : project ,
309
- "teamsToRemove" : oldTeams ,
310
- })
311
-
312
- for oldTeam := range oldTeams {
313
- resp , err := client .Projects .RemoveTeam (ctx , org , project , oldTeam )
314
- if err != nil {
315
- if resp .Response .StatusCode != http .StatusNotFound {
316
- return diag .FromErr (err )
307
+ if len (oldTeams ) > 0 {
308
+ tflog .Debug (ctx , "Removing teams from project" , map [string ]interface {}{
309
+ "org" : org ,
310
+ "project" : project ,
311
+ "teamsToRemove" : oldTeams ,
312
+ })
313
+
314
+ for oldTeam := range oldTeams {
315
+ resp , err := client .Projects .RemoveTeam (ctx , org , project , oldTeam )
316
+ if err != nil {
317
+ if resp .Response .StatusCode != http .StatusNotFound {
318
+ return diag .FromErr (err )
319
+ }
317
320
}
318
321
}
319
322
}
0 commit comments