github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/provider/host.go (about) 1 // Copyright 2016-2018, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package provider 16 17 import ( 18 "io/ioutil" 19 "os" 20 "strings" 21 22 "github.com/pulumi/pulumi/sdk/v3/go/common/diag" 23 "github.com/pulumi/pulumi/sdk/v3/go/common/resource" 24 "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" 25 "github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil" 26 lumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go" 27 "golang.org/x/net/context" 28 "google.golang.org/grpc" 29 "google.golang.org/grpc/grpclog" 30 ) 31 32 // HostClient is a client interface into the host's engine RPC interface. 33 type HostClient struct { 34 conn *grpc.ClientConn 35 client lumirpc.EngineClient 36 } 37 38 // NewHostClient dials the target address, connects over gRPC, and returns a client interface. 39 func NewHostClient(addr string) (*HostClient, error) { 40 // Provider client is sensitive to GRPC info logging to stdout, so ensure they are dropped. 41 // See https://github.com/pulumi/pulumi/issues/7156 42 grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, os.Stderr)) 43 conn, err := grpc.Dial( 44 addr, 45 grpc.WithInsecure(), 46 grpc.WithUnaryInterceptor(rpcutil.OpenTracingClientInterceptor()), 47 grpc.WithStreamInterceptor(rpcutil.OpenTracingStreamClientInterceptor()), 48 rpcutil.GrpcChannelOptions(), 49 ) 50 if err != nil { 51 return nil, err 52 } 53 return &HostClient{ 54 conn: conn, 55 client: lumirpc.NewEngineClient(conn), 56 }, nil 57 } 58 59 // Close closes and renders the connection and client unusable. 60 func (host *HostClient) Close() error { 61 return host.conn.Close() 62 } 63 64 // EngineConn provides the engine gRPC client connection. 65 func (host *HostClient) EngineConn() *grpc.ClientConn { 66 return host.conn 67 } 68 69 func (host *HostClient) log( 70 context context.Context, sev diag.Severity, urn resource.URN, msg string, ephemeral bool, 71 ) error { 72 var rpcsev lumirpc.LogSeverity 73 switch sev { 74 case diag.Debug: 75 rpcsev = lumirpc.LogSeverity_DEBUG 76 case diag.Info: 77 rpcsev = lumirpc.LogSeverity_INFO 78 case diag.Warning: 79 rpcsev = lumirpc.LogSeverity_WARNING 80 case diag.Error: 81 rpcsev = lumirpc.LogSeverity_ERROR 82 default: 83 contract.Failf("Unrecognized log severity type: %v", sev) 84 } 85 _, err := host.client.Log(context, &lumirpc.LogRequest{ 86 Severity: rpcsev, 87 Message: strings.ToValidUTF8(msg, "�"), 88 Urn: string(urn), 89 Ephemeral: ephemeral, 90 }) 91 return err 92 } 93 94 // Log logs a global message, including errors and warnings. 95 func (host *HostClient) Log( 96 context context.Context, sev diag.Severity, urn resource.URN, msg string, 97 ) error { 98 return host.log(context, sev, urn, msg, false) 99 } 100 101 // LogStatus logs a global status message, including errors and warnings. Status messages will 102 // appear in the `Info` column of the progress display, but not in the final output. 103 func (host *HostClient) LogStatus( 104 context context.Context, sev diag.Severity, urn resource.URN, msg string, 105 ) error { 106 return host.log(context, sev, urn, msg, true) 107 }