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 4d45020
Show file tree
Hide file tree
Showing 40 changed files with 160 additions and 116 deletions.
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
4 changes: 0 additions & 4 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
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) = 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) = 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) = 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) = 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) = 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) = 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) = 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
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 = 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 = 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 = 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 = 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
}
4 changes: 0 additions & 4 deletions internal/dao/pgx/account_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"github.com/rs/zerolog"
)

func init() {
dao.GetAccountDao = getAccountDao
}

type accountDao struct{}

func getAccountDao(ctx context.Context) dao.AccountDao {
Expand Down
4 changes: 0 additions & 4 deletions internal/dao/pgx/pubkey_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/georgysavva/scany/v2/pgxscan"
)

func init() {
dao.GetPubkeyDao = getPubkeyDao
}

type pubkeyDao struct{}

func getPubkeyDao(ctx context.Context) dao.PubkeyDao {
Expand Down
4 changes: 0 additions & 4 deletions internal/dao/pgx/reservation_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import (
"github.com/rs/zerolog"
)

func init() {
dao.GetReservationDao = getReservationDao
}

type reservationDao struct{}

func getReservationDao(ctx context.Context) dao.ReservationDao {
Expand Down
Loading

0 comments on commit 4d45020

Please sign in to comment.