Giter Club home page Giter Club logo

authoring-dotnet-pulumi-mlcs's Introduction

Multi-language Pulumi components using .NET

This is a sample project that shows how to create a multi-language component using .NET with a simple API to register inputs, components and their outputs. The SimpleProvider automatically generates and exposes a Pulumi schema from the user-defined types.

using Pulumi.Experimental.Provider;

var provider = 
    SimpleProvider
        .Create("test")
        .RegisterComponent<TestComponentArgs, TestComponent>("test:index:Test", 
            (request, args) => new TestComponent(request.Name, args))
        .Build();

await provider.Serve(args);
Implementation of TestComponent and TestArgs
using Pulumi;
using Pulumi.Random;

public class TestComponentArgs : ResourceArgs
{
    /// <summary>
    /// The length of the password to generate.
    /// </summary>
    [Input("passwordLength")]
    public int PasswordLength { get; set; }
}

/// <summary>
/// A component resource representing a test component.
/// </summary>
public class TestComponent : ComponentResource
{
    /// <summary>
    /// The generated password.
    /// </summary>
    [Output("passwordResult")]
    public Output<string> PasswordResult { get; private set; }
    public TestComponent(string name, TestComponentArgs args) 
        : base("test:index:Test", name, args)
    {
        var password = new RandomPassword($"{name}-database-password", new ()
        {
            Length = args.PasswordLength
        }, new CustomResourceOptions 
        {
            Parent = this
        });

        PasswordResult = password.Result;
        RegisterOutputs();
    }
}

This project uses a local build of the Pulumi .NET SDK built from branch zaid/authoring-providers-from-components.

It also uses a locally generated .NET SDK of the random provider so we can use its resources as test examples.

Build

Build the MLC using the following command:

cd src && dotnet publish

It will generate the MLC binary in the ./src/bin/Debug/net7.0/publish folder which is then referneced by the YAML program:

plugins:
  providers:
    - name: test
      path: ./bin/Debug/net7.0/publish

This works becaue YAML looks for a binary called pulumi-resource-test and we have set the option

<AssemblyName>pulumi-resource-test</AssemblyName>

Which generates a binary with that assembly name.

Run the Pulumi YAML program

See the YAML program
name: testingdotnetmlc
runtime: yaml

plugins:
  providers:
    - name: test
      path: ./bin/Debug/net7.0/publish

resources:
  testmlc:
    type: test:index:Test
    properties:
      passwordLength: 20

outputs:
  length: ${testmlc.passwordResult}

cd src && pulumi up --yes --stack <your-stack-name>

Get the generated provider schema from the binary

After building the binary using dotnet publish, you can run the following command to get the generated schema:

cd src && pulumi package get-schema bin/Debug/net7.0/publish/pulumi-resource-test
Generated Pulumi Schema
{
  "name": "test",
  "version": "0.1.0",
  "meta": {
    "moduleFormat": "(.*)"
  },
  "config": {},
  "provider": {
    "type": "object"
  },
  "resources": {
    "test:index:Test": {
      "description": "A component resource representing a test component.",
      "properties": {
        "passwordResult": {
          "type": "string",
          "description": "The generated password.",
          "language": {
            "csharp": {
              "name": "PasswordResult"
            }
          }
        }
      },
      "type": "object",
      "required": [
        "passwordResult"
      ],
      "inputProperties": {
        "passwordLength": {
          "type": "integer",
          "description": "The length of the password to generate.",
          "language": {
            "csharp": {
              "name": "PasswordLength"
            }
          }
        }
      },
      "requiredInputs": [
        "passwordLength"
      ],
      "isComponent": true
    }
  }
}

authoring-dotnet-pulumi-mlcs's People

Contributors

zaid-ajaj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.