diff --git a/provider/pkg/gen/properties.go b/provider/pkg/gen/properties.go index 93d6a10d1052..6cb4c5a912cb 100644 --- a/provider/pkg/gen/properties.go +++ b/provider/pkg/gen/properties.go @@ -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", }, diff --git a/provider/pkg/gen/schema.go b/provider/pkg/gen/schema.go index 8fa95819812c..4cc5ca451a2e 100644 --- a/provider/pkg/gen/schema.go +++ b/provider/pkg/gen/schema.go @@ -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) } @@ -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) } @@ -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) } @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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, @@ -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) @@ -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, diff --git a/provider/pkg/provider/provider.go b/provider/pkg/provider/provider.go index 29f34cf069dd..3b9f7ec23986 100644 --- a/provider/pkg/provider/provider.go +++ b/provider/pkg/provider/provider.go @@ -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 diff --git a/provider/pkg/resources/resources.go b/provider/pkg/resources/resources.go index 632e7e15c5c2..677062f6fc63 100644 --- a/provider/pkg/resources/resources.go +++ b/provider/pkg/resources/resources.go @@ -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"`