From 0900726a70060ef083e1a7f186f2f7c4ed73abd7 Mon Sep 17 00:00:00 2001 From: Huy Nguyen Date: Mon, 9 Dec 2024 11:50:53 +1000 Subject: [PATCH] Add test for project scope retrieve in deployment freeze data-source --- .../datasource_deployment_freeze_test.go | 46 ++++++++++++++----- .../schemas/deployment_freeze.go | 4 ++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/octopusdeploy_framework/datasource_deployment_freeze_test.go b/octopusdeploy_framework/datasource_deployment_freeze_test.go index 55c1b359..9e0a4a78 100644 --- a/octopusdeploy_framework/datasource_deployment_freeze_test.go +++ b/octopusdeploy_framework/datasource_deployment_freeze_test.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "strings" "testing" "time" ) @@ -24,30 +25,40 @@ func TestAccDataSourceDeploymentFreezes(t *testing.T) { ProtoV6ProviderFactories: ProtoV6ProviderFactories(), Steps: []resource.TestStep{ { - Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, false, projectName, environmentName, tenantName), + Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, false, false, projectName, environmentName, tenantName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "id"), resource.TestCheckResourceAttr(dataSourceName, "partial_name", freezeName), resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.#", "1"), resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.name", freezeName), resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "0"), ), }, { - Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, projectName, environmentName, tenantName), + Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, false, projectName, environmentName, tenantName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "1"), + ), + }, + { + Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, true, projectName, environmentName, tenantName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "id"), resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "1"), resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.tenant_id"), resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.project_id"), resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.environment_id"), + resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "1"), ), }, }, }) } -func testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName string, startTime, endTime time.Time, includeTenant bool, projectName, environmentName, tenantName string) string { +func testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName string, startTime, endTime time.Time, includeProject bool, includeTenant bool, projectName, environmentName, tenantName string) string { baseConfig := fmt.Sprintf(` resource "octopusdeploy_space" "test_space" { name = "%s" @@ -98,6 +109,17 @@ resource "octopusdeploy_deployment_freeze" "test_freeze" { } `, spaceName, environmentName, projectName, freezeName, startTime.Format(time.RFC3339), endTime.Format(time.RFC3339)) + if includeProject { + projectConfig := fmt.Sprintf(` +resource "octopusdeploy_deployment_freeze_project" "test_project_scope" { + deploymentfreeze_id = octopusdeploy_deployment_freeze.test_freeze.id + project_id = octopusdeploy_project.test_project.id + environment_ids = [octopusdeploy_environment.test_environment.id] +} +`) + baseConfig = baseConfig + projectConfig + } + if includeTenant { tenantConfig := fmt.Sprintf(` resource "octopusdeploy_tenant" "test_tenant" { @@ -134,17 +156,16 @@ data "octopusdeploy_deployment_freezes" "test_freeze" { take = 1 depends_on = [` + deps := []string{"octopusdeploy_deployment_freeze.test_freeze"} + if includeProject { + deps = append(deps, "octopusdeploy_deployment_freeze_project.test_project_scope") + } if includeTenant { - datasourceConfig += ` - octopusdeploy_deployment_freeze.test_freeze, - octopusdeploy_deployment_freeze_tenant.test_tenant_scope - ` - } else { - datasourceConfig += ` - octopusdeploy_deployment_freeze.test_freeze - ` + deps = append(deps, "octopusdeploy_deployment_freeze_tenant.test_tenant_scope") } + datasourceConfig += "\n " + strings.Join(deps, ",\n ") + "\n " + datasourceConfig += `] } @@ -156,5 +177,6 @@ output "octopus_freeze_id" { value = data.octopusdeploy_deployment_freezes.test_freeze.deployment_freezes[0].id } ` - return baseConfig + fmt.Sprintf(datasourceConfig, freezeName) + var config = baseConfig + fmt.Sprintf(datasourceConfig, freezeName) + return config } diff --git a/octopusdeploy_framework/schemas/deployment_freeze.go b/octopusdeploy_framework/schemas/deployment_freeze.go index 810c81d6..6debd90b 100644 --- a/octopusdeploy_framework/schemas/deployment_freeze.go +++ b/octopusdeploy_framework/schemas/deployment_freeze.go @@ -93,19 +93,23 @@ func (d DeploymentFreezeSchema) GetDatasourceSchema() datasourceSchema.Schema { "name": GetReadonlyNameDatasourceSchema(), "start": datasourceSchema.StringAttribute{ Description: "The start time of the freeze", + Optional: false, Computed: true, }, "end": datasourceSchema.StringAttribute{ Description: "The end time of the freeze", + Optional: false, Computed: true, }, "project_environment_scope": datasourceSchema.MapAttribute{ ElementType: types.ListType{ElemType: types.StringType}, Description: "The project environment scope of the deployment freeze", + Optional: false, Computed: true, }, "tenant_project_environment_scope": datasourceSchema.ListNestedAttribute{ Description: "The tenant project environment scope of the deployment freeze", + Optional: false, Computed: true, NestedObject: datasourceSchema.NestedAttributeObject{ Attributes: map[string]datasourceSchema.Attribute{