Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions mmv1/templates/terraform/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions mmv1/templates/terraform/resource.html.markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Loading