From b77cd93ebe4d60dfcc6042cad63ec1b84c106a57 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Tue, 6 Feb 2024 08:43:44 +0100 Subject: [PATCH] API adoption --- api/models/v1_firewall_allocate_request.go | 120 ++++---------------- api/models/v1_firewall_create_request.go | 121 ++++----------------- api/models/v1_firewall_rules.go | 17 +-- metal-api.json | 42 ++----- 4 files changed, 63 insertions(+), 237 deletions(-) diff --git a/api/models/v1_firewall_allocate_request.go b/api/models/v1_firewall_allocate_request.go index ba9d8d0..76e5c24 100644 --- a/api/models/v1_firewall_allocate_request.go +++ b/api/models/v1_firewall_allocate_request.go @@ -7,7 +7,6 @@ package models import ( "context" - "strconv" "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" @@ -19,22 +18,15 @@ import ( // swagger:model v1.FirewallAllocateRequest type V1FirewallAllocateRequest struct { - // list of egress rules to be deployed during firewall allocation - Egress []*V1FirewallEgressRule `json:"egress" yaml:"egress"` - - // list of ingress rules to be deployed during firewall allocation - Ingress []*V1FirewallIngressRule `json:"ingress" yaml:"ingress"` + // optional egress and ingress firewall rules to deploy during firewall allocation + FirewallRules *V1FirewallRules `json:"firewall_rules,omitempty" yaml:"firewall_rules,omitempty"` } // Validate validates this v1 firewall allocate request func (m *V1FirewallAllocateRequest) Validate(formats strfmt.Registry) error { var res []error - if err := m.validateEgress(formats); err != nil { - res = append(res, err) - } - - if err := m.validateIngress(formats); err != nil { + if err := m.validateFirewallRules(formats); err != nil { res = append(res, err) } @@ -44,53 +36,20 @@ func (m *V1FirewallAllocateRequest) Validate(formats strfmt.Registry) error { return nil } -func (m *V1FirewallAllocateRequest) validateEgress(formats strfmt.Registry) error { - if swag.IsZero(m.Egress) { // not required +func (m *V1FirewallAllocateRequest) validateFirewallRules(formats strfmt.Registry) error { + if swag.IsZero(m.FirewallRules) { // not required return nil } - for i := 0; i < len(m.Egress); i++ { - if swag.IsZero(m.Egress[i]) { // not required - continue - } - - if m.Egress[i] != nil { - if err := m.Egress[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("egress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("egress" + "." + strconv.Itoa(i)) - } - return err - } - } - - } - - return nil -} - -func (m *V1FirewallAllocateRequest) validateIngress(formats strfmt.Registry) error { - if swag.IsZero(m.Ingress) { // not required - return nil - } - - for i := 0; i < len(m.Ingress); i++ { - if swag.IsZero(m.Ingress[i]) { // not required - continue - } - - if m.Ingress[i] != nil { - if err := m.Ingress[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("ingress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("ingress" + "." + strconv.Itoa(i)) - } - return err + if m.FirewallRules != nil { + if err := m.FirewallRules.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("firewall_rules") + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("firewall_rules") } + return err } - } return nil @@ -100,11 +59,7 @@ func (m *V1FirewallAllocateRequest) validateIngress(formats strfmt.Registry) err func (m *V1FirewallAllocateRequest) ContextValidate(ctx context.Context, formats strfmt.Registry) error { var res []error - if err := m.contextValidateEgress(ctx, formats); err != nil { - res = append(res, err) - } - - if err := m.contextValidateIngress(ctx, formats); err != nil { + if err := m.contextValidateFirewallRules(ctx, formats); err != nil { res = append(res, err) } @@ -114,51 +69,22 @@ func (m *V1FirewallAllocateRequest) ContextValidate(ctx context.Context, formats return nil } -func (m *V1FirewallAllocateRequest) contextValidateEgress(ctx context.Context, formats strfmt.Registry) error { - - for i := 0; i < len(m.Egress); i++ { +func (m *V1FirewallAllocateRequest) contextValidateFirewallRules(ctx context.Context, formats strfmt.Registry) error { - if m.Egress[i] != nil { + if m.FirewallRules != nil { - if swag.IsZero(m.Egress[i]) { // not required - return nil - } - - if err := m.Egress[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("egress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("egress" + "." + strconv.Itoa(i)) - } - return err - } + if swag.IsZero(m.FirewallRules) { // not required + return nil } - } - - return nil -} - -func (m *V1FirewallAllocateRequest) contextValidateIngress(ctx context.Context, formats strfmt.Registry) error { - - for i := 0; i < len(m.Ingress); i++ { - - if m.Ingress[i] != nil { - - if swag.IsZero(m.Ingress[i]) { // not required - return nil - } - - if err := m.Ingress[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("ingress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("ingress" + "." + strconv.Itoa(i)) - } - return err + if err := m.FirewallRules.ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("firewall_rules") + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("firewall_rules") } + return err } - } return nil diff --git a/api/models/v1_firewall_create_request.go b/api/models/v1_firewall_create_request.go index 27f6bb1..fdb8599 100644 --- a/api/models/v1_firewall_create_request.go +++ b/api/models/v1_firewall_create_request.go @@ -23,12 +23,12 @@ type V1FirewallCreateRequest struct { // a description for this entity Description string `json:"description,omitempty" yaml:"description,omitempty"` - // list of egress rules to be deployed during firewall allocation - Egress []*V1FirewallEgressRule `json:"egress" yaml:"egress"` - // the filesystemlayout id to assing to this machine Filesystemlayoutid string `json:"filesystemlayoutid,omitempty" yaml:"filesystemlayoutid,omitempty"` + // optional egress and ingress firewall rules to deploy during firewall allocation + FirewallRules *V1FirewallRules `json:"firewall_rules,omitempty" yaml:"firewall_rules,omitempty"` + // the hostname for the allocated machine (defaults to metal) Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty"` @@ -36,9 +36,6 @@ type V1FirewallCreateRequest struct { // Required: true Imageid *string `json:"imageid" yaml:"imageid"` - // list of ingress rules to be deployed during firewall allocation - Ingress []*V1FirewallIngressRule `json:"ingress" yaml:"ingress"` - // the ips to attach to this machine additionally Ips []string `json:"ips" yaml:"ips"` @@ -81,7 +78,7 @@ type V1FirewallCreateRequest struct { func (m *V1FirewallCreateRequest) Validate(formats strfmt.Registry) error { var res []error - if err := m.validateEgress(formats); err != nil { + if err := m.validateFirewallRules(formats); err != nil { res = append(res, err) } @@ -89,10 +86,6 @@ func (m *V1FirewallCreateRequest) Validate(formats strfmt.Registry) error { res = append(res, err) } - if err := m.validateIngress(formats); err != nil { - res = append(res, err) - } - if err := m.validateNetworks(formats); err != nil { res = append(res, err) } @@ -119,27 +112,20 @@ func (m *V1FirewallCreateRequest) Validate(formats strfmt.Registry) error { return nil } -func (m *V1FirewallCreateRequest) validateEgress(formats strfmt.Registry) error { - if swag.IsZero(m.Egress) { // not required +func (m *V1FirewallCreateRequest) validateFirewallRules(formats strfmt.Registry) error { + if swag.IsZero(m.FirewallRules) { // not required return nil } - for i := 0; i < len(m.Egress); i++ { - if swag.IsZero(m.Egress[i]) { // not required - continue - } - - if m.Egress[i] != nil { - if err := m.Egress[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("egress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("egress" + "." + strconv.Itoa(i)) - } - return err + if m.FirewallRules != nil { + if err := m.FirewallRules.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("firewall_rules") + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("firewall_rules") } + return err } - } return nil @@ -154,32 +140,6 @@ func (m *V1FirewallCreateRequest) validateImageid(formats strfmt.Registry) error return nil } -func (m *V1FirewallCreateRequest) validateIngress(formats strfmt.Registry) error { - if swag.IsZero(m.Ingress) { // not required - return nil - } - - for i := 0; i < len(m.Ingress); i++ { - if swag.IsZero(m.Ingress[i]) { // not required - continue - } - - if m.Ingress[i] != nil { - if err := m.Ingress[i].Validate(formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("ingress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("ingress" + "." + strconv.Itoa(i)) - } - return err - } - } - - } - - return nil -} - func (m *V1FirewallCreateRequest) validateNetworks(formats strfmt.Registry) error { if swag.IsZero(m.Networks) { // not required return nil @@ -246,11 +206,7 @@ func (m *V1FirewallCreateRequest) validateSSHPubKeys(formats strfmt.Registry) er func (m *V1FirewallCreateRequest) ContextValidate(ctx context.Context, formats strfmt.Registry) error { var res []error - if err := m.contextValidateEgress(ctx, formats); err != nil { - res = append(res, err) - } - - if err := m.contextValidateIngress(ctx, formats); err != nil { + if err := m.contextValidateFirewallRules(ctx, formats); err != nil { res = append(res, err) } @@ -264,51 +220,22 @@ func (m *V1FirewallCreateRequest) ContextValidate(ctx context.Context, formats s return nil } -func (m *V1FirewallCreateRequest) contextValidateEgress(ctx context.Context, formats strfmt.Registry) error { - - for i := 0; i < len(m.Egress); i++ { +func (m *V1FirewallCreateRequest) contextValidateFirewallRules(ctx context.Context, formats strfmt.Registry) error { - if m.Egress[i] != nil { + if m.FirewallRules != nil { - if swag.IsZero(m.Egress[i]) { // not required - return nil - } - - if err := m.Egress[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("egress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("egress" + "." + strconv.Itoa(i)) - } - return err - } + if swag.IsZero(m.FirewallRules) { // not required + return nil } - } - - return nil -} - -func (m *V1FirewallCreateRequest) contextValidateIngress(ctx context.Context, formats strfmt.Registry) error { - - for i := 0; i < len(m.Ingress); i++ { - - if m.Ingress[i] != nil { - - if swag.IsZero(m.Ingress[i]) { // not required - return nil - } - - if err := m.Ingress[i].ContextValidate(ctx, formats); err != nil { - if ve, ok := err.(*errors.Validation); ok { - return ve.ValidateName("ingress" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("ingress" + "." + strconv.Itoa(i)) - } - return err + if err := m.FirewallRules.ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("firewall_rules") + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName("firewall_rules") } + return err } - } return nil diff --git a/api/models/v1_firewall_rules.go b/api/models/v1_firewall_rules.go index 9ba5822..42f5078 100644 --- a/api/models/v1_firewall_rules.go +++ b/api/models/v1_firewall_rules.go @@ -12,7 +12,6 @@ import ( "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" - "github.com/go-openapi/validate" ) // V1FirewallRules v1 firewall rules @@ -20,12 +19,10 @@ import ( // swagger:model v1.FirewallRules type V1FirewallRules struct { - // egress - // Required: true + // list of egress rules to be deployed during firewall allocation Egress []*V1FirewallEgressRule `json:"egress" yaml:"egress"` - // ingress - // Required: true + // list of ingress rules to be deployed during firewall allocation Ingress []*V1FirewallIngressRule `json:"ingress" yaml:"ingress"` } @@ -48,9 +45,8 @@ func (m *V1FirewallRules) Validate(formats strfmt.Registry) error { } func (m *V1FirewallRules) validateEgress(formats strfmt.Registry) error { - - if err := validate.Required("egress", "body", m.Egress); err != nil { - return err + if swag.IsZero(m.Egress) { // not required + return nil } for i := 0; i < len(m.Egress); i++ { @@ -75,9 +71,8 @@ func (m *V1FirewallRules) validateEgress(formats strfmt.Registry) error { } func (m *V1FirewallRules) validateIngress(formats strfmt.Registry) error { - - if err := validate.Required("ingress", "body", m.Ingress); err != nil { - return err + if swag.IsZero(m.Ingress) { // not required + return nil } for i := 0; i < len(m.Ingress); i++ { diff --git a/metal-api.json b/metal-api.json index 03fc7ab..cc5526a 100644 --- a/metal-api.json +++ b/metal-api.json @@ -1017,19 +1017,9 @@ }, "v1.FirewallAllocateRequest": { "properties": { - "egress": { - "description": "list of egress rules to be deployed during firewall allocation", - "items": { - "$ref": "#/definitions/v1.FirewallEgressRule" - }, - "type": "array" - }, - "ingress": { - "description": "list of ingress rules to be deployed during firewall allocation", - "items": { - "$ref": "#/definitions/v1.FirewallIngressRule" - }, - "type": "array" + "firewall_rules": { + "$ref": "#/definitions/v1.FirewallRules", + "description": "optional egress and ingress firewall rules to deploy during firewall allocation" } } }, @@ -1039,17 +1029,14 @@ "description": "a description for this entity", "type": "string" }, - "egress": { - "description": "list of egress rules to be deployed during firewall allocation", - "items": { - "$ref": "#/definitions/v1.FirewallEgressRule" - }, - "type": "array" - }, "filesystemlayoutid": { "description": "the filesystemlayout id to assing to this machine", "type": "string" }, + "firewall_rules": { + "$ref": "#/definitions/v1.FirewallRules", + "description": "optional egress and ingress firewall rules to deploy during firewall allocation" + }, "hostname": { "description": "the hostname for the allocated machine (defaults to metal)", "type": "string" @@ -1058,13 +1045,6 @@ "description": "the image id to assign this machine to", "type": "string" }, - "ingress": { - "description": "list of ingress rules to be deployed during firewall allocation", - "items": { - "$ref": "#/definitions/v1.FirewallIngressRule" - }, - "type": "array" - }, "ips": { "description": "the ips to attach to this machine additionally", "items": { @@ -1477,22 +1457,20 @@ "v1.FirewallRules": { "properties": { "egress": { + "description": "list of egress rules to be deployed during firewall allocation", "items": { "$ref": "#/definitions/v1.FirewallEgressRule" }, "type": "array" }, "ingress": { + "description": "list of ingress rules to be deployed during firewall allocation", "items": { "$ref": "#/definitions/v1.FirewallIngressRule" }, "type": "array" } - }, - "required": [ - "egress", - "ingress" - ] + } }, "v1.FirmwaresResponse": { "properties": {