Skip to content

Commit

Permalink
Wire services to use actuator scope clients
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <saugustus@vmware.com>
  • Loading branch information
justaugustus committed Jan 23, 2019
1 parent a7aaa3d commit e2d0d3d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 101 deletions.
4 changes: 2 additions & 2 deletions pkg/cloud/azure/services/compute/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (

// DeleteManagedDisk deletes a managed disk resource.
func (s *Service) DeleteManagedDisk(resourceGroup string, name string) (compute.DisksDeleteFuture, error) {
return s.DisksClient.Delete(s.ctx, resourceGroup, name)
return s.scope.AzureClients.Disks.Delete(s.scope.Context, resourceGroup, name)
}

// WaitForDisksDeleteFuture returns when the DeleteManagedDisk operation completes.
func (s *Service) WaitForDisksDeleteFuture(future compute.DisksDeleteFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.DisksClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.Disks.Client)
}
30 changes: 11 additions & 19 deletions pkg/cloud/azure/services/compute/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -14,30 +17,19 @@ limitations under the License.
package compute

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute"
"github.com/Azure/go-autorest/autorest"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
)

// Service provides the azure SDK clients to interact with the compute API.
// Service holds a collection of interfaces.
// The interfaces are broken down like this to group functions together.
// One alternative is to have a large list of functions from the ec2 client.
type Service struct {
DisksClient compute.DisksClient
VirtualMachinesClient compute.VirtualMachinesClient
ctx context.Context
scope *actuators.Scope
}

// NewService returns a new instance of Service.
func NewService(subscriptionID string) *Service {
// NewService returns a new service given the api clients.
func NewService(scope *actuators.Scope) *Service {
return &Service{
DisksClient: compute.NewDisksClient(subscriptionID),
VirtualMachinesClient: compute.NewVirtualMachinesClient(subscriptionID),
ctx: context.Background(),
scope: scope,
}
}

// SetAuthorizer sets the authorizer components of the azure clients.
func (s *Service) SetAuthorizer(authorizer autorest.Authorizer) {
s.DisksClient.BaseClient.Client.Authorizer = authorizer
s.VirtualMachinesClient.BaseClient.Client.Authorizer = authorizer
}
10 changes: 5 additions & 5 deletions pkg/cloud/azure/services/compute/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (s *Service) RunCommand(resoureGroup string, name string, cmd string) (comp
CommandID: to.StringPtr("RunShellScript"),
Script: to.StringSlicePtr([]string{cmd}),
}
return s.VirtualMachinesClient.RunCommand(s.ctx, resoureGroup, name, cmdInput)
return s.scope.AzureClients.VM.RunCommand(s.scope.Context, resoureGroup, name, cmdInput)
}

