Skip to content

Commit

Permalink
cleanup disks service
Browse files Browse the repository at this point in the history
  • Loading branch information
shysank committed Oct 28, 2020
1 parent 7aae523 commit e628a69
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 76 deletions.
14 changes: 7 additions & 7 deletions cloud/services/disks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ import (
)

// Client wraps go-sdk
type Client interface {
type client interface {
Delete(context.Context, string, string) error
}

// AzureClient contains the Azure go-sdk Client
type AzureClient struct {
type azureClient struct {
disks compute.DisksClient
}

var _ Client = &AzureClient{}
var _ client = &azureClient{}

// NewClient creates a new VM client from subscription ID.
func NewClient(auth azure.Authorizer) *AzureClient {
// newClient creates a new VM client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := newDisksClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
return &AzureClient{c}
return &azureClient{c}
}

// newDisksClient creates a new disks client from subscription ID.
Expand All @@ -51,7 +51,7 @@ func newDisksClient(subscriptionID string, baseURI string, authorizer autorest.A
}

// Delete removes the disk client
func (ac *AzureClient) Delete(ctx context.Context, resourceGroupName, name string) error {
func (ac *azureClient) Delete(ctx context.Context, resourceGroupName, name string) error {
future, err := ac.disks.Delete(ctx, resourceGroupName, name)
if err != nil {
return err
Expand Down
28 changes: 25 additions & 3 deletions cloud/services/disks/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,33 @@ package disks
import (
"context"

azure "sigs.k8s.io/cluster-api-provider-azure/cloud"

"github.com/go-logr/logr"
"github.com/pkg/errors"

azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
)

// DiskScope defines the scope interface for a disk service.
type DiskScope interface {
logr.Logger
azure.ClusterDescriber
DiskSpecs() []azure.DiskSpec
}

// Service provides operations on azure resources
type Service struct {
Scope DiskScope
client
}

// New creates a new disks service.
func New(scope DiskScope) *Service {
return &Service{
Scope: scope,
client: newClient(scope),
}
}

// Reconcile on disk is currently no-op. OS disks should only be deleted and will create with the VM automatically.
func (s *Service) Reconcile(ctx context.Context) error {
return nil
Expand All @@ -33,7 +55,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
func (s *Service) Delete(ctx context.Context) error {
for _, diskSpec := range s.Scope.DiskSpecs() {
s.Scope.V(2).Info("deleting disk", "disk", diskSpec.Name)
err := s.Client.Delete(ctx, s.Scope.ResourceGroup(), diskSpec.Name)
err := s.client.Delete(ctx, s.Scope.ResourceGroup(), diskSpec.Name)
if err != nil && azure.ResourceNotFound(err) {
// already deleted
continue
Expand Down
12 changes: 6 additions & 6 deletions cloud/services/disks/disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func TestDeleteDisk(t *testing.T) {
testcases := []struct {
name string
expectedError string
expect func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockClientMockRecorder)
expect func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockclientMockRecorder)
}{
{
name: "delete the disk",
expectedError: "",
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockClientMockRecorder) {
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockclientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.DiskSpecs().Return([]azure.DiskSpec{
{
Expand All @@ -64,7 +64,7 @@ func TestDeleteDisk(t *testing.T) {
{
name: "disk already deleted",
expectedError: "",
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockClientMockRecorder) {
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockclientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.DiskSpecs().Return([]azure.DiskSpec{
{
Expand All @@ -82,7 +82,7 @@ func TestDeleteDisk(t *testing.T) {
{
name: "error while trying to delete the disk",
expectedError: "failed to delete disk my-disk-1 in resource group my-rg: #: Internal Server Error: StatusCode=500",
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockClientMockRecorder) {
expect: func(s *mock_disks.MockDiskScopeMockRecorder, m *mock_disks.MockclientMockRecorder) {
s.V(gomock.AssignableToTypeOf(2)).AnyTimes().Return(klogr.New())
s.DiskSpecs().Return([]azure.DiskSpec{
{
Expand All @@ -107,13 +107,13 @@ func TestDeleteDisk(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
scopeMock := mock_disks.NewMockDiskScope(mockCtrl)
clientMock := mock_disks.NewMockClient(mockCtrl)
clientMock := mock_disks.NewMockclient(mockCtrl)

tc.expect(scopeMock.EXPECT(), clientMock.EXPECT())

s := &Service{
Scope: scopeMock,
Client: clientMock,
client: clientMock,
}

err := s.Delete(context.TODO())
Expand Down
28 changes: 14 additions & 14 deletions cloud/services/disks/mock_disks/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cloud/services/disks/mock_disks/disks_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cloud/services/disks/mock_disks/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

// Run go generate to regenerate this mock.
//go:generate ../../../../hack/tools/bin/mockgen -destination client_mock.go -package mock_disks -source ../client.go Client
//go:generate ../../../../hack/tools/bin/mockgen -destination disks_mock.go -package mock_disks -source ../service.go DiskScope
//go:generate ../../../../hack/tools/bin/mockgen -destination disks_mock.go -package mock_disks -source ../disks.go DiskScope
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go"
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt disks_mock.go > _disks_mock.go && mv _disks_mock.go disks_mock.go"
package mock_disks //nolint
43 changes: 0 additions & 43 deletions cloud/services/disks/service.go

This file was deleted.

2 changes: 1 addition & 1 deletion controllers/azuremachine_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newAzureMachineService(machineScope *scope.MachineScope, clusterScope *scop
networkInterfacesSvc: networkinterfaces.NewService(machineScope, cache),
virtualMachinesSvc: virtualmachines.NewService(machineScope, cache),
roleAssignmentsSvc: roleassignments.NewService(machineScope),
disksSvc: disks.NewService(machineScope),
disksSvc: disks.New(machineScope),
publicIPsSvc: publicips.NewService(machineScope),
tagsSvc: tags.NewService(machineScope),
skuCache: cache,
Expand Down

0 comments on commit e628a69

Please sign in to comment.