Skip to content

Commit

Permalink
add replace resource option (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-tspencer authored Jan 3, 2025
1 parent bb39e91 commit 3d64c17
Show file tree
Hide file tree
Showing 8 changed files with 1,212 additions and 5 deletions.
1,145 changes: 1,145 additions & 0 deletions src/Caster.Api/Data/Migrations/20241210162042_replace_addresses.Designer.cs

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/Caster.Api/Data/Migrations/20241210162042_replace_addresses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2021 Carnegie Mellon University. All Rights Reserved.
Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.
*/

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Caster.Api.Data.Migrations
{
/// <inheritdoc />
public partial class replace_addresses : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "replace_addresses",
table: "runs",
type: "text",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "replace_addresses",
table: "runs");
}
}
}
6 changes: 5 additions & 1 deletion src/Caster.Api/Data/Migrations/CasterContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.7")
.HasAnnotation("ProductVersion", "8.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "uuid-ossp");
Expand Down Expand Up @@ -591,6 +591,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("uuid")
.HasColumnName("modified_by_id");

b.Property<string>("ReplaceAddresses")
.HasColumnType("text")
.HasColumnName("replace_addresses");

b.Property<int>("Status")
.HasColumnType("integer")
.HasColumnName("status");
Expand Down
9 changes: 9 additions & 0 deletions src/Caster.Api/Domain/Models/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Run

public string[] Targets { get; set; }

public string[] ReplaceAddresses { get; set; }

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Guid? CreatedById { get; set; }
public User CreatedBy { get; set; }
Expand Down Expand Up @@ -86,6 +88,13 @@ public void Configure(EntityTypeBuilder<Run> builder)
str => str.Split('\n', StringSplitOptions.RemoveEmptyEntries)
);

builder
.Property<string[]>(r => r.ReplaceAddresses)
.HasConversion(
list => String.Join('\n', list),
str => str.Split('\n', StringSplitOptions.RemoveEmptyEntries)
);

builder
.HasOne(r => r.Plan)
.WithOne(p => p.Run)
Expand Down
9 changes: 7 additions & 2 deletions src/Caster.Api/Domain/Services/TerraformService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ITerraformService
TerraformResult InitializeWorkspace(Workspace workspace, DataReceivedEventHandler outputHandler);
TerraformResult Init(Workspace workspace, DataReceivedEventHandler outputHandler);
TerraformResult SelectWorkspace(Workspace workspace, DataReceivedEventHandler outputHandler);
TerraformResult Plan(Workspace workspace, bool destroy, string[] targets, DataReceivedEventHandler outputHandler);
TerraformResult Plan(Workspace workspace, bool destroy, string[] targets, string[] replaceAddresses, DataReceivedEventHandler outputHandler);
TerraformResult Apply(Workspace workspace, DataReceivedEventHandler outputHandler);
TerraformResult Show(Workspace workspace);
TerraformResult Taint(Workspace workspace, string address, string statePath);
Expand Down Expand Up @@ -190,7 +190,7 @@ public TerraformResult SelectWorkspace(Workspace workspace, DataReceivedEventHan
return this.Run(workspace, args, outputHandler);
}

public TerraformResult Plan(Workspace workspace, bool destroy, string[] targets, DataReceivedEventHandler outputHandler)
public TerraformResult Plan(Workspace workspace, bool destroy, string[] targets, string[] replaceAddresses, DataReceivedEventHandler outputHandler)
{
List<string> args = new List<string> { "plan", "-input=false", "-out=plan" };

Expand All @@ -209,6 +209,11 @@ public TerraformResult Plan(Workspace workspace, bool destroy, string[] targets,
args.Add($"--target={target}");
}

foreach (string address in replaceAddresses)
{
args.Add($"-replace={address}");
}

return this.Run(workspace, args, outputHandler);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Caster.Api/Features/Runs/EventHandlers/RunAddedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private async Task<bool> DoWork(Domain.Models.Run run)
}

// Plan
var planResult = _terraformService.Plan(workspace, run.IsDestroy, run.Targets, OutputHandler);
var planResult = _terraformService.Plan(workspace, run.IsDestroy, run.Targets, run.ReplaceAddresses, OutputHandler);
isError = planResult.IsError;
}

Expand Down Expand Up @@ -280,7 +280,7 @@ private void OnTimedEvent(Object source, ElapsedEventArgs e)

/// <summary>
/// Attempts to mitigate common issues with destroying Azure workspaces.
/// If previous destroys failed, remove from the state any resources that exist within an
/// If previous destroys failed, remove from the state any resources that exist within an
/// azurerm_resource_group managed by this workspace. They will be destroyed implicitly when destroying the workspace and
/// can avoid errors with destroying those resources directly.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Caster.Api/Features/Runs/Requests/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class Command : IRequest<Run>
/// </summary>
[DataMember]
public string[] Targets { get; set; }

/// <summary>
/// Optional list of resources to replace on this Run
/// </summary>
[DataMember]
public string[] ReplaceAddresses { get; set; }
}

public class Handler : IRequestHandler<Command, Run>
Expand Down
5 changes: 5 additions & 0 deletions src/Caster.Api/Features/Runs/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class Run
/// </summary>
public string[] Targets { get; set; }

/// <summary>
/// Optional list of resources to replace on this Run
/// </summary>
public string[] ReplaceAddresses { get; set; }

/// <summary>
/// The Plan for this Run, if one exists. Null if not requested
/// </summary>
Expand Down

0 comments on commit 3d64c17

Please sign in to comment.