starhawking / python-terrascript

Create Terraform files using Python scripts.
BSD 2-Clause "Simplified" License
516 stars 76 forks source link

Added support for update terrascript from another terrascript. Same behavior as dict().update #37

Closed nielsonsantana closed 6 years ago

nielsonsantana commented 6 years ago

While using Terrascript, I missed one way to join/merge two existing terrascripts. This possibility is indispensable for modularity, once we can divide a whole infrastructure into smaller parts and reuse it. In this pull request I implemented a new method called (update) on Terrascript.

The behavior of this method is similar as implemented in dict.update. In this case, the method update, does two operations:

Example 1: Override a previous declared item in ts:

    ts = Terrascript()
    ts2 = Terrascript()

    var_region = ts.add(variable('region', default='us-east-1'))
    var_region2 = ts2.add(variable('region', default='ca-central-1'))
    print('before:\n', ts.dump)
    ts.update(ts2)
    print('after:\n', ts.dump)

Result:

   before:
   {
      "variable": {
        "region": {
          "default": "us-east-1"
        }
      }
    }
   After:
   {
      "variable": {
        "region": {
          "default": "ca-central-1"
        }
      }
    }

Example 2: add a new item not declared in ts:

    ts = Terrascript()
    ts2 = Terrascript()

    var_region2 = ts2.add(variable('region', default='ca-central-1'))
    print('before:\n', ts.dump)
    ts.update(ts2)
    print('after:\n', ts.dump)

Result:

   before:
   { }
   After:
   {
      "variable": {
        "region": {
          "default": "ca-central-1"
        }
      }
    }
nielsonsantana commented 6 years ago

@mjuenema, as related in #38, was introduced a build error after add versions of Terraform.

nielsonsantana commented 6 years ago

@mjuenema, should I send this pull request against branch issue33?

mjuenema commented 6 years ago

Thanks, this will go into 0.5.2 earliest. I want to get 0.5.1 out quickly as it contains important fixes.

codecov-io commented 6 years ago

Codecov Report

Merging #37 into master will decrease coverage by 0.18%. The diff coverage is 54.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #37      +/-   ##
==========================================
- Coverage      99%   98.81%   -0.19%     
==========================================
  Files         138      138              
  Lines        2602     2612      +10     
==========================================
+ Hits         2576     2581       +5     
- Misses         26       31       +5
Impacted Files Coverage Δ
terrascript/__init__.py 95.8% <54.54%> (-2.92%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d50872a...5002daa. Read the comment docs.

nielsonsantana commented 6 years ago

Hi @mjuenema. I'm back working on terrascript. I resolved the conflict, but I think that the test suit has some problem on python-3.3. The first issue was https://travis-ci.org/mjuenema/python-terrascript/jobs/426097817 . I tried solve by add the 'setuptools>=18.5' to test_requirments.txt, but another error happen. https://travis-ci.org/mjuenema/python-terrascript/jobs/426102810 . How solve this issue? Drop test for python-3.3?

I did the same test using a docker image python-3.3.6 and I can't reproduce the second error presented by travis-ci.

mjuenema commented 6 years ago

The Travis-CI output shows that the problem seems to be installing the ``codecovandgithub3``` modules. From memory, they are only needed for the Codecov feature which I can live without.

nielsonsantana commented 6 years ago

Hi @mjuenema. I solved the bug with dependencies. Build timeline: https://travis-ci.org/mjuenema/python-terrascript/builds/426822364. The unique issue was because a timeout: https://travis-ci.org/mjuenema/python-terrascript/jobs/426822429.