|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package storagemover |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "regexp" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hashicorp/go-azure-helpers/lang/pointer" |
| 13 | + "github.com/hashicorp/go-azure-helpers/lang/response" |
| 14 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" |
| 15 | + "github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2025-07-01/endpoints" |
| 16 | + "github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2025-07-01/storagemovers" |
| 17 | + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" |
| 18 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" |
| 19 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" |
| 20 | +) |
| 21 | + |
| 22 | +type StorageMoverSmbFileShareEndpointModel struct { |
| 23 | + Name string `tfschema:"name"` |
| 24 | + StorageMoverId string `tfschema:"storage_mover_id"` |
| 25 | + StorageAccountId string `tfschema:"storage_account_id"` |
| 26 | + FileShareName string `tfschema:"file_share_name"` |
| 27 | + Description string `tfschema:"description"` |
| 28 | +} |
| 29 | + |
| 30 | +type StorageMoverSmbFileShareEndpointResource struct{} |
| 31 | + |
| 32 | +var _ sdk.ResourceWithUpdate = StorageMoverSmbFileShareEndpointResource{} |
| 33 | + |
| 34 | +func (r StorageMoverSmbFileShareEndpointResource) ResourceType() string { |
| 35 | + return "azurerm_storage_mover_smb_file_share_endpoint" |
| 36 | +} |
| 37 | + |
| 38 | +func (r StorageMoverSmbFileShareEndpointResource) ModelObject() interface{} { |
| 39 | + return &StorageMoverSmbFileShareEndpointModel{} |
| 40 | +} |
| 41 | + |
| 42 | +func (r StorageMoverSmbFileShareEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { |
| 43 | + return endpoints.ValidateEndpointID |
| 44 | +} |
| 45 | + |
| 46 | +func (r StorageMoverSmbFileShareEndpointResource) Arguments() map[string]*pluginsdk.Schema { |
| 47 | + return map[string]*pluginsdk.Schema{ |
| 48 | + "name": { |
| 49 | + Type: pluginsdk.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + ValidateFunc: validation.StringMatch( |
| 53 | + regexp.MustCompile(`^[0-9a-zA-Z][-_0-9a-zA-Z]{0,63}$`), |
| 54 | + `The name must be between 1 and 64 characters in length, begin with a letter or number, and may contain letters, numbers, dashes and underscore.`, |
| 55 | + ), |
| 56 | + }, |
| 57 | + |
| 58 | + "storage_mover_id": { |
| 59 | + Type: pluginsdk.TypeString, |
| 60 | + Required: true, |
| 61 | + ForceNew: true, |
| 62 | + ValidateFunc: storagemovers.ValidateStorageMoverID, |
| 63 | + }, |
| 64 | + |
| 65 | + "storage_account_id": { |
| 66 | + Type: pluginsdk.TypeString, |
| 67 | + Required: true, |
| 68 | + ForceNew: true, |
| 69 | + ValidateFunc: commonids.ValidateStorageAccountID, |
| 70 | + }, |
| 71 | + |
| 72 | + "file_share_name": { |
| 73 | + Type: pluginsdk.TypeString, |
| 74 | + Required: true, |
| 75 | + ForceNew: true, |
| 76 | + ValidateFunc: validation.StringIsNotEmpty, |
| 77 | + }, |
| 78 | + |
| 79 | + "description": { |
| 80 | + Type: pluginsdk.TypeString, |
| 81 | + Optional: true, |
| 82 | + ValidateFunc: validation.StringIsNotEmpty, |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func (r StorageMoverSmbFileShareEndpointResource) Attributes() map[string]*pluginsdk.Schema { |
| 88 | + return map[string]*pluginsdk.Schema{} |
| 89 | +} |
| 90 | + |
| 91 | +func (r StorageMoverSmbFileShareEndpointResource) Create() sdk.ResourceFunc { |
| 92 | + return sdk.ResourceFunc{ |
| 93 | + Timeout: 30 * time.Minute, |
| 94 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 95 | + var model StorageMoverSmbFileShareEndpointModel |
| 96 | + if err := metadata.Decode(&model); err != nil { |
| 97 | + return fmt.Errorf("decoding: %+v", err) |
| 98 | + } |
| 99 | + |
| 100 | + client := metadata.Client.StorageMover.EndpointsClient |
| 101 | + storageMoverId, err := storagemovers.ParseStorageMoverID(model.StorageMoverId) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + id := endpoints.NewEndpointID(storageMoverId.SubscriptionId, storageMoverId.ResourceGroupName, storageMoverId.StorageMoverName, model.Name) |
| 107 | + existing, err := client.Get(ctx, id) |
| 108 | + if err != nil && !response.WasNotFound(existing.HttpResponse) { |
| 109 | + return fmt.Errorf("checking for existing %s: %+v", id, err) |
| 110 | + } |
| 111 | + |
| 112 | + if !response.WasNotFound(existing.HttpResponse) { |
| 113 | + return metadata.ResourceRequiresImport(r.ResourceType(), id) |
| 114 | + } |
| 115 | + |
| 116 | + properties := endpoints.Endpoint{ |
| 117 | + Properties: endpoints.AzureStorageSmbFileShareEndpointProperties{ |
| 118 | + FileShareName: model.FileShareName, |
| 119 | + StorageAccountResourceId: model.StorageAccountId, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + if model.Description != "" { |
| 124 | + if v, ok := properties.Properties.(endpoints.AzureStorageSmbFileShareEndpointProperties); ok { |
| 125 | + v.Description = pointer.To(model.Description) |
| 126 | + properties.Properties = v |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if _, err := client.CreateOrUpdate(ctx, id, properties); err != nil { |
| 131 | + return fmt.Errorf("creating %s: %+v", id, err) |
| 132 | + } |
| 133 | + |
| 134 | + metadata.SetID(id) |
| 135 | + return nil |
| 136 | + }, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func (r StorageMoverSmbFileShareEndpointResource) Update() sdk.ResourceFunc { |
| 141 | + return sdk.ResourceFunc{ |
| 142 | + Timeout: 30 * time.Minute, |
| 143 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 144 | + client := metadata.Client.StorageMover.EndpointsClient |
| 145 | + |
| 146 | + id, err := endpoints.ParseEndpointID(metadata.ResourceData.Id()) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + var model StorageMoverSmbFileShareEndpointModel |
| 152 | + if err := metadata.Decode(&model); err != nil { |
| 153 | + return fmt.Errorf("decoding: %+v", err) |
| 154 | + } |
| 155 | + |
| 156 | + resp, err := client.Get(ctx, *id) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("retrieving %s: %+v", *id, err) |
| 159 | + } |
| 160 | + |
| 161 | + properties := resp.Model |
| 162 | + if properties == nil { |
| 163 | + return fmt.Errorf("retrieving %s: model was nil", *id) |
| 164 | + } |
| 165 | + |
| 166 | + if metadata.ResourceData.HasChange("description") { |
| 167 | + if v, ok := properties.Properties.(endpoints.AzureStorageSmbFileShareEndpointProperties); ok { |
| 168 | + v.Description = pointer.To(model.Description) |
| 169 | + properties.Properties = v |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if _, err := client.CreateOrUpdate(ctx, *id, *properties); err != nil { |
| 174 | + return fmt.Errorf("updating %s: %+v", *id, err) |
| 175 | + } |
| 176 | + |
| 177 | + return nil |
| 178 | + }, |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func (r StorageMoverSmbFileShareEndpointResource) Read() sdk.ResourceFunc { |
| 183 | + return sdk.ResourceFunc{ |
| 184 | + Timeout: 5 * time.Minute, |
| 185 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 186 | + client := metadata.Client.StorageMover.EndpointsClient |
| 187 | + |
| 188 | + id, err := endpoints.ParseEndpointID(metadata.ResourceData.Id()) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + resp, err := client.Get(ctx, *id) |
| 194 | + if err != nil { |
| 195 | + if response.WasNotFound(resp.HttpResponse) { |
| 196 | + return metadata.MarkAsGone(id) |
| 197 | + } |
| 198 | + |
| 199 | + return fmt.Errorf("retrieving %s: %+v", *id, err) |
| 200 | + } |
| 201 | + |
| 202 | + state := StorageMoverSmbFileShareEndpointModel{ |
| 203 | + Name: id.EndpointName, |
| 204 | + StorageMoverId: storagemovers.NewStorageMoverID(id.SubscriptionId, id.ResourceGroupName, id.StorageMoverName).ID(), |
| 205 | + } |
| 206 | + |
| 207 | + if model := resp.Model; model != nil { |
| 208 | + if v, ok := model.Properties.(endpoints.AzureStorageSmbFileShareEndpointProperties); ok { |
| 209 | + state.FileShareName = v.FileShareName |
| 210 | + state.StorageAccountId = v.StorageAccountResourceId |
| 211 | + |
| 212 | + des := "" |
| 213 | + if v.Description != nil { |
| 214 | + des = *v.Description |
| 215 | + } |
| 216 | + state.Description = des |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return metadata.Encode(&state) |
| 221 | + }, |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +func (r StorageMoverSmbFileShareEndpointResource) Delete() sdk.ResourceFunc { |
| 226 | + return sdk.ResourceFunc{ |
| 227 | + Timeout: 30 * time.Minute, |
| 228 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 229 | + client := metadata.Client.StorageMover.EndpointsClient |
| 230 | + |
| 231 | + id, err := endpoints.ParseEndpointID(metadata.ResourceData.Id()) |
| 232 | + if err != nil { |
| 233 | + return err |
| 234 | + } |
| 235 | + |
| 236 | + if err := client.DeleteThenPoll(ctx, *id); err != nil { |
| 237 | + return fmt.Errorf("deleting %s: %+v", id, err) |
| 238 | + } |
| 239 | + |
| 240 | + return nil |
| 241 | + }, |
| 242 | + } |
| 243 | +} |
0 commit comments