// VMIfExists returns the reference to the VM object if it exists.
func (s *Service) VMIfExists(resourceGroup string, name string) (*compute.VirtualMachine, error) {
vm, err := s.VirtualMachinesClient.Get(s.ctx, resourceGroup, name, "")
vm, err := s.scope.AzureClients.VM.Get(s.scope.Context, resourceGroup, name, "")
if err != nil {
if aerr, ok := err.(autorest.DetailedError); ok {
if aerr.StatusCode.(int) == 404 {
Expand All @@ -44,15 +44,15 @@ func (s *Service) VMIfExists(resourceGroup string, name string) (*compute.Virtua

// DeleteVM deletes the virtual machine.
func (s *Service) DeleteVM(resourceGroup string, name string) (compute.VirtualMachinesDeleteFuture, error) {
return s.VirtualMachinesClient.Delete(s.ctx, resourceGroup, name)
return s.scope.AzureClients.VM.Delete(s.scope.Context, resourceGroup, name)
}

// WaitForVMRunCommandFuture returns when the RunCommand operation completes.
func (s *Service) WaitForVMRunCommandFuture(future compute.VirtualMachinesRunCommandFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.VirtualMachinesClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.VM.Client)
}

// WaitForVMDeletionFuture returns when the DeleteVM operation completes.
func (s *Service) WaitForVMDeletionFuture(future compute.VirtualMachinesDeleteFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.VirtualMachinesClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.VM.Client)
}
4 changes: 2 additions & 2 deletions pkg/cloud/azure/services/network/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/netwo

// DeleteNetworkInterface deletes the NIC resource.
func (s *Service) DeleteNetworkInterface(resourceGroup string, networkInterfaceName string) (network.InterfacesDeleteFuture, error) {
return s.InterfacesClient.Delete(s.ctx, resourceGroup, networkInterfaceName)
return s.scope.AzureClients.Interfaces.Delete(s.scope.Context, resourceGroup, networkInterfaceName)
}

// WaitForNetworkInterfacesDeleteFuture waits for the DeleteNetworkInterface operation to complete.
func (s *Service) WaitForNetworkInterfacesDeleteFuture(future network.InterfacesDeleteFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.InterfacesClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.Interfaces.Client)
}
6 changes: 3 additions & 3 deletions pkg/cloud/azure/services/network/publicipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (

// GetPublicIPAddress retrieves the Public IP address resource.
func (s *Service) GetPublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddress, error) {
return s.PublicIPAddressesClient.Get(s.ctx, resourceGroup, IPName, "")
return s.scope.AzureClients.PublicIPAddresses.Get(s.scope.Context, resourceGroup, IPName, "")
}

// DeletePublicIPAddress deletes the Public IP address resource.
func (s *Service) DeletePublicIPAddress(resourceGroup string, IPName string) (network.PublicIPAddressesDeleteFuture, error) {
return s.PublicIPAddressesClient.Delete(s.ctx, resourceGroup, IPName)
return s.scope.AzureClients.PublicIPAddresses.Delete(s.scope.Context, resourceGroup, IPName)
}

// WaitForPublicIPAddressDeleteFuture waits for the DeletePublicIPAddress operation to complete.
func (s *Service) WaitForPublicIPAddressDeleteFuture(future network.PublicIPAddressesDeleteFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.PublicIPAddressesClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.PublicIPAddresses.Client)
}
8 changes: 4 additions & 4 deletions pkg/cloud/azure/services/network/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

// NetworkSGIfExists returns the nsg reference if the nsg resource exists.
func (s *Service) NetworkSGIfExists(resourceGroupName string, networkSecurityGroupName string) (*network.SecurityGroup, error) {
networkSG, err := s.SecurityGroupsClient.Get(s.ctx, resourceGroupName, networkSecurityGroupName, "")
networkSG, err := s.scope.AzureClients.SecurityGroups.Get(s.scope.Context, resourceGroupName, networkSecurityGroupName, "")
if err != nil {
if aerr, ok := err.(autorest.DetailedError); ok {
if aerr.StatusCode.(int) == 404 {
Expand Down Expand Up @@ -78,7 +78,7 @@ func (s *Service) CreateOrUpdateNetworkSecurityGroup(resourceGroupName string, n
Location: to.StringPtr(location),
SecurityGroupPropertiesFormat: &securityGroupProperties,
}
sgFuture, err := s.SecurityGroupsClient.CreateOrUpdate(s.ctx, resourceGroupName, networkSecurityGroupName, securityGroup)
sgFuture, err := s.scope.AzureClients.SecurityGroups.CreateOrUpdate(s.scope.Context, resourceGroupName, networkSecurityGroupName, securityGroup)
if err != nil {
return nil, err
}
Expand All @@ -87,10 +87,10 @@ func (s *Service) CreateOrUpdateNetworkSecurityGroup(resourceGroupName string, n

// DeleteNetworkSecurityGroup deletes the nsg resource.
func (s *Service) DeleteNetworkSecurityGroup(resourceGroupName string, networkSecurityGroupName string) (network.SecurityGroupsDeleteFuture, error) {
return s.SecurityGroupsClient.Delete(s.ctx, resourceGroupName, networkSecurityGroupName)
return s.scope.AzureClients.SecurityGroups.Delete(s.scope.Context, resourceGroupName, networkSecurityGroupName)
}

// WaitForNetworkSGsCreateOrUpdateFuture returns when the CreateOrUpdateNetworkSecurityGroup operation completes.
func (s *Service) WaitForNetworkSGsCreateOrUpdateFuture(future network.SecurityGroupsCreateOrUpdateFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.SecurityGroupsClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.SecurityGroups.Client)
}
36 changes: 11 additions & 25 deletions pkg/cloud/azure/services/network/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -14,36 +17,19 @@ limitations under the License.
package network

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-11-01/network"
"github.com/Azure/go-autorest/autorest"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
)

// Service provides the instances of the azure network API clients.
// Service holds a collection of interfaces.
// The interfaces are broken down like this to group functions together.
// One alternative is to have a large list of functions from the ec2 client.
type Service struct {
InterfacesClient network.InterfacesClient
PublicIPAddressesClient network.PublicIPAddressesClient
SecurityGroupsClient network.SecurityGroupsClient
VirtualNetworksClient network.VirtualNetworksClient
ctx context.Context
scope *actuators.Scope
}

// NewService returns a new instance of Service.
func NewService(subscriptionID string) *Service {
// NewService returns a new service given the api clients.
func NewService(scope *actuators.Scope) *Service {
return &Service{
InterfacesClient: network.NewInterfacesClient(subscriptionID),
PublicIPAddressesClient: network.NewPublicIPAddressesClient(subscriptionID),
SecurityGroupsClient: network.NewSecurityGroupsClient(subscriptionID),
VirtualNetworksClient: network.NewVirtualNetworksClient(subscriptionID),
ctx: context.Background(),
scope: scope,
}
}

// SetAuthorizer populates the authorizer component of the network client objects.
func (s *Service) SetAuthorizer(authorizer autorest.Authorizer) {
s.InterfacesClient.BaseClient.Client.Authorizer = authorizer
s.PublicIPAddressesClient.BaseClient.Client.Authorizer = authorizer
s.SecurityGroupsClient.BaseClient.Client.Authorizer = authorizer
s.VirtualNetworksClient.BaseClient.Client.Authorizer = authorizer
}
4 changes: 2 additions & 2 deletions pkg/cloud/azure/services/network/virtualnetworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Service) CreateOrUpdateVnet(resourceGroupName string, virtualNetworkNam
Location: to.StringPtr(location),
VirtualNetworkPropertiesFormat: &virtualNetworkProperties,
}
sgFuture, err := s.VirtualNetworksClient.CreateOrUpdate(s.ctx, resourceGroupName, virtualNetworkName, virtualNetwork)
sgFuture, err := s.scope.AzureClients.VirtualNetworks.CreateOrUpdate(s.scope.Context, resourceGroupName, virtualNetworkName, virtualNetwork)
if err != nil {
return nil, err
}
Expand All @@ -57,5 +57,5 @@ func (s *Service) CreateOrUpdateVnet(resourceGroupName string, virtualNetworkNam

// WaitForVnetCreateOrUpdateFuture returns when the CreateOrUpdateVnet operation completes.
func (s *Service) WaitForVnetCreateOrUpdateFuture(future network.VirtualNetworksCreateOrUpdateFuture) error {
return future.Future.WaitForCompletionRef(s.ctx, s.VirtualNetworksClient.Client)
return future.Future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.VirtualNetworks.Client)
}
8 changes: 4 additions & 4 deletions pkg/cloud/azure/services/resources/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Service) CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterCo
},
}

deploymentFuture, err := s.DeploymentsClient.CreateOrUpdate(s.ctx, clusterConfig.ResourceGroup, machine.ObjectMeta.Name, deployment)
deploymentFuture, err := s.scope.AzureClients.Deployments.CreateOrUpdate(s.scope.Context, clusterConfig.ResourceGroup, machine.ObjectMeta.Name, deployment)
if err != nil {
return nil, err
}
Expand All @@ -75,7 +75,7 @@ func (s *Service) ValidateDeployment(machine *clusterv1.Machine, clusterConfig *
Mode: resources.Incremental, // Do not delete and re-create matching resources that already exist
},
}
res, err := s.DeploymentsClient.Validate(s.ctx, clusterConfig.ResourceGroup, machine.ObjectMeta.Name, deployment)
res, err := s.scope.AzureClients.Deployments.Validate(s.scope.Context, clusterConfig.ResourceGroup, machine.ObjectMeta.Name, deployment)
if res.Error != nil {
return errors.New(*res.Error.Message)
}
Expand All @@ -84,12 +84,12 @@ func (s *Service) ValidateDeployment(machine *clusterv1.Machine, clusterConfig *

// GetDeploymentResult retrieves the result of the ARM deployment operation.
func (s *Service) GetDeploymentResult(future resources.DeploymentsCreateOrUpdateFuture) (de resources.DeploymentExtended, err error) {
return future.Result(s.DeploymentsClient)
return future.Result(s.scope.AzureClients.Deployments)
}

// WaitForDeploymentsCreateOrUpdateFuture returns when the ARM operation completes.
func (s *Service) WaitForDeploymentsCreateOrUpdateFuture(future resources.DeploymentsCreateOrUpdateFuture) error {
return future.WaitForCompletionRef(s.ctx, s.DeploymentsClient.Client)
return future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.Deployments.Client)
}

