0&&void 0!==arguments[0]?arguments[0]:{};this.action="function"==typeof t.action?t.action:this.defaultAction,this.target="function"==typeof t.target?t.target:this.defaultTarget,this.text="function"==typeof t.text?t.text:this.defaultText}},{key:"listenClick",value:function e(t){var n=this;this.listener=(0,f.default)(t,"click",function(e){return n.onClick(e)})}},{key:"onClick",value:function e(t){var n=t.delegateTarget||t.currentTarget;this.clipboardAction&&(this.clipboardAction=null),this.clipboardAction=new s.default({action:this.action(n),target:this.target(n),text:this.text(n),trigger:n,emitter:this})}},{key:"defaultAction",value:function e(t){return l("action",t)}},{key:"defaultTarget",value:function e(t){var n=l("target",t);if(n)return document.querySelector(n)}},{key:"defaultText",value:function e(t){return l("text",t)}},{key:"destroy",value:function e(){this.listener.destroy(),this.clipboardAction&&(this.clipboardAction.destroy(),this.clipboardAction=null)}}]),t}(u.default);e.exports=h})},{"./clipboard-action":7,"good-listener":4,"tiny-emitter":6}]},{},[8])(8)});
8 |
--------------------------------------------------------------------------------
/docs/input/blog/initial-release.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Initial Release - 0.1.0
3 | Published: 21/03/2017
4 | Category: Release
5 | Author: erikvanbrakel
6 | ---
7 |
8 | # 0.1.0 is here
9 |
10 | The first prerelease version of Cake.Terraform is available!
11 |
--------------------------------------------------------------------------------
/docs/input/blog/release-0-3-0.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: New release - 0.2.0
3 | Published: 22/03/2017
4 | Category: Release
5 | Author: erikvanbrakel
6 | ---
7 |
8 | # 0.2.0 is here
9 |
10 | This version adds functionality to format the output of the TerraformShow method as HTML. This is particularly useful in CI/CD scenarios, as a report to use within a build server such as TeamCity.
11 |
--------------------------------------------------------------------------------
/docs/input/blog/release-0.1.1.md:
--------------------------------------------------------------------------------
1 | ---
2 | Title: New release - 0.2.0
3 | Published: 22/03/2017
4 | Category: Release
5 | Author: erikvanbrakel
6 | ---
7 |
8 | # 0.2.0 is here
9 |
10 | This version adds functionality to format the output of the TerraformShow method as HTML. This is particularly useful in CI/CD scenarios, as a report to use within a build server such as TeamCity.
11 |
--------------------------------------------------------------------------------
/docs/input/docs/index.cshtml:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Documentation
3 | ---
4 | This user guide, like Cake.Terraform itself, is under active development. Some parts of it aren't
5 | documented as completely as they need to be, but we glady accept your contributions.
6 |
7 | We need your help to improve the documentation for Cake.Terraform, so if there is something that you
8 | would like to add then you can edit the content directly on GitHub.
9 |
10 | @foreach(IDocument child in Model.DocumentList(Keys.Children).OrderBy(x => x.Get(DocsKeys.Order, 1000)))
11 | {
12 | @(child.String(Keys.Title))
13 | if(child.ContainsKey(DocsKeys.Description))
14 | {
15 | @Html.Raw(child.String(DocsKeys.Description))
16 | }
17 |
18 | @Html.Partial("_ChildPages", child)
19 | }
20 |
--------------------------------------------------------------------------------
/docs/input/docs/usage/examples.md:
--------------------------------------------------------------------------------
1 | # Build Script
2 |
3 | In order to make use of the Cake.Terraform Addin, you will need to first use the `#addin` preprocessor to install Cake.Terraform, but once that is done, you can directly use the available aliases.
4 |
5 | In addition, the `terraform` tool will need to be installed and available on the machine on which the script is running. Alternatively, you can provide a `ToolPath` to where it can be located.
6 |
7 | ## TerraformInit
8 | ```csharp
9 | #addin Cake.Terraform
10 |
11 | Task("Init")
12 | .Does(() =>
13 | {
14 | TerraformInit();
15 | });
16 | ```
17 |
18 | or, with specific settings:
19 |
20 | ```csharp
21 | #addin Cake.Terraform
22 |
23 | Task("Plan")
24 | .Does(() =>
25 | {
26 | var settings = new TerraformInitSettings {
27 | WorkingDirectory = "./terraform"
28 | };
29 | TerraformInit(settings);
30 | });
31 | ```
32 |
33 | ## TerraformPlan
34 |
35 | ```csharp
36 | #addin Cake.Terraform
37 |
38 | Task("Plan")
39 | .Does(() =>
40 | {
41 | TerraformPlan();
42 | });
43 | ```
44 |
45 | or, with specific settings:
46 |
47 | ```csharp
48 | #addin Cake.Terraform
49 |
50 | Task("Plan")
51 | .Does(() =>
52 | {
53 | var settings = new TerraformPlanSettings {
54 | WorkingDirectory = "./terraform",
55 | OutFile = "./output/terraform.plan"
56 | };
57 | TerraformPlan(settings);
58 | });
59 | ```
60 |
61 | ## TerraformShow
62 |
63 | ```csharp
64 | #addin Cake.Terraform
65 |
66 | Task("Show")
67 | .Does(() =>
68 | {
69 | TerraformShow();
70 | });
71 | ```
72 |
73 | or, with specific settings:
74 |
75 | ```csharp
76 | #addin Cake.Terraform
77 |
78 | Task("Plan")
79 | .Does(() =>
80 | {
81 | var settings = new TerraformShowSettings {
82 | PlanFile = "./output/terraform.plan",
83 | OutFile = "./output/terraform.html",
84 | OutputFormat = OutputFormat.Html
85 | };
86 | TerraformShow(settings);
87 | });
88 | ```
89 |
90 | ## TerraformApply
91 |
92 | ```csharp
93 | #addin Cake.Terraform
94 |
95 | Task("Apply")
96 | .Does(() =>
97 | {
98 | TerraformApply();
99 | });
100 | ```
101 |
102 | or, with specific settings:
103 |
104 | ```csharp
105 | #addin Cake.Terraform
106 |
107 | Task("Apply")
108 | .Does(() =>
109 | {
110 | var settings = new TerraformApplySettings {
111 | Plan = "./output/terraform.plan"
112 | };
113 | TerraformApply(settings);
114 | });
115 | ```
116 |
117 | ## TerraformDestroy
118 |
119 | ```csharp
120 | #addin Cake.Terraform
121 |
122 | Task("Destroy")
123 | .Does(() =>
124 | {
125 | TerraformDestroy();
126 | });
127 | ```
128 |
129 | or, with specific settings:
130 |
131 | ```csharp
132 | #addin Cake.Terraform
133 |
134 | Task("Destroy")
135 | .Does(() =>
136 | {
137 | var settings = new TerraformDestroySettings {
138 | WorkingDirectory = ".",
139 | Force = true,
140 | InputVariables = new Dictionary {
141 | {"some-input-variable", "value"},
142 | }
143 | };
144 | TerraformDestroy(settings);
145 | });
146 | ```
147 |
148 |
149 | ## TerraformValidate
150 |
151 | ```csharp
152 | #addin Cake.Terraform
153 |
154 | Task("Validate")
155 | .Does(() =>
156 | {
157 | TerraformValidate();
158 | });
159 | ```
--------------------------------------------------------------------------------
/docs/input/docs/usage/index.cshtml:
--------------------------------------------------------------------------------
1 | ---
2 | Order: 2
3 | Description: How to obtain, configure, and execute Cake.Terraform.
4 | ---
5 | @Html.Raw(Model.String(DocsKeys.Description))
6 |
7 | @Html.Partial("_ChildPages")
8 |
--------------------------------------------------------------------------------
/docs/input/index.cshtml:
--------------------------------------------------------------------------------
1 | ---
2 | Title: Cake.Terraform
3 | NoSidebar: true
4 | NoContainer: false
5 | NoGutter: true
6 | ---
7 |
8 |
9 |
What is it?
10 |
11 | Cake.Terraform is an Addin for Cake which which allows the execution of the Terraform command line tool.
12 |
13 |
14 |
--------------------------------------------------------------------------------
/docs/packages.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
--------------------------------------------------------------------------------
/recipe.cake:
--------------------------------------------------------------------------------
1 | #load nuget:?package=Cake.Recipe&version=3.1.1
2 |
3 | Environment.SetVariableNames();
4 |
5 | BuildParameters.SetParameters(context: Context,
6 | buildSystem: BuildSystem,
7 | sourceDirectoryPath: "./src",
8 | title: "Cake.Terraform",
9 | repositoryOwner: "cake-contrib",
10 | repositoryName: "Cake.Terraform",
11 | webHost: "cake-contrib.github.io",
12 | webLinkRoot: "Cake.Terraform",
13 | webBaseEditUrl: "https://github.com/cake-contrib/Cake.Terraform/tree/develop/docs/input",
14 | shouldRunDotNetCorePack: true,
15 | shouldRunCodecov: false,
16 | shouldPostToGitter: false
17 | //preferredBuildProviderType: BuildProviderType.GitHubActions,
18 | //preferredBuildAgentOperatingSystem: PlatformFamily.Linux
19 | );
20 |
21 | BuildParameters.PrintParameters(Context);
22 |
23 | ToolSettings.SetToolPreprocessorDirectives(
24 | gitReleaseManagerGlobalTool: "#tool dotnet:?package=GitReleaseManager.Tool&version=0.17.0");
25 |
26 | ToolSettings.SetToolSettings(context: Context);
27 |
28 | Build.RunDotNetCore();
29 |
30 |
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/Cake.Terraform.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 | netcoreapp3.1
10 | net6.0;net7.0;net8.0
11 |
12 |
13 |
14 |
15 |
16 |
17 | runtime; build; native; contentfiles; analyzers; buildtransitive
18 | all
19 |
20 |
21 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformApplyTests.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core;
2 | using Cake.Terraform.Apply;
3 | using Cake.Testing;
4 | using Xunit;
5 |
6 | namespace Cake.Terraform.Tests
7 | {
8 | using System.Collections.Generic;
9 |
10 | public class TerraformApplyTests
11 | {
12 | class Fixture : TerraformFixture
13 | {
14 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
15 |
16 | protected override void RunTool()
17 | {
18 | var tool = new TerraformApplyRunner(FileSystem, Environment, ProcessRunner, Tools);
19 | tool.Run(Settings);
20 | }
21 | }
22 |
23 | public class TheExecutable
24 | {
25 | [Fact]
26 | public void Should_throw_if_terraform_runner_was_not_found()
27 | {
28 | var fixture = new Fixture();
29 | fixture.GivenDefaultToolDoNotExist();
30 |
31 | var result = Record.Exception(() => fixture.Run());
32 |
33 | Assert.IsType(result);
34 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
35 | }
36 |
37 | [Theory]
38 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
39 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
40 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
41 | {
42 | var fixture = new Fixture() {Settings = {ToolPath = toolPath}};
43 | fixture.GivenSettingsToolPathExist();
44 |
45 | var result = fixture.Run();
46 |
47 | Assert.Equal(expected, result.Path.FullPath);
48 | }
49 |
50 | [Fact]
51 | public void Should_find_terraform_if_tool_path_not_provided()
52 | {
53 | var fixture = new Fixture();
54 |
55 | var result = fixture.Run();
56 |
57 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
58 | }
59 |
60 | [Fact]
61 | public void Should_throw_if_process_has_a_non_zero_exit_code()
62 | {
63 | var fixture = new Fixture();
64 | fixture.GivenProcessExitsWithCode(1);
65 |
66 | var result = Record.Exception(() => fixture.Run());
67 |
68 | Assert.IsType(result);
69 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
70 | }
71 |
72 | [Fact]
73 | public void Should_find_linux_executable()
74 | {
75 | var fixture = new Fixture(PlatformFamily.Linux);
76 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
77 |
78 |
79 | var result = fixture.Run();
80 |
81 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
82 | }
83 |
84 | [Fact]
85 | public void Should_set_apply_parameter()
86 | {
87 | var fixture = new Fixture();
88 |
89 | var result = fixture.Run();
90 |
91 | Assert.Contains("apply", result.Args);
92 | }
93 |
94 | [Fact]
95 | public void Should_set_input_variables()
96 | {
97 | var fixture = new Fixture();
98 | fixture.Settings = new TerraformApplySettings
99 | {
100 | InputVariables = new Dictionary
101 | {
102 | { "access_key", "foo" },
103 | { "secret_key", "bar" }
104 | }
105 | };
106 | var result = fixture.Run();
107 |
108 | Assert.Contains("-var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
109 | }
110 |
111 | [Fact]
112 | public void Should_set_input_variables_file()
113 | {
114 | var fixture = new Fixture
115 | {
116 | Settings = new TerraformApplySettings
117 | {
118 | InputVariablesFile = "./aws-creds.json",
119 | InputVariables = new Dictionary
120 | {
121 | {"access_key", "foo"},
122 | {"secret_key", "bar"}
123 | }
124 | }
125 | };
126 | var result = fixture.Run();
127 |
128 | Assert.Contains("-var-file \"./aws-creds.json\" -var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
129 | }
130 |
131 | [Fact]
132 | public void Should_Append_Auto_Approve_When_AutoApprove_Is_True()
133 | {
134 | var fixture = new Fixture {
135 | Settings = new TerraformApplySettings {
136 | AutoApprove = true
137 | }
138 | };
139 |
140 | var result = fixture.Run();
141 |
142 | Assert.Contains("-auto-approve", result.Args);
143 | }
144 |
145 | [Fact]
146 | public void Should_Append_Destroy_When_AutoApprove_Is_True()
147 | {
148 | var fixture = new Fixture
149 | {
150 | Settings = new TerraformApplySettings
151 | {
152 | Destroy = true
153 | }
154 | };
155 |
156 | var result = fixture.Run();
157 |
158 | Assert.Contains("-destroy", result.Args);
159 | }
160 |
161 | [Fact]
162 | public void Should_set_plan_path()
163 | {
164 | var fixture = new Fixture
165 | {
166 | Settings = new TerraformApplySettings {
167 | Plan = "plan.out"
168 | }
169 | };
170 |
171 | var result = fixture.Run();
172 |
173 | Assert.Equal("apply \"plan.out\"", result.Args);
174 | }
175 |
176 | [Fact]
177 | public void Should_set_parallelism()
178 | {
179 | var fixture = new Fixture
180 | {
181 | Settings = new TerraformApplySettings {
182 | Parallelism = 42
183 | }
184 | };
185 |
186 | var result = fixture.Run();
187 |
188 | Assert.Contains("-parallelism=42", result.Args);
189 | }
190 |
191 | [Fact]
192 | public void Should_omit_input_flag_by_default()
193 | {
194 | var fixture = new Fixture();
195 |
196 | var result = fixture.Run();
197 |
198 | Assert.DoesNotContain("-input", result.Args);
199 | }
200 |
201 | [Fact]
202 | public void Should_include_input_flag_when_setting_is_false()
203 | {
204 | var fixture = new Fixture
205 | {
206 | Settings = new TerraformApplySettings
207 | {
208 | Input = false
209 | }
210 | };
211 |
212 | var result = fixture.Run();
213 |
214 | Assert.Contains("-input", result.Args);
215 | }
216 | }
217 | }
218 | }
219 |
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformDestroyTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Terraform.Destroy;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | public class TerraformDestroyTests
10 | {
11 | class Fixture : TerraformFixture
12 | {
13 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
14 |
15 | protected override void RunTool()
16 | {
17 | var tool = new TerraformDestroyRunner(FileSystem, Environment, ProcessRunner, Tools);
18 | tool.Run(Settings);
19 | }
20 | }
21 |
22 | public class TheExecutable
23 | {
24 | [Fact]
25 | public void Should_throw_if_terraform_runner_was_not_found()
26 | {
27 | var fixture = new Fixture();
28 | fixture.GivenDefaultToolDoNotExist();
29 |
30 | var result = Record.Exception(() => fixture.Run());
31 |
32 | Assert.IsType(result);
33 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
34 | }
35 |
36 | [Theory]
37 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
38 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
39 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
40 | {
41 | var fixture = new Fixture {Settings = {ToolPath = toolPath}};
42 | fixture.GivenSettingsToolPathExist();
43 |
44 | var result = fixture.Run();
45 |
46 | Assert.Equal(expected, result.Path.FullPath);
47 | }
48 |
49 | [Fact]
50 | public void Should_find_terraform_if_tool_path_not_provided()
51 | {
52 | var fixture = new Fixture();
53 |
54 | var result = fixture.Run();
55 |
56 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
57 | }
58 |
59 | [Fact]
60 | public void Should_throw_if_process_has_a_non_zero_exit_code()
61 | {
62 | var fixture = new Fixture();
63 | fixture.GivenProcessExitsWithCode(1);
64 |
65 | var result = Record.Exception(() => fixture.Run());
66 |
67 | Assert.IsType(result);
68 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
69 | }
70 |
71 | [Fact]
72 | public void Should_find_linux_executable()
73 | {
74 | var fixture = new Fixture(PlatformFamily.Linux);
75 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
76 |
77 |
78 | var result = fixture.Run();
79 |
80 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
81 | }
82 |
83 | [Fact]
84 | public void Should_set_destroy_parameter()
85 | {
86 | var fixture = new Fixture();
87 |
88 | var result = fixture.Run();
89 |
90 | Assert.Contains("destroy", result.Args);
91 | }
92 |
93 | [Fact]
94 | public void Should_set_force_flag_when_set_to_true()
95 | {
96 | var fixture = new Fixture
97 | {
98 | Settings = new TerraformDestroySettings
99 | {
100 | Force = true
101 | }
102 | };
103 | var result = fixture.Run();
104 |
105 | Assert.Contains("-force", result.Args);
106 | }
107 |
108 | [Fact]
109 | public void Should_set_input_variables()
110 | {
111 | var fixture = new Fixture
112 | {
113 | Settings = new TerraformDestroySettings
114 | {
115 | InputVariables = new Dictionary
116 | {
117 | {"access_key", "foo"},
118 | {"secret_key", "bar"}
119 | }
120 | }
121 | };
122 | var result = fixture.Run();
123 |
124 | Assert.Contains("-var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
125 | }
126 |
127 | [Fact]
128 | public void Should_set_input_variables_file()
129 | {
130 | var fixture = new Fixture
131 | {
132 | Settings = new TerraformDestroySettings
133 | {
134 | InputVariablesFile = "./aws-creds.json",
135 | InputVariables = new Dictionary
136 | {
137 | {"access_key", "foo"},
138 | {"secret_key", "bar"}
139 | }
140 | }
141 | };
142 | var result = fixture.Run();
143 |
144 | Assert.Contains("-var-file \"./aws-creds.json\" -var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
145 | }
146 | }
147 | }
148 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformEnvListTests.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core;
2 | using Cake.Terraform.EnvList;
3 | using Cake.Testing;
4 | using Xunit;
5 |
6 | namespace Cake.Terraform.Tests
7 | {
8 | using System.Collections.Generic;
9 |
10 | public class TerraformEnvListTests
11 | {
12 | class Fixture : TerraformFixture
13 | {
14 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
15 |
16 | public IEnumerable Environments { get; private set; } = new List();
17 |
18 | protected override void RunTool()
19 | {
20 | ProcessRunner.Process.SetStandardOutput(new List{ "default" });
21 |
22 | var tool = new TerraformEnvListRunner(FileSystem, Environment, ProcessRunner, Tools);
23 |
24 | Environments = tool.Run(Settings);
25 | }
26 | }
27 |
28 | public class TheExecutable
29 | {
30 | [Fact]
31 | public void Should_throw_if_terraform_runner_was_not_found()
32 | {
33 | var fixture = new Fixture();
34 | fixture.GivenDefaultToolDoNotExist();
35 |
36 | var result = Record.Exception(() => fixture.Run());
37 |
38 | Assert.IsType(result);
39 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
40 | }
41 |
42 | [Theory]
43 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
44 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
45 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
46 | {
47 | var fixture = new Fixture() {Settings = {ToolPath = toolPath}};
48 | fixture.GivenSettingsToolPathExist();
49 |
50 | var result = fixture.Run();
51 |
52 | Assert.Equal(expected, result.Path.FullPath);
53 | }
54 |
55 | [Fact]
56 | public void Should_find_terraform_if_tool_path_not_provided()
57 | {
58 | var fixture = new Fixture();
59 |
60 | var result = fixture.Run();
61 |
62 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
63 | }
64 |
65 | [Fact]
66 | public void Should_throw_if_process_has_a_non_zero_exit_code()
67 | {
68 | var fixture = new Fixture();
69 | fixture.GivenProcessExitsWithCode(1);
70 |
71 | var result = Record.Exception(() => fixture.Run());
72 |
73 | Assert.IsType(result);
74 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
75 | }
76 |
77 | [Fact]
78 | public void Should_find_linux_executable()
79 | {
80 | var fixture = new Fixture(PlatformFamily.Linux);
81 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
82 |
83 |
84 | var result = fixture.Run();
85 |
86 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
87 | }
88 |
89 | [Fact]
90 | public void Should_set_workspace_and_list_parameter()
91 | {
92 | var fixture = new Fixture();
93 |
94 | var result = fixture.Run();
95 |
96 | Assert.Contains("workspace list", result.Args);
97 | }
98 |
99 | [Fact]
100 | public void Should_set_env_and_list_parameter()
101 | {
102 | var fixture = new Fixture {Settings = {EnvCommand = TerraformEnvSettings.EnvCommandType.Env}};
103 |
104 | var result = fixture.Run();
105 |
106 | Assert.Contains("env list", result.Args);
107 | }
108 |
109 | [Fact]
110 | public void Should_return_existing_environments()
111 | {
112 | var fixture = new Fixture();
113 |
114 | var result = fixture.Run();
115 |
116 | Assert.Contains("default", fixture.Environments);
117 | }
118 | }
119 | }
120 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformFixture.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core;
2 | using Cake.Core.Tooling;
3 |
4 | namespace Cake.Terraform.Tests
5 | {
6 | public abstract class TerraformFixture : Testing.Fixtures.ToolFixture
7 | where TSettings : ToolSettings, new()
8 | {
9 | protected TerraformFixture(PlatformFamily platformFamily = PlatformFamily.Windows)
10 | : base(platformFamily == PlatformFamily.Windows ? "terraform.exe" : "terraform")
11 | {
12 | Environment.Platform.Family = platformFamily;
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformInitTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Terraform.Init;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | public class TerraformInitTests
10 | {
11 | class Fixture : TerraformFixture
12 | {
13 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily)
14 | {
15 | }
16 |
17 | protected override void RunTool()
18 | {
19 | var tool = new TerraformInitRunner(FileSystem, Environment, ProcessRunner, Tools);
20 | tool.Run(Settings);
21 | }
22 | }
23 |
24 | public class TheExecutable
25 | {
26 | [Fact]
27 | public void Should_throw_if_terraform_runner_was_not_found()
28 | {
29 | var fixture = new Fixture();
30 | fixture.GivenDefaultToolDoNotExist();
31 |
32 | var result = Record.Exception(() => fixture.Run());
33 |
34 | Assert.IsType(result);
35 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
36 | }
37 |
38 | [Theory]
39 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
40 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
41 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
42 | {
43 | var fixture = new Fixture() {Settings = {ToolPath = toolPath}};
44 | fixture.GivenSettingsToolPathExist();
45 |
46 | var result = fixture.Run();
47 |
48 | Assert.Equal(expected, result.Path.FullPath);
49 | }
50 |
51 | [Fact]
52 | public void Should_find_terraform_if_tool_path_not_provided()
53 | {
54 | var fixture = new Fixture();
55 |
56 | var result = fixture.Run();
57 |
58 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
59 | }
60 |
61 | [Fact]
62 | public void Should_throw_if_process_has_a_non_zero_exit_code()
63 | {
64 | var fixture = new Fixture();
65 | fixture.GivenProcessExitsWithCode(1);
66 |
67 | var result = Record.Exception(() => fixture.Run());
68 |
69 | Assert.IsType(result);
70 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
71 | }
72 |
73 | [Fact]
74 | public void Should_find_linux_executable()
75 | {
76 | var fixture = new Fixture(PlatformFamily.Linux);
77 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
78 |
79 |
80 | var result = fixture.Run();
81 |
82 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
83 | }
84 |
85 | [Fact]
86 | public void Should_set_init_parameter()
87 | {
88 | var fixture = new Fixture();
89 |
90 | var result = fixture.Run();
91 |
92 | Assert.Contains("init", result.Args);
93 | }
94 |
95 | [Fact]
96 | public void Should_pass_backend_config_overrides()
97 | {
98 | var fixture = new Fixture();
99 |
100 | fixture.Settings = new TerraformInitSettings
101 | {
102 | BackendConfigOverrides = new Dictionary
103 | {
104 | { "key", "value" },
105 | { "foo", "bar" },
106 | }
107 | };
108 |
109 | var result = fixture.Run();
110 |
111 | Assert.Contains("-backend-config \"key=value\"", result.Args);
112 | Assert.Contains("-backend-config \"foo=bar\"", result.Args);
113 | }
114 |
115 | [Fact]
116 | public void Should_omit_reconfigure_flag_by_default()
117 | {
118 | var fixture = new Fixture();
119 |
120 | var result = fixture.Run();
121 |
122 | Assert.DoesNotContain("-reconfigure", result.Args);
123 | }
124 |
125 | [Fact]
126 | public void Should_include_reconfigure_flag_when_setting_is_true()
127 | {
128 | var fixture = new Fixture
129 | {
130 | Settings = new TerraformInitSettings
131 | {
132 | ForceReconfigure = true
133 | }
134 | };
135 |
136 | var result = fixture.Run();
137 |
138 | Assert.Contains("-reconfigure", result.Args);
139 | }
140 |
141 | [Fact]
142 | public void Should_omit_force_copy_flag_by_default()
143 | {
144 | var fixture = new Fixture();
145 |
146 | var result = fixture.Run();
147 |
148 | Assert.DoesNotContain("-force-copy", result.Args);
149 | }
150 |
151 | [Fact]
152 | public void Should_include_force_copy_flag_when_setting_is_true()
153 | {
154 | var fixture = new Fixture
155 | {
156 | Settings = new TerraformInitSettings
157 | {
158 | ForceCopy = true
159 | }
160 | };
161 |
162 | var result = fixture.Run();
163 |
164 | Assert.Contains("-force-copy", result.Args);
165 | }
166 |
167 | [Fact]
168 | public void Should_omit_input_flag_by_default()
169 | {
170 | var fixture = new Fixture();
171 |
172 | var result = fixture.Run();
173 |
174 | Assert.DoesNotContain("-input", result.Args);
175 | }
176 |
177 | [Fact]
178 | public void Should_include_input_flag_when_setting_is_false()
179 | {
180 | var fixture = new Fixture
181 | {
182 | Settings = new TerraformInitSettings
183 | {
184 | Input = false
185 | }
186 | };
187 |
188 | var result = fixture.Run();
189 |
190 | Assert.Contains("-input", result.Args);
191 | }
192 |
193 | [Fact]
194 | public void Should_omit_backend_flag_by_default()
195 | {
196 | var fixture = new Fixture();
197 |
198 | var result = fixture.Run();
199 |
200 | Assert.DoesNotContain("-backend", result.Args);
201 | }
202 |
203 | [Fact]
204 | public void Should_include_backend_flag_when_setting_is_false()
205 | {
206 | var fixture = new Fixture
207 | {
208 | Settings = new TerraformInitSettings
209 | {
210 | Backend = false
211 | }
212 | };
213 |
214 | var result = fixture.Run();
215 |
216 | Assert.Contains("-backend", result.Args);
217 | }
218 | }
219 | }
220 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformOutputTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cake.Core;
4 | using Cake.Terraform.Output;
5 | using Cake.Testing;
6 | using Xunit;
7 |
8 | namespace Cake.Terraform.Tests
9 | {
10 | public class TerraformOutputTests
11 | {
12 | class Fixture : TerraformFixture
13 | {
14 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
15 |
16 | public List ToolOutput { get; set; } = new List { "foo = 123", "bar = abc" };
17 | public string Outputs { get; private set; } = null;
18 |
19 | protected override void RunTool()
20 | {
21 | ProcessRunner.Process.SetStandardOutput(ToolOutput);
22 |
23 | var tool = new TerraformOutputRunner(FileSystem, Environment, ProcessRunner, Tools);
24 |
25 | Outputs = tool.Run(Settings);
26 | }
27 | }
28 |
29 | public class TheExecutable
30 | {
31 | [Fact]
32 | public void Should_throw_if_terraform_runner_was_not_found()
33 | {
34 | var fixture = new Fixture();
35 | fixture.GivenDefaultToolDoNotExist();
36 |
37 | var result = Record.Exception(() => fixture.Run());
38 |
39 | Assert.IsType(result);
40 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
41 | }
42 |
43 | [Theory]
44 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
45 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
46 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
47 | {
48 | var fixture = new Fixture() {Settings = {ToolPath = toolPath}};
49 | fixture.GivenSettingsToolPathExist();
50 |
51 | var result = fixture.Run();
52 |
53 | Assert.Equal(expected, result.Path.FullPath);
54 | }
55 |
56 | [Fact]
57 | public void Should_find_terraform_if_tool_path_not_provided()
58 | {
59 | var fixture = new Fixture();
60 |
61 | var result = fixture.Run();
62 |
63 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
64 | }
65 |
66 | [Fact]
67 | public void Should_throw_if_process_has_a_non_zero_exit_code()
68 | {
69 | var fixture = new Fixture();
70 | fixture.GivenProcessExitsWithCode(1);
71 |
72 | var result = Record.Exception(() => fixture.Run());
73 |
74 | Assert.IsType(result);
75 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
76 | }
77 |
78 | [Fact]
79 | public void Should_find_linux_executable()
80 | {
81 | var fixture = new Fixture(PlatformFamily.Linux);
82 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
83 |
84 | var result = fixture.Run();
85 |
86 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
87 | }
88 |
89 | [Fact]
90 | public void Should_call_only_command_if_no_settings_specified()
91 | {
92 | var fixture = new Fixture();
93 |
94 | var result = fixture.Run();
95 |
96 | Assert.Equal("output -no-color", result.Args);
97 | }
98 |
99 | [Fact]
100 | public void Should_request_json_if_specified()
101 | {
102 | var fixture = new Fixture { Settings = new TerraformOutputSettings { Json = true } };
103 |
104 | var result = fixture.Run();
105 |
106 | Assert.Equal("output -no-color -json", result.Args);
107 | }
108 |
109 | [Fact]
110 | public void Should_request_raw_if_specified()
111 | {
112 | var fixture = new Fixture { Settings = new TerraformOutputSettings { Raw = true } };
113 |
114 | var result = fixture.Run();
115 |
116 | Assert.Equal("output -no-color -raw", result.Args);
117 | }
118 |
119 | [Fact]
120 | public void Should_request_raw_over_json_if_both_are_specified()
121 | {
122 | var fixture = new Fixture { Settings = new TerraformOutputSettings { Raw = true, Json = true} };
123 |
124 | var result = fixture.Run();
125 |
126 | Assert.Equal("output -no-color -raw", result.Args);
127 | }
128 |
129 | [Fact]
130 | public void Should_request_specific_state_path_if_specified()
131 | {
132 | var fixture = new Fixture { Settings = new TerraformOutputSettings { StatePath = "some_path"} };
133 |
134 | var result = fixture.Run();
135 |
136 | Assert.Equal("output -no-color -state=some_path", result.Args);
137 | }
138 |
139 | [Fact]
140 | public void Should_request_particular_output_if_specified()
141 | {
142 | var fixture = new Fixture { Settings = new TerraformOutputSettings { OutputName = "some_output"} };
143 |
144 | var result = fixture.Run();
145 |
146 | Assert.Equal("output -no-color some_output", result.Args);
147 | }
148 |
149 | [Fact]
150 | public void Should_combine_output_lines_into_string()
151 | {
152 | var fixture = new Fixture();
153 |
154 | fixture.Run();
155 |
156 | Assert.Equal("foo = 123\nbar = abc", fixture.Outputs);
157 | }
158 |
159 | [Fact]
160 | public void Should_raise_exception_if_output_not_found()
161 | {
162 | var fixture = new Fixture
163 | {
164 | ToolOutput = new List
165 | {
166 | "The output variable requested could not be found in the state",
167 | "file. If you recently added this to your configuration, be",
168 | "sure to run `terraform apply`, since the state won't be updated",
169 | "with new output variables until that command is run."
170 | }
171 | };
172 |
173 | var exception = Assert.Throws(() => fixture.Run());
174 | Assert.Equal(
175 | "The output variable requested could not be found in the state\nfile. If you recently added this to your configuration, be\nsure to run `terraform apply`, since the state won't be updated\nwith new output variables until that command is run.",
176 | exception.Message);
177 | }
178 |
179 | [Fact]
180 | public void Should_not_raise_exception_if_output_not_found_and_validation_disabled()
181 | {
182 | var fixture = new Fixture
183 | {
184 | ToolOutput = new List
185 | {
186 | "The output variable requested could not be found in the state",
187 | "file. If you recently added this to your configuration, be",
188 | "sure to run `terraform apply`, since the state won't be updated",
189 | "with new output variables until that command is run."
190 | },
191 | Settings = new TerraformOutputSettings
192 | {
193 | ValidateToolOutput = false
194 | }
195 | };
196 |
197 | fixture.Run();
198 |
199 | Assert.Equal(
200 | "The output variable requested could not be found in the state\nfile. If you recently added this to your configuration, be\nsure to run `terraform apply`, since the state won't be updated\nwith new output variables until that command is run.",
201 | fixture.Outputs);
202 | }
203 | }
204 | }
205 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformPlanTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Terraform.Plan;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | public class TerraformPlanTests
10 | {
11 | class Fixture : TerraformFixture
12 | {
13 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) {}
14 |
15 | public bool HasChanges { get; set; }
16 |
17 | protected override void RunTool()
18 | {
19 | var tool = new TerraformPlanRunner(FileSystem, Environment, ProcessRunner, Tools);
20 | tool.Run(Settings);
21 | HasChanges = tool.HasChanges;
22 | }
23 | }
24 |
25 | public class TheExecutable
26 | {
27 | [Fact]
28 | public void Should_throw_if_terraform_runner_was_not_found()
29 | {
30 | var fixture = new Fixture();
31 | fixture.GivenDefaultToolDoNotExist();
32 |
33 | var result = Record.Exception(() => fixture.Run());
34 |
35 | Assert.IsType(result);
36 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
37 | }
38 |
39 | [Theory]
40 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
41 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
42 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
43 | {
44 | var fixture = new Fixture {Settings = {ToolPath = toolPath}};
45 | fixture.GivenSettingsToolPathExist();
46 |
47 | var result = fixture.Run();
48 |
49 | Assert.Equal(expected, result.Path.FullPath);
50 | }
51 |
52 | [Fact]
53 | public void Should_find_terraform_if_tool_path_not_provided()
54 | {
55 | var fixture = new Fixture();
56 |
57 | var result = fixture.Run();
58 |
59 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
60 | }
61 |
62 | [Fact]
63 | public void Should_find_linux_executable()
64 | {
65 | var fixture = new Fixture(PlatformFamily.Linux);
66 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
67 |
68 |
69 | var result = fixture.Run();
70 |
71 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
72 | }
73 |
74 | [Fact]
75 | public void Should_not_have_changes_if_process_has_exit_code_zero()
76 | {
77 | var fixture = new Fixture();
78 | fixture.GivenProcessExitsWithCode(0);
79 |
80 | var result = fixture.Run();
81 |
82 | Assert.False(fixture.HasChanges);
83 | }
84 |
85 | [Fact]
86 | public void Should_throw_if_process_has_exit_code_one()
87 | {
88 | var fixture = new Fixture();
89 | fixture.GivenProcessExitsWithCode(1);
90 |
91 | var result = Record.Exception(() => fixture.Run());
92 |
93 | Assert.IsType(result);
94 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
95 | }
96 |
97 | [Fact]
98 | public void Should_have_changes_if_process_has_exit_code_two()
99 | {
100 | var fixture = new Fixture();
101 | fixture.GivenProcessExitsWithCode(2);
102 |
103 | var result = fixture.Run();
104 |
105 | Assert.True(fixture.HasChanges);
106 | }
107 |
108 | [Fact]
109 | public void Should_set_plan_parameter()
110 | {
111 | var fixture = new Fixture();
112 |
113 | var result = fixture.Run();
114 |
115 | Assert.Contains("plan", result.Args);
116 | }
117 |
118 | [Fact]
119 | public void Should_set_detailed_exit_code()
120 | {
121 | var fixture = new Fixture();
122 | var result = fixture.Run();
123 |
124 | Assert.Contains("-detailed-exitcode", result.Args);
125 | }
126 |
127 | [Fact]
128 | public void Should_set_parallelism()
129 | {
130 | var fixture = new Fixture();
131 | fixture.Settings = new TerraformPlanSettings {Parallelism = 10};
132 | var result = fixture.Run();
133 |
134 | Assert.Contains("-parallelism=10", result.Args);
135 | }
136 |
137 | [Fact]
138 | public void Should_not_set_parallelism_if_zero()
139 | {
140 | var fixture = new Fixture();
141 | var result = fixture.Run();
142 |
143 | Assert.DoesNotContain("-parallelism", result.Args);
144 | }
145 |
146 | [Fact]
147 | public void Should_set_input_variables()
148 | {
149 | var fixture = new Fixture();
150 | fixture.Settings = new TerraformPlanSettings
151 | {
152 | InputVariables = new Dictionary
153 | {
154 | { "access_key", "foo" },
155 | { "secret_key", "bar" }
156 | }
157 | };
158 | var result = fixture.Run();
159 |
160 | Assert.Contains("-var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
161 | }
162 |
163 | [Fact]
164 | public void Should_set_input_variables_file()
165 | {
166 | var fixture = new Fixture
167 | {
168 | Settings = new TerraformPlanSettings
169 | {
170 | InputVariablesFile = "./aws-creds.json",
171 | InputVariables = new Dictionary
172 | {
173 | {"access_key", "foo"},
174 | {"secret_key", "bar"}
175 | }
176 | }
177 | };
178 | var result = fixture.Run();
179 |
180 | Assert.Contains("-var-file \"./aws-creds.json\" -var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
181 | }
182 |
183 | [Fact]
184 | public void Should_set_destroy_flag_when_set_to_true()
185 | {
186 | var fixture = new Fixture();
187 | fixture.Settings = new TerraformPlanSettings
188 | {
189 | Destroy = true
190 | };
191 | var result = fixture.Run();
192 |
193 | Assert.Contains("-destroy", result.Args);
194 | }
195 |
196 | [Fact]
197 | public void Should_not_set_destroy_flag_if_set_to_false()
198 | {
199 | var fixture = new Fixture();
200 | fixture.Settings = new TerraformPlanSettings
201 | {
202 | Destroy = false
203 | };
204 | var result = fixture.Run();
205 |
206 | Assert.DoesNotContain("-destroy", result.Args);
207 | }
208 |
209 | [Fact]
210 | public void Should_omit_input_flag_by_default()
211 | {
212 | var fixture = new Fixture();
213 |
214 | var result = fixture.Run();
215 |
216 | Assert.DoesNotContain("-input", result.Args);
217 | }
218 |
219 | [Fact]
220 | public void Should_include_input_flag_when_setting_is_false()
221 | {
222 | var fixture = new Fixture
223 | {
224 | Settings = new TerraformPlanSettings
225 | {
226 | Input = false
227 | }
228 | };
229 |
230 | var result = fixture.Run();
231 |
232 | Assert.Contains("-input", result.Args);
233 | }
234 | }
235 | }
236 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformRefreshTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Cake.Core;
3 | using Cake.Terraform.Refresh;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | using System.Collections.Generic;
10 |
11 | public class TerraformRefreshTests
12 | {
13 | class Fixture : TerraformFixture
14 | {
15 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
16 |
17 | protected override void RunTool()
18 | {
19 | var tool = new TerraformRefreshRunner(FileSystem, Environment, ProcessRunner, Tools);
20 | tool.Run(Settings);
21 | }
22 | }
23 |
24 | public class TheExecutable
25 | {
26 | [Fact]
27 | public void Should_throw_if_terraform_runner_was_not_found()
28 | {
29 | var fixture = new Fixture();
30 | fixture.GivenDefaultToolDoNotExist();
31 |
32 | var result = Record.Exception(() => fixture.Run());
33 |
34 | Assert.IsType(result);
35 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
36 | }
37 |
38 | [Theory]
39 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
40 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
41 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
42 | {
43 | var fixture = new Fixture {Settings = {ToolPath = toolPath}};
44 | fixture.GivenSettingsToolPathExist();
45 |
46 | var result = fixture.Run();
47 |
48 | Assert.Equal(expected, result.Path.FullPath);
49 | }
50 |
51 | [Fact]
52 | public void Should_find_terraform_if_tool_path_not_provided()
53 | {
54 | var fixture = new Fixture();
55 |
56 | var result = fixture.Run();
57 |
58 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
59 | }
60 |
61 | [Fact]
62 | public void Should_throw_if_process_has_a_non_zero_exit_code()
63 | {
64 | var fixture = new Fixture();
65 | fixture.GivenProcessExitsWithCode(1);
66 |
67 | var result = Record.Exception(() => fixture.Run());
68 |
69 | Assert.IsType(result);
70 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
71 | }
72 |
73 | [Fact]
74 | public void Should_find_linux_executable()
75 | {
76 | var fixture = new Fixture(PlatformFamily.Linux);
77 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
78 |
79 |
80 | var result = fixture.Run();
81 |
82 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
83 | }
84 |
85 | [Fact]
86 | public void Should_set_refresh_parameter()
87 | {
88 | var fixture = new Fixture();
89 |
90 | var result = fixture.Run();
91 |
92 | Assert.Contains("refresh", result.Args);
93 | }
94 |
95 | [Fact]
96 | public void Should_set_input_variables()
97 | {
98 | var fixture = new Fixture
99 | {
100 | Settings = new TerraformRefreshSettings
101 | {
102 | InputVariables = new Dictionary
103 | {
104 | {"access_key", "foo"}, {"secret_key", "bar"}
105 | }
106 | }
107 | };
108 | var result = fixture.Run();
109 |
110 | Assert.Contains("-var \"access_key=foo\" -var \"secret_key=bar\"", result.Args);
111 | }
112 |
113 | [Obsolete("Remove, together with InputVariablesFile")]
114 | [Fact]
115 | public void Should_set_input_variables_file()
116 | {
117 | var fixture = new Fixture
118 | {
119 | Settings = new TerraformRefreshSettings
120 | {
121 | InputVariablesFile = "./aws-creds.json",
122 | InputVariables = new Dictionary
123 | {
124 | {"access_key", "foo"},
125 | {"secret_key", "bar"}
126 | }
127 | }
128 | };
129 | var result = fixture.Run();
130 |
131 | Assert.Contains("-var-file \"./aws-creds.json\" -var \"access_key=foo\" -var \"secret_key=bar\"",
132 | result.Args);
133 | }
134 |
135 | [Fact]
136 | public void Should_set_input_variables_files()
137 | {
138 | var fixture = new Fixture
139 | {
140 | Settings = new TerraformRefreshSettings
141 | {
142 | InputVariablesFiles = { "./aws-creds.json", "./more-vars.json" },
143 | InputVariables = new Dictionary
144 | {
145 | {"access_key", "foo"},
146 | {"secret_key", "bar"}
147 | }
148 | }
149 | };
150 | var result = fixture.Run();
151 |
152 | Assert.Contains("-var-file \"./aws-creds.json\" -var-file \"./more-vars.json\" -var \"access_key=foo\" -var \"secret_key=bar\"",
153 | result.Args);
154 | }
155 | }
156 | }
157 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformShowTests.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using Cake.Core;
3 | using Cake.Terraform.Show;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | public class TerraformShowTests
10 | {
11 | class Fixture : TerraformFixture
12 | {
13 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
14 |
15 | protected override void RunTool()
16 | {
17 | var tool = new TerraformShowRunner(FileSystem, Environment, ProcessRunner, Tools);
18 | Settings.PlanFile = "my.plan";
19 | ProcessRunner.Process.SetStandardOutput(new[] { "output" });
20 | tool.Run(Settings);
21 | }
22 | }
23 |
24 |
25 | public class TheExecutable
26 | {
27 | [Fact]
28 | public void Should_throw_if_terraform_runner_was_not_found()
29 | {
30 | var fixture = new Fixture();
31 | fixture.GivenDefaultToolDoNotExist();
32 |
33 | var result = Record.Exception(() => fixture.Run());
34 |
35 | Assert.IsType(result);
36 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
37 | }
38 |
39 | [Theory]
40 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
41 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
42 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
43 | {
44 | var fixture = new Fixture {Settings = {ToolPath = toolPath}};
45 | fixture.GivenSettingsToolPathExist();
46 |
47 | var result = fixture.Run();
48 |
49 | Assert.Equal(expected, result.Path.FullPath);
50 | }
51 |
52 | [Fact]
53 | public void Should_find_terraform_if_tool_path_not_provided()
54 | {
55 | var fixture = new Fixture();
56 |
57 | var result = fixture.Run();
58 |
59 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
60 | }
61 |
62 | [Fact]
63 | public void Should_find_linux_executable()
64 | {
65 | var fixture = new Fixture(PlatformFamily.Linux);
66 |
67 | var result = fixture.Run();
68 |
69 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
70 | }
71 |
72 | [Fact]
73 | public void Should_set_show_parameter()
74 | {
75 | var fixture = new Fixture();
76 |
77 | var result = fixture.Run();
78 |
79 | Assert.Contains("show", result.Args);
80 | }
81 |
82 | [Fact]
83 | public void Should_set_plan_file_parameter()
84 | {
85 | var fixture = new Fixture { Settings = { PlanFile = "./input/myplan.plan" }};
86 |
87 | var result = fixture.Run();
88 |
89 | Assert.Contains(fixture.Settings.PlanFile.FullPath, result.Args);
90 | }
91 |
92 | [Fact]
93 | public void Should_redirect_stdout_to_file_if_set()
94 | {
95 | var fixture = new Fixture {Settings = { OutFile = "./output.txt" }};
96 |
97 | var result = fixture.Run();
98 |
99 | Assert.True(result.Process.RedirectStandardOutput);
100 | Assert.Equal(fixture.ProcessRunner.Process.GetStandardOutput(), File.ReadAllLines(fixture.Settings.OutFile.FullPath));
101 | }
102 | }
103 | }
104 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform.Tests/TerraformValidateTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Terraform.Validate;
4 | using Cake.Testing;
5 | using Xunit;
6 |
7 | namespace Cake.Terraform.Tests
8 | {
9 | public class TerraformValidateTests
10 | {
11 | class Fixture : TerraformFixture
12 | {
13 | public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }
14 |
15 | protected override void RunTool()
16 | {
17 | ProcessRunner.Process.SetStandardOutput(new List { "default" });
18 |
19 | var tool = new TerraformValidateRunner(FileSystem, Environment, ProcessRunner, Tools);
20 |
21 | tool.Run(Settings);
22 | }
23 | }
24 |
25 | public class TheExecutable
26 | {
27 | [Fact]
28 | public void Should_throw_if_terraform_runner_was_not_found()
29 | {
30 | var fixture = new Fixture();
31 | fixture.GivenDefaultToolDoNotExist();
32 |
33 | var result = Record.Exception(() => fixture.Run());
34 |
35 | Assert.IsType(result);
36 | Assert.Equal("Terraform: Could not locate executable.", result.Message);
37 | }
38 |
39 | [Theory]
40 | [InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
41 | [InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
42 | public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
43 | {
44 | var fixture = new Fixture() { Settings = { ToolPath = toolPath } };
45 | fixture.GivenSettingsToolPathExist();
46 |
47 | var result = fixture.Run();
48 |
49 | Assert.Equal(expected, result.Path.FullPath);
50 | }
51 |
52 | [Fact]
53 | public void Should_find_terraform_if_tool_path_not_provided()
54 | {
55 | var fixture = new Fixture();
56 |
57 | var result = fixture.Run();
58 |
59 | Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
60 | }
61 |
62 | [Fact]
63 | public void Should_throw_if_process_has_a_non_zero_exit_code()
64 | {
65 | var fixture = new Fixture();
66 | fixture.GivenProcessExitsWithCode(1);
67 |
68 | var result = Record.Exception(() => fixture.Run());
69 |
70 | Assert.IsType(result);
71 | Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
72 | }
73 |
74 | [Fact]
75 | public void Should_find_linux_executable()
76 | {
77 | var fixture = new Fixture(PlatformFamily.Linux);
78 | fixture.Environment.Platform.Family = PlatformFamily.Linux;
79 |
80 |
81 | var result = fixture.Run();
82 |
83 | Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
84 | }
85 |
86 | [Fact]
87 | public void Should_set_workspace_and_list_parameter()
88 | {
89 | var fixture = new Fixture();
90 |
91 | var result = fixture.Run();
92 |
93 | Assert.Contains("validate", result.Args);
94 | }
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/src/Cake.Terraform.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26228.12
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.Terraform", "Cake.Terraform\Cake.Terraform.csproj", "{C6E9A60F-9055-4E54-9E29-7F277875ABC1}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.Terraform.Tests", "Cake.Terraform.Tests\Cake.Terraform.Tests.csproj", "{AB9EDCDE-48B6-4B6E-B0B6-28CF549A6591}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{78C1E10D-45E1-4D1A-8063-B063BE554D6E}"
11 | ProjectSection(SolutionItems) = preProject
12 | Directory.Build.targets = Directory.Build.targets
13 | Directory.Build.props = Directory.Build.props
14 | EndProjectSection
15 | EndProject
16 | Global
17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
18 | Debug|Any CPU = Debug|Any CPU
19 | Release|Any CPU = Release|Any CPU
20 | EndGlobalSection
21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
22 | {C6E9A60F-9055-4E54-9E29-7F277875ABC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {C6E9A60F-9055-4E54-9E29-7F277875ABC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {C6E9A60F-9055-4E54-9E29-7F277875ABC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {C6E9A60F-9055-4E54-9E29-7F277875ABC1}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {AB9EDCDE-48B6-4B6E-B0B6-28CF549A6591}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {AB9EDCDE-48B6-4B6E-B0B6-28CF549A6591}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {AB9EDCDE-48B6-4B6E-B0B6-28CF549A6591}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {AB9EDCDE-48B6-4B6E-B0B6-28CF549A6591}.Release|Any CPU.Build.0 = Release|Any CPU
30 | EndGlobalSection
31 | GlobalSection(SolutionProperties) = preSolution
32 | HideSolutionNode = FALSE
33 | EndGlobalSection
34 | EndGlobal
35 |
--------------------------------------------------------------------------------
/src/Cake.Terraform/Apply/TerraformApplyRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.Apply
7 | {
8 | public class TerraformApplyRunner : TerraformRunner
9 | {
10 | public TerraformApplyRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
11 | : base(fileSystem, environment, processRunner, tools)
12 | {
13 | }
14 |
15 | public void Run(TerraformApplySettings settings)
16 | {
17 | var builder = new ProcessArgumentBuilder()
18 | .Append("apply");
19 |
20 | if (settings.Destroy)
21 | {
22 | builder.Append("-destroy");
23 | }
24 |
25 | // Order of AutoApprove and Plan are important.
26 | if (settings.AutoApprove)
27 | {
28 | builder.Append("-auto-approve");
29 | }
30 |
31 | // Use Plan if it exists.
32 | if (settings.Plan != null)
33 | {
34 | builder.AppendQuoted(settings.Plan.FullPath);
35 | }
36 |
37 | if (settings.Parallelism != default(int))
38 | {
39 | builder.AppendSwitch("-parallelism", "=", settings.Parallelism.ToString());
40 | }
41 |
42 | if (!string.IsNullOrWhiteSpace(settings.InputVariablesFile))
43 | {
44 | builder.AppendSwitchQuoted("-var-file", $"{settings.InputVariablesFile}");
45 | }
46 |
47 | foreach (var inputVariable in settings.InputVariables ?? new Dictionary())
48 | {
49 | builder.AppendSwitchQuoted("-var", $"{inputVariable.Key}={inputVariable.Value}");
50 | }
51 |
52 | if (!settings.Input)
53 | {
54 | builder.AppendSwitch("-input", "=", settings.Input.ToString());
55 | }
56 |
57 | Run(settings, builder);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/Cake.Terraform/Apply/TerraformApplySettings.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core.IO;
3 |
4 | namespace Cake.Terraform.Apply
5 | {
6 | public class TerraformApplySettings : TerraformSettings
7 | {
8 | public TerraformApplySettings()
9 | {
10 | this.Input = true;
11 | }
12 |
13 | public int Parallelism { get; set; }
14 |
15 | public FilePath Plan { get; set; }
16 |
17 | ///
18 | /// Gets or sets the input variables. https://www.terraform.io/intro/getting-started/variables.html
19 | ///
20 | public Dictionary InputVariables { get; set; }
21 |
22 | ///
23 | /// Variables file
24 | /// https://www.terraform.io/docs/configuration/variables.html#variable-files
25 | ///
26 | public string InputVariablesFile { get; set; }
27 |
28 | ///
29 | /// Skip interactive approval of plan before applying.
30 | /// https://www.terraform.io/docs/commands/apply.html#auto-approve
31 | ///
32 | public bool AutoApprove { get; set; }
33 |
34 | ///
35 | /// Ask for input for variables if not directly set.
36 | /// https://www.terraform.io/docs/commands/apply.html#input-true
37 | ///
38 | public bool Input { get; set; }
39 |
40 | ///
41 | /// Does destory
42 | /// https://www.terraform.io/docs/commands/destroy.html
43 | ///
44 | public bool Destroy { get; set; }
45 | }
46 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Cake.Terraform.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0;net7.0;net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Erik van Brakel
14 | opyright (c) 2017 - Present — Erik van Brakel
15 | Cake AddIn that extends Cake with ability to execute terraform commands.
16 | This version was build for Cake v$(CakeVersion)
17 | MIT
18 | https://github.com/cake-contrib/Cake.Terraform
19 | cake;build;cake-build;script;addin;cake-addin;terraform;hashicorp
20 | $(PackageProjectUrl).git
21 | This version was build for Cake v$(CakeVersion).
22 | For details see $(PackageProjectUrl)/releases
23 | 0.0.0
24 | README.md
25 |
26 |
27 |
28 |
29 | 0.1.3
30 | all
31 | runtime; build; native; contentfiles; analyzers; buildtransitive
32 |
33 |
34 | $(CakeVersion)
35 |
36 |
37 | 1.5.1
38 | all
39 | runtime; build; native; contentfiles; analyzers; buildtransitive
40 |
41 |
42 | runtime; build; native; contentfiles; analyzers; buildtransitive
43 | all
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/Cake.Terraform/Destroy/TerraformDestroyRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.Destroy
7 | {
8 | public class TerraformDestroyRunner : TerraformRunner
9 | {
10 | public TerraformDestroyRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
11 | : base(fileSystem, environment, processRunner, tools)
12 | {
13 | }
14 |
15 | public void Run(TerraformDestroySettings settings)
16 | {
17 | var builder = new ProcessArgumentBuilder().Append("destroy");
18 |
19 | if (settings.Force)
20 | {
21 | builder = builder.Append("-force");
22 | }
23 |
24 | if (!string.IsNullOrWhiteSpace(settings.InputVariablesFile))
25 | {
26 | builder.AppendSwitchQuoted("-var-file", $"{settings.InputVariablesFile}");
27 | }
28 |
29 | foreach (var inputVariable in settings.InputVariables ?? new Dictionary())
30 | {
31 | builder.AppendSwitchQuoted("-var", $"{inputVariable.Key}={inputVariable.Value}");
32 | }
33 |
34 | Run(settings, builder);
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Destroy/TerraformDestroySettings.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Cake.Terraform.Destroy
4 | {
5 | ///
6 | /// The terraform destroy command is used to destroy the Terraform-managed infrastructure.
7 | ///
8 | public class TerraformDestroySettings : TerraformSettings
9 | {
10 | ///
11 | /// Gets or sets the input variables. https://www.terraform.io/intro/getting-started/variables.html
12 | ///
13 | public Dictionary InputVariables { get; set; }
14 |
15 | ///
16 | /// Variables file
17 | /// https://www.terraform.io/docs/configuration/variables.html#variable-files
18 | ///
19 | public string InputVariablesFile { get; set; }
20 |
21 | ///
22 | /// If set to true, then the destroy confirmation will not be shown.
23 | ///
24 | public bool Force { get; set; }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvDelete/TerraformEnvDeleteRunner.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core;
2 | using Cake.Core.IO;
3 | using Cake.Core.Tooling;
4 |
5 | namespace Cake.Terraform.EnvDelete
6 | {
7 | public class TerraformEnvDeleteRunner : TerraformRunner
8 | {
9 | public TerraformEnvDeleteRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
10 | : base(fileSystem, environment, processRunner, tools)
11 | {
12 | }
13 | public void Run(TerraformEnvDeleteSettings settings)
14 | {
15 | var builder = new ProcessArgumentBuilder()
16 | .Append(settings.EnvCommand.ToString().ToLower())
17 | .Append("delete");
18 |
19 | if (settings.Force)
20 | {
21 | builder = builder.Append("-force");
22 | }
23 |
24 | Run(settings, builder);
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvDelete/TerraformEnvDeleteSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.EnvDelete
2 | {
3 | public class TerraformEnvDeleteSettings : TerraformEnvSettings
4 | {
5 | public string Environment { get; set; }
6 |
7 | ///
8 | /// If set to true, then on Subcommand delete the state even if non-empty. Defaults to false.
9 | ///
10 | public bool Force { get; set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvList/TerraformEnvListRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Cake.Core;
4 | using Cake.Core.IO;
5 | using Cake.Core.Tooling;
6 |
7 | namespace Cake.Terraform.EnvList
8 | {
9 | public class TerraformEnvListRunner : TerraformRunner
10 | {
11 | public TerraformEnvListRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
12 | : base(fileSystem, environment, processRunner, tools)
13 | {
14 | }
15 |
16 | public List Run(TerraformEnvListSettings settings)
17 | {
18 | var builder =
19 | new ProcessArgumentBuilder()
20 | .Append(settings.EnvCommand.ToString().ToLower())
21 | .Append("list");
22 |
23 | var processSettings = new ProcessSettings
24 | {
25 | RedirectStandardOutput = true
26 | };
27 |
28 | var result = new List();
29 | this.Run(settings, builder, processSettings, x =>
30 | {
31 | result = x.GetStandardOutput()
32 | .Select(env => env.Replace("*", "").Trim())
33 | .Where(env => !string.IsNullOrWhiteSpace(env))
34 | .ToList();
35 | });
36 |
37 | return result;
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvList/TerraformEnvListSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.EnvList
2 | {
3 | public class TerraformEnvListSettings : TerraformEnvSettings
4 | {
5 | }
6 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvNew/TerraformEnvNewRunner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.EnvNew
7 | {
8 | public class TerraformEnvNewRunner : TerraformRunner
9 | {
10 | public TerraformEnvNewRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
11 | : base(fileSystem, environment, processRunner, tools)
12 | {
13 | }
14 |
15 | public void Run(TerraformEnvNewSettings settings)
16 | {
17 | if (string.IsNullOrEmpty(settings.Environment))
18 | {
19 | throw new ArgumentException(nameof(settings.Environment));
20 | }
21 |
22 | var builder = new ProcessArgumentBuilder()
23 | .Append(settings.EnvCommand.ToString().ToLower())
24 | .Append("new")
25 | .Append(settings.Environment);
26 |
27 | Run(settings, builder);
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvNew/TerraformEnvNewSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.EnvNew
2 | {
3 | public class TerraformEnvNewSettings : TerraformEnvSettings
4 | {
5 | public string Environment { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvSelect/TerraformEnvSelectRunner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.EnvSelect
7 | {
8 | public class TerraformEnvSelectRunner : TerraformRunner
9 | {
10 | public TerraformEnvSelectRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
11 | : base(fileSystem, environment, processRunner, tools)
12 | {
13 | }
14 |
15 | public void Run(TerraformEnvSelectSettings settings)
16 | {
17 | if (string.IsNullOrEmpty(settings.Environment))
18 | {
19 | throw new ArgumentException(nameof(settings.Environment));
20 | }
21 |
22 | var builder = new ProcessArgumentBuilder()
23 | .Append(settings.EnvCommand.ToString().ToLower())
24 | .Append("select")
25 | .Append(settings.Environment);
26 |
27 | Run(settings, builder);
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/EnvSelect/TerraformEnvSelectSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.EnvSelect
2 | {
3 | public class TerraformEnvSelectSettings : TerraformEnvSettings
4 | {
5 | public string Environment { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Init/TerraformInitRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.Init
7 | {
8 | public class TerraformInitRunner : TerraformRunner
9 | {
10 | public TerraformInitRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
11 | : base(fileSystem, environment, processRunner, tools)
12 | {
13 | }
14 |
15 | public void Run(TerraformInitSettings settings)
16 | {
17 | var builder = new ProcessArgumentBuilder().Append("init");
18 |
19 | foreach (var pair in settings.BackendConfigOverrides ?? new Dictionary())
20 | {
21 | // https://www.terraform.io/docs/commands/init.html#backend-config-value
22 | builder.AppendSwitchQuoted("-backend-config", $"{pair.Key}={pair.Value}");
23 | }
24 |
25 | if (settings.ForceCopy)
26 | {
27 | builder.Append("-force-copy");
28 | }
29 |
30 | if (settings.ForceReconfigure)
31 | {
32 | builder.Append("-reconfigure");
33 | }
34 |
35 | if (!settings.Input)
36 | {
37 | builder.AppendSwitch("-input", "=", settings.Input.ToString());
38 | }
39 |
40 | if (!settings.Backend)
41 | {
42 | builder.AppendSwitch("-backend", "=", settings.Backend.ToString());
43 | }
44 |
45 | Run(settings, builder);
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Init/TerraformInitSettings.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Cake.Terraform.Init
4 | {
5 | public class TerraformInitSettings : TerraformSettings
6 | {
7 | public TerraformInitSettings()
8 | {
9 | this.Input = true;
10 | this.Backend = true;
11 | }
12 |
13 | ///
14 | /// A set of backend config key-value overrides to be passed to `terraform init`
15 | /// https://www.terraform.io/docs/commands/init.html#backend-config-value
16 | ///
17 | public Dictionary BackendConfigOverrides { get; set; }
18 |
19 | ///
20 | /// Reconfigure the backend, ignoring any saved configuration.
21 | /// https://www.terraform.io/docs/commands/init.html#reconfigure
22 | ///
23 | public bool ForceReconfigure { get; set; }
24 |
25 | ///
26 | /// Suppress prompts about copying state data. This is equivalent to providing
27 | /// a "yes" to all confirmation prompts.
28 | /// https://www.terraform.io/docs/commands/init.html#force-copy
29 | ///
30 | public bool ForceCopy { get; set; }
31 |
32 | ///
33 | /// Ask for input if necessary. If false, will error if input was required.
34 | /// https://www.terraform.io/docs/commands/init.html#input-true
35 | ///
36 | public bool Input { get; set; }
37 |
38 | ///
39 | /// Configure the backend for this configuration.
40 | /// https://www.terraform.io/docs/commands/init.html#backend-initialization
41 | ///
42 | public bool Backend { get; set; }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Cake.Terraform/Output/TerraformOutputRunner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Text;
4 | using Cake.Core;
5 | using Cake.Core.IO;
6 | using Cake.Core.Tooling;
7 |
8 | namespace Cake.Terraform.Output
9 | {
10 | public class TerraformOutputRunner : TerraformRunner
11 | {
12 | public TerraformOutputRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
13 | : base(fileSystem, environment, processRunner, tools)
14 | {
15 | }
16 |
17 | private static string RemoveWhitespace(string x)
18 | {
19 | return x
20 | .Replace("\n", "")
21 | .Replace("\r", "")
22 | .Replace(" ", "");
23 | }
24 |
25 | private void ConfirmSuccess(string output)
26 | {
27 | string outputNotFoundErrorString = RemoveWhitespace("The output variable requested could not be found in the state file. If you recently added this to your configuration, be sure to run `terraform apply`, since the state won't be updated with new output variables until that command is run.");
28 |
29 | if (RemoveWhitespace(output) == outputNotFoundErrorString)
30 | {
31 | throw new ArgumentException(output);
32 | }
33 | }
34 |
35 | public string Run(TerraformOutputSettings settings)
36 | {
37 | var arguments = new ProcessArgumentBuilder()
38 | .Append("output")
39 | .Append("-no-color");
40 |
41 | if (settings.StatePath != null)
42 | {
43 | arguments.Append($"-state={settings.StatePath}");
44 | }
45 |
46 | if (settings.Raw)
47 | {
48 | arguments.Append("-raw");
49 | }
50 | else if (settings.Json)
51 | {
52 | arguments.Append("-json");
53 | }
54 |
55 | if (settings.OutputName != null)
56 | {
57 | arguments.Append(settings.OutputName);
58 | }
59 |
60 | var processSettings = new ProcessSettings
61 | {
62 | RedirectStandardOutput = true
63 | };
64 |
65 | string output = null;
66 | Run(settings, arguments, processSettings, x =>
67 | {
68 | var outputLines = x.GetStandardOutput().ToList();
69 | int lineCount = outputLines.Count();
70 |
71 | var builder = new StringBuilder();
72 | for (int i = 0; i < lineCount; i++)
73 | {
74 | builder.Append(outputLines[i]);
75 |
76 | if (i < lineCount - 1)
77 | {
78 | builder.Append("\n"); // OS consistent
79 | }
80 | }
81 |
82 | output = builder.ToString();
83 | });
84 |
85 | if (settings.ValidateToolOutput)
86 | {
87 | ConfirmSuccess(output);
88 | }
89 |
90 | return output;
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Output/TerraformOutputSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.Output
2 | {
3 | public class TerraformOutputSettings : TerraformSettings
4 | {
5 | public string OutputName { get; set; }
6 | public bool Json { get; set; }
7 | public bool Raw { get; set; }
8 | public string StatePath { get; set; }
9 | public bool ValidateToolOutput { get; set; } = true;
10 | }
11 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Plan/TerraformPlanRunner.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core;
2 | using Cake.Core.IO;
3 | using Cake.Core.Tooling;
4 |
5 | namespace Cake.Terraform.Plan
6 | {
7 | public class TerraformPlanRunner : TerraformRunner
8 | {
9 | public TerraformPlanRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
10 | : base(fileSystem, environment, processRunner, tools)
11 | {
12 | }
13 |
14 | public bool HasChanges { get; private set; }
15 |
16 | public void Run(TerraformPlanSettings settings)
17 | {
18 | var builder = new ProcessArgumentBuilder()
19 | .Append("plan")
20 | .Append($"-out={settings.OutFile}")
21 | .Append("-detailed-exitcode");
22 |
23 | if (settings.Parallelism > 0)
24 | {
25 | builder = builder.Append($"-parallelism={settings.Parallelism}");
26 | }
27 |
28 | if (settings.Destroy)
29 | {
30 | builder = builder.Append("-destroy");
31 | }
32 |
33 | if (!string.IsNullOrEmpty(settings.InputVariablesFile))
34 | {
35 | builder.AppendSwitchQuoted("-var-file", $"{settings.InputVariablesFile}");
36 | }
37 |
38 | if (settings.InputVariables != null)
39 | {
40 | foreach (var inputVariable in settings.InputVariables)
41 | {
42 | builder.AppendSwitchQuoted("-var", $"{inputVariable.Key}={inputVariable.Value}");
43 | }
44 | }
45 |
46 | if (!settings.Input)
47 | {
48 | builder.AppendSwitch("-input", "=", settings.Input.ToString());
49 | }
50 |
51 | Run(settings, builder);
52 | }
53 |
54 | protected override void ProcessExitCode(int exitCode)
55 | {
56 | if (exitCode == 2)
57 | {
58 | HasChanges = true;
59 | }
60 | else
61 | {
62 | base.ProcessExitCode(exitCode);
63 | }
64 | }
65 | }
66 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Plan/TerraformPlanSettings.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core.IO;
3 |
4 | namespace Cake.Terraform.Plan
5 | {
6 | public class TerraformPlanSettings : TerraformSettings
7 | {
8 | public TerraformPlanSettings()
9 | {
10 | this.Input = true;
11 | }
12 |
13 | public FilePath OutFile { get; set; }
14 |
15 | public int Parallelism { get; set; }
16 |
17 | ///
18 | /// Gets or sets the input variables. https://www.terraform.io/intro/getting-started/variables.html
19 | ///
20 | public Dictionary InputVariables { get; set; }
21 |
22 | ///
23 | /// Variables file
24 | /// https://www.terraform.io/docs/configuration/variables.html#variable-files
25 | ///
26 | public string InputVariablesFile { get; set; }
27 |
28 | ///
29 | /// If set to true, generates a plan to destroy all the known resources
30 | ///
31 | public bool Destroy { get; set; }
32 |
33 | ///
34 | /// Ask for input for variables if not directly set.
35 | /// https://www.terraform.io/docs/commands/plan.html#input-true
36 | ///
37 | public bool Input { get; set; }
38 | }
39 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Refresh/TerraformRefreshRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform.Refresh
7 | {
8 | public class TerraformRefreshRunner : TerraformRunner
9 | {
10 | public TerraformRefreshRunner(IFileSystem fileSystem, ICakeEnvironment environment,
11 | IProcessRunner processRunner, IToolLocator tools)
12 | : base(fileSystem, environment, processRunner, tools)
13 | {
14 | }
15 |
16 | public void Run(TerraformRefreshSettings settings)
17 | {
18 | var builder = new ProcessArgumentBuilder().Append("refresh");
19 |
20 | // Use Plan if it exists.
21 | if (settings.Plan != null)
22 | {
23 | builder.Append(settings.Plan.FullPath);
24 | }
25 |
26 | foreach (var varFile in settings.InputVariablesFiles)
27 | {
28 | builder.AppendSwitchQuoted("-var-file", $"{varFile}");
29 | }
30 |
31 | if (!string.IsNullOrEmpty(settings.InputVariablesFile))
32 | {
33 | builder.AppendSwitchQuoted("-var-file", $"{settings.InputVariablesFile}");
34 | }
35 |
36 | builder = settings.InputVariables?.Aggregate(builder,
37 | (b, input) => b.AppendSwitchQuoted("-var", $"{input.Key}={input.Value}"))
38 | ?? builder;
39 |
40 | Run(settings, builder);
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Refresh/TerraformRefreshSettings.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Cake.Core.IO;
4 |
5 | namespace Cake.Terraform.Refresh
6 | {
7 | public class TerraformRefreshSettings : TerraformSettings
8 | {
9 | public FilePath Plan { get; set; }
10 |
11 | ///
12 | /// Gets or sets the input variables. https://www.terraform.io/intro/getting-started/variables.html
13 | ///
14 | public Dictionary InputVariables { get; set; }
15 |
16 | [Obsolete("use InputVariablesFiles instead.")]
17 | public string InputVariablesFile { get; set; }
18 |
19 | ///
20 | /// Variables file
21 | /// https://www.terraform.io/docs/configuration/variables.html#variable-files
22 | ///
23 | public ICollection InputVariablesFiles { get; } = new List();
24 | }
25 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/HtmlFormatter.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Net;
4 | using System.Text;
5 | using System.Text.RegularExpressions;
6 |
7 | namespace Cake.Terraform.Show
8 | {
9 | internal class HtmlFormatter : OutputFormatter
10 | {
11 | readonly Dictionary _styles = new Dictionary {
12 | { "0", "normal"},
13 | { "1", "bold"},
14 | { "30", "black"},
15 | { "31", "red"},
16 | { "32", "green"},
17 | { "33", "yellow"},
18 | { "34", "blue"},
19 | { "35", "magenta"},
20 | { "36", "cyan"},
21 | { "37", "white"},
22 | { "90", "grey"}
23 | };
24 |
25 | private readonly string _header = @"
26 |
27 |
28 |
60 |
61 | ";
62 |
63 | private readonly string _footer = @"
64 |
65 | ";
66 |
67 | public override string FormatLines(IEnumerable lines)
68 | {
69 | var outputBuilder = new StringBuilder();
70 | outputBuilder.AppendLine(_header);
71 |
72 | var regex = new Regex(@"(.*)\e\[(3[0-7]|90|1)m(.*)");
73 | foreach(var line in lines.Select(x => new Regex(@"\e\[0m").Replace(x, "").Replace("\\r","\r").Replace("\\n", "\n")))
74 | {
75 | var match = regex.Match(line);
76 | if(match.Success) {
77 | var styledLine = $"{WebUtility.HtmlEncode(match.Groups[1].Value)}{WebUtility.HtmlEncode(match.Groups[3].Value)}";
78 | outputBuilder.AppendLine(styledLine);
79 | }
80 | else
81 | {
82 | outputBuilder.AppendLine(WebUtility.HtmlEncode(line));
83 | }
84 | }
85 |
86 | outputBuilder.AppendLine(_footer);
87 |
88 | return outputBuilder.ToString();
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/OutputFormat.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform.Show
2 | {
3 | public enum OutputFormat
4 | {
5 | PlainText,
6 | Html
7 | }
8 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/OutputFormatter.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Cake.Terraform.Show
4 | {
5 | internal abstract class OutputFormatter
6 | {
7 | public abstract string FormatLines(IEnumerable lines);
8 | }
9 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/PlainTextFormatter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Cake.Terraform.Show
5 | {
6 | internal class PlainTextFormatter : OutputFormatter
7 | {
8 | public override string FormatLines(IEnumerable lines)
9 | {
10 | return string.Join(Environment.NewLine, lines);
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/TerraformShowRunner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using Cake.Core;
5 | using Cake.Core.IO;
6 | using Cake.Core.Tooling;
7 |
8 | namespace Cake.Terraform.Show
9 | {
10 | public class TerraformShowRunner : TerraformRunner
11 | {
12 | private readonly Dictionary _formatters = new Dictionary
13 | {
14 | {OutputFormat.PlainText, new PlainTextFormatter()},
15 | {OutputFormat.Html, new HtmlFormatter()}
16 | };
17 |
18 | public TerraformShowRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
19 | : base(fileSystem, environment, processRunner, tools)
20 | {
21 | }
22 |
23 | public void Run(TerraformShowSettings settings)
24 | {
25 | if (settings.PlanFile == null)
26 | {
27 | throw new ArgumentNullException(nameof(settings.PlanFile));
28 | }
29 |
30 | var arguments = new ProcessArgumentBuilder()
31 | .Append("show")
32 | .Append(settings.PlanFile.FullPath);
33 |
34 | if (settings.OutputFormat == OutputFormat.PlainText)
35 | {
36 | arguments.Append("-no-color");
37 | }
38 |
39 | var processSettings = new ProcessSettings();
40 | if (settings.OutFile != null)
41 | {
42 | processSettings.RedirectStandardOutput = true;
43 | }
44 |
45 | Run(settings, arguments, processSettings, x =>
46 | {
47 | if (settings.OutFile != null)
48 | {
49 | if (!_formatters.TryGetValue(settings.OutputFormat, out var formatter))
50 | {
51 | formatter = _formatters[OutputFormat.PlainText];
52 | }
53 |
54 | File.WriteAllText(settings.OutFile.FullPath, formatter.FormatLines(x.GetStandardOutput()));
55 | }
56 | });
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Show/TerraformShowSettings.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core.IO;
2 |
3 | namespace Cake.Terraform.Show
4 | {
5 | public class TerraformShowSettings : TerraformSettings
6 | {
7 | public FilePath OutFile { get; set; }
8 | public FilePath PlanFile { get; set; }
9 | public OutputFormat OutputFormat { get; set; } = OutputFormat.PlainText;
10 | }
11 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/TerraformAliases.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Core.Annotations;
4 | using Cake.Terraform.Apply;
5 | using Cake.Terraform.Destroy;
6 | using Cake.Terraform.EnvDelete;
7 | using Cake.Terraform.EnvList;
8 | using Cake.Terraform.EnvNew;
9 | using Cake.Terraform.EnvSelect;
10 | using Cake.Terraform.Init;
11 | using Cake.Terraform.Output;
12 | using Cake.Terraform.Plan;
13 | using Cake.Terraform.Refresh;
14 | using Cake.Terraform.Show;
15 | using Cake.Terraform.Validate;
16 |
17 | namespace Cake.Terraform
18 | {
19 | [CakeAliasCategory("Terraform")]
20 | public static class TerraformAliases
21 | {
22 | [CakeMethodAlias]
23 | public static void TerraformInit(this ICakeContext context)
24 | {
25 | TerraformInit(context, new TerraformInitSettings());
26 | }
27 |
28 | [CakeMethodAlias]
29 | public static void TerraformInit(this ICakeContext context, TerraformInitSettings settings)
30 | {
31 | var runner = new TerraformInitRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
32 | runner.Run(settings);
33 | }
34 |
35 | [CakeMethodAlias]
36 | public static bool TerraformPlan(this ICakeContext context)
37 | {
38 | return TerraformPlan(context, new TerraformPlanSettings());
39 | }
40 |
41 | [CakeMethodAlias]
42 | public static bool TerraformPlan(this ICakeContext context, TerraformPlanSettings settings)
43 | {
44 | var runner = new TerraformPlanRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
45 | runner.Run(settings);
46 | return runner.HasChanges;
47 | }
48 |
49 | [CakeMethodAlias]
50 | public static void TerraformApply(this ICakeContext context)
51 | {
52 | TerraformApply(context, new TerraformApplySettings());
53 | }
54 |
55 | [CakeMethodAlias]
56 | public static void TerraformApply(this ICakeContext context, TerraformApplySettings settings)
57 | {
58 | var runner = new TerraformApplyRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
59 | runner.Run(settings);
60 | }
61 |
62 | [CakeMethodAlias]
63 | public static void TerraformShow(this ICakeContext context)
64 | {
65 | TerraformShow(context, new TerraformShowSettings());
66 | }
67 |
68 | [CakeMethodAlias]
69 | public static void TerraformShow(this ICakeContext context, TerraformShowSettings settings)
70 | {
71 | var runner = new TerraformShowRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
72 | runner.Run(settings);
73 | }
74 |
75 | [CakeMethodAlias]
76 | public static void TerraformDestroy(this ICakeContext context)
77 | {
78 | TerraformDestroy(context, new TerraformDestroySettings());
79 | }
80 |
81 | [CakeMethodAlias]
82 | public static void TerraformDestroy(this ICakeContext context, TerraformDestroySettings settings)
83 | {
84 | var runner = new TerraformDestroyRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
85 | runner.Run(settings);
86 | }
87 |
88 | [CakeMethodAlias]
89 | public static void TerraformEnvDelete(this ICakeContext context)
90 | {
91 | TerraformEnvDelete(context, new TerraformEnvDeleteSettings());
92 | }
93 |
94 | [CakeMethodAlias]
95 | public static void TerraformEnvDelete(this ICakeContext context, TerraformEnvDeleteSettings settings)
96 | {
97 | var runner = new TerraformEnvDeleteRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
98 | runner.Run(settings);
99 | }
100 |
101 | [CakeMethodAlias]
102 | public static void TerraformEnvList(this ICakeContext context)
103 | {
104 | TerraformEnvList(context, new TerraformEnvListSettings());
105 | }
106 |
107 | [CakeMethodAlias]
108 | public static List TerraformEnvList(this ICakeContext context, TerraformEnvListSettings settings)
109 | {
110 | var runner = new TerraformEnvListRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
111 | return runner.Run(settings);
112 | }
113 |
114 | [CakeMethodAlias]
115 | public static void TerraformEnvNew(this ICakeContext context, string environment)
116 | {
117 | TerraformEnvNew(context, new TerraformEnvNewSettings { Environment = environment });
118 | }
119 |
120 | [CakeMethodAlias]
121 | public static void TerraformEnvNew(this ICakeContext context, TerraformEnvNewSettings settings)
122 | {
123 | var runner = new TerraformEnvNewRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
124 | runner.Run(settings);
125 | }
126 |
127 | [CakeMethodAlias]
128 | public static void TerraformEnvSelect(this ICakeContext context, string environment)
129 | {
130 | TerraformEnvSelect(context, new TerraformEnvSelectSettings { Environment = environment });
131 | }
132 |
133 | [CakeMethodAlias]
134 | public static void TerraformEnvSelect(this ICakeContext context, TerraformEnvSelectSettings settings)
135 | {
136 | var runner = new TerraformEnvSelectRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
137 | runner.Run(settings);
138 | }
139 |
140 | [CakeMethodAlias]
141 | public static void TerraformRefresh(this ICakeContext context)
142 | {
143 | TerraformRefresh(context, new TerraformRefreshSettings());
144 | }
145 |
146 | [CakeMethodAlias]
147 | public static void TerraformRefresh(this ICakeContext context, TerraformRefreshSettings settings)
148 | {
149 | var runner = new TerraformRefreshRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
150 | runner.Run(settings);
151 | }
152 |
153 | [CakeMethodAlias]
154 | public static void TerraformValidate(this ICakeContext context)
155 | {
156 | TerraformValidate(context, new TerraformValidateSettings());
157 | }
158 |
159 | [CakeMethodAlias]
160 | public static void TerraformValidate(this ICakeContext context, TerraformValidateSettings settings)
161 | {
162 | var runner = new TerraformValidateRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
163 | runner.Run(settings);
164 | }
165 |
166 | [CakeMethodAlias]
167 | public static string TerraformOutput(this ICakeContext context, TerraformOutputSettings settings)
168 | {
169 | var runner = new TerraformOutputRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
170 | return runner.Run(settings);
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/src/Cake.Terraform/TerraformEnvSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Cake.Terraform
2 | {
3 | public class TerraformEnvSettings : TerraformSettings
4 | {
5 |
6 | public enum EnvCommandType
7 | {
8 | Workspace,
9 | Env
10 | }
11 |
12 | public EnvCommandType EnvCommand { get; set; } = EnvCommandType.Workspace;
13 | }
14 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/TerraformRunner.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Cake.Core;
3 | using Cake.Core.IO;
4 | using Cake.Core.Tooling;
5 |
6 | namespace Cake.Terraform
7 | {
8 | public abstract class TerraformRunner : Tool where TTerraformSettings : TerraformSettings
9 | {
10 | private readonly ICakePlatform _platform;
11 |
12 | protected TerraformRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
13 | : base(fileSystem, environment, processRunner, tools)
14 | {
15 | _platform = environment.Platform;
16 | }
17 |
18 | protected override string GetToolName()
19 | {
20 | return "Terraform";
21 | }
22 |
23 | protected override IEnumerable GetToolExecutableNames()
24 | {
25 | return new[] { _platform.IsUnix() ? "terraform" : "terraform.exe" };
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/TerraformSettings.cs:
--------------------------------------------------------------------------------
1 | using Cake.Core.Tooling;
2 |
3 | namespace Cake.Terraform
4 | {
5 | public abstract class TerraformSettings : ToolSettings
6 | {
7 | }
8 | }
--------------------------------------------------------------------------------
/src/Cake.Terraform/Validate/TerraformValidateRunner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Cake.Core;
7 | using Cake.Core.IO;
8 | using Cake.Core.Tooling;
9 | using Cake.Terraform.Init;
10 |
11 | namespace Cake.Terraform.Validate
12 | {
13 | public class TerraformValidateRunner : TerraformRunner
14 | {
15 | public TerraformValidateRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
16 | : base(fileSystem, environment, processRunner, tools)
17 | {
18 | }
19 |
20 | public void Run(TerraformValidateSettings settings)
21 | {
22 | var builder = new ProcessArgumentBuilder().Append("validate");
23 |
24 |
25 | Run(settings, builder);
26 | }
27 | }
28 |
29 | public class TerraformValidateSettings : TerraformSettings
30 | {
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/Directory.Build.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb
8 | true
9 | true
10 | $([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
23 |
24 | <_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''"/>
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------