Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ClientFactory params in readme #24180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions eng/tools/generator/cmd/v2/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package common

const (
ChangelogFileName = "CHANGELOG.md"
GoModFileName = "go.mod"
SdkRootPath = "/sdk"
ChangelogFileName = "CHANGELOG.md"
GoModFileName = "go.mod"
SdkRootPath = "/sdk"
ReadmeFileName = "README.md"
ClientFactoryFileName = "client_factory.go"
)
54 changes: 54 additions & 0 deletions eng/tools/generator/cmd/v2/common/fileProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,57 @@ func FindModuleDirByGoMod(root string) (string, error) {
}
return "", fmt.Errorf("module not found, package path:%s", root)
}

func UpdateReadmeClientFactory(path string) error {
readmePath := filepath.Join(path, ReadmeFileName)
readmeFile, err := os.ReadFile(readmePath)
if err != nil {
return err
}
noOptionalFactoryReg := regexp.MustCompile(`NewClientFactory\([^,]+,\s*cred,\s*nil\)`)
withOptionalFactoryReg := regexp.MustCompile(`NewClientFactory\([^,]+,\s*cred,\s*&options\)`)
oldnoOptionalFactory := noOptionalFactoryReg.FindString(string(readmeFile))
oldwithOptionalFactory := withOptionalFactoryReg.FindString(string(readmeFile))
if oldnoOptionalFactory == "" && oldwithOptionalFactory == "" {
return nil
}
clientFactoryFile, err := os.ReadFile(filepath.Join(path, ClientFactoryFileName))
if err != nil {
return err
}
re := regexp.MustCompile(`func\s+NewClientFactory\(([^)]+)\)`)
matches := re.FindStringSubmatch(string(clientFactoryFile))
if len(matches) <= 1 {
return nil
}
var factoryParams []string
params := strings.Split(matches[1], ",")
for param := range params {
params[param] = strings.TrimSpace(params[param])
paramDefinition := strings.Split(params[param], " ")
if len(paramDefinition) != 2 {
continue
}
paramName := paramDefinition[0]
if paramName == "credential" || paramName == "options" {
// fixed params, no need to process
continue
}
if paramName == "subscriptionID" {
// compatible with old version
factoryParams = append(factoryParams, "<subscription ID>")
continue
}
factoryParams = append(factoryParams, fmt.Sprintf("<%s>", paramName))
}
noOptionsParams := append(factoryParams, []string{"cred", "nil"}...)
withOptionsParams := append(factoryParams, []string{"cred", "&options"}...)
newNoOptionalFactory := fmt.Sprintf("NewClientFactory(%s)", strings.Join(noOptionsParams, ", "))
newWithOptionalFactory := fmt.Sprintf("NewClientFactory(%s)", strings.Join(withOptionsParams, ", "))
if oldnoOptionalFactory == newNoOptionalFactory && oldwithOptionalFactory == newWithOptionalFactory {
return nil
}
content := strings.ReplaceAll(string(readmeFile), oldnoOptionalFactory, newNoOptionalFactory)
content = strings.ReplaceAll(content, oldwithOptionalFactory, newWithOptionalFactory)
return os.WriteFile(readmePath, []byte(content), 0644)
}
18 changes: 18 additions & 0 deletions eng/tools/generator/cmd/v2/common/fileProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,21 @@ func TestFindModule(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "sdk/security/keyvault/azadmin", filepath.ToSlash(moduleRelativePath))
}

func TestUpdateReadMeClientFactory(t *testing.T) {
cwd, err := os.Getwd()
assert.NoError(t, err)
sdkRoot := utils.NormalizePath(cwd)
sdkRepo, err := repo.OpenSDKRepository(sdkRoot)
assert.NoError(t, err)

// without subscription
packagePath := fmt.Sprintf("%s/%s", filepath.ToSlash(sdkRepo.Root()), "sdk/resourcemanager/managementgroups/armmanagementgroups")
err = UpdateReadmeClientFactory(packagePath)
assert.NoError(t, err)

// with subscription
packagePath = fmt.Sprintf("%s/%s", filepath.ToSlash(sdkRepo.Root()), "sdk/resourcemanager/containerservice/armcontainerservice")
err = UpdateReadmeClientFactory(packagePath)
assert.NoError(t, err)
}
21 changes: 21 additions & 0 deletions eng/tools/generator/cmd/v2/common/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ func (t *SwaggerCommonGenerator) GenChangeLog(oriExports *exports.Content, newEx
}

func (t *SwaggerCommonGenerator) AfterGenerate(generateParam *GenerateParam, changelog *Changelog, newExports exports.Content) (*GenerateResult, error) {
log.Printf("Update README.md ClientFactory...")
err := UpdateReadmeClientFactory(t.PackagePath)
if err != nil {
// only log error, avoid breaking the process
log.Printf("Update README.md ClientFactory failed! err: %v", err)
}
return nil, nil
}

Expand Down Expand Up @@ -311,6 +317,10 @@ func (t *SwaggerOnBoardGenerator) AfterGenerate(generateParam *GenerateParam, ch
}
}

if _, err := t.SwaggerCommonGenerator.AfterGenerate(generateParam, changelog, newExports); err != nil {
return nil, err
}

// issue: https://github.com/Azure/azure-sdk-for-go/issues/23877
prl = FirstBetaLabel

Expand Down Expand Up @@ -475,6 +485,10 @@ func (t *SwaggerUpdateGenerator) AfterGenerate(generateParam *GenerateParam, cha
}
}

if _, err := t.SwaggerCommonGenerator.AfterGenerate(generateParam, changelog, newExports); err != nil {
return nil, err
}

return &GenerateResult{
Version: version.String(),
RPName: generateParam.RPName,
Expand Down Expand Up @@ -648,6 +662,13 @@ func (t *TypeSpecCommonGenerator) AfterGenerate(generateParam *GenerateParam, ch
if err := ExecuteGo(modulePath, "mod", "tidy"); err != nil {
return nil, err
}

log.Printf("Update README.md ClientFactory...")
err := UpdateReadmeClientFactory(t.PackagePath)
if err != nil {
// only log error, avoid breaking the process
log.Printf("Failed to run common AfterGenerate: %v", err)
}
return nil, nil
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/resourcemanager/quota/armquota/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For more information on authentication, please see the documentation for `aziden
Azure Quota module consists of one or more clients. We provide a client factory which could be used to create any client in this module.

```go
clientFactory, err := armquota.NewClientFactory(<subscription ID>, cred, nil)
clientFactory, err := armquota.NewClientFactory(cred, nil)
```

You can use `ClientOptions` in package `github.com/Azure/azure-sdk-for-go/sdk/azcore/arm` to set endpoint to connect with public and sovereign clouds as well as Azure Stack. For more information, please see the documentation for `azcore` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore).
Expand All @@ -47,7 +47,7 @@ options := arm.ClientOptions {
Cloud: cloud.AzureChina,
},
}
clientFactory, err := armquota.NewClientFactory(<subscription ID>, cred, &options)
clientFactory, err := armquota.NewClientFactory(cred, &options)
```

## Clients
Expand Down
Loading