-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'storagemover/new_resource_storagemover_agent' of github…
….com:sinbai/terraform-provider-azurerm into storagemover/new_resource_storagemover_agent
- Loading branch information
Showing
24 changed files
with
1,634 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
internal/services/storagemover/scripts/install_arc.sh.tftpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/bin/bash | ||
|
||
# Disable the Azure VM Guest Agent. | ||
current_hostname=$(hostname) | ||
sudo service walinuxagent stop | ||
sudo waagent -deprovision -force | ||
sudo rm -rf /var/lib/waagent | ||
sudo hostnamectl set-hostname $current_hostname | ||
|
||
# Block access to the Azure IMDS endpoint. | ||
sudo ufw --force enable | ||
sudo ufw deny out from any to 169.254.169.254 | ||
sudo ufw default allow incoming | ||
|
||
# Add the service principal application ID and secret here | ||
servicePrincipalClientId="${client_id}" | ||
servicePrincipalSecret="${client_secret}" | ||
cloud="AzureCloud" | ||
tenantId="${tenant_id}" | ||
subscriptionId="${subscription_id}" | ||
resourceGroup="${resource_group_name}" | ||
location="${location}" | ||
authType="principal" | ||
correlationId="${uuid}" | ||
output="$(wget https://aka.ms/azcmagent -O ~/install_linux_azcmagent.sh 2>&1)" | ||
if [ $? != 0 ]; then | ||
read -d '' bodyData <<EOF | ||
{ | ||
"operation": "onboarding", | ||
"messageType": "DownloadScriptFailed", | ||
"tenantId": "$tenantId", | ||
"subscriptionId": "$subscriptionId", | ||
"resourceGroup": "$resourceGroup", | ||
"location": "$location", | ||
"correlationId": "$correlationId", | ||
"authType": "$authType", | ||
"message": "$output" | ||
} | ||
EOF | ||
wget -qO- --method=PUT --body-data="$bodyData" "https://gbl.his.arc.azure.com/log" &> /dev/null || true | ||
fi | ||
echo "$output" | ||
sudo chmod +x ~/install_linux_azcmagent.sh | ||
cd ~ | ||
./install_linux_azcmagent.sh | ||
sudo azcmagent connect \ | ||
--service-principal-id "$servicePrincipalClientId" \ | ||
--service-principal-secret "$servicePrincipalSecret" \ | ||
--cloud "$cloud" \ | ||
--tenant-id "$tenantId" \ | ||
--subscription-id "$subscriptionId" \ | ||
--resource-group "$resourceGroup" \ | ||
--location "$location" \ | ||
--correlation-id "$correlationId" |
225 changes: 225 additions & 0 deletions
225
internal/services/storagemover/storage_mover_agent_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package storagemover | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/agents" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/storagemovers" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
computevalidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
) | ||
|
||
type StorageMoverAgentResourceModel struct { | ||
Name string `tfschema:"name"` | ||
StorageMoverId string `tfschema:"storage_mover_id"` | ||
ArcVirtualMachineId string `tfschema:"arc_virtual_machine_id"` | ||
ArcVmUuid string `tfschema:"arc_virtual_machine_uuid"` | ||
Description string `tfschema:"description"` | ||
} | ||
|
||
type StorageMoverAgentResource struct{} | ||
|
||
var _ sdk.ResourceWithUpdate = StorageMoverAgentResource{} | ||
|
||
func (r StorageMoverAgentResource) ResourceType() string { | ||
return "azurerm_storage_mover_agent" | ||
} | ||
|
||
func (r StorageMoverAgentResource) ModelObject() interface{} { | ||
return &StorageMoverAgentResourceModel{} | ||
} | ||
|
||
func (r StorageMoverAgentResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return agents.ValidateAgentID | ||
} | ||
|
||
func (r StorageMoverAgentResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"arc_virtual_machine_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: computevalidate.HybridMachineID, | ||
}, | ||
|
||
"arc_virtual_machine_uuid": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IsUUID, | ||
}, | ||
|
||
"storage_mover_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: storagemovers.ValidateStorageMoverID, | ||
}, | ||
|
||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverAgentResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (r StorageMoverAgentResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
var model StorageMoverAgentResourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
client := metadata.Client.StorageMover.AgentsClient | ||
storageMoverId, err := storagemovers.ParseStorageMoverID(model.StorageMoverId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := agents.NewAgentID(storageMoverId.SubscriptionId, storageMoverId.ResourceGroupName, storageMoverId.StorageMoverName, 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 := agents.Agent{ | ||
Properties: agents.AgentProperties{ | ||
ArcResourceId: model.ArcVirtualMachineId, | ||
ArcVMUuid: model.ArcVmUuid, | ||
}, | ||
} | ||
|
||
if model.Description != "" { | ||
properties.Properties.Description = &model.Description | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id, properties); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverAgentResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.AgentsClient | ||
|
||
id, err := agents.ParseAgentID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var model StorageMoverAgentResourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
resp, err := client.Get(ctx, *id) | ||
if err != nil { | ||
return fmt.Errorf("retrieving %s: %+v", *id, err) | ||
} | ||
|
||
properties := resp.Model | ||
if properties == nil { | ||
return fmt.Errorf("retrieving %s: model was nil", *id) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("description") { | ||
properties.Properties.Description = &model.Description | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, *id, *properties); err != nil { | ||
return fmt.Errorf("updating %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverAgentResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.AgentsClient | ||
|
||
id, err := agents.ParseAgentID(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) | ||
} | ||
|
||
state := StorageMoverAgentResourceModel{ | ||
Name: id.AgentName, | ||
StorageMoverId: storagemovers.NewStorageMoverID(id.SubscriptionId, id.ResourceGroupName, id.StorageMoverName).ID(), | ||
} | ||
|
||
if model := resp.Model; model != nil { | ||
state.ArcVmUuid = model.Properties.ArcVMUuid | ||
state.ArcVirtualMachineId = model.Properties.ArcResourceId | ||
|
||
state.Description = pointer.From(model.Properties.Description) | ||
} | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverAgentResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.AgentsClient | ||
|
||
id, err := agents.ParseAgentID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := client.DeleteThenPoll(ctx, *id); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
Oops, something went wrong.