package arm // Copyright (c) Microsoft Corporation. // Licensed under the Apache License 2.0. import ( "context" "encoding/json" "net/http" mgmtfeatures "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/features" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/azure" "github.com/sirupsen/logrus" "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features" "github.com/Azure/ARO-RP/pkg/util/azureerrors" ) func DeployTemplate(ctx context.Context, log *logrus.Entry, deployments features.DeploymentsClient, resourceGroupName string, deploymentName string, template *Template, parameters map[string]interface{}) error { log.Printf("deploying %s template", deploymentName) err := deployments.CreateOrUpdateAndWait(ctx, resourceGroupName, deploymentName, mgmtfeatures.Deployment{ Properties: &mgmtfeatures.DeploymentProperties{ Template: template, Parameters: parameters, Mode: mgmtfeatures.Incremental, }, }) if azureerrors.IsDeploymentActiveError(err) { log.Printf("waiting for %s template to be deployed", deploymentName) err = deployments.Wait(ctx, resourceGroupName, deploymentName) } if azureerrors.HasAuthorizationFailedError(err) || azureerrors.HasLinkedAuthorizationFailedError(err) || azureerrors.IsDeploymentMissingPermissionsError(err) { return err } serviceErr, _ := err.(*azure.ServiceError) // futures return *azure.ServiceError directly // CreateOrUpdate() returns a wrapped *azure.ServiceError if detailedErr, ok := err.(autorest.DetailedError); ok { serviceErr, _ = detailedErr.Original.(*azure.ServiceError) } if serviceErr != nil { b, _ := json.Marshal(serviceErr) return &api.CloudError{ StatusCode: http.StatusBadRequest, CloudErrorBody: &api.CloudErrorBody{ Code: api.CloudErrorCodeDeploymentFailed, Message: "Deployment failed.", Details: []api.CloudErrorBody{ { Message: string(b), }, }, }, } } return err }