Skip to content

Commit

Permalink
Use LRO machinery from runtime instead of arm/runtime (#809)
Browse files Browse the repository at this point in the history
Fixed renaming of AzurePublicCloud to AzurePublic.
Fixed renaming of PageProcessor to PagingHandler.
Update to released version of azcore.
  • Loading branch information
jhendrixMSFT committed May 12, 2022
1 parent fe6d4b8 commit 0b67118
Show file tree
Hide file tree
Showing 211 changed files with 2,006 additions and 2,229 deletions.
2 changes: 1 addition & 1 deletion src/generator/gomod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function generateGoModFile(session: Session<CodeModel>): Promise<st
text += 'go 1.18\n\n';
// here we specify the minimum version of azcore as required by the code generator.
// the version can be overwritten by passing the --azcore-version switch during generation.
const version = await session.getValue('azcore-version', '0.23.0');
const version = await session.getValue('azcore-version', '1.0.0');
if (!version.match(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9_.-]+)?$/)) {
throw new Error(`azcore version ${version} must in the format major.minor.patch[-beta.N]`);
}
Expand Down
56 changes: 23 additions & 33 deletions src/generator/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export async function generateOperations(session: Session<CodeModel>): Promise<O
clientText += '\t}\n';
}
if (azureARM) {
clientText += '\tep := cloud.AzurePublicCloud.Services[cloud.ResourceManager].Endpoint\n'
clientText += '\tep := cloud.AzurePublic.Services[cloud.ResourceManager].Endpoint\n'
clientText += '\tif c, ok := options.Cloud.Services[cloud.ResourceManager]; ok {\n';
clientText += '\t\tep = c.Endpoint\n';
clientText += '\t}\n';
Expand Down Expand Up @@ -332,19 +332,17 @@ export async function generateOperations(session: Session<CodeModel>): Promise<O
let opText = '';
group.operations.sort((a: Operation, b: Operation) => { return sortAscending(a.language.go!.name, b.language.go!.name) });
for (const op of values(group.operations)) {
// TODO: this can be removed and use azureARM once LROs have been unified
const isARM = session.model.language.go!.openApiType === 'arm';
// protocol creation can add imports to the list so
// it must be done before the imports are written out
if (isLROOperation(op)) {
// generate Begin method
opText += generateLROBeginMethod(op, imports, isARM);
opText += generateLROBeginMethod(op, imports);
}
opText += generateOperation(op, imports, isARM);
opText += generateOperation(op, imports);
opText += createProtocolRequest(group, op, imports);
if (!isLROOperation(op) || isPageableOperation(op)) {
// LRO responses are handled elsewhere, with the exception of pageable LROs
opText += createProtocolResponse(op, imports, isARM);
opText += createProtocolResponse(op, imports);
}
}

Expand Down Expand Up @@ -511,7 +509,7 @@ function emitPagerDefinition(op: Operation, imports: ImportManager): string {
const info = <OperationNaming>op.language.go!;
const nextLink = op.language.go!.paging.nextLinkName;
imports.add('context');
let text = `runtime.NewPager(runtime.PageProcessor[${getResponseEnvelopeName(op)}]{\n`;
let text = `runtime.NewPager(runtime.PagingHandler[${getResponseEnvelopeName(op)}]{\n`;
text += `\t\tMore: func(page ${getResponseEnvelopeName(op)}) bool {\n`;
// there is no advancer for single-page pagers
if (op.language.go!.paging.nextLinkName) {
Expand Down Expand Up @@ -584,14 +582,14 @@ function genApiVersionDoc(apiVersions?: ApiVersions): string {
return `// Generated from API version ${versions.join(',')}\n`;
}

function generateOperation(op: Operation, imports: ImportManager, isARM: boolean): string {
function generateOperation(op: Operation, imports: ImportManager): string {
if (op.language.go!.paging && op.language.go!.paging.isNextOp) {
// don't generate a public API for the methods used to advance pages
return '';
}
const info = <OperationNaming>op.language.go!;
const params = getAPIParametersSig(op, imports);
const returns = generateReturnsInfo(op, 'op', isARM);
const returns = generateReturnsInfo(op, 'op');
const clientName = op.language.go!.clientName;
let opName = op.language.go!.name;
if(isPageableOperation(op) && !isLROOperation(op)) {
Expand Down Expand Up @@ -1094,15 +1092,15 @@ function generateResponseUnmarshaller(op: Operation, response: SchemaResponse, u
return unmarshallerText;
}

function createProtocolResponse(op: Operation, imports: ImportManager, isARM: boolean): string {
function createProtocolResponse(op: Operation, imports: ImportManager): string {
if (!needsResponseHandler(op)) {
return '';
}
const info = <OperationNaming>op.language.go!;
const name = info.protocolNaming.responseMethod;
const clientName = op.language.go!.clientName;
let text = `${comment(name, '// ')} handles the ${info.name} response.\n`;
text += `func (client *${clientName}) ${name}(resp *http.Response) (${generateReturnsInfo(op, 'handler', isARM).join(', ')}) {\n`;
text += `func (client *${clientName}) ${name}(resp *http.Response) (${generateReturnsInfo(op, 'handler').join(', ')}) {\n`;
const addHeaders = function (props?: Property[]) {
const headerVals = new Array<Property>();
for (const prop of values(props)) {
Expand Down Expand Up @@ -1261,17 +1259,15 @@ function getAPIParametersSig(op: Operation, imports: ImportManager): string {
// api - for the API definition
// op - for the operation
// handler - for the response handler
function generateReturnsInfo(op: Operation, apiType: 'api' | 'op' | 'handler', isARM: boolean): string[] {
function generateReturnsInfo(op: Operation, apiType: 'api' | 'op' | 'handler'): string[] {
let returnType = getResponseEnvelopeName(op);
if (isLROOperation(op)) {
switch (apiType) {
case 'api':
// this should go away once we can type alias a generic type
const packageName = isARM ? 'armruntime' : 'runtime';
if (isPageableOperation(op)) {
returnType = `*${packageName}.Poller[*runtime.Pager[${getResponseEnvelopeName(op)}]]`;
returnType = `*runtime.Poller[*runtime.Pager[${getResponseEnvelopeName(op)}]]`;
} else {
returnType = `*${packageName}.Poller[${getResponseEnvelopeName(op)}]`;
returnType = `*runtime.Poller[${getResponseEnvelopeName(op)}]`;
}
break;
case 'handler':
Expand All @@ -1298,16 +1294,12 @@ function generateReturnsInfo(op: Operation, apiType: 'api' | 'op' | 'handler', i
return [returnType, 'error'];
}

function generateLROBeginMethod(op: Operation, imports: ImportManager, isARM: boolean): string {
function generateLROBeginMethod(op: Operation, imports: ImportManager): string {
const info = <OperationNaming>op.language.go!;
const params = getAPIParametersSig(op, imports);
const returns = generateReturnsInfo(op, 'api', isARM);
const returns = generateReturnsInfo(op, 'api');
const clientName = op.language.go!.clientName;
if (isARM) {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/runtime', 'armruntime');
} else {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime');
}
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime');
let text = '';
if (hasDescription(op.language.go!)) {
text += `${comment(`Begin${op.language.go!.name} - ${op.language.go!.description}`, "//", undefined, commentLength)}\n`;
Expand Down Expand Up @@ -1341,32 +1333,30 @@ function generateLROBeginMethod(op: Operation, imports: ImportManager, isARM: bo
text += `\t\t\treturn ${zeroResp}, err\n`;
text += `\t\t}\n`;

const packageName = isARM ? 'armruntime' : 'runtime';

let finalStateVia = '';
// LRO operation might have a special configuration set in x-ms-long-running-operation-options
// which indicates a specific url to perform the final Get operation on
if (op.extensions?.['x-ms-long-running-operation-options']?.['final-state-via']) {
finalStateVia = op.extensions?.['x-ms-long-running-operation-options']?.['final-state-via'];
switch (finalStateVia) {
case "azure-async-operation":
finalStateVia = `${packageName}.FinalStateViaAzureAsyncOp`;
finalStateVia = `runtime.FinalStateViaAzureAsyncOp`;
break;
case "location":
finalStateVia = `${packageName}.FinalStateViaLocation`;
finalStateVia = `runtime.FinalStateViaLocation`;
break;
case "original-uri":
finalStateVia = `${packageName}.FinalStateViaOriginalURI`;
finalStateVia = `runtime.FinalStateViaOriginalURI`;
break;
case "operation-location":
finalStateVia = `${packageName}.FinalStateViaOpLocation`;
finalStateVia = `runtime.FinalStateViaOpLocation`;
break;
default:
throw new Error(`unhandled final-state-via value ${finalStateVia}`);
}
}

text += `\t\treturn ${packageName}.NewPoller`;
text += `\t\treturn runtime.NewPoller`;
if (finalStateVia === '' && pollerType === 'nil') {
// the generic type param is redundant when it's also specified in the
// options struct so we only include it when there's no options.
Expand All @@ -1378,7 +1368,7 @@ function generateLROBeginMethod(op: Operation, imports: ImportManager, isARM: bo
text += 'nil)\n';
} else {
// at least one option
text += `&${packageName}.NewPollerOptions${pollerTypeParam}{\n`;
text += `&runtime.NewPollerOptions${pollerTypeParam}{\n`;
if (finalStateVia !== '') {
text += `\t\t\tFinalStateVia: ${finalStateVia},\n`;
}
Expand All @@ -1391,15 +1381,15 @@ function generateLROBeginMethod(op: Operation, imports: ImportManager, isARM: bo

// creating the poller from resume token branch

text += `\t\treturn ${packageName}.NewPollerFromResumeToken`;
text += `\t\treturn runtime.NewPollerFromResumeToken`;
if (pollerType === 'nil') {
text += pollerTypeParam;
}
text += '(options.ResumeToken, client.pl, ';
if (pollerType === 'nil') {
text += 'nil)\n';
} else {
text += `&${packageName}.NewPollerFromResumeTokenOptions${pollerTypeParam}{\n`;
text += `&runtime.NewPollerFromResumeTokenOptions${pollerTypeParam}{\n`;
text += `\t\t\tResponse: ${pollerType},\n`;
text += '\t\t})\n';
}
Expand Down
8 changes: 4 additions & 4 deletions test/autorest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module generatortests
go 1.18

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.23.1
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0
github.com/google/go-cmp v0.5.4
github.com/stretchr/testify v1.7.0
)

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
30 changes: 8 additions & 22 deletions test/autorest/go.sum
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.23.1 h1:3CVsSo4mp8NDWO11tHzN/mdo2zP0CtaSK5IcwBjfqRA=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.23.1/go.mod h1:w5pDIZuawUmY3Bj4tVx3Xb8KS96ToB0j315w9rqpAg0=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.1 h1:sLZ/Y+P/5RRtsXWylBjB5lkgixYfm0MQPiwrSX//JSo=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.9.1/go.mod h1:KLF4gFr6DcKFZwSuH8w8yEK6DpFl3LP5rhdvAb7Yz5I=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0 h1:sVPhtT2qjO86rTUaWMr4WoES4TkjGnzcioXcnHV9s5k=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0/go.mod h1:uGG2W01BaETf0Ozp+QxxKJdMBNRWPdstHG0Fmdwn1/U=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 h1:jp0dGvZ7ZK0mgqnTSClMxa5xuRL7NZgHameVYF6BurY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32D2C7lWs9JRc0bel53A=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA=
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 7 additions & 7 deletions test/autorest/lrogroup/lroretrys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLRORetrysBeginDelete202Retry200(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
result, err := poller.PollUntilDone(context.Background(), time.Second)
result, err := poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
require.Zero(t, result)
}
Expand All @@ -48,7 +48,7 @@ func TestLRORetrysBeginDeleteAsyncRelativeRetrySucceeded(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
_, err = poller.PollUntilDone(context.Background(), time.Second)
_, err = poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
}

Expand All @@ -62,7 +62,7 @@ func TestLRORetrysBeginDeleteProvisioning202Accepted200Succeeded(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
res, err := poller.PollUntilDone(context.Background(), time.Second)
res, err := poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
if r := cmp.Diff(res.Product, Product{
ID: to.Ptr("100"),
Expand All @@ -85,7 +85,7 @@ func TestLRORetrysBeginPost202Retry200(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
_, err = poller.PollUntilDone(context.Background(), time.Second)
_, err = poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
}

Expand All @@ -99,7 +99,7 @@ func TestLRORetrysBeginPostAsyncRelativeRetrySucceeded(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
_, err = poller.PollUntilDone(context.Background(), time.Second)
_, err = poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
}

Expand All @@ -113,7 +113,7 @@ func TestLRORetrysBeginPut201CreatingSucceeded200(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
res, err := poller.PollUntilDone(context.Background(), time.Second)
res, err := poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
if r := cmp.Diff(res.Product, Product{
ID: to.Ptr("100"),
Expand All @@ -136,7 +136,7 @@ func TestLRORetrysBeginPutAsyncRelativeRetrySucceeded(t *testing.T) {
ResumeToken: rt,
})
require.NoError(t, err)
res, err := poller.PollUntilDone(context.Background(), time.Second)
res, err := poller.PollUntilDone(context.Background(), &runtime.PollUntilDoneOptions{Frequency: time.Second})
require.NoError(t, err)
if r := cmp.Diff(res.Product, Product{
ID: to.Ptr("100"),
Expand Down
Loading

0 comments on commit 0b67118

Please sign in to comment.