55 "fmt"
66 "strings"
77
8- "github.com/google/uuid"
9-
108 dtrack "github.com/DependencyTrack/client-go"
119 "github.com/hashicorp/terraform-plugin-framework/path"
1210 "github.com/hashicorp/terraform-plugin-framework/resource"
@@ -78,21 +76,13 @@ func (r *aclMappingResource) Create(ctx context.Context, req resource.CreateRequ
7876 if resp .Diagnostics .HasError () {
7977 return
8078 }
81- team , err := uuid .Parse (plan .Team .ValueString ())
82- if err != nil {
83- resp .Diagnostics .AddAttributeError (
84- path .Root ("team" ),
85- "Within Create, unable to parse team into UUID" ,
86- "Error from: " + err .Error (),
87- )
79+ team , diag := TryParseUUID (plan .Team , LifecycleCreate , path .Root ("team" ))
80+ if diag != nil {
81+ resp .Diagnostics .Append (diag )
8882 }
89- project , err := uuid .Parse (plan .Project .ValueString ())
90- if err != nil {
91- resp .Diagnostics .AddAttributeError (
92- path .Root ("project" ),
93- "Within Create, unable to parse project into UUID" ,
94- "Error from: " + err .Error (),
95- )
83+ project , diag := TryParseUUID (plan .Project , LifecycleCreate , path .Root ("project" ))
84+ if diag != nil {
85+ resp .Diagnostics .Append (diag )
9686 }
9787 if resp .Diagnostics .HasError () {
9888 return
@@ -103,7 +93,7 @@ func (r *aclMappingResource) Create(ctx context.Context, req resource.CreateRequ
10393 Project : project ,
10494 }
10595 tflog .Debug (ctx , "Granting ACL for project " + mappingReq .Project .String ()+ " to team " + mappingReq .Team .String ())
106- err = r .client .ACL .AddProjectMapping (ctx , mappingReq )
96+ err : = r .client .ACL .AddProjectMapping (ctx , mappingReq )
10797
10898 if err != nil {
10999 resp .Diagnostics .AddError (
@@ -137,21 +127,13 @@ func (r *aclMappingResource) Read(ctx context.Context, req resource.ReadRequest,
137127 tflog .Debug (ctx , "Refreshing acl mapping for team: " + state .Team .ValueString ()+ ", and project: " + state .Project .ValueString ())
138128
139129 // Refresh
140- team , err := uuid .Parse (state .Team .ValueString ())
141- if err != nil {
142- resp .Diagnostics .AddAttributeError (
143- path .Root ("team" ),
144- "Within Read, unable to parse team into UUID" ,
145- "Error from: " + err .Error (),
146- )
130+ team , diag := TryParseUUID (state .Team , LifecycleRead , path .Root ("team" ))
131+ if diag != nil {
132+ resp .Diagnostics .Append (diag )
147133 }
148- projectId , err := uuid .Parse (state .Project .ValueString ())
149- if err != nil {
150- resp .Diagnostics .AddAttributeError (
151- path .Root ("project" ),
152- "Within Read, unable to parse project into UUID" ,
153- "Error from: " + err .Error (),
154- )
134+ projectId , diag := TryParseUUID (state .Project , LifecycleRead , path .Root ("project" ))
135+ if diag != nil {
136+ resp .Diagnostics .Append (diag )
155137 }
156138 if resp .Diagnostics .HasError () {
157139 return
@@ -194,21 +176,13 @@ func (r *aclMappingResource) Update(ctx context.Context, req resource.UpdateRequ
194176 return
195177 }
196178
197- team , err := uuid .Parse (plan .Team .ValueString ())
198- if err != nil {
199- resp .Diagnostics .AddAttributeError (
200- path .Root ("team" ),
201- "Within Update, unable to parse team into UUID" ,
202- "Error from: " + err .Error (),
203- )
179+ team , diag := TryParseUUID (plan .Team , LifecycleUpdate , path .Root ("team" ))
180+ if diag != nil {
181+ resp .Diagnostics .Append (diag )
204182 }
205- project , err := uuid .Parse (plan .Project .ValueString ())
206- if err != nil {
207- resp .Diagnostics .AddAttributeError (
208- path .Root ("project" ),
209- "Within Update, unable to parse project into UUID" ,
210- "Error from: " + err .Error (),
211- )
183+ project , diag := TryParseUUID (plan .Project , LifecycleUpdate , path .Root ("project" ))
184+ if diag != nil {
185+ resp .Diagnostics .Append (diag )
212186 }
213187 if resp .Diagnostics .HasError () {
214188 return
@@ -239,29 +213,21 @@ func (r *aclMappingResource) Delete(ctx context.Context, req resource.DeleteRequ
239213 }
240214
241215 // Map TF to SDK
242- team , err := uuid .Parse (state .Team .ValueString ())
243- if err != nil {
244- resp .Diagnostics .AddAttributeError (
245- path .Root ("team" ),
246- "Within Delete, unable to parse team into UUID" ,
247- "Error from: " + err .Error (),
248- )
216+ team , diag := TryParseUUID (state .Team , LifecycleDelete , path .Root ("team" ))
217+ if diag != nil {
218+ resp .Diagnostics .Append (diag )
249219 }
250- project , err := uuid .Parse (state .Project .ValueString ())
251- if err != nil {
252- resp .Diagnostics .AddAttributeError (
253- path .Root ("project" ),
254- "Within Delete, unable to parse project into UUID" ,
255- "Error from: " + err .Error (),
256- )
220+ project , diag := TryParseUUID (state .Project , LifecycleDelete , path .Root ("project" ))
221+ if diag != nil {
222+ resp .Diagnostics .Append (diag )
257223 }
258224 if resp .Diagnostics .HasError () {
259225 return
260226 }
261227
262228 // Execute
263229 tflog .Debug (ctx , "Deleting acl mapping for project with id: " + state .Project .ValueString ()+ ", and team with id: " + state .Team .ValueString ())
264- err = r .client .ACL .RemoveProjectMapping (ctx , team , project )
230+ err : = r .client .ACL .RemoveProjectMapping (ctx , team , project )
265231 if err != nil {
266232 resp .Diagnostics .AddError (
267233 "Unable to delete acl mapping" ,
@@ -284,19 +250,16 @@ func (r *aclMappingResource) ImportState(ctx context.Context, req resource.Impor
284250 teamIdString := idParts [0 ]
285251 projectIdString := idParts [1 ]
286252
287- teamId , err := uuid .Parse (teamIdString )
288- if err != nil {
289- resp .Diagnostics .AddError (
290- "Within Import, unable to parse team into UUID" ,
291- "Error from: " + err .Error (),
292- )
253+ teamId , diag := TryParseUUID (types .StringValue (teamIdString ), LifecycleImport , path .Root ("team" ))
254+ if diag != nil {
255+ resp .Diagnostics .Append (diag )
293256 }
294- projectId , err := uuid . Parse (projectIdString )
295- if err != nil {
296- resp .Diagnostics .AddError (
297- "Within Import, unable to parse project into UUID" ,
298- "Error from: " + err . Error (),
299- )
257+ projectId , diag := TryParseUUID ( types . StringValue (projectIdString ), LifecycleImport , path . Root ( "project" ) )
258+ if diag != nil {
259+ resp .Diagnostics .Append ( diag )
260+ }
261+ if resp . Diagnostics . HasError () {
262+ return
300263 }
301264 aclMappingState := aclMappingResourceModel {
302265 ID : types .StringValue (fmt .Sprintf ("%s/%s" , teamId .String (), projectId .String ())),
0 commit comments