|
4 | 4 | "context" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | | - "strconv" |
8 | 7 | "strings" |
9 | 8 | "time" |
10 | 9 |
|
@@ -481,6 +480,7 @@ func flattenTldOffers(offers map[string]*domain.TldOffer) []map[string]interface |
481 | 480 |
|
482 | 481 | return flattenedOffers |
483 | 482 | } |
| 483 | + |
484 | 484 | func flattenExternalDomainRegistrationStatus(status *domain.DomainRegistrationStatusExternalDomain) map[string]interface{} { |
485 | 485 | if status == nil { |
486 | 486 | return nil |
@@ -590,6 +590,118 @@ func waitForTaskCompletion(ctx context.Context, registrarAPI *domain.RegistrarAP |
590 | 590 | return retry.NonRetryableError(fmt.Errorf("unexpected task status: %v", status)) |
591 | 591 | }) |
592 | 592 | } |
| 593 | +func waitForUpdateDomainTaskCompletion(ctx context.Context, registrarAPI *domain.RegistrarAPI, domainName string, duration int) ([]*domain.Task, error) { |
| 594 | + timeout := time.Duration(duration) * time.Second |
| 595 | + var completedTasks []*domain.Task |
| 596 | + |
| 597 | + err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { |
| 598 | + tasks, err := registrarAPI.ListTasks(&domain.RegistrarAPIListTasksRequest{ |
| 599 | + Domain: &domainName, |
| 600 | + }, scw.WithContext(ctx), scw.WithAllPages()) |
| 601 | + if err != nil { |
| 602 | + return retry.NonRetryableError(fmt.Errorf("failed to list tasks: %w", err)) |
| 603 | + } |
| 604 | + |
| 605 | + allSuccess := true |
| 606 | + completedTasks = tasks.Tasks |
| 607 | + for _, task := range tasks.Tasks { |
| 608 | + if task.Type == domain.TaskTypeUpdateDomain { |
| 609 | + if task.Status != domain.TaskStatusSuccess { |
| 610 | + allSuccess = false |
| 611 | + if task.Status == domain.TaskStatusPending { |
| 612 | + return retry.RetryableError(errors.New("update_domain task is still pending, retrying")) |
| 613 | + } |
| 614 | + } |
| 615 | + } |
| 616 | + } |
| 617 | + |
| 618 | + if allSuccess { |
| 619 | + return nil |
| 620 | + } |
| 621 | + |
| 622 | + return retry.RetryableError(errors.New("not all update_domain tasks are successful, retrying")) |
| 623 | + }) |
| 624 | + |
| 625 | + if err != nil { |
| 626 | + return nil, err |
| 627 | + } |
| 628 | + |
| 629 | + return completedTasks, nil |
| 630 | +} |
| 631 | + |
| 632 | +func ExpandDSRecord(dsRecordList []interface{}) *domain.DSRecord { |
| 633 | + if len(dsRecordList) == 0 || dsRecordList[0] == nil { |
| 634 | + return nil |
| 635 | + } |
| 636 | + |
| 637 | + dsRecordMap := dsRecordList[0].(map[string]interface{}) |
| 638 | + dsRecord := &domain.DSRecord{ |
| 639 | + KeyID: uint32(dsRecordMap["key_id"].(int)), |
| 640 | + Algorithm: domain.DSRecordAlgorithm(dsRecordMap["algorithm"].(string)), |
| 641 | + } |
| 642 | + |
| 643 | + if digestList, ok := dsRecordMap["digest"].([]interface{}); ok && len(digestList) > 0 { |
| 644 | + digestMap := digestList[0].(map[string]interface{}) |
| 645 | + dsRecord.Digest = &domain.DSRecordDigest{ |
| 646 | + Type: domain.DSRecordDigestType(digestMap["type"].(string)), |
| 647 | + Digest: digestMap["digest"].(string), |
| 648 | + } |
| 649 | + |
| 650 | + if publicKeyList, ok := digestMap["public_key"].([]interface{}); ok && len(publicKeyList) > 0 { |
| 651 | + publicKeyMap := publicKeyList[0].(map[string]interface{}) |
| 652 | + dsRecord.Digest.PublicKey = &domain.DSRecordPublicKey{ |
| 653 | + Key: publicKeyMap["key"].(string), |
| 654 | + } |
| 655 | + } |
| 656 | + } |
| 657 | + |
| 658 | + if publicKeyList, ok := dsRecordMap["public_key"].([]interface{}); ok && len(publicKeyList) > 0 { |
| 659 | + publicKeyMap := publicKeyList[0].(map[string]interface{}) |
| 660 | + dsRecord.PublicKey = &domain.DSRecordPublicKey{ |
| 661 | + Key: publicKeyMap["key"].(string), |
| 662 | + } |
| 663 | + } |
| 664 | + |
| 665 | + return dsRecord |
| 666 | +} |
| 667 | + |
| 668 | +func FlattenDSRecord(dsRecord *domain.DSRecord) []map[string]interface{} { |
| 669 | + if dsRecord == nil { |
| 670 | + return nil |
| 671 | + } |
| 672 | + |
| 673 | + result := map[string]interface{}{ |
| 674 | + "key_id": dsRecord.KeyID, |
| 675 | + "algorithm": string(dsRecord.Algorithm), |
| 676 | + } |
| 677 | + |
| 678 | + if dsRecord.Digest != nil { |
| 679 | + digest := map[string]interface{}{ |
| 680 | + "type": string(dsRecord.Digest.Type), |
| 681 | + "digest": dsRecord.Digest.Digest, |
| 682 | + } |
| 683 | + |
| 684 | + if dsRecord.Digest.PublicKey != nil { |
| 685 | + digest["public_key"] = []map[string]interface{}{ |
| 686 | + { |
| 687 | + "key": dsRecord.Digest.PublicKey.Key, |
| 688 | + }, |
| 689 | + } |
| 690 | + } |
| 691 | + |
| 692 | + result["digest"] = []map[string]interface{}{digest} |
| 693 | + } |
| 694 | + |
| 695 | + if dsRecord.PublicKey != nil { |
| 696 | + result["public_key"] = []map[string]interface{}{ |
| 697 | + { |
| 698 | + "key": dsRecord.PublicKey.Key, |
| 699 | + }, |
| 700 | + } |
| 701 | + } |
| 702 | + |
| 703 | + return []map[string]interface{}{result} |
| 704 | +} |
593 | 705 |
|
594 | 706 | func ExpandDSRecord(dsRecordList []interface{}) *domain.DSRecord { |
595 | 707 | if len(dsRecordList) == 0 || dsRecordList[0] == nil { |
|
0 commit comments