Skip to content

Commit

Permalink
Update C# examples to use the new F.Invoke feature (pulumi#1093)
Browse files Browse the repository at this point in the history
* Update aws-cs-eks

* Update aws-cs-fargate

* Updated aws-cs-webserver

* Update azure-cs-aks-cosmos-helm

* Update azure-cs-aks-helm

* Update azure-cs-aks

* Updated azure-cs-appservice-docker

* Update azure-cs-appservice

* Update azure-cs-cosmosdb-logicapp

* Update azure-cs-credential-rotation-one-set

* Update azure-cs-functions

* Updated azure-cs-net5-aks-webapp

* Update aws-cs-fargate/Infra

* Update testing-unit-cs

* No-op change to trigger CI

* Force 3.17.1 upgrade

* Trigger CI again

* Fix indent
  • Loading branch information
t0yv0 authored Nov 12, 2021
1 parent 6bf0c30 commit ce1f4a9
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 207 deletions.
12 changes: 6 additions & 6 deletions aws-cs-eks/EksStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ private Output<string> GenerateKubeconfig(Output<string> clusterEndpoint, Output
public EksStack()
{
// Read back the default VPC and public subnets, which we will use.
var vpc = Output.Create(Ec2.GetVpc.InvokeAsync(new Ec2.GetVpcArgs {Default = true}));
var vpcId = vpc.Apply(vpc => vpc.Id);
var subnet = vpcId.Apply(id => Ec2.GetSubnetIds.InvokeAsync(new Ec2.GetSubnetIdsArgs {VpcId = id}));
var subnetIds = subnet.Apply(s => s.Ids);
var vpcId = Ec2.GetVpc.Invoke(new Ec2.GetVpcInvokeArgs { Default = true })
.Apply(vpc => vpc.Id);

var subnetIds = Ec2.GetSubnetIds.Invoke(new Ec2.GetSubnetIdsInvokeArgs { VpcId = vpcId })
.Apply(s => s.Ids);

// Create an IAM role that can be used by our service's task.
var eksRole = new Iam.Role("eks-iam-eksRole", new Iam.RoleArgs
Expand Down Expand Up @@ -180,7 +181,7 @@ public EksStack()
});

this.Kubeconfig = GenerateKubeconfig(cluster.Endpoint,
cluster.CertificateAuthority.Apply(x => x.Data),
cluster.CertificateAuthority.Apply(x => x.Data ?? ""),
cluster.Name);

var k8sProvider = new K8s.Provider("k8s-provider", new K8s.ProviderArgs
Expand Down Expand Up @@ -274,4 +275,3 @@ public EksStack()
[Output] public Output<string> Kubeconfig { get; set; }
[Output] public Output<string> Url { get; set; }
}

22 changes: 12 additions & 10 deletions aws-cs-fargate/Infra/FargateStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class FargateStack : Stack
public FargateStack()
{
// Read back the default VPC and public subnets, which we will use.
var vpc = Output.Create(Ec2.GetVpc.InvokeAsync(new Ec2.GetVpcArgs {Default = true}));
var vpcId = vpc.Apply(vpc => vpc.Id);
var subnet = vpcId.Apply(id => Ec2.GetSubnetIds.InvokeAsync(new Ec2.GetSubnetIdsArgs {VpcId = id}));
var subnetIds = subnet.Apply(s => s.Ids);
var vpcId = Ec2.GetVpc.Invoke(new Ec2.GetVpcInvokeArgs {Default = true})
.Apply(vpc => vpc.Id);

var subnetIds = Ec2.GetSubnetIds.Invoke(new Ec2.GetSubnetIdsInvokeArgs {VpcId = vpcId})
.Apply(s => s.Ids);

// Create a SecurityGroup that permits HTTP ingress and unrestricted egress.
var webSg = new Ec2.SecurityGroup("web-sg", new Ec2.SecurityGroupArgs
Expand Down Expand Up @@ -100,12 +101,13 @@ public FargateStack()

// Create a private ECR registry and build and publish our app's container image to it.
var appRepo = new Ecr.Repository("app-repo");
var appRepoCredentials = appRepo.RegistryId.Apply(async rid =>
{
var credentials = await Ecr.GetCredentials.InvokeAsync(new Ecr.GetCredentialsArgs {RegistryId = rid});
var data = Convert.FromBase64String(credentials.AuthorizationToken);
return Encoding.UTF8.GetString(data).Split(":").ToImmutableArray();
});
var appRepoCredentials = Ecr.GetCredentials
.Invoke(new Ecr.GetCredentialsInvokeArgs {RegistryId = appRepo.RegistryId})
.Apply(credentials =>
{
var data = Convert.FromBase64String(credentials.AuthorizationToken);
return Encoding.UTF8.GetString(data).Split(":").ToImmutableArray();
});
var image = new Docker.Image("app-img", new Docker.ImageArgs
{
Build = "../App",
Expand Down
8 changes: 3 additions & 5 deletions aws-cs-webserver/WebServerStack.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.

using Pulumi;
using Pulumi.Aws;
using Pulumi.Aws.Ec2;
using Pulumi.Aws.Ec2.Inputs;
using Pulumi.Aws.Inputs;

class WebServerStack : Stack
{
public WebServerStack()
{
var ami = Output.Create(Pulumi.Aws.Ec2.GetAmi.InvokeAsync(new Pulumi.Aws.Ec2.GetAmiArgs
var ami = GetAmi.Invoke(new GetAmiInvokeArgs
{
MostRecent = true,
Owners = {"137112412989"},
Filters = {new Pulumi.Aws.Ec2.Inputs.GetAmiFilterArgs {Name = "name", Values = {"amzn-ami-hvm-*"}}}
}));
Filters = {new GetAmiFilterInputArgs {Name = "name", Values = "amzn-ami-hvm-*"}}
});

var group = new SecurityGroup("web-secgrp", new SecurityGroupArgs
{
Expand Down
18 changes: 6 additions & 12 deletions azure-cs-aks-cosmos-helm/AksCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AksCluster(string name, AksClusterArgs args)
{
ApplicationId = adApp.ApplicationId
}, new CustomResourceOptions { Parent = this });

var pw = new RandomPassword("pw", new RandomPasswordArgs
{
Length = 20,
Expand All @@ -43,7 +43,7 @@ public AksCluster(string name, AksClusterArgs args)
Value = pw.Result,
EndDate = "2099-01-01T00:00:00Z"
}, new CustomResourceOptions { Parent = this });

var keyPair = new PrivateKey("ssh-key", new PrivateKeyArgs
{
Algorithm = "RSA",
Expand Down Expand Up @@ -95,17 +95,11 @@ public AksCluster(string name, AksClusterArgs args)

this.ClusterName = k8sCluster.Name;

this.KubeConfig = Output.Tuple(k8sCluster.Name, args.ResourceGroupName.ToOutput())
.Apply(pair =>
this.KubeConfig = ListManagedClusterUserCredentials.Invoke(
new ListManagedClusterUserCredentialsInvokeArgs
{
var k8sClusterName = pair.Item1;
var resourceGroupName = pair.Item2;
return ListManagedClusterUserCredentials.InvokeAsync(new ListManagedClusterUserCredentialsArgs
{
ResourceGroupName = resourceGroupName,
ResourceName = k8sClusterName
});
ResourceGroupName = args.ResourceGroupName,
ResourceName = k8sCluster.Name
})
.Apply(x => x.Kubeconfigs[0].Value)
.Apply(Convert.FromBase64String)
Expand Down
29 changes: 13 additions & 16 deletions azure-cs-aks-cosmos-helm/CosmosDBMongoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
public class CosmosDBMongoDB : ComponentResource
{
public Output<string> AccountName { get; set; }

public Output<string> DatabaseName { get; set; }

public CosmosDBMongoDB(string name, CosmosDBMongoDBArgs args)
: base("example:component:CosmosDBMongoDB", name)
{
var config = new Config("azure-native");
var location = config.Require("location");

var databaseAccount = new DatabaseAccount("cosmos-mongodb", new DatabaseAccountArgs
{
ResourceGroupName = args.ResourceGroupName,
Expand Down Expand Up @@ -49,22 +49,19 @@ public CosmosDBMongoDB(string name, CosmosDBMongoDBArgs args)
}, new CustomResourceOptions { Parent = this });
this.DatabaseName = database.Name;
}

public static Output<ImmutableDictionary<string, string>> KubernetesSecretData(Output<string> resourceGroupName, Output<string> accountName, Output<string> databaseName)
{
return Output.Tuple(resourceGroupName, accountName, databaseName).Apply(async values =>
var connString = ListDatabaseAccountConnectionStrings.Invoke(
new ListDatabaseAccountConnectionStringsInvokeArgs
{
var conn = await ListDatabaseAccountConnectionStrings.InvokeAsync(
new ListDatabaseAccountConnectionStringsArgs
{
ResourceGroupName = values.Item1,
AccountName = values.Item2
});
return parseConnString(conn.ConnectionStrings[0].ConnectionString, values.Item3);
}
);
ResourceGroupName = resourceGroupName,
AccountName = accountName
}).Apply(conn => conn.ConnectionStrings[0].ConnectionString);

return connString.Apply(connString => databaseName.Apply(databaseName => parseConnString(connString, databaseName)));
}

private static ImmutableDictionary<string, string> parseConnString(string conn, string database)
{
// Per the official docs[1], the format of this connection string is:
Expand Down Expand Up @@ -113,7 +110,7 @@ private static ImmutableDictionary<string, string> parseConnString(string conn,
{"password", toBase64(Uri.EscapeDataString(password))},
{"database", toBase64(database)},
}.ToImmutableDictionary();

static string toBase64(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
Expand Down
14 changes: 4 additions & 10 deletions azure-cs-aks-helm/MyCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,11 @@ public MyCluster(MyConfig cfg)

this.ClusterName = k8sCluster.Name;

this.Kubeconfig = Output.Tuple(k8sCluster.Name, resourceGroup.Name)
.Apply(pair =>
this.Kubeconfig = ListManagedClusterUserCredentials.Invoke(
new ListManagedClusterUserCredentialsInvokeArgs
{
var k8sClusterName = pair.Item1;
var resourceGroupName = pair.Item2;
return ListManagedClusterUserCredentials.InvokeAsync(new ListManagedClusterUserCredentialsArgs
{
ResourceGroupName = resourceGroupName,
ResourceName = k8sClusterName
});
ResourceGroupName = resourceGroup.Name,
ResourceName = k8sCluster.Name
})
.Apply(x => x.Kubeconfigs[0].Value)
.Apply(Convert.FromBase64String)
Expand Down
28 changes: 13 additions & 15 deletions azure-cs-aks/MyStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@ public MyStack()
{
ApplicationId = adApp.ApplicationId
});

// Generate random password
var password = new RandomPassword("password", new RandomPasswordArgs
{
Length = 20,
Special = true
});

// Create the Service Principal Password
var adSpPassword = new ServicePrincipalPassword("aksSpPassword", new ServicePrincipalPasswordArgs
{
ServicePrincipalId = adSp.Id,
Value = password.Result,
EndDate = "2099-01-01T00:00:00Z"
});

// Generate an SSH key
var sshKey = new PrivateKey("ssh-key", new PrivateKeyArgs
{
Algorithm = "RSA",
RsaBits = 4096
});

var cluster = new ManagedCluster("my-aks", new ManagedClusterArgs
{
ResourceGroupName = resourceGroup.Name,
AgentPoolProfiles =
AgentPoolProfiles =
{
new ManagedClusterAgentPoolProfileArgs
{
Expand All @@ -75,7 +75,7 @@ public MyStack()
AdminUsername = "testuser",
Ssh = new ContainerServiceSshConfigurationArgs
{
PublicKeys =
PublicKeys =
{
new ContainerServiceSshPublicKeyArgs
{
Expand All @@ -93,22 +93,20 @@ public MyStack()
});

// Export the KubeConfig
this.KubeConfig = Output.Tuple(resourceGroup.Name, cluster.Name).Apply(names =>
GetKubeConfig(names.Item1, names.Item2));
this.KubeConfig = GetKubeConfig(resourceGroup.Name, cluster.Name);
}

[Output]
public Output<string> KubeConfig { get; set; }

private static async Task<string> GetKubeConfig(string resourceGroupName, string clusterName)
{
var credentials = await ListManagedClusterUserCredentials.InvokeAsync(new ListManagedClusterUserCredentialsArgs
private static Output<string> GetKubeConfig(Output<string> resourceGroupName, Output<string> clusterName)
=> ListManagedClusterUserCredentials.Invoke(new ListManagedClusterUserCredentialsInvokeArgs
{
ResourceGroupName = resourceGroupName,
ResourceName = clusterName
}).Apply(credentials => {
var encoded = credentials.Kubeconfigs[0].Value;
var data = Convert.FromBase64String(encoded);
return Encoding.UTF8.GetString(data);
});
var encoded = credentials.Kubeconfigs[0].Value;
var data = Convert.FromBase64String(encoded);
return Encoding.UTF8.GetString(data);
}
}
25 changes: 12 additions & 13 deletions azure-cs-appservice-docker/MyStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public MyStack()
// Image: https://hub.docker.com/r/microsoft/azure-appservices-go-quickstart/
//
var imageInDockerHub = "microsoft/azure-appservices-go-quickstart";

var helloApp = new WebApp("hello-app", new WebAppArgs
{
ResourceGroupName = resourceGroup.Name,
ServerFarmId = plan.Id,
SiteConfig = new SiteConfigArgs
{
AppSettings = new[]
{
AppSettings = new[]
{
new NameValuePairArgs
{
Name = "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
Expand All @@ -55,7 +55,7 @@ public MyStack()
});

this.HelloEndpoint = Output.Format($"https://{helloApp.DefaultHostName}/hello");

//
// Scenario 2: deploying a custom image from Azure Container Registry.
//
Expand All @@ -68,12 +68,11 @@ public MyStack()
AdminUserEnabled = true
});

var credentials = Output.Tuple(resourceGroup.Name, registry.Name).Apply(values =>
ListRegistryCredentials.InvokeAsync(new ListRegistryCredentialsArgs
var credentials = ListRegistryCredentials.Invoke(new ListRegistryCredentialsInvokeArgs
{
ResourceGroupName = values.Item1,
RegistryName = values.Item2
}));
ResourceGroupName = resourceGroup.Name,
RegistryName = registry.Name
});
var adminUsername = credentials.Apply(c => c.Username ?? "");
var adminPassword = credentials.Apply(c => Output.CreateSecret(c.Passwords.First().Value ?? ""));

Expand All @@ -88,15 +87,15 @@ public MyStack()
Password = adminPassword
},
});

var getStartedApp = new WebApp("get-started", new WebAppArgs
{
ResourceGroupName = resourceGroup.Name,
ServerFarmId = plan.Id,
SiteConfig = new SiteConfigArgs
{
AppSettings = new[]
{
AppSettings = new[]
{
new NameValuePairArgs
{
Name = "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
Expand Down Expand Up @@ -128,7 +127,7 @@ public MyStack()
},
HttpsOnly = true
});

this.GetStartedEndpoint = Output.Format($"https://{getStartedApp.DefaultHostName}");
}

Expand Down
Loading

0 comments on commit ce1f4a9

Please sign in to comment.