Git Product home page Git Product logo

cble-provider-grpc's Introduction

cble-provider-grpc

This package is designed to be a communication method for the CBLE server to talk to providers.

Minimal Provider Implementation

package main

import (
  "context"

  cbleGRPC "github.com/cble-platform/cble-provider-grpc/pkg/cble"
  commonGRPC "github.com/cble-platform/cble-provider-grpc/pkg/common"
  providerGRPC "github.com/cble-platform/cble-provider-grpc/pkg/provider"
  "github.com/google/uuid"
  "github.com/sirupsen/logrus"
)

var (
  id      = uuid.New().String()
  name    = "example-provider"
  version = "v0.1"
)

type ExampleProvider struct {
  providerGRPC.DefaultProviderServer
}

func (ExampleProvider) Deploy(ctx context.Context, request *providerGRPC.DeployRequest) (*providerGRPC.DeployReply, error) {
  logrus.Infof("Deploying something with example provider")
  return &providerGRPC.DeployReply{
    DeploymentId:    request.DeploymentId,
    Status:          commonGRPC.RPCStatus_SUCCESS,
    DeploymentState: &structpb.Struct{},
    DeploymentVars:  &structpb.Struct{},
  }, nil
}

func main() {
  conn, err := cbleGRPC.DefaultConnect()
  if err != nil {
    logrus.Fatalf("failed to connect to CBLE gRPC server: %v", err)
  }
  defer conn.Close()

  ctx := context.Background()

  client, err := cbleGRPC.NewClient(ctx, conn)
  if err != nil {
    logrus.Fatalf("failed to connect client: %v", err)
  }

  registerReply, err := client.RegisterProvider(ctx, &cbleGRPC.RegistrationRequest{
    Id:      id,
    Name:    name,
    Version: version,
    Features: map[string]bool{
      string(providerGRPC.ProviderFeature_DEPLOY):  true,
      string(providerGRPC.ProviderFeature_DESTROY): false,
    },
  })
  if err != nil || registerReply.Status == commonGRPC.RPCStatus_FAILURE {
    logrus.Fatalf("registration failed: %v", err)
  } else if registerReply.Status == commonGRPC.RPCStatus_SUCCESS {
    logrus.Printf("Registration success! Starting provider server on port %d", registerReply.Port)
  } else {
    logrus.Fatalf("unknown error occurred: %v", err)
  }

  // Gracefully deregister on provider shutdown
  defer func() {
    // Time to shutdown
    unregisterReply, err := client.UnregisterProvider(ctx, &cbleGRPC.UnregistrationRequest{
      Id:      id,
      Name:    name,
      Version: version,
    })
    if err != nil || unregisterReply.Status == commonGRPC.RPCStatus_FAILURE {
      logrus.Fatalf("unregistration failed: %v", err)
    } else if unregisterReply.Status == commonGRPC.RPCStatus_SUCCESS {
      logrus.Print("Unregistration success! Shutting down...")
    } else {
      logrus.Fatalf("unknown error occurred: %v", err)
    }
  }()

  // Set up the provider gRPC server
  providerOpts := &providerGRPC.ProviderServerOptions{
    TLS:      false,
    CertFile: "",
    KeyFile:  "",
    Port:     int(registerReply.Port),
  }

  // Serve the provider gRPC server (blocking call)
  if err := providerGRPC.Serve(ExampleProvider{}, providerOpts); err != nil {
    logrus.Fatalf("failed to server provider gRPC server: %v", err)
  }

  // Provider is now ready to receive communications from CBLE
  //   (sending SIGINT/SIGTERM will shutdown the server)
}

cble-provider-grpc's People

Contributors

bradhacker avatar

cble-provider-grpc's Issues

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.