Remove checks for GetState/SetState

This commit is contained in:
katexochen 2022-08-01 12:35:35 +02:00 committed by Paul Meyer
parent f28e00659c
commit 9f599c3993
11 changed files with 182 additions and 1632 deletions

View file

@ -9,8 +9,8 @@ import (
)
type gcpclient interface {
GetState() (state.ConstellationState, error)
SetState(state.ConstellationState) error
GetState() state.ConstellationState
SetState(state.ConstellationState)
CreateVPCs(ctx context.Context) error
CreateFirewall(ctx context.Context, input gcpcl.FirewallInput) error
CreateInstances(ctx context.Context, input gcpcl.CreateInstancesInput) error
@ -25,8 +25,8 @@ type gcpclient interface {
}
type azureclient interface {
GetState() (state.ConstellationState, error)
SetState(state.ConstellationState) error
GetState() state.ConstellationState
SetState(state.ConstellationState)
CreateApplicationInsight(ctx context.Context) error
CreateResourceGroup(ctx context.Context) error
CreateExternalLoadBalancer(ctx context.Context) error

View file

@ -41,8 +41,8 @@ type fakeAzureClient struct {
adAppObjectID string
}
func (c *fakeAzureClient) GetState() (state.ConstellationState, error) {
stat := state.ConstellationState{
func (c *fakeAzureClient) GetState() state.ConstellationState {
return state.ConstellationState{
CloudProvider: cloudprovider.Azure.String(),
AzureWorkerInstances: c.workers,
AzureControlPlaneInstances: c.controlPlanes,
@ -58,10 +58,9 @@ func (c *fakeAzureClient) GetState() (state.ConstellationState, error) {
AzureControlPlaneScaleSet: c.controlPlaneScaleSet,
AzureADAppObjectID: c.adAppObjectID,
}
return stat, nil
}
func (c *fakeAzureClient) SetState(stat state.ConstellationState) error {
func (c *fakeAzureClient) SetState(stat state.ConstellationState) {
c.workers = stat.AzureWorkerInstances
c.controlPlanes = stat.AzureControlPlaneInstances
c.name = stat.Name
@ -75,7 +74,6 @@ func (c *fakeAzureClient) SetState(stat state.ConstellationState) error {
c.workerScaleSet = stat.AzureWorkerScaleSet
c.controlPlaneScaleSet = stat.AzureControlPlaneScaleSet
c.adAppObjectID = stat.AzureADAppObjectID
return nil
}
func (c *fakeAzureClient) CreateApplicationInsight(ctx context.Context) error {
@ -167,8 +165,6 @@ type stubAzureClient struct {
terminateResourceGroupCalled bool
terminateServicePrincipalCalled bool
getStateErr error
setStateErr error
createApplicationInsightErr error
createResourceGroupErr error
createVirtualNetworkErr error
@ -180,12 +176,11 @@ type stubAzureClient struct {
terminateServicePrincipalErr error
}
func (c *stubAzureClient) GetState() (state.ConstellationState, error) {
return state.ConstellationState{}, c.getStateErr
func (c *stubAzureClient) GetState() state.ConstellationState {
return state.ConstellationState{}
}
func (c *stubAzureClient) SetState(state.ConstellationState) error {
return c.setStateErr
func (c *stubAzureClient) SetState(state.ConstellationState) {
}
func (c *stubAzureClient) CreateExternalLoadBalancer(ctx context.Context) error {
@ -257,8 +252,8 @@ type fakeGcpClient struct {
forwardingRule string
}
func (c *fakeGcpClient) GetState() (state.ConstellationState, error) {
stat := state.ConstellationState{
func (c *fakeGcpClient) GetState() state.ConstellationState {
return state.ConstellationState{
CloudProvider: cloudprovider.GCP.String(),
GCPWorkerInstances: c.workers,
GCPControlPlaneInstances: c.controlPlanes,
@ -278,10 +273,9 @@ func (c *fakeGcpClient) GetState() (state.ConstellationState, error) {
GCPZone: c.zone,
GCPServiceAccount: c.serviceAccount,
}
return stat, nil
}
func (c *fakeGcpClient) SetState(stat state.ConstellationState) error {
func (c *fakeGcpClient) SetState(stat state.ConstellationState) {
c.workers = stat.GCPWorkerInstances
c.controlPlanes = stat.GCPControlPlaneInstances
c.workerInstanceGroup = stat.GCPWorkerInstanceGroup
@ -299,7 +293,6 @@ func (c *fakeGcpClient) SetState(stat state.ConstellationState) error {
c.healthCheck = stat.GCPHealthCheck
c.backendService = stat.GCPBackendService
c.forwardingRule = stat.GCPForwardingRule
return nil
}
func (c *fakeGcpClient) CreateVPCs(ctx context.Context) error {
@ -409,8 +402,6 @@ type stubGcpClient struct {
terminateServiceAccountCalled bool
closeCalled bool
getStateErr error
setStateErr error
createVPCsErr error
createFirewallErr error
createInstancesErr error
@ -424,12 +415,11 @@ type stubGcpClient struct {
closeErr error
}
func (c *stubGcpClient) GetState() (state.ConstellationState, error) {
return state.ConstellationState{}, c.getStateErr
func (c *stubGcpClient) GetState() state.ConstellationState {
return state.ConstellationState{}
}
func (c *stubGcpClient) SetState(state.ConstellationState) error {
return c.setStateErr
func (c *stubGcpClient) SetState(state.ConstellationState) {
}
func (c *stubGcpClient) CreateVPCs(ctx context.Context) error {

View file

@ -137,7 +137,7 @@ func (c *Creator) createGCP(ctx context.Context, cl gcpclient, config *config.Co
return state.ConstellationState{}, err
}
return cl.GetState()
return cl.GetState(), nil
}
func (c *Creator) createAzure(ctx context.Context, cl azureclient, config *config.Config, insType string, controlPlaneCount, workerCount int,
@ -176,5 +176,5 @@ func (c *Creator) createAzure(ctx context.Context, cl azureclient, config *confi
return state.ConstellationState{}, err
}
return cl.GetState()
return cl.GetState(), nil
}

View file

@ -68,9 +68,7 @@ func (c *ServiceAccountCreator) Create(ctx context.Context, stat state.Constella
func (c *ServiceAccountCreator) createServiceAccountGCP(ctx context.Context, cl gcpclient,
stat state.ConstellationState, config *config.Config,
) (string, state.ConstellationState, error) {
if err := cl.SetState(stat); err != nil {
return "", state.ConstellationState{}, fmt.Errorf("setting state while creating service account: %w", err)
}
cl.SetState(stat)
input := gcpcl.ServiceAccountInput{
Roles: config.Provider.GCP.ServiceAccountRoles,
@ -80,27 +78,18 @@ func (c *ServiceAccountCreator) createServiceAccountGCP(ctx context.Context, cl
return "", state.ConstellationState{}, fmt.Errorf("creating service account: %w", err)
}
stat, err = cl.GetState()
if err != nil {
return "", state.ConstellationState{}, fmt.Errorf("getting state after creating service account: %w", err)
}
return serviceAccount, stat, nil
return serviceAccount, cl.GetState(), nil
}
func (c *ServiceAccountCreator) createServiceAccountAzure(ctx context.Context, cl azureclient,
stat state.ConstellationState, _ *config.Config,
) (string, state.ConstellationState, error) {
if err := cl.SetState(stat); err != nil {
return "", state.ConstellationState{}, fmt.Errorf("setting state while creating service account: %w", err)
}
cl.SetState(stat)
serviceAccount, err := cl.CreateServicePrincipal(ctx)
if err != nil {
return "", state.ConstellationState{}, fmt.Errorf("creating service account: %w", err)
}
stat, err = cl.GetState()
if err != nil {
return "", state.ConstellationState{}, fmt.Errorf("getting state after creating service account: %w", err)
}
return serviceAccount, stat, nil
return serviceAccount, cl.GetState(), nil
}

View file

@ -60,14 +60,6 @@ func TestServiceAccountCreator(t *testing.T) {
config: config.Default(),
wantErr: true,
},
"gcp client setState error": {
newGCPClient: func(ctx context.Context) (gcpclient, error) {
return &stubGcpClient{setStateErr: someErr}, nil
},
state: someGCPState(),
config: config.Default(),
wantErr: true,
},
"gcp client createServiceAccount error": {
newGCPClient: func(ctx context.Context) (gcpclient, error) {
return &stubGcpClient{createServiceAccountErr: someErr}, nil
@ -76,14 +68,6 @@ func TestServiceAccountCreator(t *testing.T) {
config: config.Default(),
wantErr: true,
},
"gcp client getState error": {
newGCPClient: func(ctx context.Context) (gcpclient, error) {
return &stubGcpClient{getStateErr: someErr}, nil
},
state: someGCPState(),
config: config.Default(),
wantErr: true,
},
"azure": {
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
return &fakeAzureClient{}, nil
@ -102,14 +86,6 @@ func TestServiceAccountCreator(t *testing.T) {
config: config.Default(),
wantErr: true,
},
"azure client setState error": {
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
return &stubAzureClient{setStateErr: someErr}, nil
},
state: someAzureState(),
config: config.Default(),
wantErr: true,
},
"azure client createServiceAccount error": {
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
return &stubAzureClient{createServicePrincipalErr: someErr}, nil
@ -118,14 +94,6 @@ func TestServiceAccountCreator(t *testing.T) {
config: config.Default(),
wantErr: true,
},
"azure client getState error": {
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
return &stubAzureClient{getStateErr: someErr}, nil
},
state: someAzureState(),
config: config.Default(),
wantErr: true,
},
"qemu": {
state: state.ConstellationState{CloudProvider: "qemu"},
wantStateMutator: func(cs *state.ConstellationState) {},

View file

@ -51,9 +51,7 @@ func (t *Terminator) Terminate(ctx context.Context, state state.ConstellationSta
}
func (t *Terminator) terminateGCP(ctx context.Context, cl gcpclient, state state.ConstellationState) error {
if err := cl.SetState(state); err != nil {
return err
}
cl.SetState(state)
if err := cl.TerminateLoadBalancer(ctx); err != nil {
return err
@ -71,9 +69,7 @@ func (t *Terminator) terminateGCP(ctx context.Context, cl gcpclient, state state
}
func (t *Terminator) terminateAzure(ctx context.Context, cl azureclient, state state.ConstellationState) error {
if err := cl.SetState(state); err != nil {
return err
}
cl.SetState(state)
if err := cl.TerminateServicePrincipal(ctx); err != nil {
return err

View file

@ -65,11 +65,6 @@ func TestTerminator(t *testing.T) {
state: someGCPState(),
wantErr: true,
},
"gcp setState error": {
gcpclient: &stubGcpClient{setStateErr: someErr},
state: someGCPState(),
wantErr: true,
},
"gcp terminateInstances error": {
gcpclient: &stubGcpClient{terminateInstancesErr: someErr},
state: someGCPState(),
@ -99,11 +94,6 @@ func TestTerminator(t *testing.T) {
state: someAzureState(),
wantErr: true,
},
"azure setState error": {
azureclient: &stubAzureClient{setStateErr: someErr},
state: someAzureState(),
wantErr: true,
},
"azure terminateServicePrincipal error": {
azureclient: &stubAzureClient{terminateServicePrincipalErr: someErr},
state: someAzureState(),