|
| 1 | +package dataprotection |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "regexp" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" |
| 11 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" |
| 12 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" |
| 13 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/identity" |
| 14 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" |
| 15 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dataprotection/legacysdk/dataprotection" |
| 16 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dataprotection/parse" |
| 17 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" |
| 18 | + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" |
| 19 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/validation" |
| 20 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" |
| 21 | + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" |
| 22 | +) |
| 23 | + |
| 24 | +func resourceDataProtectionBackupVault() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Create: resourceDataProtectionBackupVaultCreate, |
| 27 | + Read: resourceDataProtectionBackupVaultRead, |
| 28 | + Update: resourceDataProtectionBackupVaultUpdate, |
| 29 | + Delete: resourceDataProtectionBackupVaultDelete, |
| 30 | + |
| 31 | + Timeouts: &schema.ResourceTimeout{ |
| 32 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 33 | + Read: schema.DefaultTimeout(5 * time.Minute), |
| 34 | + Update: schema.DefaultTimeout(30 * time.Minute), |
| 35 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 36 | + }, |
| 37 | + |
| 38 | + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { |
| 39 | + _, err := parse.BackupVaultID(id) |
| 40 | + return err |
| 41 | + }), |
| 42 | + |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "name": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + ValidateFunc: validation.StringMatch( |
| 49 | + regexp.MustCompile("^[-a-zA-Z0-9]{2,50}$"), |
| 50 | + "DataProtection BackupVault name must be 2 - 50 characters long, contain only letters, numbers and hyphens.).", |
| 51 | + ), |
| 52 | + }, |
| 53 | + |
| 54 | + "resource_group_name": azure.SchemaResourceGroupName(), |
| 55 | + |
| 56 | + "location": azure.SchemaLocation(), |
| 57 | + |
| 58 | + "datastore_type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + ForceNew: true, |
| 62 | + ValidateFunc: validation.StringInSlice([]string{ |
| 63 | + string(dataprotection.StorageSettingStoreTypesArchiveStore), |
| 64 | + string(dataprotection.StorageSettingStoreTypesSnapshotStore), |
| 65 | + string(dataprotection.StorageSettingStoreTypesVaultStore), |
| 66 | + }, false), |
| 67 | + }, |
| 68 | + |
| 69 | + "redundancy": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Required: true, |
| 72 | + ForceNew: true, |
| 73 | + ValidateFunc: validation.StringInSlice([]string{ |
| 74 | + string(dataprotection.GeoRedundant), |
| 75 | + string(dataprotection.LocallyRedundant), |
| 76 | + }, false), |
| 77 | + }, |
| 78 | + |
| 79 | + "identity": identity.SystemAssigned{}.Schema(), |
| 80 | + |
| 81 | + "tags": tags.Schema(), |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | +func resourceDataProtectionBackupVaultCreate(d *schema.ResourceData, meta interface{}) error { |
| 86 | + subscriptionId := meta.(*clients.Client).Account.SubscriptionId |
| 87 | + client := meta.(*clients.Client).DataProtection.BackupVaultClient |
| 88 | + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) |
| 89 | + defer cancel() |
| 90 | + |
| 91 | + name := d.Get("name").(string) |
| 92 | + resourceGroup := d.Get("resource_group_name").(string) |
| 93 | + |
| 94 | + id := parse.NewBackupVaultID(subscriptionId, resourceGroup, name) |
| 95 | + |
| 96 | + existing, err := client.Get(ctx, id.Name, id.ResourceGroup) |
| 97 | + if err != nil { |
| 98 | + if !utils.ResponseWasNotFound(existing.Response) { |
| 99 | + return fmt.Errorf("checking for existing DataProtection BackupVault (%q): %+v", id, err) |
| 100 | + } |
| 101 | + } |
| 102 | + if !utils.ResponseWasNotFound(existing.Response) { |
| 103 | + return tf.ImportAsExistsError("azurerm_data_protection_backup_vault", id.ID()) |
| 104 | + } |
| 105 | + |
| 106 | + parameters := dataprotection.BackupVaultResource{ |
| 107 | + Location: utils.String(location.Normalize(d.Get("location").(string))), |
| 108 | + Properties: &dataprotection.BackupVault{ |
| 109 | + StorageSettings: &[]dataprotection.StorageSetting{ |
| 110 | + { |
| 111 | + DatastoreType: dataprotection.StorageSettingStoreTypes(d.Get("datastore_type").(string)), |
| 112 | + Type: dataprotection.StorageSettingTypes(d.Get("redundancy").(string)), |
| 113 | + }}, |
| 114 | + }, |
| 115 | + Identity: expandBackupVaultDppIdentityDetails(d.Get("identity").([]interface{})), |
| 116 | + Tags: tags.Expand(d.Get("tags").(map[string]interface{})), |
| 117 | + } |
| 118 | + future, err := client.CreateOrUpdate(ctx, id.Name, id.ResourceGroup, parameters) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("creating DataProtection BackupVault (%q): %+v", id, err) |
| 121 | + } |
| 122 | + |
| 123 | + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { |
| 124 | + return fmt.Errorf("waiting for creation of the DataProtection BackupVault (%q): %+v", id, err) |
| 125 | + } |
| 126 | + |
| 127 | + d.SetId(id.ID()) |
| 128 | + return resourceDataProtectionBackupVaultRead(d, meta) |
| 129 | +} |
| 130 | + |
| 131 | +func resourceDataProtectionBackupVaultRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + client := meta.(*clients.Client).DataProtection.BackupVaultClient |
| 133 | + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) |
| 134 | + defer cancel() |
| 135 | + |
| 136 | + id, err := parse.BackupVaultID(d.Id()) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + resp, err := client.Get(ctx, id.Name, id.ResourceGroup) |
| 142 | + if err != nil { |
| 143 | + if utils.ResponseWasNotFound(resp.Response) { |
| 144 | + log.Printf("[INFO] DataProtection BackupVault %q does not exist - removing from state", d.Id()) |
| 145 | + d.SetId("") |
| 146 | + return nil |
| 147 | + } |
| 148 | + return fmt.Errorf("retrieving DataProtection BackupVault (%q): %+v", id, err) |
| 149 | + } |
| 150 | + d.Set("name", id.Name) |
| 151 | + d.Set("resource_group_name", id.ResourceGroup) |
| 152 | + d.Set("location", location.NormalizeNilable(resp.Location)) |
| 153 | + if props := resp.Properties; props != nil { |
| 154 | + if props.StorageSettings != nil && len(*props.StorageSettings) > 0 { |
| 155 | + d.Set("datastore_type", (*props.StorageSettings)[0].DatastoreType) |
| 156 | + d.Set("redundancy", (*props.StorageSettings)[0].Type) |
| 157 | + } |
| 158 | + } |
| 159 | + if err := d.Set("identity", flattenBackupVaultDppIdentityDetails(resp.Identity)); err != nil { |
| 160 | + return fmt.Errorf("setting `identity`: %+v", err) |
| 161 | + } |
| 162 | + return tags.FlattenAndSet(d, resp.Tags) |
| 163 | +} |
| 164 | + |
| 165 | +func resourceDataProtectionBackupVaultUpdate(d *schema.ResourceData, meta interface{}) error { |
| 166 | + client := meta.(*clients.Client).DataProtection.BackupVaultClient |
| 167 | + ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) |
| 168 | + defer cancel() |
| 169 | + |
| 170 | + id, err := parse.BackupVaultID(d.Id()) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + parameters := dataprotection.PatchResourceRequestInput{} |
| 176 | + if d.HasChange("identity") { |
| 177 | + parameters.Identity = expandBackupVaultDppIdentityDetails(d.Get("identity").([]interface{})) |
| 178 | + } |
| 179 | + if d.HasChange("tags") { |
| 180 | + parameters.Tags = tags.Expand(d.Get("tags").(map[string]interface{})) |
| 181 | + } |
| 182 | + |
| 183 | + future, err := client.Patch(ctx, id.Name, id.ResourceGroup, parameters) |
| 184 | + if err != nil { |
| 185 | + return fmt.Errorf("updating DataProtection BackupVault (%q): %+v", id, err) |
| 186 | + } |
| 187 | + |
| 188 | + if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { |
| 189 | + return fmt.Errorf("waiting for update of the DataProtection BackupVault %q: %+v", id, err) |
| 190 | + } |
| 191 | + return resourceDataProtectionBackupVaultRead(d, meta) |
| 192 | +} |
| 193 | + |
| 194 | +func resourceDataProtectionBackupVaultDelete(d *schema.ResourceData, meta interface{}) error { |
| 195 | + client := meta.(*clients.Client).DataProtection.BackupVaultClient |
| 196 | + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) |
| 197 | + defer cancel() |
| 198 | + |
| 199 | + id, err := parse.BackupVaultID(d.Id()) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + |
| 204 | + if resp, err := client.Delete(ctx, id.Name, id.ResourceGroup); err != nil { |
| 205 | + if utils.ResponseWasNotFound(resp) { |
| 206 | + return nil |
| 207 | + } |
| 208 | + return fmt.Errorf("deleting DataProtection BackupVault (%q): %+v", id, err) |
| 209 | + } |
| 210 | + return nil |
| 211 | +} |
| 212 | + |
| 213 | +func expandBackupVaultDppIdentityDetails(input []interface{}) *dataprotection.DppIdentityDetails { |
| 214 | + config, _ := identity.SystemAssigned{}.Expand(input) |
| 215 | + return &dataprotection.DppIdentityDetails{ |
| 216 | + Type: utils.String(config.Type), |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func flattenBackupVaultDppIdentityDetails(input *dataprotection.DppIdentityDetails) []interface{} { |
| 221 | + var config *identity.ExpandedConfig |
| 222 | + if input != nil { |
| 223 | + config = &identity.ExpandedConfig{ |
| 224 | + Type: *input.Type, |
| 225 | + PrincipalId: input.PrincipalID, |
| 226 | + TenantId: input.TenantID, |
| 227 | + } |
| 228 | + } |
| 229 | + return identity.SystemAssigned{}.Flatten(config) |
| 230 | +} |
0 commit comments