Skip to content

Commit

Permalink
adding support for auto_dhcp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vibaswan committed Mar 14, 2024
1 parent a5784ec commit 61dc1a7
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 11 deletions.
43 changes: 35 additions & 8 deletions openapiart/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,18 @@ def _validate_x_field_pattern(self, xpattern_path):
str(valid_formats),
)
)
valid_features = ["count", "auto", "auto_dhcp", "metric_tags"]
if "features" in xpattern:
for feature in xpattern["features"]:
if feature not in valid_features:
self._errors.append(
"%s has unspported feature %s , valid features are %s"
% (
str(xpattern_path.full_path),
feature,
str(valid_features),
)
)

def _resolve_x_field_pattern(self):
"""Find all instances of pattern_extension in the openapi content
Expand Down Expand Up @@ -851,32 +863,47 @@ def _generate_value_schema(
if xconstants is not None:
schema["x-constants"] = copy.deepcopy(xconstants)
if "features" in xpattern:
if "auto" in xpattern["features"]:
if (
"auto" in xpattern["features"]
or "auto_dhcp" in xpattern["features"]
):
choice_name = (
"auto" if "auto" in xpattern["features"] else "auto_dhcp"
)
if "default" not in xpattern:
self._errors.append(
"default must be set for property {}, when auto feature is enabled".format(
schema_name
"default must be set for property {}, when {} feature is enabled".format(
schema_name, choice_name
)
)
if choice_name == "auto_dhcp" and xpattern["format"] not in [
"ipv4",
"ipv6",
]:
self._errors.append(
"format must be either ipv4 or ipv6 for property {}, when {} feature is enabled".format(
schema_name, choice_name
)
)
schema["properties"]["choice"]["x-enum"]["auto"] = {
schema["properties"]["choice"]["x-enum"][choice_name] = {
"x-field-uid": 1
}
schema["properties"]["choice"]["default"] = "auto"
schema["properties"]["choice"]["default"] = choice_name
description = [
"The OTG implementation can provide a system generated",
"value for this property. If the OTG is unable to generate a value",
"the default value must be used.",
]
schema["properties"]["auto"] = {
schema["properties"][choice_name] = {
"description": "\n".join(description),
"type": copy.deepcopy(type_name),
"x-field-uid": auto_field.uid,
}
self._apply_common_x_field_pattern_properties(
schema["properties"]["auto"],
schema["properties"][choice_name],
xpattern,
fmt,
property_name="auto",
property_name=choice_name,
)

# skip this UID as it was previously being used for metric_groups
Expand Down
2 changes: 1 addition & 1 deletion openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ def _write_openapi_property(
):
self._write(2, "return self._get_property('%s')" % (name))
self._write()
if name == "auto":
if name == "auto" or name == "auto_dhcp":
return
self._write(1, "@%s.setter" % name)
self._write(1, "def %s(self, value):" % name)
Expand Down
2 changes: 1 addition & 1 deletion openapiart/openapiartgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@ def _build_setters_getters(self, fluent_new):
"Warning: Default should not accept for this property ",
property_name,
)
if field.name.lower() == "auto":
if field.name.lower() == "auto" or field.name.lower() == "autodhcp":
field.setter_method = None

fluent_new.interface_fields.append(field)
Expand Down
4 changes: 3 additions & 1 deletion openapiart/tests/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ components:
choice_required_default:
$ref: "#/components/schemas/ChoiceRequiredAndDefault"
x-field-uid: 57

auto_dhcp_pattern:
$ref: "../pattern/pattern.yaml#/components/schemas/AutoDhcpPattern"
x-field-uid: 58
WObject:
required: [w_name]
properties:
Expand Down
16 changes: 16 additions & 0 deletions openapiart/tests/pattern/invalid-pattern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ components:
default: 0
features: [count]
x-field-uid: 9
wrong_features_value:
x-field-pattern:
format: integer
length: 2
signed: 45
default: 0
features: [abc]
x-field-uid: 10
wrong_format_dhcp:
x-field-pattern:
format: integer
length: 2
signed: 45
default: 0
features: [auto_dhcp]
x-field-uid: 11
10 changes: 10 additions & 0 deletions openapiart/tests/pattern/pattern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ components:
format: oid
default: "0.1"
x-field-uid: 1
AutoDhcpPattern:
description: Test auto dhcp pattern
type: object
properties:
dhcp:
x-field-pattern:
format: ipv4
default: 0.0.0.0
features: [count, auto_dhcp]
x-field-uid: 1
27 changes: 27 additions & 0 deletions openapiart/tests/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,30 @@ def test_auto(config):

config.auto_field_test.auto
assert config.auto_field_test.choice == "auto"


def test_auto_dhcp(config):
try:
config.auto_dhcp_pattern.dhcp.auto_dhcp = "asd"
pytest.fail("able to set the auto field")
except Exception:
pass

assert config.auto_dhcp_pattern.dhcp.auto_dhcp == "0.0.0.0"

dt = config.serialize(config.DICT)

assert (
dt.get("auto_dhcp_pattern", {}).get("dhcp").get("choice")
== "auto_dhcp"
)
assert (
dt.get("auto_dhcp_pattern", {}).get("dhcp").get("auto_dhcp")
== "0.0.0.0"
)

config.auto_dhcp_pattern.dhcp.value = 20
assert config.auto_dhcp_pattern.dhcp.choice == "value"

config.auto_dhcp_pattern.dhcp.auto_dhcp
assert config.auto_dhcp_pattern.dhcp.choice == "auto_dhcp"
3 changes: 3 additions & 0 deletions openapiart/tests/test_validate_x_field_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def test_validate_pattern():
"components.schemas.Config.properties.int_128.x-field-pattern property using x-field-pattern with format integer cannot have length greater than 64",
"signed property can only be used if the format is set to integer in property components.schemas.Config.properties.signed_value_without_int.x-field-pattern",
"invalid value 45 in components.schemas.Config.properties.wrong_int_signed_value.x-field-pattern, signed property can either be true or false",
"components.schemas.Config.properties.wrong_features_value.x-field-pattern has unspported feature abc , valid features are ['count', 'auto', 'auto_dhcp', 'metric_tags']",
"format must be either ipv4 or ipv6 for property Pattern.Config.WrongFormatDhcp, when auto_dhcp feature is enabled",
]
with pytest.raises(Exception) as execinfo:
create_openapi_artifacts(
Expand All @@ -37,3 +39,4 @@ def test_validate_pattern():
error_value = execinfo.value.args[0]
for msg in error_msgs:
assert str_compare(msg, error_value)
print(error_value)
23 changes: 23 additions & 0 deletions pkg/serdes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,26 @@ func TestAuto(t *testing.T) {
openapiart.PatternPrefixConfigAutoFieldTestChoiceEnum("auto"),
config.AutoFieldTest().Choice())
}

func TestAutoDhcp(t *testing.T) {
config := openapiart.NewPrefixConfig()
config.SetA("asdf").SetB(12.2).SetC(1)
config.RequiredObject().SetEA(1).SetEB(2)
assert.Equal(
t,
openapiart.PatternAutoDhcpPatternDhcpChoiceEnum("auto_dhcp"),
config.AutoDhcpPattern().Dhcp().Choice())
assert.Equal(t, "0.0.0.0", config.AutoDhcpPattern().Dhcp().AutoDhcp())

config.AutoDhcpPattern().Dhcp().SetValue("10")
assert.Equal(
t,
openapiart.PatternAutoDhcpPatternDhcpChoiceEnum("value"),
config.AutoDhcpPattern().Dhcp().Choice())

config.AutoDhcpPattern().Dhcp().AutoDhcp()
assert.Equal(
t,
openapiart.PatternAutoDhcpPatternDhcpChoiceEnum("auto_dhcp"),
config.AutoDhcpPattern().Dhcp().Choice())
}

0 comments on commit 61dc1a7

Please sign in to comment.