From dd5c6f38cb0c3c742136891cab3dd97713445122 Mon Sep 17 00:00:00 2001 From: Isaac Chung <69920967+ichung08@users.noreply.github.com> Date: Fri, 23 Aug 2024 10:39:20 -0700 Subject: [PATCH] 133 bug bash script fixes (#139) --- .gitignore | 1 + CONTRIBUTING.md | 179 ++++++++++++++++++ README.md | 43 ++++- docs/data-sources/api_token.md | 7 +- docs/data-sources/api_tokens.md | 7 +- docs/data-sources/cluster.md | 7 +- docs/data-sources/cluster_options.md | 5 + docs/data-sources/clusters.md | 5 + docs/data-sources/deployment.md | 7 +- docs/data-sources/deployment_options.md | 7 +- docs/data-sources/deployments.md | 5 + docs/data-sources/organization.md | 7 +- docs/data-sources/team.md | 7 +- docs/data-sources/teams.md | 5 + docs/data-sources/user.md | 7 +- docs/data-sources/users.md | 5 + docs/data-sources/workspace.md | 7 +- docs/data-sources/workspaces.md | 5 + .../astro_api_token/data-source.tf | 7 +- .../astro_api_tokens/data-source.tf | 7 +- .../data-sources/astro_cluster/data-source.tf | 7 +- .../astro_cluster_options/data-source.tf | 5 + .../astro_clusters/data-source.tf | 5 + .../astro_deployment/data-source.tf | 7 +- .../astro_deployment_options/data-source.tf | 7 +- .../astro_deployments/data-source.tf | 5 + .../astro_organization/data-source.tf | 7 +- .../data-sources/astro_team/data-source.tf | 7 +- .../data-sources/astro_teams/data-source.tf | 5 + .../data-sources/astro_user/data-source.tf | 7 +- .../data-sources/astro_users/data-source.tf | 5 + .../astro_workspace/data-source.tf | 7 +- .../astro_workspaces/data-source.tf | 5 + import/import_script.go | 90 +++++++-- 34 files changed, 468 insertions(+), 31 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/.gitignore b/.gitignore index ef9299ab..7f2e7b5e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ website/node_modules import.tf generated.tf import/import_script +terraform-provider-astro-import-script* website/vendor test_results diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..0301e566 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,179 @@ +# Contributing to Terraform Provider Astro + +Welcome to the Terraform Provider Astro project! We're excited that you're interested in contributing. This document will guide you through the process of setting up your development environment, making changes, submitting pull requests, and reporting issues. + +## Table of Contents + +1. [Development Environment Setup](#development-environment-setup) +2. [Making Changes](#making-changes) +3. [Testing](#testing) +4. [Submitting Pull Requests](#submitting-pull-requests) +5. [Reporting Issues](#reporting-issues) +6. [Best Practices](#best-practices) +7. [Additional Resources](#additional-resources) + +## Development Environment Setup + +### Prerequisites + +Ensure you have the following installed: +- [Terraform](https://developer.hashicorp.com/terraform/downloads) >= 1.7 +- [Go](https://golang.org/doc/install) >= 1.21 + +### Setting up the Provider for Local Development + +1. Clone the repository: + ``` + git clone https://github.com/astronomer/terraform-provider-astro.git + cd terraform-provider-astro + ``` + +2. Build the provider: + ``` + make dep + make build + ``` + +3. Create a `.terraformrc` file in your home directory (`~`) with the following content: + ```hcl + provider_installation { + dev_overrides { + "registry.terraform.io/astronomer/astro" = "/path/to/your/terraform-provider-astro/bin" + } + direct {} + } + ``` + Replace `/path/to/your/terraform-provider-astro/bin` with the actual path to the `bin` directory in your cloned repository. + +4. Create a `main.tf` file for testing: + ```hcl + terraform { + required_providers { + astro = { + source = "astronomer/astro" + } + } + } + + provider "astro" { + organization_id = "" + } + + # Add resources and data sources here for testing + ``` + +5. Set up your Astro API token: + ``` + export ASTRO_API_TOKEN= + ``` + +## Making Changes + +1. Create a new branch for your changes: + ``` + git checkout -b feature/your-feature-name + ``` + +2. Make your changes in the appropriate files. Common areas for changes include: + - `internal/provider/` for provider logic + - `internal/resources/` for resource implementations + - `internal/datasources/` for data source implementations + - `examples/` for example configurations + +3. Update or add tests in the corresponding `*_test.go` files. If a new data source or resource is added, create a new test file in the `*_test.go` file for that feature. + +4. Update documentation if your changes affect the provider's behavior or add new features. + +## Testing + +1. Run unit tests: + ``` + make test + ``` + +2. Run acceptance tests (these will create real resources in your Astro account): + ``` + make testacc + ``` + Note: Ensure all required environment variables are set as described in `internal/provider/provider_test_utils.go`. + +3. Test your changes manually using the `main.tf` file you created earlier: + ``` + terraform init + terraform plan + terraform apply + ``` + +## Submitting Pull Requests + +1. Commit your changes: + ``` + git add . + git commit -m "Description of your changes" + ``` + +2. Push your branch to GitHub: + ``` + git push origin feature/your-feature-name + ``` + +3. Open a pull request on GitHub. + +4. In your pull request description, include: + - A clear description of the changes + - Any related issue numbers + - Steps to test the changes + - Screenshots or code snippets if applicable + +## Reporting Issues + +If you encounter a bug or have a suggestion for improvement, please create an issue on the GitHub repository. This helps us track and address problems efficiently. + +### Creating an Issue + +1. Go to the [Issues page](https://github.com/astronomer/terraform-provider-astro/issues) of the repository. +2. Click on "New Issue". +3. Choose the appropriate issue template if available, or start with a blank issue. +4. Provide a clear and concise title that summarizes the issue. +5. In the description, include: + - A detailed description of the bug or feature request + - Steps to reproduce the issue (for bugs) + - Expected behavior + - Actual behavior (for bugs) + - Screenshots or error messages, if applicable + - Your environment details: + - Terraform version + - Terraform Provider Astro version + - Operating System + - Any other relevant information +6. Add appropriate labels to the issue (e.g., "bug", "enhancement", "documentation") +7. Submit the issue + +### Best Practices for Issue Reporting + +- Search existing issues before creating a new one to avoid duplicates. +- One issue per report. If you have multiple bugs or feature requests, create separate issues for each. +- Be responsive to questions or requests for additional information. +- If you find a solution to your problem before the issue is resolved, add a comment describing the solution for others who might encounter the same issue. + +### Security Issues + +If you discover a security vulnerability, please do NOT open an issue. Email security@astronomer.io instead. + +## Best Practices + +- Follow Go best practices and conventions. +- Ensure your code is well-commented and easy to understand. +- Keep your changes focused. If you're working on multiple features, submit separate pull requests. +- Update the README.md if your changes affect the overall usage of the provider. +- Add example configurations to the `examples/` directory for any new features or resources. +- If you're adding a new resource or data source, ensure you've added corresponding acceptance tests. + +## Additional Resources + +- [Terraform Provider Astro Documentation](https://registry.terraform.io/providers/astronomer/astro/latest/docs) +- [Astro API Documentation](https://docs.astronomer.io/astro/api-overview) +- [Terraform Plugin Framework Documentation](https://developer.hashicorp.com/terraform/plugin/framework) +- [Go Documentation](https://golang.org/doc/) + +Thank you for contributing to the Terraform Provider Astro project! Your efforts help improve the experience for all Astro users. \ No newline at end of file diff --git a/README.md b/README.md index 88101c13..2daf6683 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,6 @@ On Windows: - `-resources`: Comma-separated list of resources to import. Accepted values are workspace, deployment, cluster, api_token, team, team_roles, user_roles. - `-token`: API token to authenticate with the Astro platform. If not provided, the script will attempt to use the `ASTRO_API_TOKEN` environment variable. -- `-host`: API host to connect to. Default is https://api.astronomer.io. Use "dev" for https://api.astronomer-dev.io or "stage" for https://api.astronomer-stage.io. - `-organizationId`: Organization ID to import resources from. - `-runTerraformInit`: Run `terraform init` after generating the import configuration. Used for initializing the Terraform state in our GitHub Actions. - `-help`: Display help information. @@ -246,7 +245,7 @@ On Windows: 3. Use a different API host (e.g., dev environment): ``` - ./terraform-provider-astro-import-script___ -resources workspace -token your_api_token -organizationId your_org_id -host dev + ./terraform-provider-astro-import-script___ -resources workspace -token your_api_token -organizationId your_org_id ``` ### Output @@ -262,3 +261,43 @@ The script will generate two main files: - The generated Terraform configurations may require some manual adjustment to match your specific requirements or to resolve any conflicts. - Always review the generated files before applying them to your Terraform state. +## FAQ and Troubleshooting + +### Frequently Asked Questions + +1. **What resources can I manage with this Terraform provider?** + - Workspaces, deployments, clusters, hybrid cluster workspace authorizations, API tokens, teams, team roles, and user roles. + +2. **How do I authenticate with the Astro API?** + - Use an API token set as the `ASTRO_API_TOKEN` environment variable or add it to the provider configuration. + +3. **Can I import existing Astro resources into Terraform?** + - Yes, use the Astro Terraform Import Script to generate import blocks and resource configurations. + +4. **What Terraform versions are required?** + - Terraform >= 1.7. + +5. **How can I contribute to the provider's development?** + - Submit pull requests, report issues, or suggest improvements on the GitHub repository. + +### Troubleshooting + +1. **Issue: 401 Unauthorized error when running `terraform plan` or `terraform apply`** + + Solution: Your API token may have expired. Update your `ASTRO_API_TOKEN` environment variable with a fresh token: + ``` + export ASTRO_API_TOKEN= + ``` + +2. **Issue: Import script fails to find resources** + + Solution: + - Ensure you have the correct permissions in your Astro organization. + - Verify that your API token is valid and has the necessary scopes and permissions. + - Double-check the organization ID provided to the script. + +3. **Issue: "Error: Invalid provider configuration" when initializing Terraform** + + Solution: Ensure your `.terraformrc` file is correctly set up, especially if you're using a local build of the provider for development. + +If you encounter any issues not listed here, please check the [GitHub Issues](https://github.com/astronomer/terraform-provider-astro/issues) page or open a new issue with details about your problem. \ No newline at end of file diff --git a/docs/data-sources/api_token.md b/docs/data-sources/api_token.md index c79f4242..a23b559a 100644 --- a/docs/data-sources/api_token.md +++ b/docs/data-sources/api_token.md @@ -13,9 +13,14 @@ API Token data source ## Example Usage ```terraform -data "astro_api_token" "example" { +data "astro_api_token" "example_api_token" { id = "clxm4836f00ql01me3nigmcr6" } + +# Output the API token using terraform apply +output "api_token" { + value = data.astro_api_token.example_api_token +} ``` diff --git a/docs/data-sources/api_tokens.md b/docs/data-sources/api_tokens.md index 4936586e..b172b53f 100644 --- a/docs/data-sources/api_tokens.md +++ b/docs/data-sources/api_tokens.md @@ -13,7 +13,7 @@ API Tokens data source ## Example Usage ```terraform -data "astro_api_tokens" "example" {} +data "astro_api_tokens" "example_api_tokens" {} data "astro_api_tokens" "organization_only_example" { include_only_organization_tokens = true @@ -26,6 +26,11 @@ data "astro_api_tokens" "workspace_example" { data "astro_api_tokens" "deployment_example" { deployment_id = "clx44jyu001m201m5dzsbexqr" } + +# Output the API tokens using terraform apply +output "api_tokens" { + value = data.astro_api_tokens.example_api_tokens +} ``` diff --git a/docs/data-sources/cluster.md b/docs/data-sources/cluster.md index 8381e519..8f5d2d9c 100644 --- a/docs/data-sources/cluster.md +++ b/docs/data-sources/cluster.md @@ -13,9 +13,14 @@ Cluster data source ## Example Usage ```terraform -data "astro_cluster" "example" { +data "astro_cluster" "example_cluster" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the cluster value using terraform apply +output "cluster" { + value = data.astro_cluster.example_cluster +} ``` diff --git a/docs/data-sources/cluster_options.md b/docs/data-sources/cluster_options.md index 55569ffc..b7497ddf 100644 --- a/docs/data-sources/cluster_options.md +++ b/docs/data-sources/cluster_options.md @@ -21,6 +21,11 @@ data "astro_cluster_options" "example_cluster_options_filter_by_provider" { type = "HYBRID" cloud_provider = "AWS" } + +# Output the cluster options value using terraform apply +output "cluster_options" { + value = data.astro_cluster_options.example_cluster_options +} ``` diff --git a/docs/data-sources/clusters.md b/docs/data-sources/clusters.md index 654ba22e..d2a7c1a5 100644 --- a/docs/data-sources/clusters.md +++ b/docs/data-sources/clusters.md @@ -22,6 +22,11 @@ data "astro_clusters" "example_clusters_filter_by_names" { data "astro_clusters" "example_clusters_filter_by_cloud_provider" { cloud_provider = "AWS" } + +# Output the clusters value using terraform apply +output "clusters" { + value = data.astro_clusters.example_clusters +} ``` diff --git a/docs/data-sources/deployment.md b/docs/data-sources/deployment.md index 5831ae3e..c79f5d53 100644 --- a/docs/data-sources/deployment.md +++ b/docs/data-sources/deployment.md @@ -13,9 +13,14 @@ Deployment data source ## Example Usage ```terraform -data "astro_deployment" "example" { +data "astro_deployment" "example_deployment" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the deployment value using terraform apply +output "deployment" { + value = data.astro_deployment.example_deployment +} ``` diff --git a/docs/data-sources/deployment_options.md b/docs/data-sources/deployment_options.md index c8a1c143..099918d4 100644 --- a/docs/data-sources/deployment_options.md +++ b/docs/data-sources/deployment_options.md @@ -13,7 +13,7 @@ Deployment options data source ## Example Usage ```terraform -data "astro_deployment_options" "example" {} +data "astro_deployment_options" "example_deployment_options" {} data "astro_deployment_options" "example_with_deployment_id_query_param" { deployment_id = "clozc036j01to01jrlgvueo8t" @@ -30,6 +30,11 @@ data "astro_deployment_options" "example_with_executor_query_param" { data "astro_deployment_options" "example_with_cloud_provider_query_param" { cloud_provider = "AWS" } + +# Output the deployment options value using terraform apply +output "deployment_options" { + value = data.astro_deployment_options.example_deployment_options +} ``` diff --git a/docs/data-sources/deployments.md b/docs/data-sources/deployments.md index 39ef2c1d..8250168f 100644 --- a/docs/data-sources/deployments.md +++ b/docs/data-sources/deployments.md @@ -26,6 +26,11 @@ data "astro_deployments" "example_deployments_filter_by_deployment_ids" { data "astro_deployments" "example_deployments_filter_by_workspace_ids" { workspace_ids = ["clozc036j01to01jrlgvu798d"] } + +# Output the deployments value using terraform apply +output "deployments" { + value = data.astro_deployments.example_deployments +} ``` diff --git a/docs/data-sources/organization.md b/docs/data-sources/organization.md index 93cb836f..48045ebf 100644 --- a/docs/data-sources/organization.md +++ b/docs/data-sources/organization.md @@ -13,7 +13,12 @@ Organization data source ## Example Usage ```terraform -data "astro_organization" "example" {} +data "astro_organization" "example_organization" {} + +# Output the organization value using terraform apply +output "organization" { + value = data.astro_organization.example_organization +} ``` diff --git a/docs/data-sources/team.md b/docs/data-sources/team.md index f8a8d12f..187def04 100644 --- a/docs/data-sources/team.md +++ b/docs/data-sources/team.md @@ -13,9 +13,14 @@ Team data source ## Example Usage ```terraform -data "astro_team" "example" { +data "astro_team" "example_team" { id = "clwbclrc100bl01ozjj5s4jmq" } + +# Output the team value using terraform apply +output "team" { + value = data.astro_team.example_team +} ``` diff --git a/docs/data-sources/teams.md b/docs/data-sources/teams.md index a6d13101..07fef681 100644 --- a/docs/data-sources/teams.md +++ b/docs/data-sources/teams.md @@ -18,6 +18,11 @@ data "astro_teams" "example_teams" {} data "astro_teams" "example_teams_filter_by_names" { names = ["my first team", "my second team"] } + +# Output the teams value using terraform apply +output "example_teams" { + value = data.astro_teams.example_teams +} ``` diff --git a/docs/data-sources/user.md b/docs/data-sources/user.md index 49ffd27c..b71bbb7b 100644 --- a/docs/data-sources/user.md +++ b/docs/data-sources/user.md @@ -13,9 +13,14 @@ User data source ## Example Usage ```terraform -data "astro_user" "example" { +data "astro_user" "example_user" { id = "clhpichn8002m01mqa4ocs7g6" } + +# Output the user value using terraform apply +output "user" { + value = data.astro_user.example_user +} ``` diff --git a/docs/data-sources/users.md b/docs/data-sources/users.md index 31d2abc4..46f7019c 100644 --- a/docs/data-sources/users.md +++ b/docs/data-sources/users.md @@ -22,6 +22,11 @@ data "astro_users" "example_users_filter_by_workspace_id" { data "astro_users" "example_users_filter_by_deployment_id" { deployment_id = "clx44jyu001m201m5dzsbexqr" } + +# Output the users value using terraform apply +output "example_users" { + value = data.astro_users.example_users +} ``` diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index b5522e12..0fae3d5d 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -13,9 +13,14 @@ Workspace data source ## Example Usage ```terraform -data "astro_workspace" "example" { +data "astro_workspace" "example_workspace" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the workspace value using terraform apply +output "workspace" { + value = data.astro_workspace.example_workspace +} ``` diff --git a/docs/data-sources/workspaces.md b/docs/data-sources/workspaces.md index acfeadc5..394f680c 100644 --- a/docs/data-sources/workspaces.md +++ b/docs/data-sources/workspaces.md @@ -22,6 +22,11 @@ data "astro_workspaces" "example_workspaces_filter_by_workspace_ids" { data "astro_workspaces" "example_workspaces_filter_by_names" { names = ["my first workspace", "my second workspace"] } + +# Output the workspaces value using terraform apply +output "example_workspaces" { + value = data.astro_workspaces.example_workspaces +} ``` diff --git a/examples/data-sources/astro_api_token/data-source.tf b/examples/data-sources/astro_api_token/data-source.tf index b67982bd..e5d90d8c 100644 --- a/examples/data-sources/astro_api_token/data-source.tf +++ b/examples/data-sources/astro_api_token/data-source.tf @@ -1,3 +1,8 @@ -data "astro_api_token" "example" { +data "astro_api_token" "example_api_token" { id = "clxm4836f00ql01me3nigmcr6" +} + +# Output the API token using terraform apply +output "api_token" { + value = data.astro_api_token.example_api_token } \ No newline at end of file diff --git a/examples/data-sources/astro_api_tokens/data-source.tf b/examples/data-sources/astro_api_tokens/data-source.tf index 6a14c893..78e07f77 100644 --- a/examples/data-sources/astro_api_tokens/data-source.tf +++ b/examples/data-sources/astro_api_tokens/data-source.tf @@ -1,4 +1,4 @@ -data "astro_api_tokens" "example" {} +data "astro_api_tokens" "example_api_tokens" {} data "astro_api_tokens" "organization_only_example" { include_only_organization_tokens = true @@ -10,4 +10,9 @@ data "astro_api_tokens" "workspace_example" { data "astro_api_tokens" "deployment_example" { deployment_id = "clx44jyu001m201m5dzsbexqr" +} + +# Output the API tokens using terraform apply +output "api_tokens" { + value = data.astro_api_tokens.example_api_tokens } \ No newline at end of file diff --git a/examples/data-sources/astro_cluster/data-source.tf b/examples/data-sources/astro_cluster/data-source.tf index 89dcc89e..619adf73 100644 --- a/examples/data-sources/astro_cluster/data-source.tf +++ b/examples/data-sources/astro_cluster/data-source.tf @@ -1,3 +1,8 @@ -data "astro_cluster" "example" { +data "astro_cluster" "example_cluster" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the cluster value using terraform apply +output "cluster" { + value = data.astro_cluster.example_cluster +} \ No newline at end of file diff --git a/examples/data-sources/astro_cluster_options/data-source.tf b/examples/data-sources/astro_cluster_options/data-source.tf index 9b9bae17..713b8b0e 100644 --- a/examples/data-sources/astro_cluster_options/data-source.tf +++ b/examples/data-sources/astro_cluster_options/data-source.tf @@ -6,3 +6,8 @@ data "astro_cluster_options" "example_cluster_options_filter_by_provider" { type = "HYBRID" cloud_provider = "AWS" } + +# Output the cluster options value using terraform apply +output "cluster_options" { + value = data.astro_cluster_options.example_cluster_options +} \ No newline at end of file diff --git a/examples/data-sources/astro_clusters/data-source.tf b/examples/data-sources/astro_clusters/data-source.tf index 365df947..ee148a84 100644 --- a/examples/data-sources/astro_clusters/data-source.tf +++ b/examples/data-sources/astro_clusters/data-source.tf @@ -6,4 +6,9 @@ data "astro_clusters" "example_clusters_filter_by_names" { data "astro_clusters" "example_clusters_filter_by_cloud_provider" { cloud_provider = "AWS" +} + +# Output the clusters value using terraform apply +output "clusters" { + value = data.astro_clusters.example_clusters } \ No newline at end of file diff --git a/examples/data-sources/astro_deployment/data-source.tf b/examples/data-sources/astro_deployment/data-source.tf index 12660926..a2251db6 100644 --- a/examples/data-sources/astro_deployment/data-source.tf +++ b/examples/data-sources/astro_deployment/data-source.tf @@ -1,3 +1,8 @@ -data "astro_deployment" "example" { +data "astro_deployment" "example_deployment" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the deployment value using terraform apply +output "deployment" { + value = data.astro_deployment.example_deployment +} \ No newline at end of file diff --git a/examples/data-sources/astro_deployment_options/data-source.tf b/examples/data-sources/astro_deployment_options/data-source.tf index 4beb8d0d..e6aeffdd 100644 --- a/examples/data-sources/astro_deployment_options/data-source.tf +++ b/examples/data-sources/astro_deployment_options/data-source.tf @@ -1,4 +1,4 @@ -data "astro_deployment_options" "example" {} +data "astro_deployment_options" "example_deployment_options" {} data "astro_deployment_options" "example_with_deployment_id_query_param" { deployment_id = "clozc036j01to01jrlgvueo8t" @@ -15,3 +15,8 @@ data "astro_deployment_options" "example_with_executor_query_param" { data "astro_deployment_options" "example_with_cloud_provider_query_param" { cloud_provider = "AWS" } + +# Output the deployment options value using terraform apply +output "deployment_options" { + value = data.astro_deployment_options.example_deployment_options +} \ No newline at end of file diff --git a/examples/data-sources/astro_deployments/data-source.tf b/examples/data-sources/astro_deployments/data-source.tf index 84d5ddc5..4ee72c88 100644 --- a/examples/data-sources/astro_deployments/data-source.tf +++ b/examples/data-sources/astro_deployments/data-source.tf @@ -10,4 +10,9 @@ data "astro_deployments" "example_deployments_filter_by_deployment_ids" { data "astro_deployments" "example_deployments_filter_by_workspace_ids" { workspace_ids = ["clozc036j01to01jrlgvu798d"] +} + +# Output the deployments value using terraform apply +output "deployments" { + value = data.astro_deployments.example_deployments } \ No newline at end of file diff --git a/examples/data-sources/astro_organization/data-source.tf b/examples/data-sources/astro_organization/data-source.tf index 6e6e6565..ba0dc691 100644 --- a/examples/data-sources/astro_organization/data-source.tf +++ b/examples/data-sources/astro_organization/data-source.tf @@ -1 +1,6 @@ -data "astro_organization" "example" {} +data "astro_organization" "example_organization" {} + +# Output the organization value using terraform apply +output "organization" { + value = data.astro_organization.example_organization +} \ No newline at end of file diff --git a/examples/data-sources/astro_team/data-source.tf b/examples/data-sources/astro_team/data-source.tf index 1828692f..92d0ff7e 100644 --- a/examples/data-sources/astro_team/data-source.tf +++ b/examples/data-sources/astro_team/data-source.tf @@ -1,3 +1,8 @@ -data "astro_team" "example" { +data "astro_team" "example_team" { id = "clwbclrc100bl01ozjj5s4jmq" +} + +# Output the team value using terraform apply +output "team" { + value = data.astro_team.example_team } \ No newline at end of file diff --git a/examples/data-sources/astro_teams/data-source.tf b/examples/data-sources/astro_teams/data-source.tf index 11230ad4..46ee2d00 100644 --- a/examples/data-sources/astro_teams/data-source.tf +++ b/examples/data-sources/astro_teams/data-source.tf @@ -3,3 +3,8 @@ data "astro_teams" "example_teams" {} data "astro_teams" "example_teams_filter_by_names" { names = ["my first team", "my second team"] } + +# Output the teams value using terraform apply +output "example_teams" { + value = data.astro_teams.example_teams +} \ No newline at end of file diff --git a/examples/data-sources/astro_user/data-source.tf b/examples/data-sources/astro_user/data-source.tf index dae7edae..fc13ccfc 100644 --- a/examples/data-sources/astro_user/data-source.tf +++ b/examples/data-sources/astro_user/data-source.tf @@ -1,3 +1,8 @@ -data "astro_user" "example" { +data "astro_user" "example_user" { id = "clhpichn8002m01mqa4ocs7g6" +} + +# Output the user value using terraform apply +output "user" { + value = data.astro_user.example_user } \ No newline at end of file diff --git a/examples/data-sources/astro_users/data-source.tf b/examples/data-sources/astro_users/data-source.tf index fbb10034..7b217180 100644 --- a/examples/data-sources/astro_users/data-source.tf +++ b/examples/data-sources/astro_users/data-source.tf @@ -7,3 +7,8 @@ data "astro_users" "example_users_filter_by_workspace_id" { data "astro_users" "example_users_filter_by_deployment_id" { deployment_id = "clx44jyu001m201m5dzsbexqr" } + +# Output the users value using terraform apply +output "example_users" { + value = data.astro_users.example_users +} \ No newline at end of file diff --git a/examples/data-sources/astro_workspace/data-source.tf b/examples/data-sources/astro_workspace/data-source.tf index 225de010..e4e38a85 100644 --- a/examples/data-sources/astro_workspace/data-source.tf +++ b/examples/data-sources/astro_workspace/data-source.tf @@ -1,3 +1,8 @@ -data "astro_workspace" "example" { +data "astro_workspace" "example_workspace" { id = "clozc036j01to01jrlgvueo8t" } + +# Output the workspace value using terraform apply +output "workspace" { + value = data.astro_workspace.example_workspace +} \ No newline at end of file diff --git a/examples/data-sources/astro_workspaces/data-source.tf b/examples/data-sources/astro_workspaces/data-source.tf index bea59091..6a6c94d0 100644 --- a/examples/data-sources/astro_workspaces/data-source.tf +++ b/examples/data-sources/astro_workspaces/data-source.tf @@ -7,3 +7,8 @@ data "astro_workspaces" "example_workspaces_filter_by_workspace_ids" { data "astro_workspaces" "example_workspaces_filter_by_names" { names = ["my first workspace", "my second workspace"] } + +# Output the workspaces value using terraform apply +output "example_workspaces" { + value = data.astro_workspaces.example_workspaces +} \ No newline at end of file diff --git a/import/import_script.go b/import/import_script.go index 9a9a0390..a60e51ad 100644 --- a/import/import_script.go +++ b/import/import_script.go @@ -12,6 +12,8 @@ import ( "strings" "sync" + "github.com/hashicorp/go-version" + "golang.org/x/exp/maps" "github.com/astronomer/terraform-provider-astro/internal/clients/iam" @@ -33,7 +35,7 @@ func main() { log.Println("Terraform Import Script Starting") // collect all arguments from the user, indicating all the resources that need to be imported - resourcesPtr := flag.String("resources", "", "Comma separated list of resources to import. The only accepted values are workspace, deployment, cluster, api_token, team, team_roles, user_roles") + resourcesPtr := flag.String("resources", "workspace,deployment,cluster,api_token,team,team_roles,user_roles", "Comma separated list of resources to import. The only accepted values are workspace, deployment, cluster, api_token, team, team_roles, user_roles") tokenPtr := flag.String("token", "", "API token to authenticate with the platform") hostPtr := flag.String("host", "https://api.astronomer.io", "API host to connect to") organizationIdPtr := flag.String("organizationId", "", "Organization ID to import resources into") @@ -48,6 +50,11 @@ func main() { return } + err := checkRequiredArguments(*resourcesPtr, *tokenPtr, *organizationIdPtr) + if err != nil { + log.Fatalf("Error: %v", err) + } + // validate the resources argument resources := strings.Split(strings.ToLower(*resourcesPtr), ",") acceptedResources := []string{"workspace", "deployment", "cluster", "api_token", "team", "team_roles", "user_roles"} @@ -71,6 +78,12 @@ func main() { return } + err = os.Setenv("ASTRO_API_TOKEN", token) + if err != nil { + log.Fatalf("Failed to set ASTRO_API_TOKEN environment variable: %v", err) + return + } + // set the host var host string if *hostPtr == "dev" { @@ -78,7 +91,7 @@ func main() { } else if *hostPtr == "stage" { host = "https://api.astronomer-stage.io" } else { - host = *hostPtr + host = "https://api.astronomer.io" } // set the organization ID @@ -89,10 +102,10 @@ func main() { log.Printf("Using organization ID: %s", organizationId) - // Check if Terraform is installed - _, err := exec.LookPath("terraform") + // Check if Terraform is installed and the version is supported + err = checkTerraformVersion() if err != nil { - log.Fatalf("Error: Terraform is not installed or not in PATH. Please install Terraform and make sure it's in your system PATH") + log.Fatalf("Error: %v", err) } // connect to v1beta1 client @@ -121,9 +134,8 @@ func main() { provider "astro" { organization_id = "%s" host = "%s" - token = "%s" } -`, organizationId, host, token) +`, organizationId, host) // for each resource, we get the list of entities and generate the terraform import command @@ -254,10 +266,6 @@ func printHelp() { log.Println(" workspace, deployment, cluster, api_token, team, team_roles, user_roles") log.Println(" -token string") log.Println(" API token to authenticate with the platform") - log.Println(" -host string") - log.Println(" API host to connect to (default: https://api.astronomer.io)") - log.Println(" Use 'dev' for https://api.astronomer-dev.io") - log.Println(" Use 'stage' for https://api.astronomer-stage.io") log.Println(" -organizationId string") log.Println(" Organization ID to import resources into") log.Println(" -runTerraformInit") @@ -269,6 +277,66 @@ func printHelp() { log.Println("\nNote: If the -token flag is not provided, the script will attempt to use the ASTRO_API_TOKEN environment variable.") } +// checkRequiredArguments checks if the required arguments are provided +func checkRequiredArguments(resourcesPtr string, tokenPtr string, organizationIdPtr string) error { + var missingArgs []string + + if resourcesPtr == "" { + missingArgs = append(missingArgs, "-resources (comma-separated list: workspace, deployment, cluster, api_token, team, team_roles, user_roles)") + } + + if tokenPtr == "" && len(os.Getenv("ASTRO_API_TOKEN")) == 0 { + missingArgs = append(missingArgs, "-token (or ASTRO_API_TOKEN environment variable)") + } + + if organizationIdPtr == "" { + missingArgs = append(missingArgs, "-organizationId") + } + + if len(missingArgs) > 0 { + return fmt.Errorf("Missing required argument(s):\n%s", strings.Join(missingArgs, "\n")) + } + + return nil +} + +// checkTerraformVersion checks if Terraform is installed and the version is supported +func checkTerraformVersion() error { + // Check if Terraform is installed + _, err := exec.LookPath("terraform") + if err != nil { + return fmt.Errorf("Terraform is not installed or not in PATH. Please install Terraform and make sure it's in your system PATH") + } + + // Get Terraform version + cmd := exec.Command("terraform", "version") + output, err := cmd.Output() + if err != nil { + return fmt.Errorf("Failed to get Terraform version: %v", err) + } + + // Parse the version string + versionStr := strings.TrimSpace(strings.Split(string(output), "\n")[0]) + versionStr = strings.TrimPrefix(versionStr, "Terraform v") + + // Parse the version + currentVersion, err := version.NewVersion(versionStr) + if err != nil { + return fmt.Errorf("Failed to parse Terraform version: %v", err) + } + + // Define the minimum required version + minVersion, _ := version.NewVersion("1.7.0") + + // Compare versions + if currentVersion.LessThan(minVersion) { + return fmt.Errorf("Terraform version %s is required. Your version (%s) is too old. Please upgrade Terraform", minVersion, currentVersion) + } + + fmt.Printf("Terraform version %s is installed and meets the minimum required version.\n", currentVersion) + return nil +} + // generateTerraformConfig runs terraform plan to generate the configuration func generateTerraformConfig() error { // delete the generated.tf file if it exists