Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_resource_deployment_script #19436

Merged
merged 5 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2015-12-01/features" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DeploymentsClient *resources.DeploymentsClient
DeploymentScriptsClient *deploymentscripts.DeploymentScriptsClient
FeaturesClient *features.Client
GroupsClient *resources.GroupsClient
LocksClient *locks.ManagementLocksClient
Expand All @@ -27,6 +29,9 @@ func NewClient(o *common.ClientOptions) *Client {
deploymentsClient := resources.NewDeploymentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&deploymentsClient.Client, o.ResourceManagerAuthorizer)

deploymentScriptsClient := deploymentscripts.NewDeploymentScriptsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&deploymentScriptsClient.Client, o.ResourceManagerAuthorizer)

featuresClient := features.NewClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&featuresClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -56,6 +61,7 @@ func NewClient(o *common.ClientOptions) *Client {
return &Client{
GroupsClient: &groupsClient,
DeploymentsClient: &deploymentsClient,
DeploymentScriptsClient: &deploymentScriptsClient,
FeaturesClient: &featuresClient,
LocksClient: &locksClient,
ProvidersClient: &providersClient,
Expand Down
2 changes: 2 additions & 0 deletions internal/services/resource/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ResourceProviderRegistrationResource{},
ResourceDeploymentScriptAzurePowerShellResource{},
ResourceDeploymentScriptAzureCliResource{},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package resource

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type ResourceDeploymentScriptAzureCliModel ResourceDeploymentScriptModel

type ResourceDeploymentScriptAzureCliResource struct{}

var _ sdk.ResourceWithUpdate = ResourceDeploymentScriptAzureCliResource{}

func (r ResourceDeploymentScriptAzureCliResource) ResourceType() string {
return "azurerm_resource_deployment_script_azure_cli"
}

func (r ResourceDeploymentScriptAzureCliResource) ModelObject() interface{} {
return &ResourceDeploymentScriptAzureCliModel{}
}

func (r ResourceDeploymentScriptAzureCliResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return deploymentscripts.ValidateDeploymentScriptID
}

func (r ResourceDeploymentScriptAzureCliResource) Arguments() map[string]*pluginsdk.Schema {
return getDeploymentScriptArguments(AzureCliKind)
}

func (r ResourceDeploymentScriptAzureCliResource) Attributes() map[string]*pluginsdk.Schema {
return getDeploymentScriptAttributes()
}

func (r ResourceDeploymentScriptAzureCliResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ResourceDeploymentScriptAzureCliModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Resource.DeploymentScriptsClient
subscriptionId := metadata.Client.Account.SubscriptionId
id := deploymentscripts.NewDeploymentScriptID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}

if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

properties := &deploymentscripts.AzureCliScript{
Location: location.Normalize(model.Location),
Properties: deploymentscripts.AzureCliScriptProperties{
AzCliVersion: model.Version,
CleanupPreference: &model.CleanupPreference,
RetentionInterval: model.RetentionInterval,
SupportingScriptUris: &model.SupportingScriptUris,
ContainerSettings: expandContainerConfigurationModel(model.ContainerSettings),
EnvironmentVariables: expandEnvironmentVariableModelArray(model.EnvironmentVariables),
StorageAccountSettings: expandStorageAccountConfigurationModel(model.StorageAccountSettings),
},
}

identityValue, err := expandManagedServiceIdentityModel(metadata.ResourceData.Get("identity").([]interface{}))
if err != nil {
return err
}

properties.Identity = identityValue

if model.Arguments != "" {
properties.Properties.Arguments = &model.Arguments
}

if model.ForceUpdateTag != "" {
properties.Properties.ForceUpdateTag = &model.ForceUpdateTag
}

if model.PrimaryScriptUri != "" {
properties.Properties.PrimaryScriptUri = &model.PrimaryScriptUri
}

if model.ScriptContent != "" {
properties.Properties.ScriptContent = &model.ScriptContent
}

if model.Timeout != "" {
properties.Properties.Timeout = &model.Timeout
}

if model.Tags != nil {
properties.Tags = &model.Tags
}

if err := client.CreateThenPoll(ctx, id, *properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r ResourceDeploymentScriptAzureCliResource) Update() sdk.ResourceFunc {
return updateDeploymentScript()
}

func (r ResourceDeploymentScriptAzureCliResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.DeploymentScriptsClient

id, err := deploymentscripts.ParseDeploymentScriptID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

model, ok := (*resp.Model).(deploymentscripts.AzureCliScript)
if !ok {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state := ResourceDeploymentScriptAzureCliModel{
Name: id.ScriptName,
ResourceGroupName: id.ResourceGroupName,
Location: location.Normalize(model.Location),
}

identityValue, err := flattenManagedServiceIdentityModel(model.Identity)
if err != nil {
return err
}

if err := metadata.ResourceData.Set("identity", identityValue); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

properties := &model.Properties
if properties.Arguments != nil {
state.Arguments = *properties.Arguments
}

state.Version = properties.AzCliVersion

if properties.CleanupPreference != nil {
state.CleanupPreference = *properties.CleanupPreference
}

state.ContainerSettings = flattenContainerConfigurationModel(properties.ContainerSettings)

var originalModel ResourceDeploymentScriptAzureCliModel
if err := metadata.Decode(&originalModel); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

state.EnvironmentVariables = flattenEnvironmentVariableModelArray(properties.EnvironmentVariables, originalModel.EnvironmentVariables)

if properties.ForceUpdateTag != nil {
state.ForceUpdateTag = *properties.ForceUpdateTag
}

if properties.Outputs != nil && *properties.Outputs != nil {
outputsValue, err := json.Marshal(*properties.Outputs)
if err != nil {
return err
}

state.Outputs = string(outputsValue)
}

if properties.PrimaryScriptUri != nil {
state.PrimaryScriptUri = *properties.PrimaryScriptUri
}

state.RetentionInterval = properties.RetentionInterval

if properties.ScriptContent != nil {
state.ScriptContent = *properties.ScriptContent
}

state.StorageAccountSettings = flattenStorageAccountConfigurationModel(properties.StorageAccountSettings, originalModel.StorageAccountSettings)

if properties.SupportingScriptUris != nil {
state.SupportingScriptUris = *properties.SupportingScriptUris
}

if properties.Timeout != nil {
state.Timeout = *properties.Timeout
}
if model.Tags != nil {
state.Tags = *model.Tags
}

return metadata.Encode(&state)
},
}
}

func (r ResourceDeploymentScriptAzureCliResource) Delete() sdk.ResourceFunc {
return deleteDeploymentScript()
}
Loading