Skip to content
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

[datadog_integration_gcp] Migrate to FW Provider, Add ResourceCollectionEnabled and IsSecurityCommandCenterEnabled fields #2230

Conversation

smuhit
Copy link
Contributor

@smuhit smuhit commented Jan 5, 2024

Added a couple of new fields to the (legacy) gcp integration resource. (NOTE: Despite being legacy, a couple of fields were added to the legacy APIs and hence needed to be added here.)

Tested locally and verified the following:

  • A state from pre-mirgration works post migration
  • Changes to new and old fields are picked up
    • Host Filters were strings pre-migration. For backwards compatibility, kept them as strings
  • Creation and deletion also work post-migration.

NOTE: Waiting on a new release of the datadog go client library to be released.

@smuhit smuhit requested review from a team and nina9753 and removed request for a team January 5, 2024 19:55
@smuhit smuhit requested review from a team as code owners January 5, 2024 19:55
@smuhit smuhit marked this pull request as draft January 5, 2024 20:18
@smuhit smuhit changed the title [GCP V1 (Legacy)] Migrate to FW Provider, Add ResourceCollectionEnabled and IsSecurityCommandCenterEnabled fields [datadog_integration_gcp] Migrate to FW Provider, Add ResourceCollectionEnabled and IsSecurityCommandCenterEnabled fields Jan 5, 2024
@smuhit smuhit force-pushed the sabbir.muhit/gcp-1258/add-resource-collection-flag-to-gcp-resources branch from f51f638 to f758230 Compare January 8, 2024 19:27
@smuhit smuhit marked this pull request as ready for review January 8, 2024 20:03
brett0000FF
brett0000FF previously approved these changes Jan 8, 2024
docs/resources/integration_gcp.md Outdated Show resolved Hide resolved
@@ -8,7 +8,7 @@ description: |-

# datadog_integration_gcp (Resource)

This resource is deprecated — use the `datadog_integration_gcp_sts resource` instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.
This resource is deprecated — use the `datadog_integration_gcp_sts` resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This resource is deprecateduse the `datadog_integration_gcp_sts` resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.
This resource is deprecateduse the `datadog_integration_gcp_sts` resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brett0000FF per line 6 should we just update both to stay consistent?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we generally advise not to put spaces around hyphens, so that would be a good edit. I wasn't sure if the integration's name already contained the spacing, which is why I didn't edit it before.

@smuhit smuhit requested a review from ash-ddog January 9, 2024 15:11
nina9753
nina9753 previously approved these changes Jan 9, 2024
@dtru-ddog dtru-ddog self-requested a review January 10, 2024 15:12
@dtru-ddog dtru-ddog dismissed stale reviews from nina9753 and brett0000FF via 7f7ae53 January 11, 2024 21:19
Copy link

@ash-ddog ash-ddog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple things to change and/or discuss, lmk if you have questions!

var (
_ resource.ResourceWithConfigure = &integrationGcpResource{}
_ resource.ResourceWithImportState = &integrationGcpResource{}
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits:

  • I'd move this right above the definition of integrationGcpResource, since these lines are just trying to prove that it implements certain interfaces.
  • This is typically written as
var (
	_ resource.ResourceWithConfigure   = (*integrationGcpResource)(nil)
	_ resource.ResourceWithImportState = (*integrationGcpResource)(nil)
)

i.e. no point in actually building structs since they are assigned to _ and so are not accessible.

_ resource.ResourceWithImportState = &integrationGcpResource{}
)

var integrationGcpMutex = sync.Mutex{}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd move this to the top and write this as

var (
  integrationGcpMutex sync.Mutex
)

i.e. the zero-value mutex is valid for use


