Skip to content

Commit

Permalink
Validation error for subnet by UUID without network by UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Sep 20, 2021
1 parent 6483837 commit 82780b2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
10 changes: 7 additions & 3 deletions api/v1alpha4/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ type SecurityGroupFilter struct {
}

type NetworkParam struct {
// The UUID of the network. Required if you omit the port attribute.
// Optional UUID of the network.
// If specified this will not be validated prior to server creation.
// Required if `Subnets` specifies a subnet by UUID.
UUID string `json:"uuid,omitempty"`
// A fixed IPv4 address for the NIC.
FixedIP string `json:"fixedIP,omitempty"`
Expand Down Expand Up @@ -85,10 +87,12 @@ type Filter struct {
}

type SubnetParam struct {
// The UUID of the network. Required if you omit the port attribute.
// Optional UUID of the subnet.
// If specified this will not be validated prior to server creation.
// If specified, the enclosing `NetworkParam` must also be specified by UUID.
UUID string `json:"uuid,omitempty"`

// Filters for optional network query
// Filters for optional subnet query
Filter SubnetFilter `json:"filter,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ spec:
items:
properties:
filter:
description: Filters for optional network query
description: Filters for optional subnet query
properties:
cidr:
type: string
Expand Down Expand Up @@ -1239,14 +1239,17 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network. Required
if you omit the port attribute.
description: Optional UUID of the subnet. If specified
this will not be validated prior to server creation.
If specified, the enclosing `NetworkParam` must
also be specified by UUID.
type: string
type: object
type: array
uuid:
description: The UUID of the network. Required if you
omit the port attribute.
description: Optional UUID of the network. If specified
this will not be validated prior to server creation.
Required if `Subnets` specifies a subnet by UUID.
type: string
type: object
type: array
Expand Down Expand Up @@ -1475,7 +1478,7 @@ spec:
Gateway of this router
properties:
filter:
description: Filters for optional network query
description: Filters for optional subnet query
properties:
cidr:
type: string
Expand Down Expand Up @@ -1521,8 +1524,10 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network. Required if you omit
the port attribute.
description: Optional UUID of the subnet. If specified this
will not be validated prior to server creation. If specified,
the enclosing `NetworkParam` must also be specified by
UUID.
type: string
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ spec:
items:
properties:
filter:
description: Filters for optional network
description: Filters for optional subnet
query
properties:
cidr:
Expand Down Expand Up @@ -220,14 +220,19 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network.
Required if you omit the port attribute.
description: Optional UUID of the subnet.
If specified this will not be validated
prior to server creation. If specified,
the enclosing `NetworkParam` must also
be specified by UUID.
type: string
type: object
type: array
uuid:
description: The UUID of the network. Required
if you omit the port attribute.
description: Optional UUID of the network. If
specified this will not be validated prior
to server creation. Required if `Subnets`
specifies a subnet by UUID.
type: string
type: object
type: array
Expand Down Expand Up @@ -463,7 +468,7 @@ spec:
for the Gateway of this router
properties:
filter:
description: Filters for optional network query
description: Filters for optional subnet query
properties:
cidr:
type: string
Expand Down Expand Up @@ -509,8 +514,10 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network. Required if
you omit the port attribute.
description: Optional UUID of the subnet. If specified
this will not be validated prior to server creation.
If specified, the enclosing `NetworkParam` must
also be specified by UUID.
type: string
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ spec:
items:
properties:
filter:
description: Filters for optional network query
description: Filters for optional subnet query
properties:
cidr:
type: string
Expand Down Expand Up @@ -533,14 +533,17 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network. Required if you
omit the port attribute.
description: Optional UUID of the subnet. If specified
this will not be validated prior to server creation.
If specified, the enclosing `NetworkParam` must also
be specified by UUID.
type: string
type: object
type: array
uuid:
description: The UUID of the network. Required if you omit the
port attribute.
description: Optional UUID of the network. If specified this
will not be validated prior to server creation. Required if
`Subnets` specifies a subnet by UUID.
type: string
type: object
type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ spec:
items:
properties:
filter:
description: Filters for optional network query
description: Filters for optional subnet query
properties:
cidr:
type: string
Expand Down Expand Up @@ -478,14 +478,17 @@ spec:
type: string
type: object
uuid:
description: The UUID of the network. Required
if you omit the port attribute.
description: Optional UUID of the subnet. If specified
this will not be validated prior to server creation.
If specified, the enclosing `NetworkParam` must
also be specified by UUID.
type: string
type: object
type: array
uuid:
description: The UUID of the network. Required if you
omit the port attribute.
description: Optional UUID of the network. If specified
this will not be validated prior to server creation.
Required if `Subnets` specifies a subnet by UUID.
type: string
type: object
type: array
Expand Down
7 changes: 7 additions & 0 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]inf
for _, subnet := range networkParam.Subnets {
// Don't lookup subnet if it was specified explicitly by UUID
if subnet.UUID != "" {
// If subnet was supplied by UUID then network
// must also have been supplied by UUID.
if netID == "" {
return fmt.Errorf("validation error adding network for subnet %s: "+
"network uuid must be specified when subnet uuid is specified", subnet.UUID)
}

addSubnet(netID, subnet.UUID)
continue
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloud/services/compute/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ func TestService_getServerNetworks(t *testing.T) {
},
wantErr: true,
},
{
name: "Subnet UUID without network",
networkParams: []infrav1.NetworkParam{
{Subnets: []infrav1.SubnetParam{
{UUID: subnetA1UUID},
}},
},
want: nil,
expect: func(m *mock_networking.MockNetworkClientMockRecorder) {
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 82780b2

Please sign in to comment.