Skip to content

Commit 2d6b08c

Browse files
authored
Fix Deprecated SDK Function Usage (#382)
* Remove usage of deprecated ValidateRFC3339TimeString function * Remove usage of deprecated Partial and SetPartial methods Since Terraform detects drift on its own, these methods are not providing value * Remove redundant archived state checks since Terraform detects drift on read
1 parent 72e14c5 commit 2d6b08c

File tree

2 files changed

+11
-133
lines changed

2 files changed

+11
-133
lines changed

gitlab/resource_gitlab_deploy_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func resourceGitlabDeployToken() *schema.Resource {
4545
"expires_at": {
4646
Type: schema.TypeString,
4747
Optional: true,
48-
ValidateFunc: validation.ValidateRFC3339TimeString,
48+
ValidateFunc: validation.IsRFC3339Time,
4949
ForceNew: true,
5050
},
5151
"scopes": {

gitlab/resource_gitlab_project.go

Lines changed: 10 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -271,57 +271,28 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
271271
RemoveSourceBranchAfterMerge: gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool)),
272272
}
273273

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-
297274
if v, ok := d.GetOk("path"); ok {
298275
options.Path = gitlab.String(v.(string))
299-
setProperties = append(setProperties, "path")
300276
}
301277

302278
if v, ok := d.GetOk("namespace_id"); ok {
303279
options.NamespaceID = gitlab.Int(v.(int))
304-
setProperties = append(setProperties, "namespace_id")
305280
}
306281

307282
if v, ok := d.GetOk("description"); ok {
308283
options.Description = gitlab.String(v.(string))
309-
setProperties = append(setProperties, "description")
310284
}
311285

312286
if v, ok := d.GetOk("tags"); ok {
313287
options.TagList = stringSetToStringSlice(v.(*schema.Set))
314-
setProperties = append(setProperties, "tags")
315288
}
316289

317290
if v, ok := d.GetOk("initialize_with_readme"); ok {
318291
options.InitializeWithReadme = gitlab.Bool(v.(bool))
319-
setProperties = append(setProperties, "initialize_with_readme")
320292
}
321293

322294
if v, ok := d.GetOk("import_url"); ok {
323295
options.ImportURL = gitlab.String(v.(string))
324-
setProperties = append(setProperties, "import_url")
325296
}
326297

327298
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
@@ -331,11 +302,6 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
331302
return err
332303
}
333304

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-
339305
// from this point onwards no matter how we return, resource creation
340306
// is committed to state since we set its ID
341307
d.SetId(fmt.Sprintf("%d", project.ID))
@@ -368,23 +334,15 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
368334
return err
369335
}
370336
}
371-
d.SetPartial("shared_with_groups")
372337
}
373338

374-
v := d.Get("archived")
375-
if v.(bool) {
339+
if d.Get("archived").(bool) {
376340
// 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)
381343
}
382-
d.SetPartial(("archived"))
383344
}
384345

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)
388346
return resourceGitlabProjectRead(d, meta)
389347
}
390348

@@ -412,114 +370,88 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
412370
options := &gitlab.EditProjectOptions{}
413371
transferOptions := &gitlab.TransferProjectOptions{}
414372

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-
420373
if d.HasChange("name") {
421374
options.Name = gitlab.String(d.Get("name").(string))
422-
updatedProperties = append(updatedProperties, "name")
423375
}
424376

425377
if d.HasChange("path") && (d.Get("path").(string) != "") {
426378
options.Path = gitlab.String(d.Get("path").(string))
427-
updatedProperties = append(updatedProperties, "path")
428379
}
429380

430381
if d.HasChange("namespace_id") {
431382
transferOptions.Namespace = gitlab.Int(d.Get("namespace_id").(int))
432-
updatedProperties = append(updatedProperties, "namespace_id")
433383
}
434384

435385
if d.HasChange("description") {
436386
options.Description = gitlab.String(d.Get("description").(string))
437-
updatedProperties = append(updatedProperties, "description")
438387
}
439388

440389
if d.HasChange("default_branch") {
441390
options.DefaultBranch = gitlab.String(d.Get("default_branch").(string))
442-
updatedProperties = append(updatedProperties, "default_branch")
443391
}
444392

445393
if d.HasChange("visibility_level") {
446394
options.Visibility = stringToVisibilityLevel(d.Get("visibility_level").(string))
447-
updatedProperties = append(updatedProperties, "visibility_level")
448395
}
449396

