Skip to content

Commit

Permalink
refactor: gate init calls with build tags
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Zapletal <[email protected]>
  • Loading branch information
lzap committed May 13, 2024
1 parent 0556979 commit b72166f
Show file tree
Hide file tree
Showing 47 changed files with 171 additions and 127 deletions.
2 changes: 1 addition & 1 deletion cmd/spec/example_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/RHEnVision/provisioning-backend/internal/ptr"
)

var ReservationTime time.Time = MustParseTime("2013-05-13T19:20:25Z")
var ReservationTime = MustParseTime("2013-05-13T19:20:25Z")

func MustParseTime(t string) time.Time {
result, err := time.Parse(time.RFC3339, t)
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (

// Forever is used for items that should be cached "forever". Expiration of 30 days
// is used to allow cleanup of unused items.
const Forever time.Duration = 24 * time.Hour * 30
const Forever = 24 * time.Hour * 30

type Cacheable interface {
CacheKeyName() string
Expand Down
10 changes: 10 additions & 0 deletions internal/clients/http/azure/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !integration && !test

package azure

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetAzureClient = newAzureClient
clients.GetServiceAzureClient = newServiceClient
}
4 changes: 0 additions & 4 deletions internal/clients/http/azure/azure_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type client struct {
credential *azidentity.ClientSecretCredential
}

func init() {
clients.GetAzureClient = newAzureClient
}

func newAzureClient(ctx context.Context, auth *clients.Authentication) (clients.Azure, error) {
opts := azidentity.ClientSecretCredentialOptions{}
identityClient, err := azidentity.NewClientSecretCredential(config.Azure.TenantID, config.Azure.ClientID, config.Azure.ClientSecret, &opts)
Expand Down
4 changes: 0 additions & 4 deletions internal/clients/http/azure/azure_service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ type serviceClient struct {
credential *azidentity.ClientSecretCredential
}

func init() {
clients.GetServiceAzureClient = newServiceClient
}

func newServiceClient(ctx context.Context) (clients.ServiceAzure, error) {
opts := azidentity.ClientSecretCredentialOptions{}
identityClient, err := azidentity.NewClientSecretCredential(config.Azure.TenantID, config.Azure.ClientID, config.Azure.ClientSecret, &opts)
Expand Down
10 changes: 10 additions & 0 deletions internal/clients/http/ec2/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !integration && !test

package ec2

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetEC2Client = newAssumedEC2ClientWithRegion
clients.GetServiceEC2Client = newEC2ClientWithRegion
}
5 changes: 0 additions & 5 deletions internal/clients/http/ec2/ec2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ type ec2Client struct {
assumed bool
}

func init() {
clients.GetEC2Client = newAssumedEC2ClientWithRegion
clients.GetServiceEC2Client = newEC2ClientWithRegion
}

func logger(ctx context.Context) *zerolog.Logger {
logger := zerolog.Ctx(ctx).With().Str("client", "ec2").Logger()
return &logger
Expand Down
10 changes: 10 additions & 0 deletions internal/clients/http/gcp/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !integration && !test

package gcp

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetGCPClient = newGCPClient
clients.GetServiceGCPClient = newServiceGCPClient
}
4 changes: 0 additions & 4 deletions internal/clients/http/gcp/gcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ type gcpClient struct {
options []option.ClientOption
}

func init() {
clients.GetGCPClient = newGCPClient
}

// GCP SDK does not provide a single client, so only configuration can be shared and
// clients need to be created and closed in each function.
// The difference between the customer and service authentication is which Project ID was given: the service or the customer
Expand Down
6 changes: 1 addition & 5 deletions internal/clients/http/gcp/gcp_service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type gcpServiceClient struct {
options []option.ClientOption
}

func init() {
clients.GetServiceGCPClient = newServiceGCPClient
}

func newServiceGCPClient(ctx context.Context) (clients.ServiceGCP, error) {
options := []option.ClientOption{
option.WithCredentialsJSON([]byte(config.GCP.JSON)),
Expand Down Expand Up @@ -161,6 +157,6 @@ func getTotalStorage(disks []*computepb.ScratchDisks) int64 {
}

func mbToMib(mb float32) int64 {
var mib float32 = mb * 0.9536
var mib = mb * 0.9536

Check failure on line 160 in internal/clients/http/gcp/gcp_service_client.go

View workflow job for this annotation

GitHub Actions / 🏫 Go linter

File is not `gofumpt`-ed (gofumpt)
return int64(mib)
}
9 changes: 9 additions & 0 deletions internal/clients/http/image_builder/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !integration && !test

package image_builder

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetImageBuilderClient = newImageBuilderClient
}
4 changes: 0 additions & 4 deletions internal/clients/http/image_builder/image_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ type ibClient struct {
client *ClientWithResponses
}

func init() {
clients.GetImageBuilderClient = newImageBuilderClient
}

func logger(ctx context.Context) zerolog.Logger {
return zerolog.Ctx(ctx).With().Str("client", "ib").Logger()
}
Expand Down
9 changes: 9 additions & 0 deletions internal/clients/http/rbac/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !integration && !test

package rbac

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetRbacClient = newClient
}
4 changes: 0 additions & 4 deletions internal/clients/http/rbac/rbac_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ type rbac struct {
client *ClientWithResponses
}

func init() {
clients.GetRbacClient = newClient
}

func logger(ctx context.Context) zerolog.Logger {
return zerolog.Ctx(ctx).With().Str("client", "ib").Logger()
}
Expand Down
9 changes: 9 additions & 0 deletions internal/clients/http/sources/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !integration && !test

package sources

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetSourcesClient = newSourcesClient
}
4 changes: 0 additions & 4 deletions internal/clients/http/sources/sources_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type sourcesClient struct {
client *ClientWithResponses
}

func init() {
clients.GetSourcesClient = newSourcesClient
}

func logger(ctx context.Context) zerolog.Logger {
return zerolog.Ctx(ctx).With().Str("client", "sources").Logger()
}
Expand Down
28 changes: 21 additions & 7 deletions internal/clients/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type AzureResourceGroup struct {

// GetSourcesClient returns Sources interface implementation. There are currently
// two implementations available: HTTP and stub
var GetSourcesClient func(ctx context.Context) (Sources, error)
var GetSourcesClient = func(ctx context.Context) (Sources, error) {
panic("client func not initialized")
}

// Sources interface provides access to the Sources backend service API
type Sources interface {
Expand All @@ -34,7 +36,9 @@ type Sources interface {

// GetImageBuilderClient returns ImageBuilder interface implementation. There are currently
// two implementations available: HTTP and stub
var GetImageBuilderClient func(ctx context.Context) (ImageBuilder, error)
var GetImageBuilderClient = func(ctx context.Context) (ImageBuilder, error) {
panic("client func not initialized")
}

// ImageBuilder interface provides access to the Image Builder backend service API
type ImageBuilder interface {
Expand Down Expand Up @@ -62,10 +66,14 @@ type ClientStatuser interface {
}

// GetEC2Client returns an EC2 facade interface with assumed role.
var GetEC2Client func(ctx context.Context, auth *Authentication, region string) (EC2, error)
var GetEC2Client = func(ctx context.Context, auth *Authentication, region string) (EC2, error) {
panic("client func not initialized")
}

// GetServiceEC2Client returns an EC2 client for the service account.
var GetServiceEC2Client func(ctx context.Context, region string) (EC2, error)
var GetServiceEC2Client = func(ctx context.Context, region string) (EC2, error) {
panic("client func not initialized")
}

type EC2 interface {
ClientStatuser
Expand Down Expand Up @@ -106,10 +114,14 @@ type EC2 interface {
}

// GetAzureClient returns an Azure client with customer's subscription ID.
var GetAzureClient func(ctx context.Context, auth *Authentication) (Azure, error)
var GetAzureClient = func(ctx context.Context, auth *Authentication) (Azure, error) {
panic("client func not initialized")
}

// GetServiceAzureClient returns an Azure client for the service account itself.
var GetServiceAzureClient func(ctx context.Context) (ServiceAzure, error)
var GetServiceAzureClient = func(ctx context.Context) (ServiceAzure, error) {
panic("client func not initialized")
}

type Azure interface {
ClientStatuser
Expand All @@ -132,7 +144,9 @@ type ServiceAzure interface {
}

// GetGCPClient returns a GCP facade interface.
var GetGCPClient func(ctx context.Context, auth *Authentication) (GCP, error)
var GetGCPClient = func(ctx context.Context, auth *Authentication) (GCP, error) {
panic("client func not initialized")
}

// GetServiceGCPClient returns a GCP client for the service account.
var GetServiceGCPClient func(ctx context.Context) (ServiceGCP, error)
Expand Down
2 changes: 1 addition & 1 deletion internal/clients/regional_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewRegionalInstanceTypes() *RegionalTypeAvailability {
}
}

var ErrUnknownRegionZoneCombination error = errors.New("unknown region and zone combination")
var ErrUnknownRegionZoneCombination = errors.New("unknown region and zone combination")

func key(region, zone string) string {
if zone == "" {
Expand Down
16 changes: 16 additions & 0 deletions internal/clients/stubs/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build integration || test

package stubs

import "github.com/RHEnVision/provisioning-backend/internal/clients"

func init() {
clients.GetAzureClient = getAzureClient
clients.GetEC2Client = newEC2CustomerClientStubWithRegion
clients.GetServiceEC2Client = newEC2ServiceClientStubWithRegion
clients.GetGCPClient = newGCPCustomerClientStub
clients.GetServiceGCPClient = getServiceGCPClientStub
clients.GetImageBuilderClient = getImageBuilderClientStub
clients.GetRbacClient = getRbacClient
clients.GetSourcesClient = getSourcesClient
}
4 changes: 0 additions & 4 deletions internal/clients/stubs/context_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ type ctxKeyType int

const azureCtxKey ctxKeyType = iota

func init() {
clients.GetAzureClient = getAzureClient
}

func WithAzureClient(parent context.Context) context.Context {
ctx := context.WithValue(parent, azureCtxKey, &AzureClientStub{})
return ctx
Expand Down
5 changes: 0 additions & 5 deletions internal/clients/stubs/ec2_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ type EC2ClientStub struct {
Imported []*types.KeyPairInfo
}

func init() {
clients.GetEC2Client = newEC2CustomerClientStubWithRegion
clients.GetServiceEC2Client = newEC2ServiceClientStubWithRegion
}

func WithEC2Client(parent context.Context) context.Context {
ctx := context.WithValue(parent, ec2CtxKey, &EC2ClientStub{})
return ctx
Expand Down
5 changes: 0 additions & 5 deletions internal/clients/stubs/gcp_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ type (
GCPServiceClientStub struct{}
)

func init() {
clients.GetGCPClient = newGCPCustomerClientStub
clients.GetServiceGCPClient = getServiceGCPClientStub
}

func newGCPCustomerClientStub(ctx context.Context, auth *clients.Authentication) (clients.GCP, error) {
return getCustomerGCPClientStub(ctx, auth)
}
Expand Down
4 changes: 0 additions & 4 deletions internal/clients/stubs/image_builder_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ var imageBuilderCtxKey imageBuilderCtxKeyType = "image-builder-interface"

type ImageBuilderClientStub struct{}

func init() {
clients.GetImageBuilderClient = getImageBuilderClientStub
}

func WithImageBuilderClient(parent context.Context) context.Context {
ctx := context.WithValue(parent, imageBuilderCtxKey, &ImageBuilderClientStub{})
return ctx
Expand Down
4 changes: 0 additions & 4 deletions internal/clients/stubs/rbac_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (

type rbacClient struct{}

func init() {
clients.GetRbacClient = getRbacClient
}

func getRbacClient(ctx context.Context) clients.Rbac {
return rbacClient{}
}
Expand Down
6 changes: 0 additions & 6 deletions internal/clients/stubs/sources_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ type SourcesClientStub struct {
auths map[string]*clients.Authentication
}

func init() {
// We are currently using SourcesClientStub
clients.GetSourcesClient = getSourcesClient
}

// SourcesClient
func WithSourcesClient(parent context.Context) context.Context {
ctx := context.WithValue(parent, sourcesCtxKey, &SourcesClientStub{auths: make(map[string]*clients.Authentication)})
return ctx
Expand Down
16 changes: 12 additions & 4 deletions internal/dao/dao_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
"github.com/RHEnVision/provisioning-backend/internal/models"
)

var GetAccountDao func(ctx context.Context) AccountDao
var GetAccountDao = func(ctx context.Context) AccountDao {
panic("dao not initialized")
}

// AccountDao represents an account (tenant)
type AccountDao interface {
Expand All @@ -29,7 +31,9 @@ type AccountDao interface {
List(ctx context.Context, limit, offset int64) ([]*models.Account, error)
}

var GetPubkeyDao func(ctx context.Context) PubkeyDao
var GetPubkeyDao = func(ctx context.Context) PubkeyDao {
panic("dao not initialized")
}

// PubkeyDao represents Pubkeys (public part of ssh key pair) and corresponding Resources (uploaded pubkeys
// to specific cloud providers in specific regions).
Expand All @@ -47,7 +51,9 @@ type PubkeyDao interface {
UnscopedDeleteResource(ctx context.Context, id int64) error
}

var GetReservationDao func(ctx context.Context) ReservationDao
var GetReservationDao = func(ctx context.Context) ReservationDao {
panic("dao not initialized")
}

// ReservationDao represents a reservation, an abstraction of one or more background jobs with
// associated detail information different for different cloud providers (like number of vCPUs,
Expand Down Expand Up @@ -118,7 +124,9 @@ type ReservationDao interface {
Cleanup(ctx context.Context) error
}

var GetStatDao func(ctx context.Context) StatDao
var GetStatDao = func(ctx context.Context) StatDao {
panic("dao not initialized")
}

// StatDao represents stats about the application run
type StatDao interface {
Expand Down
13 changes: 13 additions & 0 deletions internal/dao/pgx/0init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !integration && !test

package pgx

import "github.com/RHEnVision/provisioning-backend/internal/dao"

func init() {
dao.GetAccountDao = getAccountDao
dao.GetPubkeyDao = getPubkeyDao
dao.GetReservationDao = getReservationDao
dao.GetServiceDao = getServiceDao
dao.GetStatDao = getStatDao
}
Loading

0 comments on commit b72166f

Please sign in to comment.