-
Notifications
You must be signed in to change notification settings - Fork 926
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
Add API types for configuration metadata #14132
Open
markylaing
wants to merge
11
commits into
canonical:main
Choose a base branch
from
markylaing:metadata-api-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+877
−625
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3efef59
api: Add `metadata_configuration_entity_types` API extension.
markylaing 86b70de
lxd-metadata: All configuration keys are strings.
markylaing cfbf1aa
metadata: Runs `make update-metadata`.
markylaing 0194217
shared/api: Add MetadataConfiguration API struct definitions.
markylaing 8dac37f
lxd/auth/generate: Generate entity type JSON conforming to API type.
markylaing 312ab0c
lxd-metadata: Update lxd-metadata to use new entity metadata format.
markylaing 9c0c1da
lxd-metadata: Validate that the generated JSON conforms to the API de…
markylaing 284da9a
lxd/metadata: Run `make update-metadata`.
markylaing b5ea215
lxd: Render api.MetadataConfiguration on /1.0/metadata/configuration.
markylaing 2420033
lxd: Add MetadataConfiguration to swagger doc string.
markylaing d594de4
doc: Runs `make update-api`.
markylaing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ import ( | |
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/canonical/lxd/shared" | ||
"github.com/canonical/lxd/shared/api" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
@@ -43,37 +43,6 @@ type doc struct { | |
Entities json.RawMessage `json:"entities"` | ||
} | ||
|
||
// detectType detects the type of a string and returns the corresponding value. | ||
func detectType(s string) any { | ||
i, err := strconv.Atoi(s) | ||
if err == nil { | ||
return i | ||
} | ||
|
||
b, err := strconv.ParseBool(s) | ||
if err == nil { | ||
return b | ||
} | ||
|
||
f, err := strconv.ParseFloat(s, 64) | ||
if err == nil { | ||
return f | ||
} | ||
|
||
t, err := time.Parse(time.RFC3339, s) | ||
if err == nil { | ||
return t | ||
} | ||
|
||
// special characters handling | ||
if s == "-" { | ||
return "" | ||
} | ||
|
||
// If all conversions fail, it's a string | ||
return s | ||
} | ||
|
||
// sortConfigKeys alphabetically sorts the entries by key (config option key) within each config group in an entity. | ||
func sortConfigKeys(allEntries map[string]map[string]map[string][]any) { | ||
for _, entityValue := range allEntries { | ||
|
@@ -285,7 +254,7 @@ func parse(path string, outputJSONPath string, excludedPaths []string, substitut | |
continue | ||
} | ||
|
||
configKeyEntry[metadataMap["key"]].(map[string]any)[dataKVMatch[1]] = detectType(dataKVMatch[2]) | ||
configKeyEntry[metadataMap["key"]].(map[string]any)[dataKVMatch[1]] = dataKVMatch[2] | ||
} | ||
|
||
// There can be multiple entities for a given group | ||
|
@@ -340,6 +309,13 @@ func parse(path string, outputJSONPath string, excludedPaths []string, substitut | |
return nil, fmt.Errorf("Error while marshaling project documentation: %v", err) | ||
} | ||
|
||
// Validate that what we've generated is valid against our API definition. | ||
var metadataConfiguration api.MetadataConfiguration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice touch |
||
err = json.Unmarshal(data, &metadataConfiguration) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to unmarshal generated metadata into MetadataConfiguration API type: %w", err) | ||
} | ||
|
||
if outputJSONPath != "" { | ||
buf := bytes.NewBufferString("") | ||
_, err = buf.Write(data) | ||
|
@@ -494,7 +470,7 @@ func writeDocFile(inputJSONPath, outputTxtPath string) error { | |
} | ||
} | ||
|
||
entities := make(map[string][]map[string]string) | ||
entities := make(map[string]api.MetadataConfigurationEntity) | ||
err = json.Unmarshal(jsonDoc.Entities, &entities) | ||
if err != nil { | ||
return err | ||
|
@@ -508,11 +484,11 @@ func writeDocFile(inputJSONPath, outputTxtPath string) error { | |
sort.Strings(sortedEntityNames) | ||
|
||
for _, entityName := range sortedEntityNames { | ||
entitlements := entities[entityName] | ||
entity := entities[entityName] | ||
buffer.WriteString(fmt.Sprintf("<!-- entity group %s start -->\n", entityName)) | ||
for _, entitlement := range entitlements { | ||
buffer.WriteString(fmt.Sprintf("`%s`\n", entitlement["name"])) | ||
buffer.WriteString(fmt.Sprintf(": %s\n\n", entitlement["description"])) | ||
for _, entitlement := range entity.Entitlements { | ||
buffer.WriteString(fmt.Sprintf("`%s`\n", entitlement.Name)) | ||
buffer.WriteString(fmt.Sprintf(": %s\n\n", entitlement.Description)) | ||
} | ||
|
||
buffer.WriteString("\n") | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this change.
Where is the value in
dataKVMatch[2]
coming from?As for how these are classified, whilst I agree that all config values are strings, my understanding of the metadata definition here is in order to indicate in the docs what sort of string values are allowed for a particular config key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like
dataKVMatch
is coming from this regular expression(?m)([\S]+):[\s]+([\S \"\']+)
on the key value section of thelxdmeta:generate
section of the doc block e.g.:In this example, the first
dataKVMatch
outputted fromlxdDocDataRegex.FindAllStringSubmatch(data, -1)
would haveMy feeling is that lxd-generate shouldn't be too clever about figuring out which config keys are which type. We should just include that in the description of the key. There is only one config key for which this appears to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit directly after this change shows the result of
make update-metadata
. Only one field is changed: 2614bb5#diff-d3459341f992192c3b18b6130aa7e165d305183794a84f219c7ab7512b1b31bcL4316It is changing the value of
defaultdesc
from a booleantrue
to a string"true"
.