Skip to content

Commit

Permalink
refactor: Reference IPv4CIDR type in meta instead of storage API
Browse files Browse the repository at this point in the history
  • Loading branch information
9marco committed May 28, 2024
1 parent fd2d420 commit edcc524
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 44 deletions.
2 changes: 1 addition & 1 deletion create/keyvaluestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type keyValueStoreCmd struct {
Location string `default:"nine-es34" help:"Location where the KeyValueStore instance is created."`
MemorySize string `help:"MemorySize configures KeyValueStore to use a specified amount of memory for the data set." placeholder:"1Gi"`
MaxMemoryPolicy storage.KeyValueStoreMaxMemoryPolicy `help:"MaxMemoryPolicy specifies the exact behavior KeyValueStore follows when the maxmemory limit is reached." placeholder:"allkeys-lru"`
AllowedCidrs []storage.IPv4CIDR `help:"AllowedCIDRs specify the allowed IP addresses, connecting to the instance." placeholder:"0.0.0.0/0"`
AllowedCidrs []meta.IPv4CIDR `help:"AllowedCIDRs specify the allowed IP addresses, connecting to the instance." placeholder:"0.0.0.0/0"`
Wait bool `default:"true" help:"Wait until KeyValueStore is created."`
WaitTimeout time.Duration `default:"600s" help:"Duration to wait for KeyValueStore getting ready. Only relevant if --wait is set."`
}
Expand Down
5 changes: 3 additions & 2 deletions create/keyvaluestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -35,8 +36,8 @@ func TestKeyValueStore(t *testing.T) {
},
{
"allowedCIDRs",
keyValueStoreCmd{AllowedCidrs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
keyValueStoreCmd{AllowedCidrs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
false,
},
{
Expand Down
6 changes: 3 additions & 3 deletions create/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type mySQLCmd struct {
Name string `arg:"" default:"" help:"Name of the MySQL instance. A random name is generated if omitted."`
Location string `placeholder:"${mysql_location_default}" help:"Location where the MySQL instance is created. Available locations are: ${mysql_location_options}"`
MachineType infra.MachineType `placeholder:"${mysql_machine_default}" help:"Defines the sizing for a particular MySQL instance. Available types: ${mysql_machine_types}"`
AllowedCidrs []storage.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
AllowedCidrs []meta.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
SSHKeys []storage.SSHKey `help:"Contains a list of SSH public keys, allowed to connect to the db server, in order to up-/download and directly restore database backups."`
SSHKeysFile string `help:"Path to a file containing a list of SSH public keys (see above), separated by newlines."`
SQLMode *[]storage.MySQLMode `placeholder:"\"MODE1, MODE2, ...\"" help:"Configures the sql_mode setting. Modes affect the SQL syntax MySQL supports and the data validation checks it performs. Defaults to: ${mysql_mode}"`
Expand Down Expand Up @@ -89,8 +89,8 @@ func (cmd *mySQLCmd) newMySQL(namespace string) *storage.MySQL {
ForProvider: storage.MySQLParameters{
Location: meta.LocationName(cmd.Location),
MachineType: cmd.MachineType,
AllowedCIDRs: []storage.IPv4CIDR{}, // avoid missing parameter error
SSHKeys: []storage.SSHKey{}, // avoid missing parameter error
AllowedCIDRs: []meta.IPv4CIDR{}, // avoid missing parameter error
SSHKeys: []storage.SSHKey{}, // avoid missing parameter error
SQLMode: cmd.SQLMode,
CharacterSet: storage.MySQLCharacterSet{
Name: cmd.CharacterSetName,
Expand Down
7 changes: 4 additions & 3 deletions create/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -57,8 +58,8 @@ func TestMySQL(t *testing.T) {
},
{
name: "allowedCIDRs",
create: mySQLCmd{AllowedCidrs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
create: mySQLCmd{AllowedCidrs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "characterSet",
Expand Down Expand Up @@ -118,7 +119,7 @@ func TestMySQL(t *testing.T) {

// we set defaults for the slices
if tt.want.AllowedCIDRs == nil {
tt.want.AllowedCIDRs = []storage.IPv4CIDR{}
tt.want.AllowedCIDRs = []meta.IPv4CIDR{}
}
if tt.want.SSHKeys == nil {
tt.want.SSHKeys = []storage.SSHKey{}
Expand Down
6 changes: 3 additions & 3 deletions create/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type postgresCmd struct {
Name string `arg:"" default:"" help:"Name of the PostgreSQL instance. A random name is generated if omitted."`
Location string `placeholder:"${postgres_location_default}" help:"Location where the PostgreSQL instance is created. Available locations are: ${postgres_location_options}"`
MachineType infra.MachineType `placeholder:"${postgres_machine_default}" help:"Defines the sizing for a particular PostgreSQL instance. Available types: ${postgres_machine_types}"`
AllowedCidrs []storage.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
AllowedCidrs []meta.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
SSHKeys []storage.SSHKey `help:"Contains a list of SSH public keys, allowed to connect to the db server, in order to up-/download and directly restore database backups."`
SSHKeysFile string `help:"Path to a file containing a list of SSH public keys (see above), separated by newlines."`
PostgresVersion storage.PostgresVersion `placeholder:"${postgres_version_default}" help:"Release version with which the PostgreSQL instance is created"`
Expand Down Expand Up @@ -84,8 +84,8 @@ func (cmd *postgresCmd) newPostgres(namespace string) *storage.Postgres {
ForProvider: storage.PostgresParameters{
Location: meta.LocationName(cmd.Location),
MachineType: cmd.MachineType,
AllowedCIDRs: []storage.IPv4CIDR{}, // avoid missing parameter error
SSHKeys: []storage.SSHKey{}, // avoid missing parameter error
AllowedCIDRs: []meta.IPv4CIDR{}, // avoid missing parameter error
SSHKeys: []storage.SSHKey{}, // avoid missing parameter error
Version: cmd.PostgresVersion,
KeepDailyBackups: cmd.KeepDailyBackups,
},
Expand Down
7 changes: 4 additions & 3 deletions create/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -52,8 +53,8 @@ func TestPostgres(t *testing.T) {
},
{
name: "allowedCIDRs",
create: postgresCmd{AllowedCidrs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
create: postgresCmd{AllowedCidrs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "version",
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestPostgres(t *testing.T) {

// we set defaults for the slices
if tt.want.AllowedCIDRs == nil {
tt.want.AllowedCIDRs = []storage.IPv4CIDR{}
tt.want.AllowedCIDRs = []meta.IPv4CIDR{}
}
if tt.want.SSHKeys == nil {
tt.want.SSHKeys = []storage.SSHKey{}
Expand Down
3 changes: 2 additions & 1 deletion update/keyvaluestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/crossplane/crossplane-runtime/pkg/resource"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
kresource "k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -15,7 +16,7 @@ type keyValueStoreCmd struct {
Name string `arg:"" default:"" help:"Name of the KeyValueStore instance to update."`
MemorySize *string `help:"MemorySize configures KeyValueStore to use a specified amount of memory for the data set." placeholder:"1Gi"`
MaxMemoryPolicy *storage.KeyValueStoreMaxMemoryPolicy `help:"MaxMemoryPolicy specifies the exact behavior KeyValueStore follows when the maxmemory limit is reached." placeholder:"allkeys-lru"`
AllowedCidrs *[]storage.IPv4CIDR `help:"AllowedCIDRs specify the allowed IP addresses, connecting to the instance." placeholder:"0.0.0.0/0"`
AllowedCidrs *[]meta.IPv4CIDR `help:"AllowedCIDRs specify the allowed IP addresses, connecting to the instance." placeholder:"0.0.0.0/0"`
}

func (cmd *keyValueStoreCmd) Run(ctx context.Context, client *api.Client) error {
Expand Down
15 changes: 8 additions & 7 deletions update/keyvaluestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"testing"

meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/test"
Expand Down Expand Up @@ -61,22 +62,22 @@ func TestKeyValueStore(t *testing.T) {
{
"allowedCIDRs",
storage.KeyValueStoreParameters{},
keyValueStoreCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
keyValueStoreCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
false,
},
{
"allowedCIDRs",
storage.KeyValueStoreParameters{AllowedCIDRs: []storage.IPv4CIDR{"192.168.0.1/24"}},
keyValueStoreCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []meta.IPv4CIDR{"192.168.0.1/24"}},
keyValueStoreCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
false,
},
{
"allowedCIDRs",
storage.KeyValueStoreParameters{AllowedCIDRs: []storage.IPv4CIDR{"0.0.0.0/0"}},
storage.KeyValueStoreParameters{AllowedCIDRs: []meta.IPv4CIDR{"0.0.0.0/0"}},
keyValueStoreCmd{MemorySize: ptr.To("1G")},
storage.KeyValueStoreParameters{MemorySize: memorySize("1G"), AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
storage.KeyValueStoreParameters{MemorySize: memorySize("1G"), AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
false,
},
}
Expand Down
3 changes: 2 additions & 1 deletion update/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/resource"
infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/file"
Expand All @@ -15,7 +16,7 @@ import (
type mySQLCmd struct {
Name string `arg:"" default:"" help:"Name of the MySQL instance to update."`
MachineType *infra.MachineType `placeholder:"${mysql_machine_default}" help:"Defines the sizing for a particular MySQL instance. Available types: ${mysql_machine_types}"`
AllowedCidrs *[]storage.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
AllowedCidrs *[]meta.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
SSHKeys []storage.SSHKey `help:"Contains a list of SSH public keys, allowed to connect to the db server, in order to up-/download and directly restore database backups."`
SSHKeysFile string `help:"Path to a file containing a list of SSH public keys (see above), separated by newlines."`
SQLMode *[]storage.MySQLMode `placeholder:"\"MODE1, MODE2, ...\"" help:"Configures the sql_mode setting. Modes affect the SQL syntax MySQL supports and the data validation checks it performs. Defaults to: ${mysql_mode}"`
Expand Down
15 changes: 8 additions & 7 deletions update/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/test"
Expand Down Expand Up @@ -78,20 +79,20 @@ func TestMySQL(t *testing.T) {
},
{
name: "allowedCIDRs-nothing-set-initially",
update: mySQLCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
update: mySQLCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "allowedCIDRs-set-initially",
create: storage.MySQLParameters{AllowedCIDRs: []storage.IPv4CIDR{"192.168.0.1/24"}},
update: mySQLCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
create: storage.MySQLParameters{AllowedCIDRs: []meta.IPv4CIDR{"192.168.0.1/24"}},
update: mySQLCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "multi-update",
create: storage.MySQLParameters{AllowedCIDRs: []storage.IPv4CIDR{"0.0.0.0/0"}},
create: storage.MySQLParameters{AllowedCIDRs: []meta.IPv4CIDR{"0.0.0.0/0"}},
update: mySQLCmd{MachineType: ptr.To(infra.MachineType("nine-standard-1"))},
want: storage.MySQLParameters{MachineType: infra.MachineType("nine-standard-1"), AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.MySQLParameters{MachineType: infra.MachineType("nine-standard-1"), AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
}
for _, tt := range tests {
Expand Down
13 changes: 7 additions & 6 deletions update/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/resource"
infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/file"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type postgresCmd struct {
Name string `arg:"" default:"" help:"Name of the PostgreSQL instance to update."`
MachineType *infra.MachineType `placeholder:"${postgres_machine_default}" help:"Defines the sizing for a particular PostgreSQL instance. Available types: ${postgres_machine_types}"`
AllowedCidrs *[]storage.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
SSHKeys []storage.SSHKey `help:"Contains a list of SSH public keys, allowed to connect to the db server, in order to up-/download and directly restore database backups."`
SSHKeysFile string `help:"Path to a file containing a list of SSH public keys (see above), separated by newlines."`
KeepDailyBackups *int `placeholder:"${postgres_backup_retention_days}" help:"Number of daily database backups to keep. Note that setting this to 0, backup will be disabled and existing dumps deleted immediately."`
Name string `arg:"" default:"" help:"Name of the PostgreSQL instance to update."`
MachineType *infra.MachineType `placeholder:"${postgres_machine_default}" help:"Defines the sizing for a particular PostgreSQL instance. Available types: ${postgres_machine_types}"`
AllowedCidrs *[]meta.IPv4CIDR `placeholder:"0.0.0.0/0" help:"Specifies the IP addresses allowed to connect to the instance." `
SSHKeys []storage.SSHKey `help:"Contains a list of SSH public keys, allowed to connect to the db server, in order to up-/download and directly restore database backups."`
SSHKeysFile string `help:"Path to a file containing a list of SSH public keys (see above), separated by newlines."`
KeepDailyBackups *int `placeholder:"${postgres_backup_retention_days}" help:"Number of daily database backups to keep. Note that setting this to 0, backup will be disabled and existing dumps deleted immediately."`
}

func (cmd *postgresCmd) Run(ctx context.Context, client *api.Client) error {
Expand Down
15 changes: 8 additions & 7 deletions update/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/internal/test"
Expand Down Expand Up @@ -47,20 +48,20 @@ func TestPostgres(t *testing.T) {
},
{
name: "allowedCIDRs-nothing-set-initially",
update: postgresCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
update: postgresCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "allowedCIDRs-set-initially",
create: storage.PostgresParameters{AllowedCIDRs: []storage.IPv4CIDR{"192.168.0.1/24"}},
update: postgresCmd{AllowedCidrs: &[]storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
create: storage.PostgresParameters{AllowedCIDRs: []meta.IPv4CIDR{"192.168.0.1/24"}},
update: postgresCmd{AllowedCidrs: &[]meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
{
name: "multi-update",
create: storage.PostgresParameters{AllowedCIDRs: []storage.IPv4CIDR{"0.0.0.0/0"}},
create: storage.PostgresParameters{AllowedCIDRs: []meta.IPv4CIDR{"0.0.0.0/0"}},
update: postgresCmd{MachineType: ptr.To(infra.MachineType("nine-standard-1"))},
want: storage.PostgresParameters{MachineType: infra.MachineType("nine-standard-1"), AllowedCIDRs: []storage.IPv4CIDR{storage.IPv4CIDR("0.0.0.0/0")}},
want: storage.PostgresParameters{MachineType: infra.MachineType("nine-standard-1"), AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")}},
},
}
for _, tt := range tests {
Expand Down

0 comments on commit edcc524

Please sign in to comment.