From 322849517fc98e3ab42dfef537938e9b309d8473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= <16577123+gregito@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:07:43 +0100 Subject: [PATCH] CDPCP-11620 - include encryptionAtHost parameter for Azure environment creation (#93) --- .../environments/model_azure_environment.go | 2 + .../resource_azure_environment_test.go | 11 +- .../environments/schema_azure_environment.go | 5 + .../schema_azure_environment_test.go | 214 ++++++++++++++++++ resources/environments/schema_test_utils.go | 18 ++ 5 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 resources/environments/schema_azure_environment_test.go create mode 100644 resources/environments/schema_test_utils.go diff --git a/resources/environments/model_azure_environment.go b/resources/environments/model_azure_environment.go index 42042f53..f5ae1e63 100644 --- a/resources/environments/model_azure_environment.go +++ b/resources/environments/model_azure_environment.go @@ -37,6 +37,8 @@ type azureEnvironmentResourceModel struct { EncryptionKeyURL types.String `tfsdk:"encryption_key_url"` + EncryptionAtHost types.Bool `tfsdk:"encryption_at_host"` + EnvironmentName types.String `tfsdk:"environment_name"` ExistingNetworkParams types.Object `tfsdk:"existing_network_params"` diff --git a/resources/environments/resource_azure_environment_test.go b/resources/environments/resource_azure_environment_test.go index 3df65c06..c9bcf001 100644 --- a/resources/environments/resource_azure_environment_test.go +++ b/resources/environments/resource_azure_environment_test.go @@ -15,15 +15,16 @@ import ( "errors" "testing" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" - "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models" - mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models" + mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" ) func createRawAzureEnvironmentResource() tftypes.Value { @@ -46,6 +47,7 @@ func createRawAzureEnvironmentResource() tftypes.Value { "credential_name": tftypes.String, "description": tftypes.String, "encryption_key_resource_group_name": tftypes.String, + "encryption_at_host": tftypes.Bool, "public_key": tftypes.String, "status": tftypes.String, "enable_tunnel": tftypes.Bool, @@ -124,6 +126,7 @@ func createRawAzureEnvironmentResource() tftypes.Value { "credential_name": tftypes.NewValue(tftypes.String, ""), "description": tftypes.NewValue(tftypes.String, ""), "encryption_key_resource_group_name": tftypes.NewValue(tftypes.String, ""), + "encryption_at_host": tftypes.NewValue(tftypes.Bool, false), "public_key": tftypes.NewValue(tftypes.String, ""), "status": tftypes.NewValue(tftypes.String, ""), "enable_tunnel": tftypes.NewValue(tftypes.Bool, false), diff --git a/resources/environments/schema_azure_environment.go b/resources/environments/schema_azure_environment.go index c4560b95..baa013db 100644 --- a/resources/environments/schema_azure_environment.go +++ b/resources/environments/schema_azure_environment.go @@ -105,6 +105,10 @@ var AzureEnvironmentSchema = schema.Schema{ "encryption_key_url": schema.StringAttribute{ Optional: true, }, + "encryption_at_host": schema.BoolAttribute{ + Optional: true, + Default: booldefault.StaticBool(false), + }, "environment_name": schema.StringAttribute{ Required: true, }, @@ -279,6 +283,7 @@ func ToAzureEnvironmentRequest(ctx context.Context, model *azureEnvironmentResou req.EnvironmentName = model.EnvironmentName.ValueStringPointer() req.EndpointAccessGatewayScheme = model.EndpointAccessGatewayScheme.ValueString() req.EndpointAccessGatewaySubnetIds = utils.FromSetValueToStringList(model.EndpointAccessGatewaySubnetIds) + req.EncryptionAtHost = model.EncryptionAtHost.ValueBool() if !model.ExistingNetworkParams.IsNull() && !model.ExistingNetworkParams.IsUnknown() { tflog.Debug(ctx, "existing network params") var existingNetworkParams existingAzureNetwork diff --git a/resources/environments/schema_azure_environment_test.go b/resources/environments/schema_azure_environment_test.go new file mode 100644 index 00000000..57adb883 --- /dev/null +++ b/resources/environments/schema_azure_environment_test.go @@ -0,0 +1,214 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package environments + +import ( + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "testing" +) + +var schemaElements = []SchemaTestCaseStructure{ + { + name: "id field must exist and be valid", + field: "id", + computed: true, + shouldBeRequired: false, + }, + { + name: "polling_options should exist and be valid", + field: "polling_options", + computed: false, + shouldBeRequired: false, + }, + { + name: "crn should exist and be valid", + field: "crn", + computed: true, + shouldBeRequired: false, + }, + { + name: "create_private_endpoints should exist and be valid", + field: "create_private_endpoints", + computed: false, + shouldBeRequired: false, + }, + { + name: "credential_name must exist and be valid", + field: "credential_name", + computed: false, + shouldBeRequired: true, + }, + { + name: "description should exist and be valid", + field: "description", + computed: true, + shouldBeRequired: false, + }, + { + name: "enable_outbound_load_balancer should exist and be valid", + field: "enable_outbound_load_balancer", + computed: false, + shouldBeRequired: false, + }, + { + name: "enable_tunnel should exist and be valid", + field: "enable_tunnel", + computed: true, + shouldBeRequired: false, + }, + { + name: "endpoint_access_gateway_scheme should exist and be valid", + field: "endpoint_access_gateway_scheme", + computed: false, + shouldBeRequired: false, + }, + { + name: "endpoint_access_gateway_subnet_ids should exist and be valid", + field: "endpoint_access_gateway_subnet_ids", + computed: false, + shouldBeRequired: false, + }, + { + name: "encryption_key_resource_group_name should exist and be valid", + field: "encryption_key_resource_group_name", + computed: false, + shouldBeRequired: false, + }, + { + name: "encryption_key_url should exist and be valid", + field: "encryption_key_url", + computed: false, + shouldBeRequired: false, + }, + { + name: "encryption_at_host should exist and be valid", + field: "encryption_at_host", + computed: false, + shouldBeRequired: false, + }, + { + name: "environment_name should exist and be valid", + field: "environment_name", + computed: false, + shouldBeRequired: true, + }, + { + name: "existing_network_params should exist and be valid", + field: "existing_network_params", + computed: true, + shouldBeRequired: false, + }, + { + name: "freeipa should exist and be valid", + field: "freeipa", + computed: true, + shouldBeRequired: false, + }, + { + name: "log_storage should exist and be valid", + field: "log_storage", + computed: false, + shouldBeRequired: true, + }, + { + name: "new_network_params should exist and be valid", + field: "new_network_params", + computed: true, + shouldBeRequired: false, + }, + { + name: "proxy_config_name should exist and be valid", + field: "proxy_config_name", + computed: false, + shouldBeRequired: false, + }, + { + name: "public_key should exist and be valid", + field: "public_key", + computed: false, + shouldBeRequired: true, + }, + { + name: "region should exist and be valid", + field: "region", + computed: false, + shouldBeRequired: true, + }, + { + name: "report_deployment_logs should exist and be valid", + field: "report_deployment_logs", + computed: true, + shouldBeRequired: false, + }, + { + name: "resource_group_name should exist and be valid", + field: "resource_group_name", + computed: true, + shouldBeRequired: false, + }, + { + name: "security_access should exist and be valid", + field: "security_access", + computed: false, + shouldBeRequired: true, + }, + { + name: "status should exist and be valid", + field: "status", + computed: true, + shouldBeRequired: false, + }, + { + name: "status_reason should exist and be valid", + field: "status_reason", + computed: true, + shouldBeRequired: false, + }, + { + name: "tags should exist and be valid", + field: "tags", + computed: true, + shouldBeRequired: false, + }, + { + name: "use_public_ip should exist and be valid", + field: "use_public_ip", + computed: false, + shouldBeRequired: true, + }, + { + name: "workload_analytics should exist and be valid", + field: "workload_analytics", + computed: true, + shouldBeRequired: false, + }, +} + +func TestSchemaContainsElements(t *testing.T) { + for _, test := range schemaElements { + performValidation(t, test, AzureEnvironmentSchema.Attributes[test.field]) + } +} + +func performValidation(t *testing.T, test SchemaTestCaseStructure, attr schema.Attribute) { + t.Run(test.name, func(t *testing.T) { + if attr == nil { + t.Errorf("The following field does not exists, however it should: " + test.field) + t.FailNow() + } + if attr.IsRequired() != test.shouldBeRequired { + t.Errorf("The '%s' filed's >required< property should be: %t", test.field, test.shouldBeRequired) + } + if attr.IsComputed() != test.computed { + t.Errorf("The '%s' filed's >computed< property should be: %t", test.field, test.computed) + } + }) +} diff --git a/resources/environments/schema_test_utils.go b/resources/environments/schema_test_utils.go new file mode 100644 index 00000000..2cd5f4d7 --- /dev/null +++ b/resources/environments/schema_test_utils.go @@ -0,0 +1,18 @@ +// Copyright 2024 Cloudera. All Rights Reserved. +// +// This file is licensed under the Apache License Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. +// +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS +// OF ANY KIND, either express or implied. Refer to the License for the specific +// permissions and limitations governing your use of the file. + +package environments + +type SchemaTestCaseStructure struct { + name string + field string + computed bool + shouldBeRequired bool +}