Explorar o código

Add Cidr table (#4319)

Stefan McShane %!s(int64=2) %!d(string=hai) anos
pai
achega
b8336911cf

+ 27 - 0
internal/models/ipam.go

@@ -0,0 +1,27 @@
+package models
+
+import (
+	"net"
+
+	"github.com/google/uuid"
+	"gorm.io/gorm"
+)
+
+// Ipam represents an entry in the Ipam table
+type Ipam struct {
+	gorm.Model
+
+	// ID is a UUID for the APIContract
+	ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
+
+	// ProjectID is the ID of the project that the config belongs to.
+	// This should be a foreign key, but GORM doesnt play well with FKs.
+	ProjectID int `json:"project_id"`
+
+	CIDR net.IPNet `gorm:"type:cidr;column:cidr_range"`
+}
+
+// TableName overrides the table name
+func (Ipam) TableName() string {
+	return "ipam"
+}

+ 16 - 0
internal/repository/gorm/ipam.go

@@ -0,0 +1,16 @@
+package gorm
+
+import (
+	"github.com/porter-dev/porter/internal/repository"
+	"gorm.io/gorm"
+)
+
+// Ipam uses gorm.DB for querying the database
+type Ipam struct {
+	db *gorm.DB
+}
+
+// NewIpamRepository creates an IPAM connection
+func NewIpamRepository(db *gorm.DB) repository.IpamRepository {
+	return &Ipam{db}
+}

+ 1 - 0
internal/repository/gorm/migrate.go

@@ -84,5 +84,6 @@ func AutoMigrate(db *gorm.DB, debug bool) error {
 		&ints.GithubAppInstallation{},
 		&ints.GithubAppOAuthIntegration{},
 		&ints.SlackIntegration{},
+		&models.Ipam{},
 	)
 }

+ 7 - 0
internal/repository/gorm/repository.go

@@ -59,6 +59,7 @@ type GormRepository struct {
 	githubWebhook             repository.GithubWebhookRepository
 	datastore                 repository.DatastoreRepository
 	appInstance               repository.AppInstanceRepository
+	ipam                      repository.IpamRepository
 }
 
 func (t *GormRepository) User() repository.UserRepository {
@@ -275,6 +276,11 @@ func (t *GormRepository) AppInstance() repository.AppInstanceRepository {
 	return t.appInstance
 }
 
+// Ipam returns the IpamRepository interface implemented by gorm
+func (t *GormRepository) Ipam() repository.IpamRepository {
+	return t.ipam
+}
+
 // NewRepository returns a Repository which persists users in memory
 // and accepts a parameter that can trigger read/write errors
 func NewRepository(db *gorm.DB, key *[32]byte, storageBackend credentials.CredentialStorage) repository.Repository {
@@ -331,5 +337,6 @@ func NewRepository(db *gorm.DB, key *[32]byte, storageBackend credentials.Creden
 		githubWebhook:             NewGithubWebhookRepository(db),
 		datastore:                 NewDatastoreRepository(db),
 		appInstance:               NewAppInstanceRepository(db),
+		ipam:                      NewIpamRepository(db),
 	}
 }

+ 4 - 0
internal/repository/ipam.go

@@ -0,0 +1,4 @@
+package repository
+
+// IpamRepository represents the set of queries on the Ipam model
+type IpamRepository interface{}