From c39ddf0b0ed1a3fcf556b347aff805314407a0af Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Thu, 24 Aug 2023 17:57:16 -0700 Subject: [PATCH] Move patch into resources.go This is an alternative strategy to restore an old field, and does not require a patch in the upstream provider. Instead, we create the field in resources.go and make sure that if a user sets it, we move it to `restEndpoint` (which the provider still has as valid). --- ...deprecated-and-removed-http_endpoint.patch | 138 ------------------ ...sert-shim.patch => 0001-Insert-shim.patch} | 6 +- .../bridge-metadata.json | 1 - .../schema.json | 18 +-- provider/go.mod | 2 +- provider/go.sum | 4 +- provider/resources.go | 62 +++++++- 7 files changed, 64 insertions(+), 167 deletions(-) delete mode 100644 patches/0001-Add-support-for-deprecated-and-removed-http_endpoint.patch rename patches/{0002-Insert-shim.patch => 0001-Insert-shim.patch} (83%) diff --git a/patches/0001-Add-support-for-deprecated-and-removed-http_endpoint.patch b/patches/0001-Add-support-for-deprecated-and-removed-http_endpoint.patch deleted file mode 100644 index 3a405790..00000000 --- a/patches/0001-Add-support-for-deprecated-and-removed-http_endpoint.patch +++ /dev/null @@ -1,138 +0,0 @@ -From 370769e695bbad71cdf775c497940044f20a7edd Mon Sep 17 00:00:00 2001 -From: Ramon Quitales -Date: Thu, 24 Aug 2023 10:00:27 -0700 -Subject: [PATCH 1/2] Add support for deprecated and removed http_endpoint - field - ---- - internal/provider/data_source_kafka_topic.go | 7 +++++ - internal/provider/resource_kafka_topic.go | 28 ++++++++++++++++---- - internal/provider/state_upgraders.go | 13 ++++----- - 3 files changed, 37 insertions(+), 11 deletions(-) - -diff --git a/internal/provider/data_source_kafka_topic.go b/internal/provider/data_source_kafka_topic.go -index b952f18..378d791 100644 ---- a/internal/provider/data_source_kafka_topic.go -+++ b/internal/provider/data_source_kafka_topic.go -@@ -17,6 +17,7 @@ package provider - import ( - "context" - "fmt" -+ - "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -@@ -31,6 +32,12 @@ func kafkaTopicDataSource() *schema.Resource { - Type: schema.TypeString, - Required: true, - }, -+ paramHttpEndpoint: { -+ Deprecated: "This parameter has been deprecated in favour of Rest Endpoint", -+ Type: schema.TypeString, -+ Optional: true, -+ Computed: true, -+ }, - paramRestEndpoint: { - Type: schema.TypeString, - Required: true, -diff --git a/internal/provider/resource_kafka_topic.go b/internal/provider/resource_kafka_topic.go -index 459471d..90e6b7f 100644 ---- a/internal/provider/resource_kafka_topic.go -+++ b/internal/provider/resource_kafka_topic.go -@@ -18,15 +18,16 @@ import ( - "context" - "encoding/json" - "fmt" -+ "net/http" -+ "regexp" -+ "strings" -+ "time" -+ - kafkarestv3 "github.com/confluentinc/ccloud-sdk-go-v2/kafkarest/v3" - "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" -- "net/http" -- "regexp" -- "strings" -- "time" - ) - - const ( -@@ -96,6 +97,15 @@ func kafkaTopicResource() *schema.Resource { - Description: "The number of partitions to create in the topic.", - ValidateFunc: validation.IntAtLeast(1), - }, -+ paramHttpEndpoint: { -+ Deprecated: "This parameter has been deprecated in favour of Rest Endpoint", -+ Type: schema.TypeString, -+ Optional: true, -+ Computed: true, -+ Description: "The HTTP endpoint of the Kafka cluster (e.g., `https://pkc-00000.us-central1.gcp.confluent.cloud:443`).", -+ ValidateFunc: validation.StringMatch(regexp.MustCompile("^http"), "the HTTP endpoint must start with 'https://'"), -+ ConflictsWith: []string{paramRestEndpoint}, -+ }, - paramRestEndpoint: { - Type: schema.TypeString, - Optional: true, -@@ -165,7 +175,12 @@ func extractRestEndpoint(client *Client, d *schema.ResourceData, isImportOperati - if restEndpoint != "" { - return restEndpoint, nil - } -- return "", fmt.Errorf("one of provider.kafka_rest_endpoint (defaults to KAFKA_REST_ENDPOINT environment variable) or resource.rest_endpoint must be set") -+ httpEndpoint := d.Get(paramHttpEndpoint).(string) -+ if httpEndpoint != "" { -+ return httpEndpoint, nil -+ } -+ -+ return "", fmt.Errorf("one of provider.kafka_rest_endpoint (defaults to KAFKA_REST_ENDPOINT environment variable), resource.rest_endpoint or resource.http_endpoint must be set") - } - - func extractClusterApiKeyAndApiSecret(client *Client, d *schema.ResourceData, isImportOperation bool) (string, string, error) { -@@ -468,6 +483,9 @@ func readTopicAndSetAttributes(ctx context.Context, d *schema.ResourceData, c *K - if err := d.Set(paramRestEndpoint, c.restEndpoint); err != nil { - return nil, err - } -+ if err := d.Set(paramHttpEndpoint, c.restEndpoint); err != nil { -+ return nil, err -+ } - } - - d.SetId(createKafkaTopicId(c.clusterId, topicName)) -diff --git a/internal/provider/state_upgraders.go b/internal/provider/state_upgraders.go -index 4d3ed5b..43323da 100644 ---- a/internal/provider/state_upgraders.go -+++ b/internal/provider/state_upgraders.go -@@ -16,9 +16,10 @@ package provider - - import ( - "context" -+ "regexp" -+ - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" -- "regexp" - ) - - func kafkaResourceV0() *schema.Resource { -@@ -88,11 +89,11 @@ func kafkaResourceV0() *schema.Resource { - // Modifies the attribute(s) appropriately for the migration. - func kafkaStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { - // 1. When upgrading from 0.10.0, rename "http_endpoint" to "rest_endpoint" by copying the value and deleting "http_endpoint" attribute -- if httpEndpoint, found := rawState[paramHttpEndpoint]; found { -- httpEndpointString := httpEndpoint.(string) -- rawState[paramRestEndpoint] = httpEndpointString -- delete(rawState, paramHttpEndpoint) -- } -+ // if httpEndpoint, found := rawState[paramHttpEndpoint]; found { -+ // httpEndpointString := httpEndpoint.(string) -+ // rawState[paramRestEndpoint] = httpEndpointString -+ // delete(rawState, paramHttpEndpoint) -+ // } - // 2. When upgrading from 0.11.0 no changes are necessary: "rest_endpoint" exists already - - return rawState, nil --- -2.39.2 (Apple Git-143) - diff --git a/patches/0002-Insert-shim.patch b/patches/0001-Insert-shim.patch similarity index 83% rename from patches/0002-Insert-shim.patch rename to patches/0001-Insert-shim.patch index 294f82bf..9df45823 100644 --- a/patches/0002-Insert-shim.patch +++ b/patches/0001-Insert-shim.patch @@ -1,7 +1,7 @@ -From 1ccb7127739cf3a3a6dbe124f1937cd4b4fcdbe1 Mon Sep 17 00:00:00 2001 +From 84980b38abac2616149d8218d59b61687a580fb9 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Thu, 9 Mar 2023 18:22:25 +0100 -Subject: [PATCH 2/2] Insert shim +Subject: [PATCH] Insert shim --- shim/shim.go | 10 ++++++++++ @@ -25,5 +25,5 @@ index 0000000..ac43027 + return provider.New(version, customUserAgent)() +} -- -2.39.2 (Apple Git-143) +2.41.0 diff --git a/provider/cmd/pulumi-resource-confluentcloud/bridge-metadata.json b/provider/cmd/pulumi-resource-confluentcloud/bridge-metadata.json index 5586ab90..b787ba91 100644 --- a/provider/cmd/pulumi-resource-confluentcloud/bridge-metadata.json +++ b/provider/cmd/pulumi-resource-confluentcloud/bridge-metadata.json @@ -1827,7 +1827,6 @@ "encryptionKey": "encryption_key" }, "confluentcloud:index/getKafkaTopic:getKafkaTopic": { - "httpEndpoint": "http_endpoint", "kafkaCluster": "kafka_cluster", "partitionsCount": "partitions_count", "restEndpoint": "rest_endpoint", diff --git a/provider/cmd/pulumi-resource-confluentcloud/schema.json b/provider/cmd/pulumi-resource-confluentcloud/schema.json index 183876de..16a4cb10 100644 --- a/provider/cmd/pulumi-resource-confluentcloud/schema.json +++ b/provider/cmd/pulumi-resource-confluentcloud/schema.json @@ -5044,8 +5044,7 @@ }, "httpEndpoint": { "type": "string", - "description": "The HTTP endpoint of the Kafka cluster (e.g., `https://pkc-00000.us-central1.gcp.confluent.cloud:443`).\n", - "deprecationMessage": "This parameter has been deprecated in favour of Rest Endpoint" + "deprecationMessage": "This property has been deprecated. Please use \"restEndpoint\" instead." }, "kafkaCluster": { "$ref": "#/types/confluentcloud:index/KafkaTopicKafkaCluster:KafkaTopicKafkaCluster" @@ -5083,8 +5082,7 @@ }, "httpEndpoint": { "type": "string", - "description": "The HTTP endpoint of the Kafka cluster (e.g., `https://pkc-00000.us-central1.gcp.confluent.cloud:443`).\n", - "deprecationMessage": "This parameter has been deprecated in favour of Rest Endpoint" + "deprecationMessage": "This property has been deprecated. Please use \"restEndpoint\" instead." }, "kafkaCluster": { "$ref": "#/types/confluentcloud:index/KafkaTopicKafkaCluster:KafkaTopicKafkaCluster", @@ -5125,8 +5123,7 @@ }, "httpEndpoint": { "type": "string", - "description": "The HTTP endpoint of the Kafka cluster (e.g., `https://pkc-00000.us-central1.gcp.confluent.cloud:443`).\n", - "deprecationMessage": "This parameter has been deprecated in favour of Rest Endpoint" + "deprecationMessage": "This property has been deprecated. Please use \"restEndpoint\" instead." }, "kafkaCluster": { "$ref": "#/types/confluentcloud:index/KafkaTopicKafkaCluster:KafkaTopicKafkaCluster", @@ -7951,10 +7948,6 @@ "$ref": "#/types/confluentcloud:index/getKafkaTopicCredentials:getKafkaTopicCredentials", "secret": true }, - "httpEndpoint": { - "type": "string", - "deprecationMessage": "This parameter has been deprecated in favour of Rest Endpoint" - }, "kafkaCluster": { "$ref": "#/types/confluentcloud:index/getKafkaTopicKafkaCluster:getKafkaTopicKafkaCluster" }, @@ -7987,10 +7980,6 @@ "$ref": "#/types/confluentcloud:index/getKafkaTopicCredentials:getKafkaTopicCredentials", "secret": true }, - "httpEndpoint": { - "type": "string", - "deprecationMessage": "This parameter has been deprecated in favour of Rest Endpoint" - }, "id": { "type": "string", "description": "The provider-assigned unique ID for this managed resource.\n" @@ -8012,7 +8001,6 @@ "type": "object", "required": [ "config", - "httpEndpoint", "partitionsCount", "restEndpoint", "topicName", diff --git a/provider/go.mod b/provider/go.mod index 9927fe14..bf63cb6e 100644 --- a/provider/go.mod +++ b/provider/go.mod @@ -9,7 +9,7 @@ replace ( require ( github.com/confluentinc/terraform-provider-confluent v1.32.0 - github.com/pulumi/pulumi-terraform-bridge/v3 v3.55.0 + github.com/pulumi/pulumi-terraform-bridge/v3 v3.57.0 github.com/pulumi/pulumi/sdk/v3 v3.76.1 ) diff --git a/provider/go.sum b/provider/go.sum index 3f5c9a99..b0b6c44e 100644 --- a/provider/go.sum +++ b/provider/go.sum @@ -2193,8 +2193,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/pulumi/pulumi-java/pkg v0.9.4 h1:gIQZmlUI1o9ye8CL2XFqtmAX6Lwr9uj/+HzjboiSmK4= github.com/pulumi/pulumi-java/pkg v0.9.4/go.mod h1:c6rSw/+q4O0IImgJ9axxoC6QesbPYWBaG5gimbHouUQ= github.com/pulumi/pulumi-terraform-bridge/testing v0.0.1 h1:SCg1gjfY9N4yn8U8peIUYATifjoDABkyR7H9lmefsfc= -github.com/pulumi/pulumi-terraform-bridge/v3 v3.55.0 h1:A33Ji/QSCYy2Jk5+1BzA5vFmK7Rvq6XFo8jS69QahVo= -github.com/pulumi/pulumi-terraform-bridge/v3 v3.55.0/go.mod h1:ykaml8e6XS/yI9JOcNZ+6gLirs6EWTB0FmjbT+JyEdU= +github.com/pulumi/pulumi-terraform-bridge/v3 v3.57.0 h1:munOxi56glme47MT8/wI29o9wrRBJrEQuwjAgm1zviI= +github.com/pulumi/pulumi-terraform-bridge/v3 v3.57.0/go.mod h1:ykaml8e6XS/yI9JOcNZ+6gLirs6EWTB0FmjbT+JyEdU= github.com/pulumi/pulumi-terraform-bridge/x/muxer v0.0.4 h1:rIzMmtcVpPX8ynaz6/nW5AHNY63DiNfCohqmxWvMpM4= github.com/pulumi/pulumi-terraform-bridge/x/muxer v0.0.4/go.mod h1:Kt8RIZWa/N8rW3+0g6NrqCBmF3o+HuIhFaZpssEkG6w= github.com/pulumi/pulumi-yaml v1.1.1 h1:8pyBNIU8+ym0wYpjhsCqN+cutygfK1XbhY2YEeNfyXY= diff --git a/provider/resources.go b/provider/resources.go index 38a02b86..ebf2dc19 100644 --- a/provider/resources.go +++ b/provider/resources.go @@ -17,15 +17,19 @@ package confluentcloud import ( "fmt" // embed is used to store bridge-metadata.json in the compiled binary + "context" _ "embed" "path/filepath" - "github.com/confluentinc/terraform-provider-confluent/shim" - "github.com/pulumi/pulumi-confluentcloud/provider/pkg/version" + tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/x" shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2" + "github.com/pulumi/pulumi/sdk/v3/go/common/resource" "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" + + "github.com/confluentinc/terraform-provider-confluent/shim" + "github.com/pulumi/pulumi-confluentcloud/provider/pkg/version" ) // all of the token components used below. @@ -90,11 +94,55 @@ func Provider() tfbridge.ProviderInfo { }}, "confluent_kafka_cluster_config": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "KafkaClusterConfig")}, "confluent_kafka_mirror_topic": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "KafkaMirrorTopic")}, - "confluent_kafka_topic": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "KafkaTopic")}, - "confluent_network": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Network")}, - "confluent_peering": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Peering")}, - "confluent_private_link_access": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "PrivateLinkAccess")}, - "confluent_role_binding": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "RoleBinding")}, + "confluent_kafka_topic": { + Tok: tfbridge.MakeResource(mainPkg, mainMod, "KafkaTopic"), + PreCheckCallback: func( + ctx context.Context, config resource.PropertyMap, _ resource.PropertyMap, + ) (resource.PropertyMap, error) { + const ( + httpEndpoint = "httpEndpoint" + restEndpoint = "restEndpoint" + ) + endpoint, ok := config[httpEndpoint] + if !ok { + // The optional key is not present, so we can return the config as is. + return config, nil + } + + // The user has set both httpEndpoint and restEndpoint. These are semantically + // the same, so it is forbidden to set both. + if _, ok := config[restEndpoint]; ok { + return config, fmt.Errorf(`Cannot specify both "httpEndpoint" and "restEndpoint"` + + `, please set only "restEndpoint"`) + } + + tfbridge.GetLogger(ctx).Warn(`"httpEndpoint" is deprecated, use "restEndpoint" instead.`) + + delete(config, httpEndpoint) + config[restEndpoint] = endpoint + + return config, nil + }, + Fields: map[string]*tfbridge.SchemaInfo{ + "http_endpoint": func() *tfbridge.SchemaInfo { + p.ResourcesMap().Get("confluent_kafka_topic").Schema(). + Set("http_endpoint", shimv2.NewSchema(&tfschema.Schema{ + Type: tfschema.TypeString, + Optional: true, + Computed: true, + })) + return &tfbridge.SchemaInfo{ + Name: "httpEndpoint", + DeprecationMessage: "This property has been deprecated. " + + `Please use "restEndpoint" instead.`, + } + }(), + }, + }, + "confluent_network": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Network")}, + "confluent_peering": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Peering")}, + "confluent_private_link_access": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "PrivateLinkAccess")}, + "confluent_role_binding": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "RoleBinding")}, "confluent_service_account": { Tok: tfbridge.MakeResource(mainPkg, mainMod, "ServiceAccount"), Fields: map[string]*tfbridge.SchemaInfo{