|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + matlas "go.mongodb.org/atlas/mongodbatlas" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceMongoDBAtlasBackupCompliancePolicy() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: dataSourceMongoDBAtlasBackupCompliancePolicyRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "project_id": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + }, |
| 21 | + "authorized_email": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + "copy_protection_enabled": { |
| 26 | + Type: schema.TypeBool, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "encryption_at_rest_enabled": { |
| 30 | + Type: schema.TypeBool, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "on_demand_policy_item": { |
| 34 | + Type: schema.TypeList, |
| 35 | + MaxItems: 1, |
| 36 | + Optional: true, |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "frequency_interval": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "frequency_type": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "retention_unit": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "retention_value": { |
| 56 | + Type: schema.TypeInt, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + "pit_enabled": { |
| 63 | + Type: schema.TypeBool, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "state": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "updated_date": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "updated_user": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "restore_window_days": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "policy_item_hourly": { |
| 83 | + Type: schema.TypeList, |
| 84 | + MaxItems: 1, |
| 85 | + Optional: true, |
| 86 | + Elem: &schema.Resource{ |
| 87 | + Schema: map[string]*schema.Schema{ |
| 88 | + "id": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "frequency_type": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "frequency_interval": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Required: true, |
| 99 | + }, |
| 100 | + "retention_value": { |
| 101 | + Type: schema.TypeInt, |
| 102 | + Required: true, |
| 103 | + }, |
| 104 | + "retention_unit": { |
| 105 | + Type: schema.TypeString, |
| 106 | + Required: true, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + "policy_item_daily": { |
| 112 | + Type: schema.TypeList, |
| 113 | + MaxItems: 1, |
| 114 | + Optional: true, |
| 115 | + Elem: &schema.Resource{ |
| 116 | + Schema: map[string]*schema.Schema{ |
| 117 | + "id": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + }, |
| 121 | + "frequency_type": { |
| 122 | + Type: schema.TypeString, |
| 123 | + Computed: true, |
| 124 | + }, |
| 125 | + "frequency_interval": { |
| 126 | + Type: schema.TypeInt, |
| 127 | + Required: true, |
| 128 | + }, |
| 129 | + "retention_unit": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Required: true, |
| 132 | + }, |
| 133 | + "retention_value": { |
| 134 | + Type: schema.TypeInt, |
| 135 | + Required: true, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + "policy_item_weekly": { |
| 141 | + Type: schema.TypeList, |
| 142 | + Optional: true, |
| 143 | + Elem: &schema.Resource{ |
| 144 | + Schema: map[string]*schema.Schema{ |
| 145 | + "id": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Computed: true, |
| 148 | + }, |
| 149 | + "frequency_type": { |
| 150 | + Type: schema.TypeString, |
| 151 | + Computed: true, |
| 152 | + }, |
| 153 | + "frequency_interval": { |
| 154 | + Type: schema.TypeInt, |
| 155 | + Required: true, |
| 156 | + }, |
| 157 | + "retention_unit": { |
| 158 | + Type: schema.TypeString, |
| 159 | + Required: true, |
| 160 | + }, |
| 161 | + "retention_value": { |
| 162 | + Type: schema.TypeInt, |
| 163 | + Required: true, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + "policy_item_monthly": { |
| 169 | + Type: schema.TypeList, |
| 170 | + Optional: true, |
| 171 | + Elem: &schema.Resource{ |
| 172 | + Schema: map[string]*schema.Schema{ |
| 173 | + "id": { |
| 174 | + Type: schema.TypeString, |
| 175 | + Computed: true, |
| 176 | + }, |
| 177 | + "frequency_type": { |
| 178 | + Type: schema.TypeString, |
| 179 | + Computed: true, |
| 180 | + }, |
| 181 | + "frequency_interval": { |
| 182 | + Type: schema.TypeInt, |
| 183 | + Required: true, |
| 184 | + }, |
| 185 | + "retention_unit": { |
| 186 | + Type: schema.TypeString, |
| 187 | + Required: true, |
| 188 | + }, |
| 189 | + "retention_value": { |
| 190 | + Type: schema.TypeInt, |
| 191 | + Required: true, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +func dataSourceMongoDBAtlasBackupCompliancePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 201 | + conn := meta.(*MongoDBClient).Atlas |
| 202 | + |
| 203 | + projectID := d.Get("project_id").(string) |
| 204 | + |
| 205 | + backupPolicy, resp, err := conn.BackupCompliancePolicy.Get(ctx, projectID) |
| 206 | + if resp != nil && resp.StatusCode == http.StatusNotFound || backupPolicy.ProjectID == "" { |
| 207 | + return nil |
| 208 | + } |
| 209 | + if err != nil { |
| 210 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicyRead, projectID, err)) |
| 211 | + } |
| 212 | + |
| 213 | + if err := d.Set("authorized_email", backupPolicy.AuthorizedEmail); err != nil { |
| 214 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "authorized_email", projectID, err)) |
| 215 | + } |
| 216 | + |
| 217 | + if err := d.Set("state", backupPolicy.State); err != nil { |
| 218 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "state", projectID, err)) |
| 219 | + } |
| 220 | + |
| 221 | + if err := d.Set("restore_window_days", backupPolicy.RestoreWindowDays); err != nil { |
| 222 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "restore_window_days", projectID, err)) |
| 223 | + } |
| 224 | + |
| 225 | + if err := d.Set("pit_enabled", backupPolicy.PitEnabled); err != nil { |
| 226 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "pit_enabled", projectID, err)) |
| 227 | + } |
| 228 | + |
| 229 | + if err := d.Set("copy_protection_enabled", backupPolicy.CopyProtectionEnabled); err != nil { |
| 230 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "copy_protection_enabled", projectID, err)) |
| 231 | + } |
| 232 | + |
| 233 | + if err := d.Set("encryption_at_rest_enabled", backupPolicy.EncryptionAtRestEnabled); err != nil { |
| 234 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "encryption_at_rest_enabled", projectID, err)) |
| 235 | + } |
| 236 | + |
| 237 | + if err := d.Set("updated_date", backupPolicy.UpdatedDate); err != nil { |
| 238 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "updated_date", projectID, err)) |
| 239 | + } |
| 240 | + |
| 241 | + if err := d.Set("updated_user", backupPolicy.UpdatedUser); err != nil { |
| 242 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "updated_user", projectID, err)) |
| 243 | + } |
| 244 | + |
| 245 | + if err := d.Set("on_demand_policy_item", flattenOnDemandBackupPolicyItem(backupPolicy.OnDemandPolicyItem)); err != nil { |
| 246 | + return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "policies", projectID, err)) |
| 247 | + } |
| 248 | + |
| 249 | + if err := d.Set("policy_item_hourly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleHourly)); err != nil { |
| 250 | + return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_hourly", projectID, err) |
| 251 | + } |
| 252 | + |
| 253 | + if err := d.Set("policy_item_daily", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleDaily)); err != nil { |
| 254 | + return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_daily", projectID, err) |
| 255 | + } |
| 256 | + |
| 257 | + if err := d.Set("policy_item_weekly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleWeekly)); err != nil { |
| 258 | + return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_weekly", projectID, err) |
| 259 | + } |
| 260 | + |
| 261 | + if err := d.Set("policy_item_monthly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleMonthly)); err != nil { |
| 262 | + return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_monthly", projectID, err) |
| 263 | + } |
| 264 | + |
| 265 | + d.SetId(encodeStateID(map[string]string{ |
| 266 | + "project_id": projectID, |
| 267 | + })) |
| 268 | + |
| 269 | + return nil |
| 270 | +} |
| 271 | + |
| 272 | +func flattenBackupPolicyItems(items []matlas.ScheduledPolicyItem, frequencyType string) []map[string]interface{} { |
| 273 | + policyItems := make([]map[string]interface{}, 0) |
| 274 | + for _, v := range items { |
| 275 | + if frequencyType == v.FrequencyType { |
| 276 | + policyItems = append(policyItems, map[string]interface{}{ |
| 277 | + "id": v.ID, |
| 278 | + "frequency_interval": v.FrequencyInterval, |
| 279 | + "frequency_type": v.FrequencyType, |
| 280 | + "retention_unit": v.RetentionUnit, |
| 281 | + "retention_value": v.RetentionValue, |
| 282 | + }) |
| 283 | + } |
| 284 | + } |
| 285 | + return policyItems |
| 286 | +} |
0 commit comments