Skip to content

Commit

Permalink
feat: added support for IP masks in ligato proto model annotations an…
Browse files Browse the repository at this point in the history
…d implemented proper json schema export according to new annotation possibilities

Signed-off-by: Filip Gschwandtner <filip.gschwandtner@pantheon.tech>
  • Loading branch information
fgschwan committed Mar 4, 2021
1 parent d04592e commit c83c55b
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 271 deletions.
65 changes: 65 additions & 0 deletions plugins/restapi/jsonschema/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/xeipuuv/gojsonschema"
)

const (
PatternIpv6WithMask = "^(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2}))(\\\\/(12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))$"
PatternIpv4WithMask = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(/(3[0-2]|[1-2][0-9]|[0-9]))$"
)

var (
globalPkg = newProtoPackage(nil, "")

Expand Down Expand Up @@ -101,6 +106,7 @@ func (c *Converter) applyAllowNullValuesOption(schema *jsonschema.Type, schemaCh
}, {
Type: schemaChanges.Type,
Format: schemaChanges.Format,
Pattern: schemaChanges.Pattern,
Minimum: schemaChanges.Minimum,
ExclusiveMinimum: schemaChanges.ExclusiveMinimum,
Maximum: schemaChanges.Maximum,
Expand All @@ -115,6 +121,7 @@ func (c *Converter) applyAllowNullValuesOption(schema *jsonschema.Type, schemaCh
} else { // direct mapping (schema could be already partially built -> need to fill new values into it)
schema.Type = schemaChanges.Type
schema.Format = schemaChanges.Format
schema.Pattern = schemaChanges.Pattern
schema.Minimum = schemaChanges.Minimum
schema.ExclusiveMinimum = schemaChanges.ExclusiveMinimum
schema.Maximum = schemaChanges.Maximum
Expand Down Expand Up @@ -224,6 +231,64 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
Format: "ipv6",
},
}
case ligato.LigatoOptions_IPV4_WITH_MASK:
schema.Type = gojsonschema.TYPE_STRING
schema.Pattern = PatternIpv4WithMask
case ligato.LigatoOptions_IPV6_WITH_MASK:
schema.Type = gojsonschema.TYPE_STRING
schema.Pattern = PatternIpv6WithMask
case ligato.LigatoOptions_IP_WITH_MASK:
schema.OneOf = []*jsonschema.Type{
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv4WithMask,
},
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv6WithMask,
},
}
case ligato.LigatoOptions_IPV4_OPTIONAL_MASK:
schema.OneOf = []*jsonschema.Type{
{
Type: gojsonschema.TYPE_STRING,
Format: "ipv4",
},
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv4WithMask,
},
}
case ligato.LigatoOptions_IPV6_OPTIONAL_MASK:
schema.OneOf = []*jsonschema.Type{
{
Type: gojsonschema.TYPE_STRING,
Format: "ipv6",
},
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv6WithMask,
},
}
case ligato.LigatoOptions_IP_OPTIONAL_MASK:
schema.OneOf = []*jsonschema.Type{
{
Type: gojsonschema.TYPE_STRING,
Format: "ipv4",
},
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv4WithMask,
},
{
Type: gojsonschema.TYPE_STRING,
Format: "ipv6",
},
{
Type: gojsonschema.TYPE_STRING,
Pattern: PatternIpv6WithMask,
},
}
default: // no annotations or annotation used are not applicable here
schema.Type = gojsonschema.TYPE_STRING
}
Expand Down
70 changes: 48 additions & 22 deletions proto/ligato/annotations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proto/ligato/annotations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ message LigatoOptions {
IP = 1;
IPV4 = 2;
IPV6 = 3;
IP_WITH_MASK = 4;
IPV4_WITH_MASK = 5;
IPV6_WITH_MASK = 6;
IP_OPTIONAL_MASK = 7;
IPV4_OPTIONAL_MASK = 8;
IPV6_OPTIONAL_MASK = 9;
}
Type type = 1;

Expand Down
Loading

0 comments on commit c83c55b

Please sign in to comment.