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

Add terraform import script #130

Merged
merged 56 commits into from
Aug 21, 2024
Merged

Add terraform import script #130

merged 56 commits into from
Aug 21, 2024

Conversation

ichung08
Copy link
Collaborator

@ichung08 ichung08 commented Aug 14, 2024

Description

Add an import script that will enable users to migrate all of their resources to be managed by terraform. The script will read from existing resources (by listing all orgs, deployments, clusters, etc) and then correctly import them into terraform.

How the script works:

  1. Parses all the resources to be imported (which are passed in as args)
  2. For each type of resource (eg. Workspace, Cluster, Deployment etc), it gets all the entity ids
  3. For each entity id, it generates a Terraform import block in the form of:
import {
	id = "%v"
	to = astro_resource.resource_%v
}
  1. After generating all the import blocks, it concatenates all the outputs and writes it to a file, import.tf
  2. Once the import blocks are written, it then generates the Terraform HCL configuration using terraform plan -generate-config-out=generated.tf, writing all Terraform resource configs to a file, generated.tf
  3. Lastly, the script auto approves and runs terraform apply on the new resources, successfully importing all resources!

Example import.tf file:

terraform {
	required_providers {
		astro = {
			source = "registry.terraform.io/astronomer/astro"
		}
	}
}

provider "astro" {
	organization_id = "clx42kkcm01fo01o06agtmshg"
	host = "https://api.astronomer-dev.io"
}

import {
	id = "clzlybvvw00kv01j6o0fq77ep"
	to = astro_workspace.workspace_clzlybvvw00kv01j6o0fq77ep
}

import {
	id = "clx42trhg01go01o07e0dozht"
	to = astro_cluster.cluster_clx42trhg01go01o07e0dozht
}

import {
	id = "clx42ugkb01gq01od03k7c49a"
	to = astro_cluster.cluster_clx42ugkb01gq01od03k7c49a
}

import {
	id = "clzawipbm00bm01qw98vzzoca"
	to = astro_user_roles.user_clzawipbm00bm01qw98vzzoca
}

Example generated.tf file:

resource "astro_workspace" "workspace_clzlm6cev00ki01lyvqyyoaal" {
  cicd_enforced_default = false
  description           = "Created by Terraform Acceptance Test - should self-cleanup but can delete manually if needed after 2 hours."
  name                  = "TFAcceptanceTest_QPROZQXNVZ_workspace"
}

# __generated__ by Terraform from "clx42trhg01go01o07e0dozht"
resource "astro_cluster" "cluster_clx42trhg01go01o07e0dozht" {
  cloud_provider        = "AWS"
  name                  = "Terraform_AWS_Cluster_DND"
  pod_subnet_range      = null
  region                = "us-east-1"
  service_peering_range = null
  service_subnet_range  = null
  timeouts              = null
  type                  = "DEDICATED"
  vpc_subnet_range      = "172.20.0.0/20"
  workspace_ids         = []
}

# __generated__ by Terraform from "cl7qqe4tf264442d28fttoe7g8"
resource "astro_user_roles" "user_cl7qqe4tf264442d28fttoe7g8" {
  deployment_roles  = null
  organization_role = "ORGANIZATION_OWNER"
  user_id           = "cl7qqe4tf264442d28fttoe7g8"
  workspace_roles   = null
}

🎟 Issue(s)

#115

Migration Doc

🧪 Functional Testing

Workspace
Screenshot 2024-08-14 at 7 17 33 PM

Cluster
Screenshot 2024-08-14 at 7 22 08 PM

Hybrid Cluster Workspace Authorization
No hybrid clusters to test with

Deployment

Team
Screenshot 2024-08-14 at 7 33 51 PM

Team Roles
Screenshot 2024-08-14 at 7 34 36 PM

API Token
Screenshot 2024-08-14 at 7 36 25 PM

User Roles
Screenshot 2024-08-14 at 7 38 19 PM

Importing Multiple Resources
Screenshot 2024-08-14 at 7 43 17 PM

📸 Screenshots

📋 Checklist

  • Added/updated applicable tests
  • Added/updated examples in the examples/ directory
  • Updated any related documentation

@ichung08 ichung08 self-assigned this Aug 14, 2024
@ichung08 ichung08 linked an issue Aug 14, 2024 that may be closed by this pull request
import/import.go Outdated Show resolved Hide resolved
@ichung08 ichung08 marked this pull request as ready for review August 15, 2024 03:38
@ichung08 ichung08 requested review from vandyliu, sunkickr and a team as code owners August 15, 2024 03:38
@ichung08
Copy link
Collaborator Author

Any go logging library recommendations? The logging works, but want to improve the look of the logs

import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
import/import.go Outdated Show resolved Hide resolved
on:
push:
tags:
- 'import-v*'
Copy link
Collaborator

Choose a reason for hiding this comment

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

dont forget to test this after merge

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
}(resource)
}

go func() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious why we need a goroutine here

Copy link
Collaborator Author

@ichung08 ichung08 Aug 21, 2024

Choose a reason for hiding this comment

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

the block above parallelizes the import block generation step for each resource in individual go routines

to ensure that all the go routines are finished, we need a separate goroutine to wait for all the goroutines to finish writing to the channel before closing the channel

Copy link
Collaborator

Choose a reason for hiding this comment

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

would it work the same without the goroutine?
to me, it seems like without go routine makes more sense so just

wg.Wait()
close(results)

but maybe im missing something

import/import_script.go Outdated Show resolved Hide resolved
import/import_script.go Outdated Show resolved Hide resolved
return nil
}

func generateDeploymentHCL(ctx context.Context, platformClient *platform.ClientWithResponses, organizationId string) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

whats HCL mean here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

HashiCorp Configuration Language

"golang.org/x/exp/maps"
)

func HandleWorkspaces(ctx context.Context, platformClient *mocksPlatform.ClientWithResponsesInterface, iamClient *mocksIam.ClientWithResponsesInterface, organizationId string) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can all these functions just go in the test file instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, i wanted to separate it so its cleaner but i can move them all in there too

@ichung08 ichung08 enabled auto-merge (squash) August 21, 2024 01:14
@ichung08 ichung08 merged commit 532f085 into main Aug 21, 2024
9 checks passed
@ichung08 ichung08 deleted the 115-add-terraform-import-script branch August 21, 2024 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add terraform import script
2 participants