|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 |
|
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
7 | 8 | "github.com/hashicorp/terraform-plugin-framework/path" |
8 | 9 | "github.com/hashicorp/terraform-plugin-framework/resource" |
9 | 10 | "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
@@ -348,6 +349,10 @@ func (r *KeyResource) Update(ctx context.Context, req resource.UpdateRequest, re |
348 | 349 | return |
349 | 350 | } |
350 | 351 |
|
| 352 | + if err := r.readKey(ctx, &data); err != nil { |
| 353 | + resp.Diagnostics.AddWarning("Read Error", fmt.Sprintf("Key updated but failed to read back: %s", err)) |
| 354 | + } |
| 355 | + |
351 | 356 | resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
352 | 357 | } |
353 | 358 |
|
@@ -619,6 +624,226 @@ func (r *KeyResource) readKey(ctx context.Context, data *KeyResourceModel) error |
619 | 624 | if budgetID, ok := result["budget_id"].(string); ok && budgetID != "" { |
620 | 625 | data.BudgetID = types.StringValue(budgetID) |
621 | 626 | } |
| 627 | + if keyAlias, ok := result["key_alias"].(string); ok && keyAlias != "" { |
| 628 | + data.KeyAlias = types.StringValue(keyAlias) |
| 629 | + } |
| 630 | + if duration, ok := result["duration"].(string); ok && duration != "" { |
| 631 | + data.Duration = types.StringValue(duration) |
| 632 | + } |
| 633 | + if tpmLimitType, ok := result["tpm_limit_type"].(string); ok && tpmLimitType != "" { |
| 634 | + data.TPMLimitType = types.StringValue(tpmLimitType) |
| 635 | + } |
| 636 | + if rpmLimitType, ok := result["rpm_limit_type"].(string); ok && rpmLimitType != "" { |
| 637 | + data.RPMLimitType = types.StringValue(rpmLimitType) |
| 638 | + } |
| 639 | + if budgetDuration, ok := result["budget_duration"].(string); ok && budgetDuration != "" { |
| 640 | + data.BudgetDuration = types.StringValue(budgetDuration) |
| 641 | + } |
| 642 | + if teamID, ok := result["team_id"].(string); ok && teamID != "" { |
| 643 | + data.TeamID = types.StringValue(teamID) |
| 644 | + } |
| 645 | + if userID, ok := result["user_id"].(string); ok && userID != "" { |
| 646 | + data.UserID = types.StringValue(userID) |
| 647 | + } |
| 648 | + if keyValue, ok := result["key"].(string); ok && keyValue != "" { |
| 649 | + data.Key = types.StringValue(keyValue) |
| 650 | + data.ID = types.StringValue(keyValue) |
| 651 | + } |
| 652 | + |
| 653 | + // Handle models list - preserve null when API returns empty and config didn't specify models |
| 654 | + if models, ok := result["models"].([]interface{}); ok && len(models) > 0 { |
| 655 | + modelsList := make([]attr.Value, 0, len(models)) |
| 656 | + for _, m := range models { |
| 657 | + if str, ok := m.(string); ok { |
| 658 | + modelsList = append(modelsList, types.StringValue(str)) |
| 659 | + } |
| 660 | + } |
| 661 | + data.Models, _ = types.ListValue(types.StringType, modelsList) |
| 662 | + } else if !data.Models.IsNull() { |
| 663 | + data.Models, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 664 | + } |
| 665 | + |
| 666 | + // Handle allowed_routes list - preserve null when API returns empty and config didn't specify allowed_routes |
| 667 | + if routes, ok := result["allowed_routes"].([]interface{}); ok && len(routes) > 0 { |
| 668 | + routesList := make([]attr.Value, 0, len(routes)) |
| 669 | + for _, r := range routes { |
| 670 | + if str, ok := r.(string); ok { |
| 671 | + routesList = append(routesList, types.StringValue(str)) |
| 672 | + } |
| 673 | + } |
| 674 | + data.AllowedRoutes, _ = types.ListValue(types.StringType, routesList) |
| 675 | + } else if !data.AllowedRoutes.IsNull() { |
| 676 | + data.AllowedRoutes, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 677 | + } |
| 678 | + |
| 679 | + // Handle allowed_passthrough_routes list - preserve null when API returns empty and config didn't specify allowed_passthrough_routes |
| 680 | + if routes, ok := result["allowed_passthrough_routes"].([]interface{}); ok && len(routes) > 0 { |
| 681 | + routesList := make([]attr.Value, 0, len(routes)) |
| 682 | + for _, r := range routes { |
| 683 | + if str, ok := r.(string); ok { |
| 684 | + routesList = append(routesList, types.StringValue(str)) |
| 685 | + } |
| 686 | + } |
| 687 | + data.AllowedPassthroughRoutes, _ = types.ListValue(types.StringType, routesList) |
| 688 | + } else if !data.AllowedPassthroughRoutes.IsNull() { |
| 689 | + data.AllowedPassthroughRoutes, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 690 | + } |
| 691 | + |
| 692 | + // Handle allowed_cache_controls list - preserve null when API returns empty and config didn't specify allowed_cache_controls |
| 693 | + if controls, ok := result["allowed_cache_controls"].([]interface{}); ok && len(controls) > 0 { |
| 694 | + controlsList := make([]attr.Value, 0, len(controls)) |
| 695 | + for _, c := range controls { |
| 696 | + if str, ok := c.(string); ok { |
| 697 | + controlsList = append(controlsList, types.StringValue(str)) |
| 698 | + } |
| 699 | + } |
| 700 | + data.AllowedCacheControls, _ = types.ListValue(types.StringType, controlsList) |
| 701 | + } else if !data.AllowedCacheControls.IsNull() { |
| 702 | + data.AllowedCacheControls, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 703 | + } |
| 704 | + |
| 705 | + // Handle guardrails list - preserve null when API returns empty and config didn't specify guardrails |
| 706 | + if guardrails, ok := result["guardrails"].([]interface{}); ok && len(guardrails) > 0 { |
| 707 | + guardrailsList := make([]attr.Value, 0, len(guardrails)) |
| 708 | + for _, g := range guardrails { |
| 709 | + if str, ok := g.(string); ok { |
| 710 | + guardrailsList = append(guardrailsList, types.StringValue(str)) |
| 711 | + } |
| 712 | + } |
| 713 | + data.Guardrails, _ = types.ListValue(types.StringType, guardrailsList) |
| 714 | + } else if !data.Guardrails.IsNull() { |
| 715 | + data.Guardrails, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 716 | + } |
| 717 | + |
| 718 | + // Handle prompts list - preserve null when API returns empty and config didn't specify prompts |
| 719 | + if prompts, ok := result["prompts"].([]interface{}); ok && len(prompts) > 0 { |
| 720 | + promptsList := make([]attr.Value, 0, len(prompts)) |
| 721 | + for _, p := range prompts { |
| 722 | + if str, ok := p.(string); ok { |
| 723 | + promptsList = append(promptsList, types.StringValue(str)) |
| 724 | + } |
| 725 | + } |
| 726 | + data.Prompts, _ = types.ListValue(types.StringType, promptsList) |
| 727 | + } else if !data.Prompts.IsNull() { |
| 728 | + data.Prompts, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 729 | + } |
| 730 | + |
| 731 | + // Handle enforced_params list - preserve null when API returns empty and config didn't specify enforced_params |
| 732 | + if enforcedParams, ok := result["enforced_params"].([]interface{}); ok && len(enforcedParams) > 0 { |
| 733 | + paramsList := make([]attr.Value, 0, len(enforcedParams)) |
| 734 | + for _, p := range enforcedParams { |
| 735 | + if str, ok := p.(string); ok { |
| 736 | + paramsList = append(paramsList, types.StringValue(str)) |
| 737 | + } |
| 738 | + } |
| 739 | + data.EnforcedParams, _ = types.ListValue(types.StringType, paramsList) |
| 740 | + } else if !data.EnforcedParams.IsNull() { |
| 741 | + data.EnforcedParams, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 742 | + } |
| 743 | + |
| 744 | + // Handle tags list - preserve null when API returns empty and config didn't specify tags |
| 745 | + if tags, ok := result["tags"].([]interface{}); ok && len(tags) > 0 { |
| 746 | + tagsList := make([]attr.Value, 0, len(tags)) |
| 747 | + for _, t := range tags { |
| 748 | + if str, ok := t.(string); ok { |
| 749 | + tagsList = append(tagsList, types.StringValue(str)) |
| 750 | + } |
| 751 | + } |
| 752 | + data.Tags, _ = types.ListValue(types.StringType, tagsList) |
| 753 | + } else if !data.Tags.IsNull() { |
| 754 | + data.Tags, _ = types.ListValue(types.StringType, []attr.Value{}) |
| 755 | + } |
| 756 | + |
| 757 | + // Handle metadata map - preserve null when API returns empty and config didn't specify metadata |
| 758 | + if metadata, ok := result["metadata"].(map[string]interface{}); ok && len(metadata) > 0 { |
| 759 | + metaMap := make(map[string]attr.Value) |
| 760 | + for k, v := range metadata { |
| 761 | + if str, ok := v.(string); ok { |
| 762 | + metaMap[k] = types.StringValue(str) |
| 763 | + } |
| 764 | + } |
| 765 | + data.Metadata, _ = types.MapValue(types.StringType, metaMap) |
| 766 | + } else if !data.Metadata.IsNull() { |
| 767 | + data.Metadata, _ = types.MapValue(types.StringType, map[string]attr.Value{}) |
| 768 | + } |
| 769 | + |
| 770 | + // Handle aliases map - preserve null when API returns empty and config didn't specify aliases |
| 771 | + if aliases, ok := result["aliases"].(map[string]interface{}); ok && len(aliases) > 0 { |
| 772 | + aliasMap := make(map[string]attr.Value) |
| 773 | + for k, v := range aliases { |
| 774 | + if str, ok := v.(string); ok { |
| 775 | + aliasMap[k] = types.StringValue(str) |
| 776 | + } |
| 777 | + } |
| 778 | + data.Aliases, _ = types.MapValue(types.StringType, aliasMap) |
| 779 | + } else if !data.Aliases.IsNull() { |
| 780 | + data.Aliases, _ = types.MapValue(types.StringType, map[string]attr.Value{}) |
| 781 | + } |
| 782 | + |
| 783 | + // Handle config map - preserve null when API returns empty and config didn't specify config |
| 784 | + if configMapRaw, ok := result["config"].(map[string]interface{}); ok && len(configMapRaw) > 0 { |
| 785 | + configMap := make(map[string]attr.Value) |
| 786 | + for k, v := range configMapRaw { |
| 787 | + if str, ok := v.(string); ok { |
| 788 | + configMap[k] = types.StringValue(str) |
| 789 | + } |
| 790 | + } |
| 791 | + data.Config, _ = types.MapValue(types.StringType, configMap) |
| 792 | + } else if !data.Config.IsNull() { |
| 793 | + data.Config, _ = types.MapValue(types.StringType, map[string]attr.Value{}) |
| 794 | + } |
| 795 | + |
| 796 | + // Handle permissions map - preserve null when API returns empty and config didn't specify permissions |
| 797 | + if permissions, ok := result["permissions"].(map[string]interface{}); ok && len(permissions) > 0 { |
| 798 | + permMap := make(map[string]attr.Value) |
| 799 | + for k, v := range permissions { |
| 800 | + if str, ok := v.(string); ok { |
| 801 | + permMap[k] = types.StringValue(str) |
| 802 | + } |
| 803 | + } |
| 804 | + data.Permissions, _ = types.MapValue(types.StringType, permMap) |
| 805 | + } else if !data.Permissions.IsNull() { |
| 806 | + data.Permissions, _ = types.MapValue(types.StringType, map[string]attr.Value{}) |
| 807 | + } |
| 808 | + |
| 809 | + // Handle model_max_budget map - preserve null when API returns empty and config didn't specify model_max_budget |
| 810 | + if modelMaxBudget, ok := result["model_max_budget"].(map[string]interface{}); ok && len(modelMaxBudget) > 0 { |
| 811 | + budgetMap := make(map[string]attr.Value) |
| 812 | + for k, v := range modelMaxBudget { |
| 813 | + if num, ok := v.(float64); ok { |
| 814 | + budgetMap[k] = types.Float64Value(num) |
| 815 | + } |
| 816 | + } |
| 817 | + data.ModelMaxBudget, _ = types.MapValue(types.Float64Type, budgetMap) |
| 818 | + } else if !data.ModelMaxBudget.IsNull() { |
| 819 | + data.ModelMaxBudget, _ = types.MapValue(types.Float64Type, map[string]attr.Value{}) |
| 820 | + } |
| 821 | + |
| 822 | + // Handle model_rpm_limit map - preserve null when API returns empty and config didn't specify model_rpm_limit |
| 823 | + if modelRPM, ok := result["model_rpm_limit"].(map[string]interface{}); ok && len(modelRPM) > 0 { |
| 824 | + rpmMap := make(map[string]attr.Value) |
| 825 | + for k, v := range modelRPM { |
| 826 | + if num, ok := v.(float64); ok { |
| 827 | + rpmMap[k] = types.Int64Value(int64(num)) |
| 828 | + } |
| 829 | + } |
| 830 | + data.ModelRPMLimit, _ = types.MapValue(types.Int64Type, rpmMap) |
| 831 | + } else if !data.ModelRPMLimit.IsNull() { |
| 832 | + data.ModelRPMLimit, _ = types.MapValue(types.Int64Type, map[string]attr.Value{}) |
| 833 | + } |
| 834 | + |
| 835 | + // Handle model_tpm_limit map - preserve null when API returns empty and config didn't specify model_tpm_limit |
| 836 | + if modelTPM, ok := result["model_tpm_limit"].(map[string]interface{}); ok && len(modelTPM) > 0 { |
| 837 | + tpmMap := make(map[string]attr.Value) |
| 838 | + for k, v := range modelTPM { |
| 839 | + if num, ok := v.(float64); ok { |
| 840 | + tpmMap[k] = types.Int64Value(int64(num)) |
| 841 | + } |
| 842 | + } |
| 843 | + data.ModelTPMLimit, _ = types.MapValue(types.Int64Type, tpmMap) |
| 844 | + } else if !data.ModelTPMLimit.IsNull() { |
| 845 | + data.ModelTPMLimit, _ = types.MapValue(types.Int64Type, map[string]attr.Value{}) |
| 846 | + } |
622 | 847 |
|
623 | 848 | return nil |
624 | 849 | } |
0 commit comments