diff --git a/pkg/portal/cluster/fetcher.go b/pkg/portal/cluster/fetcher.go index 1a6824d71..7a5cc76e5 100644 --- a/pkg/portal/cluster/fetcher.go +++ b/pkg/portal/cluster/fetcher.go @@ -22,8 +22,11 @@ import ( "github.com/Azure/ARO-RP/pkg/util/stringutils" ) -type ResourceFactory interface { +type ResourceClientFactory interface { NewResourcesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) features.ResourcesClient +} + +type VirtualMachinesClientFactory interface { NewVirtualMachinesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) compute.VirtualMachinesClient } @@ -53,12 +56,13 @@ type client struct { // contains Kubernetes clients and returns the frontend-suitable data // structures. The concrete implementation of FetchClient wraps this. type realFetcher struct { - log *logrus.Entry - configCli configclient.Interface - kubernetesCli kubernetes.Interface - machineClient machineclient.Interface - azureSideFetcher azureSideFetcher - resourceFactory ResourceFactory + log *logrus.Entry + configCli configclient.Interface + kubernetesCli kubernetes.Interface + machineClient machineclient.Interface + azureSideFetcher azureSideFetcher + resourceClientFactory ResourceClientFactory + virtualMachinesClientFactory VirtualMachinesClientFactory } type azureSideFetcher struct { @@ -67,13 +71,13 @@ type azureSideFetcher struct { env env.Interface } -type resourceFactory struct{} +type clientFactory struct{} -func (rf resourceFactory) NewResourcesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) features.ResourcesClient { +func (cf clientFactory) NewResourcesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) features.ResourcesClient { return features.NewResourcesClient(environment, subscriptionID, authorizer) } -func (rf resourceFactory) NewVirtualMachinesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) compute.VirtualMachinesClient { +func (cf clientFactory) NewVirtualMachinesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) compute.VirtualMachinesClient { return compute.NewVirtualMachinesClient(environment, subscriptionID, authorizer) } @@ -85,7 +89,7 @@ func newAzureSideFetcher(resourceGroupName string, subscriptionDoc *api.Subscrip } } -func newRealFetcher(log *logrus.Entry, dialer proxy.Dialer, doc *api.OpenShiftClusterDocument, azureSideFetcher azureSideFetcher, resourceFactory ResourceFactory) (*realFetcher, error) { +func newRealFetcher(log *logrus.Entry, dialer proxy.Dialer, doc *api.OpenShiftClusterDocument, azureSideFetcher azureSideFetcher, resourceClientFactory ResourceClientFactory, virtualMachinesClientFactory VirtualMachinesClientFactory) (*realFetcher, error) { restConfig, err := restconfig.RestConfig(dialer, doc.OpenShiftCluster) if err != nil { log.Error(err) @@ -110,20 +114,21 @@ func newRealFetcher(log *logrus.Entry, dialer proxy.Dialer, doc *api.OpenShiftCl } return &realFetcher{ - log: log, - configCli: configCli, - kubernetesCli: kubernetesCli, - machineClient: machineClient, - azureSideFetcher: azureSideFetcher, - resourceFactory: resourceFactory, + log: log, + configCli: configCli, + kubernetesCli: kubernetesCli, + machineClient: machineClient, + azureSideFetcher: azureSideFetcher, + resourceClientFactory: resourceClientFactory, + virtualMachinesClientFactory: virtualMachinesClientFactory, }, nil } func NewFetchClient(log *logrus.Entry, dialer proxy.Dialer, cluster *api.OpenShiftClusterDocument, subscriptionDoc *api.SubscriptionDocument, env env.Interface) (FetchClient, error) { resourceGroupName := stringutils.LastTokenByte(cluster.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/') azureSideFetcher := newAzureSideFetcher(resourceGroupName, subscriptionDoc, env) - rf := resourceFactory{} - fetcher, err := newRealFetcher(log, dialer, cluster, azureSideFetcher, rf) + cf := clientFactory{} + fetcher, err := newRealFetcher(log, dialer, cluster, azureSideFetcher, cf, cf) if err != nil { return nil, err } diff --git a/pkg/portal/cluster/machines.go b/pkg/portal/cluster/machines.go index caf282358..4153e0a2b 100644 --- a/pkg/portal/cluster/machines.go +++ b/pkg/portal/cluster/machines.go @@ -78,12 +78,12 @@ func (f *realFetcher) VMAllocationStatus(ctx context.Context) (map[string]string return nil, err } // Getting Virtual Machine resources through the Cluster's Resource Group - computeResources, err := f.resourceFactory.NewResourcesClient(env.Environment(), subscriptionDoc.ID, fpAuth).ListByResourceGroup(ctx, clusterRGName, "resourceType eq 'Microsoft.Compute/virtualMachines'", "", nil) + computeResources, err := f.resourceClientFactory.NewResourcesClient(env.Environment(), subscriptionDoc.ID, fpAuth).ListByResourceGroup(ctx, clusterRGName, "resourceType eq 'Microsoft.Compute/virtualMachines'", "", nil) if err != nil { return nil, err } vmAllocationStatus := make(map[string]string) - virtualMachineClient := f.resourceFactory.NewVirtualMachinesClient(env.Environment(), subscriptionDoc.ID, fpAuth) + virtualMachineClient := f.virtualMachinesClientFactory.NewVirtualMachinesClient(env.Environment(), subscriptionDoc.ID, fpAuth) for _, res := range computeResources { putAllocationStatusToMap(ctx, clusterRGName, vmAllocationStatus, res, virtualMachineClient, f.log) } diff --git a/pkg/portal/cluster/machines_test.go b/pkg/portal/cluster/machines_test.go index 2143fe34b..414af41f6 100644 --- a/pkg/portal/cluster/machines_test.go +++ b/pkg/portal/cluster/machines_test.go @@ -112,21 +112,21 @@ func TestMachines(t *testing.T) { } } -type mockResourceFactory struct { +type MockClientFactory struct { mockResourcesClient *mock_features.MockResourcesClient mockVirtualMachinesClient *mock_compute.MockVirtualMachinesClient } -func newMockResourceFactory(mockResourcesClient *mock_features.MockResourcesClient, mockVirtualMachinesClient *mock_compute.MockVirtualMachinesClient) mockResourceFactory { - return mockResourceFactory{mockResourcesClient: mockResourcesClient, mockVirtualMachinesClient: mockVirtualMachinesClient} +func newMockClientFactory(mockResourcesClient *mock_features.MockResourcesClient, mockVirtualMachinesClient *mock_compute.MockVirtualMachinesClient) MockClientFactory { + return MockClientFactory{mockResourcesClient: mockResourcesClient, mockVirtualMachinesClient: mockVirtualMachinesClient} } -func (mrf mockResourceFactory) NewResourcesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) features.ResourcesClient { - return mrf.mockResourcesClient +func (mcf MockClientFactory) NewResourcesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) features.ResourcesClient { + return mcf.mockResourcesClient } -func (mrf mockResourceFactory) NewVirtualMachinesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) compute.VirtualMachinesClient { - return mrf.mockVirtualMachinesClient +func (mcf MockClientFactory) NewVirtualMachinesClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) compute.VirtualMachinesClient { + return mcf.mockVirtualMachinesClient } func TestVMAllocationStatus(t *testing.T) { @@ -248,11 +248,12 @@ func TestVMAllocationStatus(t *testing.T) { env: mockEnv, subscriptionDoc: subscriptionDoc, } - mrf := newMockResourceFactory(mockResourcesClient, mockVirtualMachinesClient) + mcf := newMockClientFactory(mockResourcesClient, mockVirtualMachinesClient) realFetcher := &realFetcher{ - log: log, - azureSideFetcher: azureSideFetcher, - resourceFactory: mrf, + log: log, + azureSideFetcher: azureSideFetcher, + resourceClientFactory: mcf, + virtualMachinesClientFactory: mcf, } client := &client{fetcher: realFetcher, log: log} _, err := client.VMAllocationStatus(ctx)