|
| 1 | +package pluginfw |
| 2 | + |
| 3 | +// This file contains all of the utils for controlling the plugin framework rollout. |
| 4 | +// For migrated resources and data sources, we can add them to the two maps below to have them registered with the plugin framework. |
| 5 | +// Users can manually specify resources and data sources to use SDK V2 instead of the plugin framework by setting the USE_SDK_V2_RESOURCES and USE_SDK_V2_DATA_SOURCES environment variables. |
| 6 | +// |
| 7 | +// Example: USE_SDK_V2_RESOURCES="databricks_library" would force the library resource to use SDK V2 instead of the plugin framework. |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "os" |
| 12 | + "slices" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/catalog" |
| 16 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/cluster" |
| 17 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/library" |
| 18 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/notificationdestinations" |
| 19 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/qualitymonitor" |
| 20 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/registered_model" |
| 21 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/sharing" |
| 22 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/volume" |
| 23 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 24 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 25 | +) |
| 26 | + |
| 27 | +// List of resources that have been migrated from SDK V2 to plugin framework |
| 28 | +var migratedResources = []func() resource.Resource{ |
| 29 | + qualitymonitor.ResourceQualityMonitor, |
| 30 | + library.ResourceLibrary, |
| 31 | +} |
| 32 | + |
| 33 | +// List of data sources that have been migrated from SDK V2 to plugin framework |
| 34 | +var migratedDataSources = []func() datasource.DataSource{ |
| 35 | + volume.DataSourceVolumes, |
| 36 | +} |
| 37 | + |
| 38 | +// List of resources that have been onboarded to the plugin framework - not migrated from sdkv2. |
| 39 | +var pluginFwOnlyResources = []func() resource.Resource{ |
| 40 | + // TODO Add resources here |
| 41 | + sharing.ResourceShare, // Using the staging name (with pluginframework suffix) |
| 42 | +} |
| 43 | + |
| 44 | +// List of data sources that have been onboarded to the plugin framework - not migrated from sdkv2. |
| 45 | +var pluginFwOnlyDataSources = []func() datasource.DataSource{ |
| 46 | + registered_model.DataSourceRegisteredModel, |
| 47 | + notificationdestinations.DataSourceNotificationDestinations, |
| 48 | + catalog.DataSourceFunctions, |
| 49 | + // TODO: Add DataSourceCluster into migratedDataSources after fixing unit tests. |
| 50 | + cluster.DataSourceCluster, // Using the staging name (with pluginframework suffix) |
| 51 | + sharing.DataSourceShare, // Using the staging name (with pluginframework suffix) |
| 52 | + sharing.DataSourceShares, // Using the staging name (with pluginframework suffix) |
| 53 | +} |
| 54 | + |
| 55 | +type sdkV2FallbackOptions struct { |
| 56 | + resourceFallbacks []string |
| 57 | + dataSourceFallbacks []string |
| 58 | +} |
| 59 | + |
| 60 | +// SdkV2FallbackOption is an interface for acceptance tests to specify resources / data sources to fallback to SDK V2 |
| 61 | +type SdkV2FallbackOption interface { |
| 62 | + Apply(*sdkV2FallbackOptions) |
| 63 | +} |
| 64 | + |
| 65 | +type sdkV2ResourceFallback struct { |
| 66 | + resourceFallbacks []string |
| 67 | +} |
| 68 | + |
| 69 | +func (o *sdkV2ResourceFallback) Apply(options *sdkV2FallbackOptions) { |
| 70 | + options.resourceFallbacks = o.resourceFallbacks |
| 71 | +} |
| 72 | + |
| 73 | +// WithSdkV2ResourceFallbacks is a helper function to specify resources to fallback to SDK V2 |
| 74 | +func WithSdkV2ResourceFallbacks(fallbacks ...string) SdkV2FallbackOption { |
| 75 | + return &sdkV2ResourceFallback{resourceFallbacks: fallbacks} |
| 76 | +} |
| 77 | + |
| 78 | +type sdkv2DataSourceFallback struct { |
| 79 | + dataSourceFallbacks []string |
| 80 | +} |
| 81 | + |
| 82 | +func (o *sdkv2DataSourceFallback) Apply(options *sdkV2FallbackOptions) { |
| 83 | + options.dataSourceFallbacks = o.dataSourceFallbacks |
| 84 | +} |
| 85 | + |
| 86 | +// WithSdkV2DataSourceFallbacks is a helper function to specify data sources to fallback to SDK V2 |
| 87 | +func WithSdkV2DataSourceFallbacks(fallbacks []string) SdkV2FallbackOption { |
| 88 | + return &sdkv2DataSourceFallback{dataSourceFallbacks: fallbacks} |
| 89 | +} |
| 90 | + |
| 91 | +// GetUseSdkV2DataSources is a helper function to get name of resources that should use SDK V2 instead of plugin framework |
| 92 | +func getUseSdkV2Resources() []string { |
| 93 | + useSdkV2 := os.Getenv("USE_SDK_V2_RESOURCES") |
| 94 | + if useSdkV2 == "" { |
| 95 | + return []string{} |
| 96 | + } |
| 97 | + return strings.Split(useSdkV2, ",") |
| 98 | +} |
| 99 | + |
| 100 | +// GetUseSdkV2DataSources is a helper function to get name of data sources that should use SDK V2 instead of plugin framework |
| 101 | +func getUseSdkV2DataSources() []string { |
| 102 | + useSdkV2 := os.Getenv("USE_SDK_V2_DATA_SOURCES") |
| 103 | + if useSdkV2 == "" { |
| 104 | + return []string{} |
| 105 | + } |
| 106 | + return strings.Split(useSdkV2, ",") |
| 107 | +} |
| 108 | + |
| 109 | +// Helper function to check if a resource should use be in SDK V2 instead of plugin framework |
| 110 | +func shouldUseSdkV2Resource(resourceName string) bool { |
| 111 | + useSdkV2Resources := getUseSdkV2Resources() |
| 112 | + return slices.Contains(useSdkV2Resources, resourceName) |
| 113 | +} |
| 114 | + |
| 115 | +// Helper function to check if a data source should use be in SDK V2 instead of plugin framework |
| 116 | +func shouldUseSdkV2DataSource(dataSourceName string) bool { |
| 117 | + sdkV2DataSources := getUseSdkV2DataSources() |
| 118 | + return slices.Contains(sdkV2DataSources, dataSourceName) |
| 119 | +} |
| 120 | + |
| 121 | +// getPluginFrameworkResourcesToRegister is a helper function to get the list of resources that are migrated away from sdkv2 to plugin framework |
| 122 | +func getPluginFrameworkResourcesToRegister(sdkV2Fallbacks ...SdkV2FallbackOption) []func() resource.Resource { |
| 123 | + fallbackOption := sdkV2FallbackOptions{} |
| 124 | + for _, o := range sdkV2Fallbacks { |
| 125 | + o.Apply(&fallbackOption) |
| 126 | + } |
| 127 | + |
| 128 | + var resources []func() resource.Resource |
| 129 | + |
| 130 | + // Loop through the map and add resources if they're not specifically marked to use the SDK V2 |
| 131 | + for _, resourceFunc := range migratedResources { |
| 132 | + name := getResourceName(resourceFunc) |
| 133 | + if !shouldUseSdkV2Resource(name) && !slices.Contains(fallbackOption.resourceFallbacks, name) { |
| 134 | + resources = append(resources, resourceFunc) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return append(resources, pluginFwOnlyResources...) |
| 139 | +} |
| 140 | + |
| 141 | +// getPluginFrameworkDataSourcesToRegister is a helper function to get the list of data sources that are migrated away from sdkv2 to plugin framework |
| 142 | +func getPluginFrameworkDataSourcesToRegister(sdkV2Fallbacks ...SdkV2FallbackOption) []func() datasource.DataSource { |
| 143 | + fallbackOption := sdkV2FallbackOptions{} |
| 144 | + for _, o := range sdkV2Fallbacks { |
| 145 | + o.Apply(&fallbackOption) |
| 146 | + } |
| 147 | + |
| 148 | + var dataSources []func() datasource.DataSource |
| 149 | + |
| 150 | + // Loop through the map and add data sources if they're not specifically marked to use the SDK V2 |
| 151 | + for _, dataSourceFunc := range migratedDataSources { |
| 152 | + name := getDataSourceName(dataSourceFunc) |
| 153 | + if !shouldUseSdkV2DataSource(name) && !slices.Contains(fallbackOption.dataSourceFallbacks, name) { |
| 154 | + dataSources = append(dataSources, dataSourceFunc) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return append(dataSources, pluginFwOnlyDataSources...) |
| 159 | +} |
| 160 | + |
| 161 | +func getResourceName(resourceFunc func() resource.Resource) string { |
| 162 | + resp := resource.MetadataResponse{} |
| 163 | + resourceFunc().Metadata(context.Background(), resource.MetadataRequest{ProviderTypeName: "databricks"}, &resp) |
| 164 | + return resp.TypeName |
| 165 | +} |
| 166 | + |
| 167 | +func getDataSourceName(dataSourceFunc func() datasource.DataSource) string { |
| 168 | + resp := datasource.MetadataResponse{} |
| 169 | + dataSourceFunc().Metadata(context.Background(), datasource.MetadataRequest{ProviderTypeName: "databricks"}, &resp) |
| 170 | + return resp.TypeName |
| 171 | +} |
| 172 | + |
| 173 | +// GetSdkV2ResourcesToRemove is a helper function to get the list of resources that are migrated away from sdkv2 to plugin framework |
| 174 | +func GetSdkV2ResourcesToRemove(sdkV2Fallbacks ...SdkV2FallbackOption) []string { |
| 175 | + fallbackOption := sdkV2FallbackOptions{} |
| 176 | + for _, o := range sdkV2Fallbacks { |
| 177 | + o.Apply(&fallbackOption) |
| 178 | + } |
| 179 | + |
| 180 | + resourcesToRemove := []string{} |
| 181 | + for _, resourceFunc := range migratedResources { |
| 182 | + name := getResourceName(resourceFunc) |
| 183 | + if !shouldUseSdkV2Resource(name) && !slices.Contains(fallbackOption.resourceFallbacks, name) { |
| 184 | + resourcesToRemove = append(resourcesToRemove, name) |
| 185 | + } |
| 186 | + } |
| 187 | + return resourcesToRemove |
| 188 | +} |
| 189 | + |
| 190 | +// GetSdkV2DataSourcesToRemove is a helper function to get the list of data sources that are migrated away from sdkv2 to plugin framework |
| 191 | +func GetSdkV2DataSourcesToRemove(sdkV2Fallbacks ...SdkV2FallbackOption) []string { |
| 192 | + fallbackOption := sdkV2FallbackOptions{} |
| 193 | + for _, o := range sdkV2Fallbacks { |
| 194 | + o.Apply(&fallbackOption) |
| 195 | + } |
| 196 | + |
| 197 | + dataSourcesToRemove := []string{} |
| 198 | + for _, dataSourceFunc := range migratedDataSources { |
| 199 | + name := getDataSourceName(dataSourceFunc) |
| 200 | + if !shouldUseSdkV2DataSource(name) && !slices.Contains(fallbackOption.dataSourceFallbacks, name) { |
| 201 | + dataSourcesToRemove = append(dataSourcesToRemove, name) |
| 202 | + } |
| 203 | + } |
| 204 | + return dataSourcesToRemove |
| 205 | +} |
0 commit comments