Skip to content

Commit

Permalink
[minor_change] Added generator function to re-fetch latest metadata a…
Browse files Browse the repository at this point in the history
…nd generate.

- Added a GitHub workflow that runs the code regeneration weekly.
  • Loading branch information
samiib committed Oct 28, 2024
1 parent b557ed1 commit 46f6279
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/update-metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update Metadata

on:
schedule:
- cron: '0 9 * * MON' # Every Monday at 9AM UTC
workflow_dispatch:

jobs:
update-metadata:
runs-on: ubuntu-latest
steps:
# Checkout
- name: Checkout
uses: actions/checkout@v4
- name: Unshallow
run: git fetch --prune --unshallow

# Get latest metadata
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Get latest metadata and generate provider code
run: go generate
env:
RE_GEN_CLASSES: '1'
GEN_ANNOTATION_UNSUPPORTED: '1'

- name: Check generated code for diffs
run: git diff --exit-code
2 changes: 2 additions & 0 deletions gen/definitions/properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ pkiTP:
parent_dn: "uni/userext/pkiext"
overwrites:
cert_chain: "certificate_chain"
ignores:
- "certUsage"
resource_required:
- "certChain"
test_values:
Expand Down
57 changes: 35 additions & 22 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,19 @@ func getDefinitions() Definitions {

// Remove all files in a directory except when the files that do not match the ignore list
func cleanDirectory(dir string, ignores []string) {
names := getFileNames(dir)
for _, name := range names {
if !slices.Contains(ignores, name) {
err := os.RemoveAll(filepath.Join(dir, name))
if err != nil {
panic(err)
}
}
}
}

// Returns all the files names in a directory
func getFileNames(dir string) []string {
d, err := os.Open(dir)
if err != nil {
panic(err)
Expand All @@ -686,14 +699,7 @@ func cleanDirectory(dir string, ignores []string) {
if err != nil {
panic(err)
}
for _, name := range names {
if !slices.Contains(ignores, name) {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
panic(err)
}
}
}
return names
}

// One time function used to migrate the legacy documentation to the new documentation structure
Expand All @@ -706,15 +712,7 @@ func migrateLegacyDocumentation() {
if dirPath == "./legacy-docs/docs/r" {
newDirPath = resourcesDocsPath
}
d, err := os.Open(dirPath)
if err != nil {
panic(err)
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
panic(err)
}
names := getFileNames(dirPath)
for _, name := range names {
newName := strings.Replace(name, ".html.markdown", ".md", 1)
source, err := os.Open(filepath.Join(dirPath, name))
Expand Down Expand Up @@ -764,16 +762,30 @@ func getExampleCode(filePath string) []byte {
return content
}

// When GEN_CLASSES environment variable is set, the class metadata is retrieved from the APIC or devnet docs and stored in the meta directory.
func getClassMetadata() {
// When RE_GEN_CLASSES environment variable is set, the existing class metadata is retrieved from APIC or the latest devnet docs and stored in the meta directory.
func reGenClassMetadata() {
_, re_gen_classes := os.LookupEnv("RE_GEN_CLASSES")
if re_gen_classes {
names := getFileNames(metaPath)
classNames := strings.Join(names, ",")
getClassMetadata(classNames)
}
}

classNames := os.Getenv("GEN_CLASSES")
// When GEN_CLASSES environment variable is set, the class metadata is retrieved from the APIC or the latest devnet docs and stored in the meta directory.
func getClassMetadata(classNames string) {

if classNames == "" {
classNames = os.Getenv("GEN_CLASSES")
}

if classNames != "" {
var name, nameSpace, url string
classNameList := strings.Split(classNames, ",")
for _, className := range classNameList {

if strings.HasSuffix(className, ".json") {
className = strings.Replace(className, ".json", "", 1)
}
for index, character := range className {
if unicode.IsUpper(character) {
nameSpace = className[:index]
Expand Down Expand Up @@ -849,7 +861,8 @@ func genAnnotationUnsupported() []string {
}

func main() {
getClassMetadata()
reGenClassMetadata()
getClassMetadata("")
cleanDirectories()

definitions := getDefinitions()
Expand Down

0 comments on commit 46f6279

Please sign in to comment.