mirror of
https://github.com/hashicorp/terraform.git
synced 2026-06-09 08:58:34 -04:00
rpcapi: codegen for RPC functions with streaming responses
Our generator for the wrapper stubs for the dynamically-instantiated gRPC services could not previously handle the slightly different signature used for the methods generated for RPC functions that return streaming responses: they take a stream object as an argument instead of returning a message directly. Now we can generate wrapper stubs for those methods too, so that subsequent commits can include the real implementations of those functions. This commit includes just an unimplemented husk of Stacks.PlanStackChanges as a placeholder to demonstrate that it's working.
This commit is contained in:
parent
dd97b81620
commit
97ee2f14c3
4 changed files with 538 additions and 209 deletions
|
|
@ -45,6 +45,7 @@ func main() {
|
|||
// containing the protobuf specification.
|
||||
outDir := filepath.Join(filepath.Dir(pkg.GoFiles[0]), "../dynrpcserver")
|
||||
|
||||
Types:
|
||||
for _, obj := range pkg.TypesInfo.Defs {
|
||||
typ, ok := obj.(*types.TypeName)
|
||||
if !ok {
|
||||
|
|
@ -60,6 +61,22 @@ func main() {
|
|||
continue
|
||||
}
|
||||
|
||||
// The interfaces used for streaming requests/responses unfortunately
|
||||
// also have a "Server" suffix in the generated Go code, and so
|
||||
// we need to detect those more surgically by noticing that they
|
||||
// have grpc.ServerStream embedded inside.
|
||||
for i := 0; i < iface.NumEmbeddeds(); i++ {
|
||||
emb, ok := iface.EmbeddedType(i).(*types.Named)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
pkg := emb.Obj().Pkg().Path()
|
||||
name := emb.Obj().Name()
|
||||
if pkg == "google.golang.org/grpc" && name == "ServerStream" {
|
||||
continue Types
|
||||
}
|
||||
}
|
||||
|
||||
// If we get here then what we're holding _seems_ to be a gRPC
|
||||
// server interface, and so we'll generate a dynamic initialization
|
||||
// wrapper for it.
|
||||
|
|
@ -127,12 +144,24 @@ func main() {
|
|||
if sig.Results().Len() > 1 {
|
||||
buf.WriteString(")")
|
||||
}
|
||||
fmt.Fprintf(&buf, ` {
|
||||
switch n := sig.Results().Len(); n {
|
||||
case 1:
|
||||
fmt.Fprintf(&buf, ` {
|
||||
impl, err := s.realRPCServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
`)
|
||||
case 2:
|
||||
fmt.Fprintf(&buf, ` {
|
||||
impl, err := s.realRPCServer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
`)
|
||||
default:
|
||||
log.Fatalf("don't know how to make a stub for method with %d results", n)
|
||||
}
|
||||
fmt.Fprintf(&buf, "return impl.%s(", method.Name())
|
||||
for i := 0; i < sig.Params().Len(); i++ {
|
||||
if i > 0 {
|
||||
|
|
@ -186,8 +215,12 @@ func typeRef(fullType string) string {
|
|||
switch {
|
||||
case fullType == "context.Context" || fullType == "error":
|
||||
return fullType
|
||||
case fullType == "interface{}" || fullType == "any":
|
||||
return "any"
|
||||
case strings.HasPrefix(fullType, "*"+protobufPkg+"."):
|
||||
return "*tf1." + fullType[len(protobufPkg)+2:]
|
||||
case strings.HasPrefix(fullType, protobufPkg+"."):
|
||||
return "tf1." + fullType[len(protobufPkg)+1:]
|
||||
default:
|
||||
log.Fatalf("don't know what to do with parameter type %s", fullType)
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -50,6 +50,14 @@ func (s *Stacks) OpenStackConfiguration(a0 context.Context, a1 *tf1.OpenStackCon
|
|||
return impl.OpenStackConfiguration(a0, a1)
|
||||
}
|
||||
|
||||
func (s *Stacks) PlanStackChanges(a0 *tf1.PlanStackChanges_Request, a1 tf1.Stacks_PlanStackChangesServer) error {
|
||||
impl, err := s.realRPCServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return impl.PlanStackChanges(a0, a1)
|
||||
}
|
||||
|
||||
func (s *Stacks) ActivateRPCServer(impl tf1.StacksServer) {
|
||||
s.mu.Lock()
|
||||
s.impl = impl
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -84,6 +84,11 @@ service Stacks {
|
|||
// such as the number of instances of each component.
|
||||
rpc FindStackConfigurationComponents(FindStackConfigurationComponents.Request)
|
||||
returns (FindStackConfigurationComponents.Response);
|
||||
// Calculate a desired state from the given configuration and compare it
|
||||
// with the current state to propose a set of changes to converge the
|
||||
// current state with the desired state, at least in part.
|
||||
rpc PlanStackChanges(PlanStackChanges.Request)
|
||||
returns (stream PlanStackChanges.Event);
|
||||
}
|
||||
|
||||
message OpenStackConfiguration {
|
||||
|
|
@ -142,6 +147,18 @@ message FindStackConfigurationProviders {
|
|||
}
|
||||
}
|
||||
|
||||
message PlanStackChanges {
|
||||
message Request {
|
||||
// TODO
|
||||
}
|
||||
message Event {
|
||||
oneof event {
|
||||
Diagnostic diagnostic = 1;
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A source address in the same form as it would appear in a Terraform
|
||||
// configuration: a source string combined with an optional version constraint
|
||||
// string, where the latter is valid only for registry module addresses.
|
||||
|
|
|
|||
Loading…
Reference in a new issue