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

Updating deployment code and adding tests #6

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/api/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func CreateDeployment(apiKey string, organizationId string, createRequest *Deplo
return decoded, nil
}

func UpdateDeployment(apiKey string, organizationId string, deploymentId string, updateRequest *DeploymentUpdateRequest) (*Workspace, error) {
func UpdateDeployment(apiKey string, organizationId string, deploymentId string, updateRequest *DeploymentUpdateRequest) (*DeploymentResponse, error) {
//TODO add validation etc here
//TODO consolidate marshalling code
if deploymentId == "" {
Expand All @@ -223,7 +223,7 @@ func UpdateDeployment(apiKey string, organizationId string, deploymentId string,

request, _ := http.NewRequest("POST", urlBase+organizationId+"/deployments/"+deploymentId, bytes.NewBuffer(b))

decoded := new(Workspace)
decoded := new(DeploymentResponse)
err = getObjectFromApi(apiKey, request, &decoded)
if err != nil {
return nil, fmt.Errorf("%s", err)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/deployment_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (d *DeploymentDataSource) Configure(ctx context.Context, req datasource.Con
}

d.token = provider.Token
d.organizationId = provider.OrganizationId
}

func (d *DeploymentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
74 changes: 74 additions & 0 deletions internal/provider/deployment_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestDeploymentDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testDeploymentDataSourceConfig("Deployment Workspace"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.astronomer_deployment.test", "name", "Test Deployment TF"),
resource.TestCheckResourceAttr("data.astronomer_deployment.test", "is_cicd_enforced", "true"),
resource.TestCheckResourceAttr("data.astronomer_deployment.test", "description", "A Standard Deployment"),
),
},
},
})
}

