@@ -271,57 +271,28 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
271
271
RemoveSourceBranchAfterMerge : gitlab .Bool (d .Get ("remove_source_branch_after_merge" ).(bool )),
272
272
}
273
273
274
- // need to manage partial state since project creation may require
275
- // more than a single API call, and they may all fail independently;
276
- // the default set of attributes is prepopulated with those used above
277
- d .Partial (true )
278
- setProperties := []string {
279
- "name" ,
280
- "request_access_enabled" ,
281
- "issues_enabled" ,
282
- "merge_requests_enabled" ,
283
- "pipelines_enabled" ,
284
- "approvals_before_merge" ,
285
- "wiki_enabled" ,
286
- "snippets_enabled" ,
287
- "container_registry_enabled" ,
288
- "lfs_enabled" ,
289
- "visibility_level" ,
290
- "merge_method" ,
291
- "only_allow_merge_if_pipeline_succeeds" ,
292
- "only_allow_merge_if_all_discussions_are_resolved" ,
293
- "shared_runners_enabled" ,
294
- "remove_source_branch_after_merge" ,
295
- }
296
-
297
274
if v , ok := d .GetOk ("path" ); ok {
298
275
options .Path = gitlab .String (v .(string ))
299
- setProperties = append (setProperties , "path" )
300
276
}
301
277
302
278
if v , ok := d .GetOk ("namespace_id" ); ok {
303
279
options .NamespaceID = gitlab .Int (v .(int ))
304
- setProperties = append (setProperties , "namespace_id" )
305
280
}
306
281
307
282
if v , ok := d .GetOk ("description" ); ok {
308
283
options .Description = gitlab .String (v .(string ))
309
- setProperties = append (setProperties , "description" )
310
284
}
311
285
312
286
if v , ok := d .GetOk ("tags" ); ok {
313
287
options .TagList = stringSetToStringSlice (v .(* schema.Set ))
314
- setProperties = append (setProperties , "tags" )
315
288
}
316
289
317
290
if v , ok := d .GetOk ("initialize_with_readme" ); ok {
318
291
options .InitializeWithReadme = gitlab .Bool (v .(bool ))
319
- setProperties = append (setProperties , "initialize_with_readme" )
320
292
}
321
293
322
294
if v , ok := d .GetOk ("import_url" ); ok {
323
295
options .ImportURL = gitlab .String (v .(string ))
324
- setProperties = append (setProperties , "import_url" )
325
296
}
326
297
327
298
log .Printf ("[DEBUG] create gitlab project %q" , * options .Name )
@@ -331,11 +302,6 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
331
302
return err
332
303
}
333
304
334
- for _ , setProperty := range setProperties {
335
- log .Printf ("[DEBUG] partial gitlab project %s creation of property %q" , d .Id (), setProperty )
336
- d .SetPartial (setProperty )
337
- }
338
-
339
305
// from this point onwards no matter how we return, resource creation
340
306
// is committed to state since we set its ID
341
307
d .SetId (fmt .Sprintf ("%d" , project .ID ))
@@ -368,23 +334,15 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
368
334
return err
369
335
}
370
336
}
371
- d .SetPartial ("shared_with_groups" )
372
337
}
373
338
374
- v := d .Get ("archived" )
375
- if v .(bool ) {
339
+ if d .Get ("archived" ).(bool ) {
376
340
// strange as it may seem, this project is created in archived state...
377
- err := archiveProject (d , meta )
378
- if err != nil {
379
- log .Printf ("[WARN] New project (%s) could not be created in archived state: error %#v" , d .Id (), err )
380
- return err
341
+ if _ , _ , err := client .Projects .ArchiveProject (d .Id ()); err != nil {
342
+ return fmt .Errorf ("new project %q could not be archived: %w" , d .Id (), err )
381
343
}
382
- d .SetPartial (("archived" ))
383
344
}
384
345
385
- // everything went OK, we can revert to ordinary state management
386
- // and let the Gitlab server fill in the resource state via a read
387
- d .Partial (false )
388
346
return resourceGitlabProjectRead (d , meta )
389
347
}
390
348
@@ -412,114 +370,88 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
412
370
options := & gitlab.EditProjectOptions {}
413
371
transferOptions := & gitlab.TransferProjectOptions {}
414
372
415
- // need to manage partial state since project archiving requires
416
- // a separate API call which could fail
417
- d .Partial (true )
418
- updatedProperties := []string {}
419
-
420
373
if d .HasChange ("name" ) {
421
374
options .Name = gitlab .String (d .Get ("name" ).(string ))
422
- updatedProperties = append (updatedProperties , "name" )
423
375
}
424
376
425
377
if d .HasChange ("path" ) && (d .Get ("path" ).(string ) != "" ) {
426
378
options .Path = gitlab .String (d .Get ("path" ).(string ))
427
- updatedProperties = append (updatedProperties , "path" )
428
379
}
429
380
430
381
if d .HasChange ("namespace_id" ) {
431
382
transferOptions .Namespace = gitlab .Int (d .Get ("namespace_id" ).(int ))
432
- updatedProperties = append (updatedProperties , "namespace_id" )
433
383
}
434
384
435
385
if d .HasChange ("description" ) {
436
386
options .Description = gitlab .String (d .Get ("description" ).(string ))
437
- updatedProperties = append (updatedProperties , "description" )
438
387
}
439
388
440
389
if d .HasChange ("default_branch" ) {
441
390
options .DefaultBranch = gitlab .String (d .Get ("default_branch" ).(string ))
442
- updatedProperties = append (updatedProperties , "default_branch" )
443
391
}
444
392
445
393
if d .HasChange ("visibility_level" ) {
446
394
options .Visibility = stringToVisibilityLevel (d .Get ("visibility_level" ).(string ))
447
- updatedProperties = append (updatedProperties , "visibility_level" )
448
395
}
449
396
450
397
if d .HasChange ("merge_method" ) {
451
398
options .MergeMethod = stringToMergeMethod (d .Get ("merge_method" ).(string ))
452
- updatedProperties = append (updatedProperties , "merge_method" )
453
399
}
454
400
455
401
if d .HasChange ("only_allow_merge_if_pipeline_succeeds" ) {
456
402
options .OnlyAllowMergeIfPipelineSucceeds = gitlab .Bool (d .Get ("only_allow_merge_if_pipeline_succeeds" ).(bool ))
457
- updatedProperties = append (updatedProperties , "only_allow_merge_if_pipeline_succeeds" )
458
403
}
459
404
460
405
if d .HasChange ("only_allow_merge_if_all_discussions_are_resolved" ) {
461
406
options .OnlyAllowMergeIfAllDiscussionsAreResolved = gitlab .Bool (d .Get ("only_allow_merge_if_all_discussions_are_resolved" ).(bool ))
462
- updatedProperties = append (updatedProperties , "only_allow_merge_if_all_discussions_are_resolved" )
463
407
}
464
408
465
409
if d .HasChange ("request_access_enabled" ) {
466
410
options .RequestAccessEnabled = gitlab .Bool (d .Get ("request_access_enabled" ).(bool ))
467
- updatedProperties = append (updatedProperties , "request_access_enabled" )
468
411
}
469
412
470
413
if d .HasChange ("issues_enabled" ) {
471
414
options .IssuesEnabled = gitlab .Bool (d .Get ("issues_enabled" ).(bool ))
472
- updatedProperties = append (updatedProperties , "issues_enabled" )
473
415
}
474
416
475
417
if d .HasChange ("merge_requests_enabled" ) {
476
418
options .MergeRequestsEnabled = gitlab .Bool (d .Get ("merge_requests_enabled" ).(bool ))
477
- updatedProperties = append (updatedProperties , "merge_requests_enabled" )
478
419
}
479
420
480
421
if d .HasChange ("pipelines_enabled" ) {
481
422
options .JobsEnabled = gitlab .Bool (d .Get ("pipelines_enabled" ).(bool ))
482
- updatedProperties = append (updatedProperties , "pipelines_enabled" )
483
423
}
484
424
485
425
if d .HasChange ("approvals_before_merge" ) {
486
426
options .ApprovalsBeforeMerge = gitlab .Int (d .Get ("approvals_before_merge" ).(int ))
487
- updatedProperties = append (updatedProperties , "approvals_before_merge" )
488
427
}
489
428
490
429
if d .HasChange ("wiki_enabled" ) {
491
430
options .WikiEnabled = gitlab .Bool (d .Get ("wiki_enabled" ).(bool ))
492
- updatedProperties = append (updatedProperties , "wiki_enabled" )
493
431
}
494
432
495
433
if d .HasChange ("snippets_enabled" ) {
496
434
options .SnippetsEnabled = gitlab .Bool (d .Get ("snippets_enabled" ).(bool ))
497
- updatedProperties = append (updatedProperties , "snippets_enabled" )
498
435
}
499
436
500
437
if d .HasChange ("shared_runners_enabled" ) {
501
438
options .SharedRunnersEnabled = gitlab .Bool (d .Get ("shared_runners_enabled" ).(bool ))
502
- updatedProperties = append (updatedProperties , "shared_runners_enabled" )
503
439
}
504
440
505
441
if d .HasChange ("tags" ) {
506
442
options .TagList = stringSetToStringSlice (d .Get ("tags" ).(* schema.Set ))
507
- updatedProperties = append (updatedProperties , "tags" )
508
443
}
509
444
510
445
if d .HasChange ("container_registry_enabled" ) {
511
446
options .ContainerRegistryEnabled = gitlab .Bool (d .Get ("container_registry_enabled" ).(bool ))
512
- updatedProperties = append (updatedProperties , "container_registry_enabled" )
513
447
}
514
448
515
449
if d .HasChange ("lfs_enabled" ) {
516
450
options .LFSEnabled = gitlab .Bool (d .Get ("lfs_enabled" ).(bool ))
517
- updatedProperties = append (updatedProperties , "lfs_enabled" )
518
451
}
519
452
520
453
if d .HasChange ("remove_source_branch_after_merge" ) {
521
454
options .RemoveSourceBranchAfterMerge = gitlab .Bool (d .Get ("remove_source_branch_after_merge" ).(bool ))
522
- updatedProperties = append (updatedProperties , "remove_source_branch_after_merge" )
523
455
}
524
456
525
457
if * options != (gitlab.EditProjectOptions {}) {
@@ -528,10 +460,6 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
528
460
if err != nil {
529
461
return err
530
462
}
531
- for _ , updatedProperty := range updatedProperties {
532
- log .Printf ("[DEBUG] partial gitlab project %s update of property %q" , d .Id (), updatedProperty )
533
- d .SetPartial (updatedProperty )
534
- }
535
463
}
536
464
537
465
if * transferOptions != (gitlab.TransferProjectOptions {}) {
@@ -540,38 +468,26 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
540
468
if err != nil {
541
469
return err
542
470
}
543
- log .Printf ("[DEBUG] partial gitlab project %s update of property namespace_id" , d .Id ())
544
- d .SetPartial ("namespace_id" )
545
471
}
546
472
547
473
if d .HasChange ("shared_with_groups" ) {
548
- err := updateSharedWithGroups (d , meta )
549
- // TODO: check if handling partial state update in this simplistic
550
- // way is ok when an error in the "shared groups" API calls occurs
551
- if err != nil {
552
- d .SetPartial ("shared_with_groups" )
474
+ if err := updateSharedWithGroups (d , meta ); err != nil {
475
+ return err
553
476
}
554
477
}
555
478
556
479
if d .HasChange ("archived" ) {
557
- v := d .Get ("archived" )
558
- if v .(bool ) {
559
- err := archiveProject (d , meta )
560
- if err != nil {
561
- log .Printf ("[WARN] Project (%s) could not be archived: error %#v" , d .Id (), err )
562
- return err
480
+ if d .Get ("archived" ).(bool ) {
481
+ if _ , _ , err := client .Projects .ArchiveProject (d .Id ()); err != nil {
482
+ return fmt .Errorf ("project %q could not be archived: %w" , d .Id (), err )
563
483
}
564
484
} else {
565
- err := unarchiveProject (d , meta )
566
- if err != nil {
567
- log .Printf ("[WARN] Project (%s) could not be unarchived: error %#v" , d .Id (), err )
568
- return err
485
+ if _ , _ , err := client .Projects .UnarchiveProject (d .Id ()); err != nil {
486
+ return fmt .Errorf ("project %q could not be unarchived: %w" , d .Id (), err )
569
487
}
570
488
}
571
- d .SetPartial ("archived" )
572
489
}
573
490
574
- d .Partial (false )
575
491
return resourceGitlabProjectRead (d , meta )
576
492
}
577
493
@@ -734,41 +650,3 @@ func updateSharedWithGroups(d *schema.ResourceData, meta interface{}) error {
734
650
735
651
return nil
736
652
}
737
-
738
- // archiveProject calls the Gitlab server to archive a project; if the
739
- // project is already archived, the call will do nothing (the API is
740
- // idempotent).
741
- func archiveProject (d * schema.ResourceData , meta interface {}) error {
742
- log .Printf ("[TRACE] Project (%s) will be archived" , d .Id ())
743
- client := meta .(* gitlab.Client )
744
- out , _ , err := client .Projects .ArchiveProject (d .Id ())
745
- if err != nil {
746
- log .Printf ("[ERROR] Error archiving project (%s), received %#v" , d .Id (), err )
747
- return err
748
- }
749
- if ! out .Archived {
750
- log .Printf ("[ERROR] Project (%s) is still not archived" , d .Id ())
751
- return fmt .Errorf ("error archiving project (%s): its status on the server is still unarchived" , d .Id ())
752
- }
753
- log .Printf ("[TRACE] Project (%s) archived" , d .Id ())
754
- return nil
755
- }
756
-
757
- // unarchiveProject calls the Gitlab server to unarchive a project; if the
758
- // project is already not archived, the call will do nothing (the API is
759
- // idempotent).
760
- func unarchiveProject (d * schema.ResourceData , meta interface {}) error {
761
- log .Printf ("[INFO] Project (%s) will be unarchived" , d .Id ())
762
- client := meta .(* gitlab.Client )
763
- out , _ , err := client .Projects .UnarchiveProject (d .Id ())
764
- if err != nil {
765
- log .Printf ("[ERROR] Error unarchiving project (%s), received %#v" , d .Id (), err )
766
- return err
767
- }
768
- if out .Archived {
769
- log .Printf ("[ERROR] Project (%s) is still archived" , d .Id ())
770
- return fmt .Errorf ("error unarchiving project (%s): its status on the server is still archived" , d .Id ())
771
- }
772
- log .Printf ("[TRACE] Project (%s) unarchived" , d .Id ())
773
- return nil
774
- }
0 commit comments