type integrationGcpResource struct {
Api *datadogV1.GCPIntegrationApi
Auth context.Context

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason these are exported fields? Feels weird to have exported fields on an unexported type. That's usually only useful for struct tags (i.e. `json:"blah"`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to unexported

Automute types.Bool `tfsdk:"automute"`
HostFilters types.String `tfsdk:"host_filters"`
ResourceCollectionEnabled types.Bool `tfsdk:"resource_collection_enabled"`
CspmResourceCollectionEnabled types.Bool `tfsdk:"cspm_resource_collection_enabled"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this typically called is_cspm_enabled?

Copy link

@ash-ddog ash-ddog Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we have to leave the `tfsdk:"cspm_resource_collection_enabled"` , we could rename the field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but keeping it named as is for backwards compatibility (and the fact that this is a public repo) with https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/integration_gcp#optional

var state integrationGcpModel

integrationGcpMutex.Lock()
defer integrationGcpMutex.Unlock()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be a global lock? Any reason to not move it to being defined on integrationGcpResource so it's scoped to the instance? Even if integrationGcpResource is a singleton, an instance lock will in that case act like a global lock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform will do some automated parallelism behind the scenes. This is to ensure that the API calls aren't clobbering each other.

}

func (r *integrationGcpResource) getGCPIntegration(state integrationGcpModel) (*datadogV1.GCPAccount, diag.Diagnostics) {
diags := diag.Diagnostics{}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: prefer var diags diag.Diagnostics

state.ID = projectId
}

func (r *integrationGcpResource) getGCPIntegration(state integrationGcpModel) (*datadogV1.GCPAccount, diag.Diagnostics) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda nit:

IMO all of this code would be cleaner if we left out terraform-specific stuff until the last moment. i.e. make the return signature of this method (datadogV1.GCPAccount, error), and add the error to the diag. That way we don't have to rely on pointers to tell if something worked or not, and we don't have to switch back and forth btw pointers and values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

diags.AddError("response contains unparsedObject", err.Error())
return nil, diags
}
account = &integration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preference for making this line return &integration, diags and making the last line return nil, diags, and removing the local var and break all together.

return account, diags
}

func (r *integrationGcpResource) buildIntegrationGcpRequestBodyBase(state integrationGcpModel) (*datadogV1.GCPAccount, diag.Diagnostics) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is diags.Diagnostics part of this return signature? It's always empty?

body.SetHostFilters(state.HostFilters.ValueString())
if !state.ResourceCollectionEnabled.IsUnknown() {
body.SetResourceCollectionEnabled(state.ResourceCollectionEnabled.ValueBool())
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this is special case? (i.e. I don't see IsUnknown() used anywhere else but ResourceCollectionEnabled)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly for backwards compatibility. The API will return an error if the CSPM flag is on but the Resource Collection Flag is explicitly set to False. In this case, we don't want to set any value for the Resource Collection flag unless a value was explicitly set by the user.

@dtru-ddog dtru-ddog marked this pull request as draft February 14, 2024 15:10
@dtru-ddog
Copy link
Contributor

Moving to draft until ready for re-review

…SecurityCommandCenterEnabled fields

Update datadog client library

Remove unused variable

Fixes for migration to fwprovider

More fixes

Update codeowners for changes to gcp
@smuhit smuhit force-pushed the sabbir.muhit/gcp-1258/add-resource-collection-flag-to-gcp-resources branch from 7f7ae53 to 03e539a Compare February 22, 2024 00:00
@smuhit smuhit marked this pull request as ready for review February 22, 2024 14:44
Copy link

@ash-ddog ash-ddog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of very small nits, thanks for addressing feedback!

I'd be curious about testing here. Seems we don't have any automated tests (bad). Is there usually a manual testing phase when we make large changes to our terraform stuff?

}

func (r *integrationGcpResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
providerData, _ := request.ProviderData.(*FrameworkProvider)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I believe the , _ can be removed? It's a bool that makes this a safe cast, but we ignore the bool anyways. I think we'll get an NPE on the next two lines anyway if the cast fails.

---

# datadog_integration_gcp (Resource)

This resource is deprecateduse the `datadog_integration_gcp_sts resource` instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.
This resource is deprecateduse the `datadog_integration_gcp_sts` resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: why remove the spacing around the dash in these two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically guidance from documentation: #2230 (comment)

@@ -3,12 +3,12 @@
page_title: "datadog_integration_gcp Resource - terraform-provider-datadog"
subcategory: ""
description: |-
This resource is deprecateduse the datadog_integration_gcp_sts resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.
This resource is deprecateduse the datadog_integration_gcp_sts resource instead. Provides a Datadog - Google Cloud Platform integration resource. This can be used to create and manage Datadog - Google Cloud Platform integration.
Copy link

@ash-ddog ash-ddog Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add backticks to datadog_integration_gcp_sts as you did below?

@skarimo skarimo merged commit ab5bb04 into master Mar 12, 2024
10 checks passed
@skarimo skarimo deleted the sabbir.muhit/gcp-1258/add-resource-collection-flag-to-gcp-resources branch March 12, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants