Skip to content

Commit

Permalink
codegen fixes for invokes
Browse files Browse the repository at this point in the history
  • Loading branch information
EronWright committed Jan 30, 2025
1 parent b22c722 commit 0704737
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion provider/pkg/gen/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (m *moduleGenerator) genProperties(resolvedSchema *openapi.Schema, variants
if variants.isTopLevel && variants.isOutput {
// Emit the actual apiVersion of the resource as a output property.
result.specs["apiVersion"] = pschema.PropertySpec{
Description: "The API version of the resource or function.",
Description: "The API version of the resource.",
TypeSpec: pschema.TypeSpec{
Type: "string",
},
Expand Down
22 changes: 12 additions & 10 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ func (g *packageGenerator) genResourceVariant(apiSpec *openapi.ResourceSpec, res
updateMethod = "PATCH"
}

resourceResponse, err := gen.genResponse(updateOp.Responses.StatusCodeResponses, swagger.ReferenceContext, resource.response)
resourceResponse, err := gen.genResponse(updateOp.Responses.StatusCodeResponses, swagger.ReferenceContext, resource.response, true)
if err != nil {
return errors.Wrapf(err, "failed to generate '%s': response type", resourceTok)
}
Expand All @@ -895,7 +895,7 @@ func (g *packageGenerator) genResourceVariant(apiSpec *openapi.ResourceSpec, res
parameters := resource.Swagger.MergeParameters(updateOp.Parameters, path.Parameters)
autoNamer := resources.NewAutoNamer(resource.Path)

resourceRequest, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, &autoNamer, resource.body)
resourceRequest, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, &autoNamer, resource.body, true)
if err != nil {
return errors.Wrapf(err, "failed to generate '%s': request type", resourceTok)
}
Expand Down Expand Up @@ -952,11 +952,11 @@ func (g *packageGenerator) genResourceVariant(apiSpec *openapi.ResourceSpec, res
}

parameters = swagger.MergeParameters(readOp.Parameters, path.Parameters)
requestFunction, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, nil, resource.body)
requestFunction, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, nil, resource.body, true)
if err != nil {
return errors.Wrapf(err, "failed to generate '%s': request type", functionTok)
}
responseFunction, err := gen.genResponse(readOp.Responses.StatusCodeResponses, swagger.ReferenceContext, resource.response)
responseFunction, err := gen.genResponse(readOp.Responses.StatusCodeResponses, swagger.ReferenceContext, resource.response, true)
if err != nil {
return errors.Wrapf(err, "failed to generate '%s': response type", functionTok)
}
Expand Down Expand Up @@ -985,6 +985,7 @@ func (g *packageGenerator) genResourceVariant(apiSpec *openapi.ResourceSpec, res
Path: resource.Path,
GetParameters: requestFunction.parameters,
Response: responseFunction.properties,
GetResource: true,
}
g.metadata.Invokes[functionTok] = f
}
Expand Down Expand Up @@ -1157,12 +1158,12 @@ func (g *packageGenerator) genFunctions(typeName, path string, specParams []spec
}

parameters := swagger.MergeParameters(operation.Parameters, specParams)
request, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, nil, nil)
request, err := gen.genMethodParameters(parameters, swagger.ReferenceContext, nil, nil, false)
if err != nil {
log.Printf("failed to generate '%s': request type: %s", functionTok, err.Error())
return
}
response, err := gen.genResponse(operation.Responses.StatusCodeResponses, swagger.ReferenceContext, nil)
response, err := gen.genResponse(operation.Responses.StatusCodeResponses, swagger.ReferenceContext, nil, false)
if err != nil {
log.Printf("failed to generate '%s': response type: %s", functionTok, err.Error())
return
Expand Down Expand Up @@ -1195,6 +1196,7 @@ func (g *packageGenerator) genFunctions(typeName, path string, specParams []spec
Path: path,
PostParameters: request.parameters,
Response: response.properties,
GetResource: false,
}
g.metadata.Invokes[functionTok] = f
}
Expand Down Expand Up @@ -1446,7 +1448,7 @@ func normalizeParamPattern(param *openapi.Parameter) string {
}

func (m *moduleGenerator) genMethodParameters(parameters []spec.Parameter, ctx *openapi.ReferenceContext,
namer *resources.AutoNamer, bodySchema *openapi.Schema) (*parameterBag, error) {
namer *resources.AutoNamer, bodySchema *openapi.Schema, isTopLevel bool) (*parameterBag, error) {
result := newParameterBag()
var autoNamedSpec string

Expand Down Expand Up @@ -1493,7 +1495,7 @@ func (m *moduleGenerator) genMethodParameters(parameters []spec.Parameter, ctx *

// The body parameter is flattened, so that all its properties become the properties of the type.
props, err := m.genProperties(bodySchema, genPropertiesVariant{
isTopLevel: true,
isTopLevel: isTopLevel,
isOutput: false,
isType: false,
isResponse: false,
Expand Down Expand Up @@ -1574,7 +1576,7 @@ func isMethodParameter(param *openapi.Parameter) bool {
}

func (m *moduleGenerator) genResponse(statusCodeResponses map[int]spec.Response, ctx *openapi.ReferenceContext,
responseSchema *openapi.Schema) (*propertyBag, error) {
responseSchema *openapi.Schema, isTopLevel bool) (*propertyBag, error) {

if responseSchema == nil {
v, err := getResponseSchema(ctx, statusCodeResponses)
Expand All @@ -1590,7 +1592,7 @@ func (m *moduleGenerator) genResponse(statusCodeResponses map[int]spec.Response,
}

result, err := m.genProperties(responseSchema, genPropertiesVariant{
isTopLevel: true,
isTopLevel: isTopLevel,
isOutput: true,
isType: false,
isResponse: true,
Expand Down
15 changes: 13 additions & 2 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,22 @@ func tokenRequestOpts(endpoint string) policy.TokenRequestOptions {
}

func (k *azureNativeProvider) invokeResponseToOutputs(response any, res resources.AzureAPIInvoke) map[string]any {
var outputs map[string]any
if responseMap, ok := response.(map[string]any); ok {
// Map the raw response to the shape of outputs that the SDKs expect.
return k.converter.ResponseBodyToSdkOutputs(res.Response, responseMap)
outputs = k.converter.ResponseBodyToSdkOutputs(res.Response, responseMap)
} else {
outputs = map[string]any{resources.SingleValueProperty: response}
}
return map[string]any{resources.SingleValueProperty: response}

if res.GetResource {
if version.GetVersion().Major >= 3 {
// resources have an apiVersion output property.
outputs["apiVersion"] = res.APIVersion
}
}

return outputs
}

// StreamInvoke dynamically executes a built-in function in the provider. The result is streamed
Expand Down
1 change: 1 addition & 0 deletions provider/pkg/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ type AzureAPIExample struct {
type AzureAPIInvoke struct {
APIVersion string `json:"apiVersion"`
Path string `json:"path"`
GetResource bool `json:"getResource"`
GetParameters []AzureAPIParameter `json:"GET"`
PostParameters []AzureAPIParameter `json:"POST"`
Response map[string]AzureAPIProperty `json:"response"`
Expand Down

0 comments on commit 0704737

Please sign in to comment.