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

Create pipeline stage to generate list of supported resources #1791

Merged
merged 17 commits into from
Sep 14, 2021
Merged
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
13 changes: 13 additions & 0 deletions hack/crossplane/apis/resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### microsoft.cache

v1alpha1api20200601

- Redis

### microsoft.sql

v1alpha1api20201101preview

- Server
- ServersDatabase

65 changes: 65 additions & 0 deletions hack/generated/apis/resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
### microsoft.batch

v1alpha1api20210101

- BatchAccount

### microsoft.compute

v1alpha1api20200930

- Disk

v1alpha1api20201201

- VirtualMachineScaleSet

### microsoft.containerservice

v1alpha1api20210501

- ManagedCluster
- ManagedClustersAgentPool

### microsoft.documentdb

v1alpha1api20210515

- DatabaseAccount

### microsoft.managedidentity

v1alpha1api20181130

- UserAssignedIdentity

### microsoft.network

v1alpha1api20201101
Copy link
Member

@matthchr matthchr Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider bolding this for emphasis?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the only thing on the line, and it's followed by a bullet list, so I don't think it needs further emphasis.


- LoadBalancer
- NetworkSecurityGroup
- NetworkSecurityGroupsSecurityRule
- PublicIPAddress
- VirtualNetwork
- VirtualNetworkGateway
- VirtualNetworkTap
- VirtualNetworksSubnet
- VirtualNetworksVirtualNetworkPeering

### microsoft.servicebus

v1alpha1api20210101preview

- Namespace
- NamespacesQueue
- NamespacesTopic

### microsoft.storage

v1alpha1api20210401

- StorageAccount
- StorageAccountsBlobService
- StorageAccountsBlobServicesContainer

2 changes: 2 additions & 0 deletions hack/generator/pkg/codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ func createAllPipelineStages(idFactory astmodel.IdentifierFactory, configuration
pipeline.ExportPackages(configuration.FullTypesOutputPath()),

pipeline.ExportControllerResourceRegistrations(configuration.FullTypesRegistrationOutputFilePath()).UsedFor(pipeline.ARMTarget),

pipeline.ReportResourceVersions(configuration),
}
}

Expand Down
8 changes: 6 additions & 2 deletions hack/generator/pkg/codegen/golden_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig
pipeline.ImplementConvertibleSpecInterfaceStageId,
pipeline.ImplementConvertibleStatusInterfaceStageId,
pipeline.ReportOnTypesAndVersionsStageID,
pipeline.AddStatusConditionsStageID)
pipeline.AddStatusConditionsStageID,
pipeline.ReportResourceVersionsStageID)
if !testConfig.HasARMResources {
codegen.RemoveStages(pipeline.CreateARMTypesStageID, pipeline.ApplyARMConversionInterfaceStageID)

Expand All @@ -176,7 +177,10 @@ func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig
codegen.ReplaceStage(pipeline.AddCrossResourceReferencesStageID, addCrossResourceReferencesForTest(idFactory))
}
case config.GenerationPipelineCrossplane:
codegen.RemoveStages(pipeline.DeleteGeneratedCodeStageID, pipeline.CheckForAnyTypeStageID)
codegen.RemoveStages(
pipeline.DeleteGeneratedCodeStageID,
pipeline.CheckForAnyTypeStageID,
pipeline.ReportResourceVersionsStageID)
if !testConfig.HasARMResources {
codegen.ReplaceStage(pipeline.StripUnreferencedTypeDefinitionsStageID, stripUnusedTypesPipelineStage())
}
Expand Down
124 changes: 124 additions & 0 deletions hack/generator/pkg/codegen/pipeline/report_resource_versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package pipeline

import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strings"

"github.com/pkg/errors"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/config"
)

// ReportResourceVersionsStageID is the unique identifier of this stage
const ReportResourceVersionsStageID = "reportResourceVersions"

// ReportResourceVersions creates a pipeline stage that generates a report listing all generated resources.
func ReportResourceVersions(configuration *config.Configuration) Stage {

return MakeStage(
ReportResourceVersionsStageID,
"Generate a report listing all the resources generated",
func(ctx context.Context, state *State) (*State, error) {
report := NewResourceVersionsReport(state.Types())
err := report.WriteTo(configuration.FullTypesOutputPath())
return state, err
})
}

type ResourceVersionsReport struct {
// A separate list of resources for each package
lists map[astmodel.PackageReference][]string
}

func NewResourceVersionsReport(types astmodel.Types) *ResourceVersionsReport {
result := &ResourceVersionsReport{
lists: make(map[astmodel.PackageReference][]string),
}

result.summarize(types)
return result
}

