|
1 | 1 | package provider
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
| 6 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 7 | + resource2 "github.com/hashicorp/terraform-plugin-framework/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
5 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
6 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
7 | 13 | "github.com/hashicorp/terraform-plugin-testing/terraform"
|
|
359 | 365 |
|
360 | 366 | func TestAccresourceCurlSkipRead(t *testing.T) {
|
361 | 367 | t.Setenv("TF_ACC", "true")
|
| 368 | + t.Setenv("TF_LOG", "DEBUG") |
362 | 369 | t.Setenv("USE_DEFAULT_CLIENT_FOR_TESTS", "true")
|
363 | 370 |
|
364 | 371 | httpmock.Activate()
|
|
418 | 425 |
|
419 | 426 | }
|
420 | 427 |
|
| 428 | +func TestAccresourceCurlSkipReadNoReadFields(t *testing.T) { |
| 429 | + t.Setenv("TF_ACC", "true") |
| 430 | + t.Setenv("USE_DEFAULT_CLIENT_FOR_TESTS", "true") |
| 431 | + |
| 432 | + httpmock.Activate() |
| 433 | + defer httpmock.DeactivateAndReset() |
| 434 | + httpmock.RegisterResponder( |
| 435 | + "POST", |
| 436 | + "https://example.com/create", |
| 437 | + httpmock.NewStringResponder(200, `{"name": "devopsrob"}`), |
| 438 | + ) |
| 439 | + |
| 440 | + rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) |
| 441 | + |
| 442 | + resource.Test(t, resource.TestCase{ |
| 443 | + PreCheck: func() { testAccPreCheck(t) }, |
| 444 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 445 | + Steps: []resource.TestStep{ |
| 446 | + { |
| 447 | + Config: testAccresourceCurlSkipReadNoReadFields(rName, RequestBody), |
| 448 | + }, |
| 449 | + }, |
| 450 | + }) |
| 451 | +} |
| 452 | + |
| 453 | +func testAccresourceCurlSkipReadNoReadFields(name string, body string) string { |
| 454 | + return fmt.Sprintf(` |
| 455 | +resource "terracurl_request" "test" { |
| 456 | + name = "%s" |
| 457 | + url = "https://example.com/create" |
| 458 | + response_codes = ["200"] |
| 459 | +
|
| 460 | + request_body = <<EOF |
| 461 | +%s |
| 462 | +EOF |
| 463 | +
|
| 464 | + retry_interval = 1 |
| 465 | + max_retry = 1 |
| 466 | + method = "POST" |
| 467 | +
|
| 468 | + skip_destroy = true |
| 469 | + skip_read = true |
| 470 | +} |
| 471 | +`, name, body) |
| 472 | +} |
| 473 | + |
421 | 474 | func TestAccresourceCurlRead(t *testing.T) {
|
422 | 475 | t.Setenv("TF_ACC", "true")
|
423 | 476 | t.Setenv("USE_DEFAULT_CLIENT_FOR_TESTS", "true")
|
@@ -731,3 +784,143 @@ func testMockEndpointRegister(endpoint string) resource.TestCheckFunc {
|
731 | 784 | return nil
|
732 | 785 | }
|
733 | 786 | }
|
| 787 | + |
| 788 | +func TestCurlResource_StateUpgrade(t *testing.T) { |
| 789 | + ctx := context.Background() |
| 790 | + r := &CurlResource{} |
| 791 | + |
| 792 | + upgraders := r.UpgradeState(ctx) |
| 793 | + upgrader, ok := upgraders[0] |
| 794 | + if !ok { |
| 795 | + t.Fatal("No upgrader found for version 0") |
| 796 | + } |
| 797 | + |
| 798 | + // Define the complete schema |
| 799 | + schemaVar := schema.Schema{ |
| 800 | + Attributes: map[string]schema.Attribute{ |
| 801 | + "id": schema.StringAttribute{Computed: true}, |
| 802 | + "name": schema.StringAttribute{Optional: true}, |
| 803 | + "url": schema.StringAttribute{Required: true}, |
| 804 | + "method": schema.StringAttribute{Optional: true}, |
| 805 | + "headers": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 806 | + "request_parameters": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 807 | + "request_body": schema.StringAttribute{Optional: true}, |
| 808 | + "cert_file": schema.StringAttribute{Optional: true}, |
| 809 | + "key_file": schema.StringAttribute{Optional: true}, |
| 810 | + "ca_cert_file": schema.StringAttribute{Optional: true}, |
| 811 | + "ca_cert_directory": schema.StringAttribute{Optional: true}, |
| 812 | + "skip_tls_verify": schema.BoolAttribute{Optional: true}, |
| 813 | + "timeout": schema.Int64Attribute{Optional: true}, |
| 814 | + "response_codes": schema.ListAttribute{ElementType: types.StringType, Optional: true}, |
| 815 | + "status_code": schema.StringAttribute{Computed: true}, |
| 816 | + "response": schema.StringAttribute{Computed: true}, |
| 817 | + "request_url_string": schema.StringAttribute{Computed: true}, |
| 818 | + "max_retry": schema.Int64Attribute{Optional: true}, |
| 819 | + "retry_interval": schema.Int64Attribute{Optional: true}, |
| 820 | + "ignore_response_fields": schema.ListAttribute{ElementType: types.StringType, Optional: true}, |
| 821 | + "drift_marker": schema.StringAttribute{Optional: true}, |
| 822 | + |
| 823 | + // Read-related fields |
| 824 | + "skip_read": schema.BoolAttribute{Optional: true}, |
| 825 | + "read_url": schema.StringAttribute{Optional: true}, |
| 826 | + "read_method": schema.StringAttribute{Optional: true}, |
| 827 | + "read_headers": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 828 | + "read_parameters": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 829 | + "read_request_body": schema.StringAttribute{Optional: true}, |
| 830 | + "read_cert_file": schema.StringAttribute{Optional: true}, |
| 831 | + "read_key_file": schema.StringAttribute{Optional: true}, |
| 832 | + "read_ca_cert_file": schema.StringAttribute{Optional: true}, |
| 833 | + "read_ca_cert_directory": schema.StringAttribute{Optional: true}, |
| 834 | + "read_skip_tls_verify": schema.BoolAttribute{Optional: true}, |
| 835 | + "read_response_codes": schema.ListAttribute{ElementType: types.StringType, Optional: true}, |
| 836 | + |
| 837 | + // Destroy-related fields |
| 838 | + "skip_destroy": schema.BoolAttribute{Optional: true}, |
| 839 | + "destroy_url": schema.StringAttribute{Optional: true}, |
| 840 | + "destroy_method": schema.StringAttribute{Optional: true}, |
| 841 | + "destroy_headers": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 842 | + "destroy_request_parameters": schema.MapAttribute{ElementType: types.StringType, Optional: true}, |
| 843 | + "destroy_request_body": schema.StringAttribute{Optional: true}, |
| 844 | + "destroy_cert_file": schema.StringAttribute{Optional: true}, |
| 845 | + "destroy_key_file": schema.StringAttribute{Optional: true}, |
| 846 | + "destroy_ca_cert_file": schema.StringAttribute{Optional: true}, |
| 847 | + "destroy_ca_cert_directory": schema.StringAttribute{Optional: true}, |
| 848 | + "destroy_skip_tls_verify": schema.BoolAttribute{Optional: true}, |
| 849 | + "destroy_response_codes": schema.ListAttribute{ElementType: types.StringType, Optional: true}, |
| 850 | + "destroy_timeout": schema.Int64Attribute{Optional: true}, |
| 851 | + "destroy_max_retry": schema.Int64Attribute{Optional: true}, |
| 852 | + "destroy_retry_interval": schema.Int64Attribute{Optional: true}, |
| 853 | + "destroy_request_url_string": schema.StringAttribute{Computed: true}, |
| 854 | + }, |
| 855 | + } |
| 856 | + |
| 857 | + // Create initial state |
| 858 | + oldState := &CurlResourceModel{ |
| 859 | + Id: types.StringValue("test-resource"), |
| 860 | + Name: types.StringValue("test"), |
| 861 | + Url: types.StringValue("https://api.example.com"), |
| 862 | + Method: types.StringValue("POST"), |
| 863 | + ReadUrl: types.StringValue("https://api.example.com/read"), |
| 864 | + Headers: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 865 | + ReadHeaders: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 866 | + ReadParameters: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 867 | + ReadResponseCodes: types.ListValueMust( |
| 868 | + types.StringType, |
| 869 | + []attr.Value{ |
| 870 | + types.StringValue("200"), |
| 871 | + }, |
| 872 | + ), |
| 873 | + ResponseCodes: types.ListValueMust(types.StringType, []attr.Value{}), |
| 874 | + IgnoreResponseFields: types.ListValueMust(types.StringType, []attr.Value{}), |
| 875 | + RequestParameters: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 876 | + DestroyHeaders: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 877 | + DestroyRequestParameters: types.MapValueMust(types.StringType, map[string]attr.Value{}), |
| 878 | + DestroyResponseCodes: types.ListValueMust(types.StringType, []attr.Value{}), |
| 879 | + } |
| 880 | + |
| 881 | + state := tfsdk.State{ |
| 882 | + Schema: schemaVar, |
| 883 | + } |
| 884 | + diags := state.Set(ctx, oldState) |
| 885 | + if diags.HasError() { |
| 886 | + t.Fatalf("error setting initial state: %v", diags) |
| 887 | + } |
| 888 | + |
| 889 | + req := resource2.UpgradeStateRequest{ |
| 890 | + State: &state, |
| 891 | + } |
| 892 | + |
| 893 | + resp := &resource2.UpgradeStateResponse{ |
| 894 | + State: tfsdk.State{ |
| 895 | + Schema: schemaVar, |
| 896 | + }, |
| 897 | + } |
| 898 | + |
| 899 | + upgrader.StateUpgrader(ctx, req, resp) |
| 900 | + if resp.Diagnostics.HasError() { |
| 901 | + t.Fatalf("upgrade failed: %v", resp.Diagnostics) |
| 902 | + } |
| 903 | + |
| 904 | + var upgradedState CurlResourceModel |
| 905 | + diags = resp.State.Get(ctx, &upgradedState) |
| 906 | + if diags.HasError() { |
| 907 | + t.Fatalf("error getting upgraded state: %v", diags) |
| 908 | + } |
| 909 | + |
| 910 | + // Verify the results |
| 911 | + if !upgradedState.ReadUrl.IsNull() { |
| 912 | + t.Error("ReadUrl should be null after upgrade") |
| 913 | + } |
| 914 | + |
| 915 | + if !upgradedState.ReadResponseCodes.IsNull() { |
| 916 | + t.Error("ReadResponseCodes should be null after upgrade") |
| 917 | + } |
| 918 | + |
| 919 | + if upgradedState.Id.ValueString() != "test-resource" { |
| 920 | + t.Error("Id was not preserved") |
| 921 | + } |
| 922 | + |
| 923 | + if upgradedState.Url.ValueString() != "https://api.example.com" { |
| 924 | + t.Error("Url was not preserved") |
| 925 | + } |
| 926 | +} |
0 commit comments