func convertMachineToDeploymentParams(machine *clusterv1.Machine, machineConfig *azureconfigv1.AzureMachineProviderSpec) (*map[string]interface{}, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cloud/azure/services/resources/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import (

// CreateOrUpdateGroup creates or updates an azure resource group.
func (s *Service) CreateOrUpdateGroup(resourceGroupName string, location string) (resources.Group, error) {
return s.GroupsClient.CreateOrUpdate(s.ctx, resourceGroupName, resources.Group{Location: to.StringPtr(location)})
return s.scope.AzureClients.Groups.CreateOrUpdate(s.scope.Context, resourceGroupName, resources.Group{Location: to.StringPtr(location)})
}

// DeleteGroup deletes an azure resource group.
func (s *Service) DeleteGroup(resourceGroupName string) (resources.GroupsDeleteFuture, error) {
return s.GroupsClient.Delete(s.ctx, resourceGroupName)
return s.scope.AzureClients.Groups.Delete(s.scope.Context, resourceGroupName)
}

// CheckGroupExistence checks oif the resource group exists or not.
func (s *Service) CheckGroupExistence(resourceGroupName string) (autorest.Response, error) {
return s.GroupsClient.CheckExistence(s.ctx, resourceGroupName)
return s.scope.AzureClients.Groups.CheckExistence(s.scope.Context, resourceGroupName)
}

// WaitForGroupsDeleteFuture returns when the DeleteGroup operation completes.
func (s *Service) WaitForGroupsDeleteFuture(future resources.GroupsDeleteFuture) error {
return future.WaitForCompletionRef(s.ctx, s.GroupsClient.Client)
return future.WaitForCompletionRef(s.scope.Context, s.scope.AzureClients.Groups.Client)
}
42 changes: 11 additions & 31 deletions pkg/cloud/azure/services/resources/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -14,42 +17,19 @@ limitations under the License.
package resources

import (
"context"
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
)

// Service implements the AzureResourceManagementClient interface.
// Service holds a collection of interfaces.
// The interfaces are broken down like this to group functions together.
// One alternative is to have a large list of functions from the ec2 client.
type Service struct {
DeploymentsClient resources.DeploymentsClient
GroupsClient resources.GroupsClient
ctx context.Context
scope *actuators.Scope
}

// NewService returns a new instance of Service.
func NewService(subscriptionID string) *Service {
// NewService returns a new service given the api clients.
func NewService(scope *actuators.Scope) *Service {
return &Service{
DeploymentsClient: resources.NewDeploymentsClient(subscriptionID),
GroupsClient: resources.NewGroupsClient(subscriptionID),
ctx: context.Background(),
}
}

// SetAuthorizer sets the authorizer component of the azure clients.
func (s *Service) SetAuthorizer(authorizer autorest.Authorizer) {
s.DeploymentsClient.BaseClient.Client.Authorizer = authorizer
s.GroupsClient.BaseClient.Client.Authorizer = authorizer
}

// ResourceName extracts the name of the resource from its ID.
func ResourceName(id string) (string, error) {
parts := strings.Split(id, "/")
name := parts[len(parts)-1]
if len(name) == 0 {
return "", fmt.Errorf("identifier did not contain resource name")
scope: scope,
}
return name, nil
}

0 comments on commit e2d0d3d

Please sign in to comment.