450397
if d.HasChange("merge_method") {
451398
options.MergeMethod = stringToMergeMethod(d.Get("merge_method").(string))
452-
updatedProperties = append(updatedProperties, "merge_method")
453399
}
454400

455401
if d.HasChange("only_allow_merge_if_pipeline_succeeds") {
456402
options.OnlyAllowMergeIfPipelineSucceeds = gitlab.Bool(d.Get("only_allow_merge_if_pipeline_succeeds").(bool))
457-
updatedProperties = append(updatedProperties, "only_allow_merge_if_pipeline_succeeds")
458403
}
459404

460405
if d.HasChange("only_allow_merge_if_all_discussions_are_resolved") {
461406
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")
463407
}
464408

465409
if d.HasChange("request_access_enabled") {
466410
options.RequestAccessEnabled = gitlab.Bool(d.Get("request_access_enabled").(bool))
467-
updatedProperties = append(updatedProperties, "request_access_enabled")
468411
}
469412

470413
if d.HasChange("issues_enabled") {
471414
options.IssuesEnabled = gitlab.Bool(d.Get("issues_enabled").(bool))
472-
updatedProperties = append(updatedProperties, "issues_enabled")
473415
}
474416

475417
if d.HasChange("merge_requests_enabled") {
476418
options.MergeRequestsEnabled = gitlab.Bool(d.Get("merge_requests_enabled").(bool))
477-
updatedProperties = append(updatedProperties, "merge_requests_enabled")
478419
}
479420

480421
if d.HasChange("pipelines_enabled") {
481422
options.JobsEnabled = gitlab.Bool(d.Get("pipelines_enabled").(bool))
482-
updatedProperties = append(updatedProperties, "pipelines_enabled")
483423
}
484424

485425
if d.HasChange("approvals_before_merge") {
486426
options.ApprovalsBeforeMerge = gitlab.Int(d.Get("approvals_before_merge").(int))
487-
updatedProperties = append(updatedProperties, "approvals_before_merge")
488427
}
489428

490429
if d.HasChange("wiki_enabled") {
491430
options.WikiEnabled = gitlab.Bool(d.Get("wiki_enabled").(bool))
492-
updatedProperties = append(updatedProperties, "wiki_enabled")
493431
}
494432

495433
if d.HasChange("snippets_enabled") {
496434
options.SnippetsEnabled = gitlab.Bool(d.Get("snippets_enabled").(bool))
497-
updatedProperties = append(updatedProperties, "snippets_enabled")
498435
}
499436

500437
if d.HasChange("shared_runners_enabled") {
501438
options.SharedRunnersEnabled = gitlab.Bool(d.Get("shared_runners_enabled").(bool))
502-
updatedProperties = append(updatedProperties, "shared_runners_enabled")
503439
}
504440

505441
if d.HasChange("tags") {
506442
options.TagList = stringSetToStringSlice(d.Get("tags").(*schema.Set))
507-
updatedProperties = append(updatedProperties, "tags")
508443
}
509444

510445
if d.HasChange("container_registry_enabled") {
511446
options.ContainerRegistryEnabled = gitlab.Bool(d.Get("container_registry_enabled").(bool))
512-
updatedProperties = append(updatedProperties, "container_registry_enabled")
513447
}
514448

515449
if d.HasChange("lfs_enabled") {
516450
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
517-
updatedProperties = append(updatedProperties, "lfs_enabled")
518451
}
519452

520453
if d.HasChange("remove_source_branch_after_merge") {
521454
options.RemoveSourceBranchAfterMerge = gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool))
522-
updatedProperties = append(updatedProperties, "remove_source_branch_after_merge")
523455
}
524456

525457
if *options != (gitlab.EditProjectOptions{}) {
@@ -528,10 +460,6 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
528460
if err != nil {
529461
return err
530462
}
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-
}
535463
}
536464

537465
if *transferOptions != (gitlab.TransferProjectOptions{}) {
@@ -540,38 +468,26 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
540468
if err != nil {
541469
return err
542470
}
543-
log.Printf("[DEBUG] partial gitlab project %s update of property namespace_id", d.Id())
544-
d.SetPartial("namespace_id")
545471
}
546472

547473
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
553476
}
554477
}
555478

556479
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)
563483
}
564484
} 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)
569487
}
570488
}
571-
d.SetPartial("archived")
572489
}
573490

574-
d.Partial(false)
575491
return resourceGitlabProjectRead(d, meta)
576492
}
577493

@@ -734,41 +650,3 @@ func updateSharedWithGroups(d *schema.ResourceData, meta interface{}) error {
734650

735651
return nil
736652
}
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

Comments
 (0)