// summarize collates a list of all resources, grouped by package
func (r *ResourceVersionsReport) summarize(types astmodel.Types) {
resources := astmodel.FindResourceTypes(types)
for _, rsrc := range resources {
name := rsrc.Name()
pkg := name.PackageReference
r.lists[pkg] = append(r.lists[pkg], name.Name())
}
}

// WriteTo creates a file containing the generated report
func (r *ResourceVersionsReport) WriteTo(outputPath string) error {
var buffer strings.Builder
r.WriteToBuffer(&buffer)

if _, err := os.Stat(outputPath); os.IsNotExist(err) {
err = os.MkdirAll(outputPath, 0700)
if err != nil {
return errors.Wrapf(err, "Unable to create directory %q", outputPath)
}
}

destination := path.Join(outputPath, "resources.md")
return ioutil.WriteFile(destination, []byte(buffer.String()), 0600)
}

// WriteToBuffer creates the report in the provided buffer
func (r *ResourceVersionsReport) WriteToBuffer(buffer *strings.Builder) {
// Sort packages into increasing order
// Skip storage versions
var packages []astmodel.PackageReference
for pkg := range r.lists {
if !astmodel.IsStoragePackageReference(pkg) {
packages = append(packages, pkg)
}
}

astmodel.SortPackageReferencesByPathAndVersion(packages)

lastService := ""
for _, pkg := range packages {

// Write a header for each service
svc := r.serviceName(pkg)
if lastService != svc {
buffer.WriteString(fmt.Sprintf("### %s\n\n", svc))
lastService = svc
}

// For each version, write an alphabetical list of resources
buffer.WriteString(fmt.Sprintf("%s\n\n", pkg.PackageName()))

resources := r.lists[pkg]
sort.Strings(resources)

for _, rsrc := range resources {
buffer.WriteString(fmt.Sprintf("- %s\n", rsrc))
}

buffer.WriteString("\n")
}
}

func (r *ResourceVersionsReport) serviceName(ref astmodel.PackageReference) string {
pathBits := strings.Split(ref.PackagePath(), "/")
index := len(pathBits) - 1
if index > 0 {
index--
}

return pathBits[index]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package pipeline

import (
"strings"
"testing"

"github.com/sebdah/goldie/v2"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/test"
)

func TestGolden_ReportResourceVersions(t *testing.T) {
g := goldie.New(t)

person2020 := test.CreateResource(
test.Pkg2020,
"Person",
test.CreateSpec(test.Pkg2020, "Person"),
test.CreateStatus(test.Pkg2020, "Person"))

address2020 := test.CreateResource(
test.Pkg2020,
"Address",
test.CreateSpec(test.Pkg2020, "Address"),
test.CreateStatus(test.Pkg2020, "Address"))

person2021 := test.CreateResource(
test.Pkg2021,
"Person",
test.CreateSpec(test.Pkg2021, "Person"),
test.CreateStatus(test.Pkg2021, "Person"))

address2021 := test.CreateResource(
test.Pkg2021,
"Address",
test.CreateSpec(test.Pkg2021, "Address"),
test.CreateStatus(test.Pkg2021, "Address"))

types := make(astmodel.Types)
types.AddAll(person2020, address2020, person2021, address2021)

report := NewResourceVersionsReport(types)

var buffer strings.Builder
report.WriteToBuffer(&buffer)

g.Assert(t, t.Name(), []byte(buffer.String()))
}
3 changes: 2 additions & 1 deletion hack/generator/pkg/codegen/pipeline/report_type_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// ReportOnTypesAndVersionsStageID is the unique identifier of this stage
const ReportOnTypesAndVersionsStageID = "reportTypesAndVersions"

// ReportOnTypesAndVersions creates a pipeline stage that removes any wrapper types prior to actual code generation
// ReportOnTypesAndVersions creates a pipeline stage that generates a report for each group showing a matrix of all
// types and versions
func ReportOnTypesAndVersions(configuration *config.Configuration) Stage {

return MakeLegacyStage(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### microsoft.person

v20200101

- Address
- Person

v20211231

- Address
- Person

Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ ensureArmTypeExistsForEveryType azure Check that an ARM type exis
deleteGenerated Delete generated code from .
exportPackages Export packages to "."
exportControllerResourceRegistrations azure Export resource registrations to ""
reportResourceVersions Generate a report listing all the resources generated
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ markStorageVersion Mark the latest version of ea
rogueCheck Check for rogue definitions using AnyTypes
deleteGenerated Delete generated code from .
exportPackages Export packages to "."
reportResourceVersions Generate a report listing all the resources generated