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

feat(lib): add resource move functionality #3152

Merged
merged 23 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
991a7b8
feat: add resource move functionality
Maed223 Oct 13, 2023
01f0803
chore: add required copyright headers
hashicorp-copywrite[bot] Sep 28, 2023
3922349
feat: improved move workflow
Maed223 Oct 13, 2023
44457b4
chore: addition of examples, and better naming
Maed223 Oct 13, 2023
3539b63
chore: add required copyright headers
hashicorp-copywrite[bot] Oct 1, 2023
d77ca7d
feat: added support for multi-stack deploys with move, added renameRe…
Maed223 Oct 13, 2023
870b14f
chore: better naming, refactoring, error handling
Maed223 Oct 13, 2023
c7c7c66
chore: add unit testing for additions to TerraformResource
Maed223 Oct 13, 2023
39442e0
chore: changed naming conventions, added some more error handling
Maed223 Oct 13, 2023
1622fb0
chore: add documentation and docstrings
Maed223 Oct 4, 2023
388cdbf
chore: make unit testing more explicit
Maed223 Oct 4, 2023
aaf7191
chore: add validations for proper terrafrom version
Maed223 Oct 5, 2023
ca02eb7
chore: update error messages and js docs with feedback
Maed223 Oct 5, 2023
e98311a
chore: doc improvements
Maed223 Oct 6, 2023
18a8793
feat: moved building of move blocks to toTerraform
Maed223 Oct 13, 2023
14b8da2
chore: docs update
Maed223 Oct 9, 2023
7572951
chore: refactor of move functionality
Maed223 Oct 13, 2023
cbd7fa3
chore: update docs with feedback
Maed223 Oct 9, 2023
1b58ca4
chore: add plan and deploy flags for move example
Maed223 Oct 9, 2023
5203942
chore: descope renameResourceId, move example and test improvement
Maed223 Oct 10, 2023
c2e2e99
chore: update CHANGELOG, add upgrade guide, move resource refactor do…
Maed223 Oct 13, 2023
d5d437f
fix: add new upgrade guide to index
Maed223 Oct 11, 2023
ca192c0
chore: add upgrade guide to nav data
Maed223 Oct 11, 2023
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

Since the long-term support for Node.js 16 ended on 2023-09-11, we updated our minimum compatible Node.js version to 18.12.

### Potential provider naming collision with instance function `moveTo` on `TerraformResource`

We have added support for resource refactoring and renaming with the addition of the instance function `moveTo` on `TerraformResource`. We forsee the potential for naming collision with providers using `moveTo` as an attribute. In instances where provider bindings fail to compile due to the collision, regenerate your provider bindings and replace the provider related usage of `moveTo` to `moveToAttribute` in your configuration if applicable.

## 0.18.2

Fixes a bug in 0.18.1 that broke crash reporting due to a partial dependency upgrade.
Expand Down
3 changes: 3 additions & 0 deletions examples/python/aws-move/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
imports/
terraform.tfstate*
10 changes: 10 additions & 0 deletions examples/python/aws-move/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "3.7"

[packages]
cdktf = {path = "./../../../dist/python/cdktf-0.0.0-py3-none-any.whl"}
131 changes: 131 additions & 0 deletions examples/python/aws-move/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions examples/python/aws-move/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# python-aws

A CDK for Terraform application in Python.

## Usage

Install Pipenv using Homebrew by running:

```bash
$ brew install pipenv
```

You can install Pipenv by visiting the [website](https://pipenv.pypa.io/en/latest/).

Install project dependencies

```shell
pipenv install
```

Generate CDK for Terraform constructs for Terraform providers and modules used in the project.

```bash
cdktf get
```

You can now edit the `main.py` file if you want to modify any code.

```python
vim main.py
#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack
from imports.aws import SnsTopic, AwsProvider
from imports.terraform_aws_modules.vpc.aws import Vpc

class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)

AwsProvider(self, 'Aws', region='eu-central-1')

Vpc(self, 'CustomVpc',
name='custom-vpc',
cidr='10.0.0.0/16',
azs=["us-east-1a", "us-east-1b"],
public_subnets=["10.0.1.0/24", "10.0.2.0/24"]
)
SnsTopic(self, 'Topic', display_name='my-first-sns-topic')

app = App()
MyStack(app, "python-aws")

app.synth()
```

Compile and generate Terraform configuration

```bash
cdktf synth
```

The above command will create a folder called `cdktf.out` that contains all Terraform JSON configuration that was generated.

Run cdktf-cli commands

```bash
cdktf diff
cdktf deploy
```
11 changes: 11 additions & 0 deletions examples/python/aws-move/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"language": "python",
"app": "pipenv run python main.py",
"terraformProviders": [
"aws@~> 3.0"
],
"terraformModules": [
"terraform-aws-modules/vpc/[email protected]"
],
"codeMakerOutput": "imports"
}
22 changes: 22 additions & 0 deletions examples/python/aws-move/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
========================================================================================================

Your cdktf Python project is ready!

cat help Prints this message

Compile:
pipenv run ./main.py Compile and run the python code.

Synthesize:
cdktf synth Synthesize Terraform resources to cdktf.out/

Diff:
cdktf diff Perform a diff (terraform plan) for the given stack

Deploy:
cdktf deploy Deploy the given stack

Destroy:
cdktf destroy Destroy the given stack

========================================================================================================
30 changes: 30 additions & 0 deletions examples/python/aws-move/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

from constructs import Construct
from cdktf import App, TerraformStack
import os
from imports.aws.provider import AwsProvider
from imports.aws.s3_bucket import S3Bucket

# To properly see the move functionality in action, the resource being moved needs to be deployed
# First run "deploy" with the STEP_1 env flag set, then "plan" or "deploy" with the STEP_2 env flag set

class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)

AwsProvider(self, 'Aws', region='eu-central-1')

if os.environ.get('STEP_1')=='True':
S3Bucket(self, "old-bucket", bucket='python-move-test-bucket')

if os.environ.get('STEP_2')=='True':
S3Bucket(self, "old-bucket", bucket='python-move-test-bucket').move_to("move-s3")
S3Bucket(self, "new-bucket", bucket='python-move-test-bucket').add_move_target("move-s3")


app = App()
MyStack(app, "python-aws-move")

app.synth()
12 changes: 12 additions & 0 deletions examples/python/aws-move/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"//": "This example test is disabled via the 'private' attribute since Terraform 1.5 is required for proper synth, and as of writing CI has a lesser version",
"private": true,
"name": "@examples/python-aws-move",
"version": "0.0.0",
"license": "MPL-2.0",
"scripts": {
"reinstall": "rm Pipfile.lock && PIPENV_IGNORE_VIRTUALENVS=1 pipenv install ./../../../dist/python/*.whl",
"build": "cdktf get",
"synth": "cdktf synth"
}
}
11 changes: 11 additions & 0 deletions examples/typescript/aws-move/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.d.ts
*.js
node_modules
cdktf.out
cdktf.log
*terraform.*.tfstate*
.gen
.terraform
tsconfig.tsbuildinfo
!jest.config.js
!setup.js
Loading
Loading