func testDeploymentDataSourceConfig(workspaceName string) string {
orgId := os.Getenv("ORGANIZATION_ID")
return fmt.Sprintf(`
provider "astronomer" {
organization_id = %[1]q
}

resource "astronomer_workspace" "test" {
name = "Test Deployment DS Workspace"
cicd_enforced_default = true
description = "TestAccDataSource"
}

resource "astronomer_deployment" "test" {
astro_runtime_version = "9.1.0"
cloud_provider = "AWS"
default_task_pod_cpu = "0.5"
default_task_pod_memory = "1Gi"
description = "A Standard Deployment"
executor = "CELERY"
is_dag_deploy_enforced = true
is_cicd_enforced = true
is_high_availability = true
name = "Test Deployment TF"
region = "us-east-1"
resource_quota_cpu = "160"
resource_quota_memory = "320Gi"
scheduler_size = "MEDIUM"
type = "STANDARD"
workspace_id = astronomer_workspace.test.id
worker_queues = [
{
astro_machine: "A5",
is_default: true,
max_worker_count: 1,
min_worker_count: 1,
name: "default",
worker_concurrency: 1,
},
]
}

data "astronomer_deployment" "test" {
id = astronomer_deployment.test.id
}
`, orgId, workspaceName)
}
87 changes: 62 additions & 25 deletions internal/provider/deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (r *DeploymentResource) Schema(ctx context.Context, req resource.SchemaRequ
Required: true,
},
"id": schema.StringAttribute{
Optional: true,
Computed: true,
},
"is_default": schema.BoolAttribute{
Required: true,
Expand Down Expand Up @@ -213,15 +213,15 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
CloudProvider: data.CloudProvider.ValueString(),
DefaultTaskPodCpu: data.DefaultTaskPodCpu.ValueString(),
DefaultTaskPodMemory: data.DefaultTaskPodMemory.ValueString(),
// Description: data.Description,
Executor: data.Executor.ValueString(),
IsCicdEnforced: data.IsCicdEnforced.ValueBool(),
IsDagDeployEnabled: data.IsDagDeployEnforced.ValueBool(),
IsHighAvailability: data.IsHighAvailability.ValueBool(),
Name: data.Name.ValueString(),
Region: data.Region.ValueString(),
ResourceQuotaCpu: data.ResourceQuotaCpu.ValueString(),
ResourceQuotaMemory: data.ResourceQuotaMemory.ValueString(),
Description: data.Description.ValueString(),
Executor: data.Executor.ValueString(),
IsCicdEnforced: data.IsCicdEnforced.ValueBool(),
IsDagDeployEnabled: data.IsDagDeployEnforced.ValueBool(),
IsHighAvailability: data.IsHighAvailability.ValueBool(),
Name: data.Name.ValueString(),
Region: data.Region.ValueString(),
ResourceQuotaCpu: data.ResourceQuotaCpu.ValueString(),
ResourceQuotaMemory: data.ResourceQuotaMemory.ValueString(),
// Scheduler: data.Sch,
SchedulerSize: data.SchedulerSize.ValueString(),
// TaskPodNodePoolId: data.Task,
Expand Down Expand Up @@ -259,26 +259,13 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
// data.TenantId
data.Type = types.StringValue(deployResponse.Type)
// data.VpcSubnetRange
workerQueuesDeployment := loadWorkerQueuesFromResponse(deployResponse)
data.WorkerQueues = workerQueuesDeployment
data.WorkspaceId = types.StringValue(deployResponse.WorkspaceId)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func loadWorkerQueuesFromTFState(data DeploymentResourceModel) []api.WorkerQueue {
var workerQueues []api.WorkerQueue
for _, value := range data.WorkerQueues {
workerQueues = append(workerQueues, api.WorkerQueue{
AstroMachine: value.AstroMachine.ValueString(),
IsDefault: value.IsDefault.ValueBool(),
MaxWorkerCount: int(value.MaxWorkerCount.ValueInt64()),
MinWorkerCount: int(value.MinWorkerCount.ValueInt64()),
Name: value.Name.ValueString(),
WorkerConcurrency: int(value.WorkerConcurrency.ValueInt64()),
})
}
return workerQueues
}

func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data DeploymentResourceModel

Expand All @@ -290,8 +277,17 @@ func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

// data.AstroRuntimeVersion = types.StringValue(deployment.Astro)
data.CloudProvider = types.StringValue(strings.ToUpper(deployment.CloudProvider))
data.Id = types.StringValue(deployment.Id)
data.DefaultTaskPodCpu = types.StringValue(deployment.DefaultTaskPodCpu)
data.DefaultTaskPodMemory = types.StringValue(deployment.DefaultTaskPodMemory)
data.Description = types.StringValue(deployment.Description)
data.Executor = types.StringValue(deployment.Executor)
data.IsCicdEnforced = types.BoolValue(deployment.IsCicdEnforced)
data.IsDagDeployEnforced = types.BoolValue(deployment.IsDagDeployEnabled) //TODO check names on this
data.IsHighAvailability = types.BoolValue(deployment.IsHighAvailability)

// data.DbInstanceType = types.StringValue(deployResponse.CloudProvider)
// data.IsLimited
// data.Metadata
Expand All @@ -300,6 +296,13 @@ func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest,
// data.PodSubnetRange = types.StringValue(deployResponse.OrganizationId)
// data.ProviderAccount = types.StringValue(deployResponse.OrganizationId)
data.Region = types.StringValue(deployment.Region)
data.ResourceQuotaCpu = types.StringValue(deployment.ResourceQuotaCpu)
data.ResourceQuotaMemory = types.StringValue(deployment.ResourceQuotaMemory)
data.SchedulerSize = types.StringValue(deployment.SchedulerSize)

workerQueues := loadWorkerQueuesFromResponse(deployment)
data.WorkerQueues = workerQueues

// data.ServicePeeringRange
// data.ServiceSubnetRange
// data.Tags
Expand All @@ -316,6 +319,38 @@ func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest,
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func loadWorkerQueuesFromTFState(data DeploymentResourceModel) []api.WorkerQueue {
var workerQueues []api.WorkerQueue
for _, value := range data.WorkerQueues {
workerQueues = append(workerQueues, api.WorkerQueue{
AstroMachine: value.AstroMachine.ValueString(),
Id: value.Id.ValueString(),
IsDefault: value.IsDefault.ValueBool(),
MaxWorkerCount: int(value.MaxWorkerCount.ValueInt64()),
MinWorkerCount: int(value.MinWorkerCount.ValueInt64()),
Name: value.Name.ValueString(),
WorkerConcurrency: int(value.WorkerConcurrency.ValueInt64()),
})
}
return workerQueues
}

func loadWorkerQueuesFromResponse(deployment *api.DeploymentResponse) []WorkerQueueModel {
var workerQueues []WorkerQueueModel
for _, value := range deployment.WorkerQueues {
workerQueues = append(workerQueues, WorkerQueueModel{
AstroMachine: types.StringValue(value.AstroMachine),
Id: types.StringValue(value.Id),
IsDefault: types.BoolValue(value.IsDefault),
MaxWorkerCount: types.Int64Value(int64(value.MaxWorkerCount)),
MinWorkerCount: types.Int64Value(int64(value.MinWorkerCount)),
Name: types.StringValue(value.Name),
WorkerConcurrency: types.Int64Value(int64(value.WorkerConcurrency)),
})
}
return workerQueues
}

func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data DeploymentResourceModel

Expand Down Expand Up @@ -354,6 +389,8 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

data.WorkerQueues = loadWorkerQueuesFromResponse(deployResponse)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand Down
84 changes: 84 additions & 0 deletions internal/provider/deployment_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDeploymentResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testDeploymentResourceConfig("TestDeployment"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("astronomer_deployment.test", "name", "TestDeployment"),
resource.TestCheckResourceAttr("astronomer_deployment.test", "astro_runtime_version", "9.1.0"),
resource.TestCheckResourceAttr("astronomer_deployment.test", "is_high_availability", "true"),
resource.TestCheckResourceAttr("astronomer_deployment.test", "is_cicd_enforced", "true"),
resource.TestCheckResourceAttr("astronomer_deployment.test", "is_dag_deploy_enforced", "true"),
),
},
{
ResourceName: "astronomer_deployment.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"astro_runtime_version"},
},
{
Config: testDeploymentResourceConfig("TestDeploymentUpdate"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("astronomer_deployment.test", "name", "TestDeploymentUpdate"),
),
},
},
})
}

func testDeploymentResourceConfig(name string) string {
orgId := os.Getenv("ORGANIZATION_ID")
return fmt.Sprintf(`
provider "astronomer" {
organization_id = %[1]q
}

resource "astronomer_workspace" "test" {
name = "TestDeploymentWorkspace"
cicd_enforced_default = true
description = "TestAccDataSource"
}

resource "astronomer_deployment" "test" {
astro_runtime_version = "9.1.0"
cloud_provider = "AWS"
default_task_pod_cpu = "0.5"
default_task_pod_memory = "1Gi"
description = "A Standard Deployment"
executor = "CELERY"
is_dag_deploy_enforced = true
is_cicd_enforced = true
is_high_availability = true
name = %[2]q
region = "us-east-1"
resource_quota_cpu = "160"
resource_quota_memory = "320Gi"
scheduler_size = "MEDIUM"
type = "STANDARD"
workspace_id = astronomer_workspace.test.id
worker_queues = [
{
astro_machine: "A5",
is_default: true,
max_worker_count: 1,
min_worker_count: 1,
name: "default",
worker_concurrency: 1,
},
]
}
`, orgId, name)
}