Skip to content

Commit

Permalink
Updating DeploymentsClient and DeploymentOperationsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ytimocin committed Dec 22, 2022
1 parent 5795f05 commit 0aff153
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 106 deletions.
102 changes: 27 additions & 75 deletions pkg/azure/clientv2/resourcedeploymentclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (
"net/url"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
armruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
Expand Down Expand Up @@ -88,126 +85,81 @@ type ProviderConfig struct {
// DeploymentsClient is a deployments client for Azure Resource Manager.
// It is used by both Azure and UCP clients.
type DeploymentsClient struct {
*armresources.DeploymentsClient
Pipeline *runtime.Pipeline
BaseURI string
client *armresources.DeploymentsClient
pipeline *runtime.Pipeline
baseURI string
}

// NewDeploymentsClient creates an instance of the DeploymentsClient using the default endpoint.
func NewDeploymentsClient(cred azcore.TokenCredential, subscriptionID string) (*DeploymentsClient, error) {
client, err := NewDeploymentsClientWithBaseURI(cred, subscriptionID, DefaultBaseURI)
if err != nil {
return nil, err
// NewDeploymentsClient creates an instance of the DeploymentsClient.
func NewDeploymentsClient(subscriptionID string, options *Options) (*DeploymentsClient, error) {
baseURI := DefaultBaseURI
if options.BaseURI != "" {
baseURI = options.BaseURI
}

return client, err
}

// NewDeploymentsClientWithBaseURI creates an instance of the DeploymentsClient using a custom endpoint.
// Use this when interacting with UCP or Azure resources that uses a non-standard base URI.
func NewDeploymentsClientWithBaseURI(credential azcore.TokenCredential, subscriptionID string, baseURI string) (*DeploymentsClient, error) {
options := &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloud.Configuration{
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
cloud.ResourceManager: {
Audience: "https://management.core.windows.net",
Endpoint: baseURI,
},
},
},
},
}
client, err := armresources.NewDeploymentsClient(subscriptionID, credential, options)
client, err := armresources.NewDeploymentsClient(subscriptionID, options.Cred, defaultClientOptions)
if err != nil {
return nil, err
}

pipeline, err := armruntime.NewPipeline(ModuleName, ModuleVersion, credential, runtime.PipelineOptions{}, options)
pipeline, err := armruntime.NewPipeline(ModuleName, ModuleVersion, options.Cred, runtime.PipelineOptions{}, defaultClientOptions)
if err != nil {
return nil, err
}

return &DeploymentsClient{
DeploymentsClient: client,
Pipeline: &pipeline,
BaseURI: baseURI,
client: client,
pipeline: &pipeline,
baseURI: baseURI,
}, nil
}

type ClientBeginCreateOrUpdateOptions struct {
resourceID string
apiVersion string
}

func NewClientBeginCreateOrUpdateOptions(resourceID, apiVersion string) *ClientBeginCreateOrUpdateOptions {
_, err := resources.ParseResource(resourceID)
if err != nil {
return nil
}

return &ClientBeginCreateOrUpdateOptions{
resourceID: resourceID,
apiVersion: apiVersion,
}
}

// ClientCreateOrUpdateResponse contains the response from method Client.CreateOrUpdate.
type ClientCreateOrUpdateResponse struct {
armresources.GenericResource
}

// CreateOrUpdate creates a deployment or updates the existing deployment.
func (client *DeploymentsClient) BeginCreateOrUpdate(ctx context.Context, parameters Deployment, options *ClientBeginCreateOrUpdateOptions) (*runtime.Poller[ClientCreateOrUpdateResponse], error) {
// TODO: resourceID needs to be parsed to see if it is valid
if !strings.HasPrefix(options.resourceID, "/") {
func (client *DeploymentsClient) CreateOrUpdate(ctx context.Context, parameters Deployment, resourceID, apiVersion string) (*runtime.Poller[ClientCreateOrUpdateResponse], error) {
if !strings.HasPrefix(resourceID, "/") {
return nil, fmt.Errorf("error creating or updating a deployment: resourceID must start with a slash")
}

_, err := resources.ParseResource(options.resourceID)
_, err := resources.ParseResource(resourceID)
if err != nil {
return nil, fmt.Errorf("invalid resourceID: %v", options.resourceID)
return nil, fmt.Errorf("invalid resourceID: %v", resourceID)
}

resp, err := client.createOrUpdate(ctx, parameters, options)
req, err := client.createOrUpdateCreateRequest(ctx, resourceID, apiVersion, parameters)
if err != nil {
return nil, err
}
return runtime.NewPoller[ClientCreateOrUpdateResponse](resp, *client.Pipeline, nil)
}

// CreateOrUpdate - Creates a resource.
// If the operation fails it returns an *azcore.ResponseError type.
// Generated from API version 2021-04-01
func (client *DeploymentsClient) createOrUpdate(ctx context.Context, parameters Deployment, options *ClientBeginCreateOrUpdateOptions) (*http.Response, error) {
req, err := client.createOrUpdateCreateRequest(ctx, parameters, options)
if err != nil {
return nil, err
}
resp, err := client.Pipeline.Do(req)
resp, err := client.pipeline.Do(req)
if err != nil {
return nil, err
}
if !runtime.HasStatusCode(resp, http.StatusOK, http.StatusCreated, http.StatusAccepted) {
return nil, runtime.NewResponseError(resp)
}
return resp, nil

return runtime.NewPoller[ClientCreateOrUpdateResponse](resp, *client.pipeline, nil)
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *DeploymentsClient) createOrUpdateCreateRequest(ctx context.Context, parameters Deployment, options *ClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
urlPath := "/{resourceID}"
if options.resourceID == "" {
func (client *DeploymentsClient) createOrUpdateCreateRequest(ctx context.Context, resourceID, apiVersion string, parameters Deployment) (*policy.Request, error) {
urlPath := "{resourceID}"
if resourceID == "" {
return nil, errors.New("resourceID cannot be empty")
}
urlPath = strings.ReplaceAll(urlPath, "{resourceID}", url.PathEscape(options.resourceID))
urlPath = strings.ReplaceAll(urlPath, "{resourceID}", url.PathEscape(resourceID))

req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.BaseURI, urlPath))
req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.baseURI, urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", options.apiVersion)
reqQP.Set("api-version", apiVersion)
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
return req, runtime.MarshalAsJSON(req, parameters)
Expand Down
54 changes: 23 additions & 31 deletions pkg/azure/clientv2/resourcedeploymentoperationsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,55 @@ package clientv2
import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
armruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)

// ResourceDeploymentOperationsClient is an operations client which takes in a resourceID as the destination to query.
// DeploymentOperationsClient is an operations client which takes in a resourceID as the destination to query.
// It is used by both Azure and UCP clients.
type ResourceDeploymentOperationsClient struct {
armresources.DeploymentOperationsClient
type DeploymentOperationsClient struct {
client *armresources.DeploymentOperationsClient
pipeline *runtime.Pipeline
baseURI string
}

// NewResourceDeploymentOperationsClient creates an instance of the ResourceDeploymentOperations client using the default endpoint.
func NewResourceDeploymentOperationsClient(cred azcore.TokenCredential, subscriptionID string) (*ResourceDeploymentOperationsClient, error) {
client, err := NewResourceDeploymentOperationsClientWithBaseURI(cred, subscriptionID, DefaultBaseURI)
// NewDeploymentsClient creates an instance of the DeploymentsClient.
func NewDeploymentOperationsClient(subscriptionID string, options *Options) (*DeploymentOperationsClient, error) {
baseURI := DefaultBaseURI
if options.BaseURI != "" {
baseURI = options.BaseURI
}

client, err := armresources.NewDeploymentOperationsClient(subscriptionID, options.Cred, defaultClientOptions)
if err != nil {
return nil, err
}

return client, err
}

// NewResourceDeploymentOperationsClientWithBaseURI creates an instance of the ResourceDeploymentOperations client using a custom endpoint.
// Use this when interacting with UCP resources that uses a non-standard base URI.
func NewResourceDeploymentOperationsClientWithBaseURI(cred azcore.TokenCredential, subscriptionID string, baseURI string) (*ResourceDeploymentOperationsClient, error) {
options := &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloud.Configuration{
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
cloud.ResourceManager: {
Audience: "https://management.core.windows.net",
Endpoint: baseURI,
},
},
},
},
}
client, err := armresources.NewDeploymentOperationsClient(subscriptionID, cred, options)
pipeline, err := armruntime.NewPipeline(ModuleName, ModuleVersion, options.Cred, runtime.PipelineOptions{}, defaultClientOptions)
if err != nil {
return nil, err
}

return &ResourceDeploymentOperationsClient{*client}, nil
return &DeploymentOperationsClient{
client: client,
pipeline: &pipeline,
baseURI: baseURI,
}, nil
}

// List gets all deployments operations for a deployment.
// Parameters:
// resourceId - the resourceId to deploy to. NOTE, must start with a '/'. Ex: "/resourcegroups/{resourceGroupName}/deployments/{deploymentName}/operations
// top - the number of results to return.
func (client *ResourceDeploymentOperationsClient) List(ctx context.Context, resourceGroupName string, deploymentName string, resourceID string, top *int32) (*armresources.DeploymentOperationsListResult, error) {
func (client *DeploymentOperationsClient) List(ctx context.Context, resourceGroupName string, deploymentName string, resourceID string, top *int32) (*armresources.DeploymentOperationsListResult, error) {
result := &armresources.DeploymentOperationsListResult{
Value: make([]*armresources.DeploymentOperation, 0),
NextLink: to.Ptr(""),
}
// TODO: Validate resourceID
pager := client.NewListPager(resourceGroupName, deploymentName,
pager := client.client.NewListPager(resourceGroupName, deploymentName,
&armresources.DeploymentOperationsClientListOptions{
Top: top,
})
Expand Down

0 comments on commit 0aff153

Please sign in to comment.