Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[auto/dotnet] Support for remote operations #11194

Merged
merged 1 commit into from Oct 31, 2022
Merged

Conversation

justinvp
Copy link
Member

@justinvp justinvp commented Oct 31, 2022

This change adds preview support for remote operations in .NET's Automation API.

Note: I've tried many incantations of dotnet format dotnet.sln analyzers --diagnostics=RS0016, but have not been able to get the code fix applied to update sdk/dotnet/Pulumi.Automation/PublicAPI.Shipped.txt. Perhaps a .NET 6 bug (I'm running on an M1 Mac)? dotnet/format#1416 might be relevant. I'll try again a bit later, but I might have to manually update the file, unless someone else's tools are working and can pull down the branch and run it.

Update: I didn't see any failures in the PR related to not having updated sdk/dotnet/Pulumi.Automation/PublicAPI.Shipped.txt, so maybe we're no longer checking this or something is broken? In any case, I still wasn't able to get dotnet format to automatically apply the updates, so I manually edited PublicAPI.Shipped.txt. I think I got everything, but it's possible I missed something. Let's see what CI says.

Here's an example of using it:

using System;
using System.Linq;
using Pulumi.Automation;

bool destroy = args.Any() && args[0] == "destroy";

const string org = "justinvp";
const string projectName = "aws-ts-s3-folder";
var stackName = $"{org}/{projectName}/devdotnet";

var stackArgs = new RemoteGitProgramArgs(stackName, "https://github.com/pulumi/examples.git")
{
    Branch = "refs/heads/master",
    ProjectPath = projectName,
    EnvironmentVariables =
   {
      { "AWS_REGION", new EnvironmentVariableValue("us-west-2") },
      { "AWS_ACCESS_KEY_ID", RequireFromEnvironment("AWS_ACCESS_KEY_ID") },
      { "AWS_SECRET_ACCESS_KEY", RequireFromEnvironment("AWS_SECRET_ACCESS_KEY", isSecret: true) },
      { "AWS_SESSION_TOKEN", RequireFromEnvironment("AWS_SESSION_TOKEN", isSecret: true) },
   },
};
var stack = await RemoteWorkspace.CreateOrSelectStackAsync(stackArgs);

if (destroy)
{
    await stack.DestroyAsync(new RemoteDestroyOptions { OnStandardOutput = Console.WriteLine });
}
else
{
    Console.WriteLine("updating stack...");
    var result = await stack.UpAsync(new RemoteUpOptions { OnStandardOutput = Console.WriteLine });

    if (result.Summary.ResourceChanges != null)
    {
        Console.WriteLine("update summary:");
        foreach (var change in result.Summary.ResourceChanges)
            Console.WriteLine($"    {change.Key}: {change.Value}");
    }

    Console.WriteLine($"url: {result.Outputs["websiteUrl"].Value}");
}

static EnvironmentVariableValue RequireFromEnvironment(string variable, bool isSecret = false)
{
    var value = Environment.GetEnvironmentVariable(variable)
       ?? throw new InvalidOperationException($"Required environment variable {variable} not set.");
    return new EnvironmentVariableValue(value, isSecret);
}

I will add sanity tests subsequently.

@pulumi-bot
Copy link
Contributor

Changelog

[uncommitted] (2022-10-31)

Features

  • [auto/dotnet] Support for remote operations
    #11194

@Frassle
Copy link
Member

Frassle commented Oct 31, 2022

bors merge

@bors
Copy link
Contributor

bors bot commented Oct 31, 2022

Build succeeded:

@bors bors bot merged commit 6a74bc1 into master Oct 31, 2022
@bors bors bot deleted the justin/remote_dotnet branch October 31, 2022 09:24
abhinav pushed a commit to pulumi/pulumi-dotnet that referenced this pull request Jan 11, 2023
11194: [auto/dotnet] Support for remote operations r=Frassle a=justinvp

This change adds preview support for remote operations in .NET's Automation API.

**Note:** ~~I've tried _many_ incantations of `dotnet format dotnet.sln analyzers --diagnostics=RS0016`, but have not been able to get the code fix applied to update `sdk/dotnet/Pulumi.Automation/PublicAPI.Shipped.txt`. Perhaps a .NET 6 bug (I'm running on an M1 Mac)? dotnet/format#1416 might be relevant. I'll try again a bit later, but I might have to manually update the file, unless someone else's tools are working and can pull down the branch and run it.~~

Update: I didn't see any failures in the PR related to not having updated `sdk/dotnet/Pulumi.Automation/PublicAPI.Shipped.txt`, so maybe we're no longer checking this or something is broken? In any case, I still wasn't able to get `dotnet format` to automatically apply the updates, so I manually edited `PublicAPI.Shipped.txt`. I _think_ I got everything, but it's possible I missed something. Let's see what CI says.

Here's an example of using it:

```c#
using System;
using System.Linq;
using Pulumi.Automation;

bool destroy = args.Any() && args[0] == "destroy";

const string org = "justinvp";
const string projectName = "aws-ts-s3-folder";
var stackName = $"{org}/{projectName}/devdotnet";

var stackArgs = new RemoteGitProgramArgs(stackName, "https://github.com/pulumi/examples.git")
{
    Branch = "refs/heads/master",
    ProjectPath = projectName,
    EnvironmentVariables =
   {
      { "AWS_REGION", new EnvironmentVariableValue("us-west-2") },
      { "AWS_ACCESS_KEY_ID", RequireFromEnvironment("AWS_ACCESS_KEY_ID") },
      { "AWS_SECRET_ACCESS_KEY", RequireFromEnvironment("AWS_SECRET_ACCESS_KEY", isSecret: true) },
      { "AWS_SESSION_TOKEN", RequireFromEnvironment("AWS_SESSION_TOKEN", isSecret: true) },
   },
};
var stack = await RemoteWorkspace.CreateOrSelectStackAsync(stackArgs);

if (destroy)
{
    await stack.DestroyAsync(new RemoteDestroyOptions { OnStandardOutput = Console.WriteLine });
}
else
{
    Console.WriteLine("updating stack...");
    var result = await stack.UpAsync(new RemoteUpOptions { OnStandardOutput = Console.WriteLine });

    if (result.Summary.ResourceChanges != null)
    {
        Console.WriteLine("update summary:");
        foreach (var change in result.Summary.ResourceChanges)
            Console.WriteLine($"    {change.Key}: {change.Value}");
    }

    Console.WriteLine($"url: {result.Outputs["websiteUrl"].Value}");
}

static EnvironmentVariableValue RequireFromEnvironment(string variable, bool isSecret = false)
{
    var value = Environment.GetEnvironmentVariable(variable)
       ?? throw new InvalidOperationException($"Required environment variable {variable} not set.");
    return new EnvironmentVariableValue(value, isSecret);
}
```

I will add sanity tests subsequently.

Co-authored-by: Justin Van Patten <jvp@justinvp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants