-
Notifications
You must be signed in to change notification settings - Fork 208
chore: Adds provider_meta support to the provider #3618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EspenAlbert
wants to merge
15
commits into
master
Choose a base branch
from
CLOUDP-340210_provider_meta_modules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d020917
feat: Adds initial provider_meta support and UserAgentTransport
EspenAlbert d9cfbe8
feat: Adds AnalyticsResource support to all resources
EspenAlbert 674267c
feat: PoC SDKv2 and use provider to override
EspenAlbert 73277ba
feat: Refactor Analytics resource initialization and add context hand…
EspenAlbert b4a6333
revert non config/provider-files
EspenAlbert 3c04f7d
refactor: Small cleanups
EspenAlbert 2216dbf
revert nolint
EspenAlbert 9d22131
chore: Fully implement the sdkv2 behavior
EspenAlbert c78545c
refactor: Support PlanModifier
EspenAlbert aef3be7
feat: Add MoveState and UpgradeState methods to RSCommon; enhance use…
EspenAlbert b6a75e4
fix: Add comment to clarify embedding in ProviderMocked struct
EspenAlbert 8518f30
fix: Handle null type in provider meta
EspenAlbert d79c43b
chore: Minor change and test fix
EspenAlbert 716023e
Update internal/config/resource_base_test.go
EspenAlbert ac083fc
Merge branch 'master' into CLOUDP-340210_provider_meta_modules
EspenAlbert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func NewAnalyticsResourceSDKv2(d *schema.Resource, name string) *schema.Resource { | ||
analyticsResource := &AnalyticsResourceSDKv2{ | ||
resource: d, | ||
name: name, | ||
} | ||
/* | ||
We are not initializing deprecated fields, for example Update to avoid the message: | ||
resource mongodbatlas_cloud_backup_snapshot: All fields are ForceNew or Computed w/out Optional, Update is superfluous | ||
|
||
Ensure no deprecated fields are used by running `staticcheck ./internal/service/... | grep -v 'd.GetOkExists'` and looking for (SA1019) | ||
GetOkExists we are using in many places; therefore, we use -v (invert match) to filter out lines with different deprecations | ||
Example line: | ||
internal/service/cluster/model_cluster.go:306:14: d.GetOkExists is deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK (SA1019) | ||
*/ | ||
resource := &schema.Resource{ | ||
CustomizeDiff: d.CustomizeDiff, | ||
DeprecationMessage: d.DeprecationMessage, | ||
Description: d.Description, | ||
EnableLegacyTypeSystemApplyErrors: d.EnableLegacyTypeSystemApplyErrors, | ||
EnableLegacyTypeSystemPlanErrors: d.EnableLegacyTypeSystemPlanErrors, | ||
Identity: d.Identity, | ||
ResourceBehavior: d.ResourceBehavior, | ||
Schema: d.Schema, | ||
SchemaFunc: d.SchemaFunc, | ||
SchemaVersion: d.SchemaVersion, | ||
StateUpgraders: d.StateUpgraders, | ||
Timeouts: d.Timeouts, | ||
UpdateWithoutTimeout: d.UpdateWithoutTimeout, | ||
UseJSONNumber: d.UseJSONNumber, | ||
ValidateRawResourceConfigFuncs: d.ValidateRawResourceConfigFuncs, | ||
} | ||
importer := d.Importer | ||
if importer != nil { | ||
resource.Importer = &schema.ResourceImporter{ | ||
StateContext: analyticsResource.resourceImport, | ||
} | ||
} | ||
// CreateContext or CreateWithoutTimeout, cannot use both | ||
if d.CreateContext != nil { | ||
resource.CreateContext = analyticsResource.CreateContext | ||
} | ||
if d.CreateWithoutTimeout != nil { | ||
resource.CreateWithoutTimeout = analyticsResource.CreateWithoutTimeout | ||
} | ||
// ReadContext or ReadWithoutTimeout, cannot use both | ||
if d.ReadContext != nil { | ||
resource.ReadContext = analyticsResource.ReadContext | ||
} | ||
if d.ReadWithoutTimeout != nil { | ||
resource.ReadWithoutTimeout = analyticsResource.ReadWithoutTimeout | ||
} | ||
// UpdateContext is not set on all resources | ||
if d.UpdateContext != nil { | ||
resource.UpdateContext = analyticsResource.UpdateContext | ||
} | ||
if d.UpdateWithoutTimeout != nil { | ||
resource.UpdateWithoutTimeout = analyticsResource.UpdateWithoutTimeout | ||
} | ||
// DeleteContext or DeleteWithoutTimeout, cannot use both | ||
if d.DeleteContext != nil { | ||
resource.DeleteContext = analyticsResource.DeleteContext | ||
} | ||
if d.DeleteWithoutTimeout != nil { | ||
resource.DeleteWithoutTimeout = analyticsResource.DeleteWithoutTimeout | ||
} | ||
return resource | ||
} | ||
|
||
type ProviderMetaSDKv2 struct { | ||
UserAgentExtra map[string]string `cty:"user_agent_extra"` | ||
ModuleName *string `cty:"module_name"` | ||
ModuleVersion *string `cty:"module_version"` | ||
} | ||
|
||
type AnalyticsResourceSDKv2 struct { | ||
resource *schema.Resource | ||
name string | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) CreateContext(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.CreateContext(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueCreate) | ||
return a.resource.CreateContext(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) CreateWithoutTimeout(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.CreateWithoutTimeout(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueCreate) | ||
return a.resource.CreateWithoutTimeout(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) ReadWithoutTimeout(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.ReadWithoutTimeout(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueRead) | ||
return a.resource.ReadWithoutTimeout(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) ReadContext(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.ReadContext(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueRead) | ||
return a.resource.ReadContext(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) UpdateContext(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.UpdateContext(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueUpdate) | ||
return a.resource.UpdateContext(ctx, r, m) | ||
} | ||
func (a *AnalyticsResourceSDKv2) UpdateWithoutTimeout(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.UpdateWithoutTimeout(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueUpdate) | ||
return a.resource.UpdateWithoutTimeout(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) DeleteContext(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.DeleteContext(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueDelete) | ||
return a.resource.DeleteContext(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) DeleteWithoutTimeout(ctx context.Context, r *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
meta, err := parseProviderMeta(r) | ||
if err != nil { | ||
return a.resource.DeleteWithoutTimeout(ctx, r, m) | ||
} | ||
ctx = a.updateContextWithProviderMeta(ctx, meta, UserAgentOperationValueDelete) | ||
return a.resource.DeleteWithoutTimeout(ctx, r, m) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { | ||
// Import doesn't have providerMeta | ||
ctx = AddUserAgentExtra(ctx, UserAgentExtra{ | ||
Name: a.name, | ||
Operation: UserAgentOperationValueImport, | ||
}) | ||
return a.resource.Importer.StateContext(ctx, d, meta) | ||
} | ||
|
||
func (a *AnalyticsResourceSDKv2) updateContextWithProviderMeta(ctx context.Context, meta ProviderMetaSDKv2, operationName string) context.Context { | ||
moduleName := "" | ||
if meta.ModuleName != nil { | ||
moduleName = *meta.ModuleName | ||
} | ||
moduleVersion := "" | ||
if meta.ModuleVersion != nil { | ||
moduleVersion = *meta.ModuleVersion | ||
} | ||
|
||
uaExtra := UserAgentExtra{ | ||
Name: a.name, | ||
Operation: operationName, | ||
Extras: meta.UserAgentExtra, | ||
ModuleName: moduleName, | ||
ModuleVersion: moduleVersion, | ||
} | ||
ctx = AddUserAgentExtra(ctx, uaExtra) | ||
return ctx | ||
} | ||
|
||
func parseProviderMeta(r *schema.ResourceData) (ProviderMetaSDKv2, error) { | ||
meta := ProviderMetaSDKv2{} | ||
err := r.GetProviderMeta(&meta) | ||
if err != nil { | ||
log.Printf("[WARN] failed to decode provider meta: %s, meta: %v", err, meta) | ||
} | ||
return meta, err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NewClient is changing extensively in SA dev branch but that shouldn't be a problem