diff --git a/mmv1/api/resource.go b/mmv1/api/resource.go index fc35a997a64d..fe4ebc9778bd 100644 --- a/mmv1/api/resource.go +++ b/mmv1/api/resource.go @@ -275,6 +275,14 @@ type Resource struct { // public ca external account keys ExcludeRead bool `yaml:"exclude_read,omitempty"` + // Set to true for resources that are to generate deletion policy fields by default + // TODO: this will be inverted to an opt-out flag very soon + DeletionPolicy bool `yaml:"deletion_policy,omitempty"` + + // Set to the default deletion policy value for the resource. + // By default this will be "DELETE". + DeletionPolicyDefault string `yaml:"deletion_policy_default,omitempty"` + // Set to true for resources that wish to disable automatic generation of default provider // value customdiff functions // TODO rewrite: 1 instance used @@ -488,6 +496,11 @@ func (r *Resource) setShallowDefaults() { if r.Timeouts == nil { r.Timeouts = NewTimeouts() // This only sets defaults if Timeouts is nil } + if r.DeletionPolicy { + if r.DeletionPolicyDefault == "" { + r.DeletionPolicyDefault = "DELETE" + } + } } // SetDefault sets default values for this Resource and all its properties. diff --git a/mmv1/templates/terraform/resource.go.tmpl b/mmv1/templates/terraform/resource.go.tmpl index 0cf108a3c138..af565ec539e5 100644 --- a/mmv1/templates/terraform/resource.go.tmpl +++ b/mmv1/templates/terraform/resource.go.tmpl @@ -186,6 +186,20 @@ func Resource{{ $.ResourceName -}}() *schema.Resource { Type: schema.TypeString, Computed: true, }, +{{- end}} +{{- if $.DeletionPolicy }} + "deletion_policy": { + Type: schema.TypeString, + Optional: true, + Description: `Whether Terraform will be prevented from destroying the instance. Defaults to "{{$.DeletionPolicyDefault}}". +When a 'terraform destroy' or 'terraform apply' would delete the instance, +the command will fail if this field is set to "PREVENT" in Terraform state. +When set to "ABANDON", the command will remove the resource from Terraform +management without updating or deleting the resource in the API. +When set to "DELETE", deleting the resource is allowed. +`, + Default: "{{$.DeletionPolicyDefault}}", + }, {{- end}} }, UseJSONNumber: true, @@ -1125,6 +1139,15 @@ func resource{{ $.ResourceName }}Delete(d *schema.ResourceData, meta interface{} {{- if $.CustomCode.PreDelete }} {{ customTemplate $ $.CustomCode.PreDelete false -}} {{- end }} + {{- if $.DeletionPolicy }} + if d.Get("deletion_policy").(string) == "PREVENT" { + return fmt.Errorf("cannot destroy {{$.ResourceName}} without setting deletion_policy=\"DELETE\" and running `terraform apply`") + } + if d.Get("deletion_policy").(string) == "ABANDON" { + log.Printf("[DEBUG] deletion_policy set to \"ABANDON\", removing {{ $.Name }} %q from Terraform state without deletion", d.Id()) + return nil + } + {{- end }} log.Printf("[DEBUG] Deleting {{ $.Name }} %q", d.Id()) res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ diff --git a/mmv1/templates/terraform/resource.html.markdown.tmpl b/mmv1/templates/terraform/resource.html.markdown.tmpl index 42f408ce39de..a72fc437340d 100644 --- a/mmv1/templates/terraform/resource.html.markdown.tmpl +++ b/mmv1/templates/terraform/resource.html.markdown.tmpl @@ -125,6 +125,14 @@ The following arguments are supported: If it is not provided, the provider project is used. {{ "" }} {{- end }} +{{- if $.DeletionPolicy }} +* `deletion_policy` - (Optional) Whether Terraform will be prevented from destroying the resource. Defaults to {{$.DeletionPolicyDefault}}. + When a 'terraform destroy' or 'terraform apply' would delete the resource, + the command will fail if this field is set to "PREVENT" in Terraform state. + When set to "ABANDON", the command will remove the resource from Terraform + management without updating or deleting the resource in the API. + When set to "DELETE", deleting the resource is allowed. +{{- end }} {{- range $f := $.VirtualFields }} * `{{$f.Name}}` - (Optional) {{$f.Description}} {{- end }}