Skip to content

Commit

Permalink
Add state upgrader to DCL resource google_dataproc_workflow_template (G…
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored and BBBmau committed Nov 28, 2023
1 parent 2cec8ce commit 1a9a20e
Show file tree
Hide file tree
Showing 8 changed files with 2,186 additions and 0 deletions.

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions mmv1/third_party/terraform/tpgresource/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,19 @@ func LabelsStateUpgrade(rawState map[string]interface{}, labesPrefix string) (ma

return rawState, nil
}

// Upgrade the field "terraform_labels" in the state to have the value of filed "labels"
// when it is not set but "labels" field is set in the state
func TerraformLabelsStateUpgrade(rawState map[string]interface{}) (map[string]interface{}, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
log.Printf("[DEBUG] Attributes before migration terraform_labels: %#v", rawState["terraform_labels"])

if rawState["terraform_labels"] == nil && rawState["labels"] != nil {
rawState["terraform_labels"] = rawState["labels"]
}

log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
log.Printf("[DEBUG] Attributes after migration terraform_labels: %#v", rawState["terraform_labels"])

return rawState, nil
}
1 change: 1 addition & 0 deletions tpgtools/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
CustomSerializer = "CUSTOM_SERIALIZER"
TerraformProductName = "CUSTOM_TERRAFORM_PRODUCT_NAME"
CustomTimeout = "CUSTOM_TIMEOUT"
StateUpgrade = "STATE_UPGRADE"
)

// Field-level Overrides
Expand Down
5 changes: 5 additions & 0 deletions tpgtools/override_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,8 @@ type CustomTimeoutDetails struct {
// The overriding Timeouts in Terraform
TimeoutMinutes int
}

type StateUpgradeDetails struct {
// The current schema version
SchemaVersion int
}
3 changes: 3 additions & 0 deletions tpgtools/overrides/dataproc/beta/workflow_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
details:
functions:
- tpgresource.DefaultProviderProject
- type: STATE_UPGRADE
details:
schemaversion: 1
3 changes: 3 additions & 0 deletions tpgtools/overrides/dataproc/workflow_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
details:
functions:
- tpgresource.DefaultProviderProject
- type: STATE_UPGRADE
details:
schemaversion: 1
19 changes: 19 additions & 0 deletions tpgtools/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ type Resource struct {
Reference *Link
// Guides point to non-rest useful context for the resource.
Guides []Link

// The current schema version
SchemaVersion int
// The schema versions from 0 to the current schema version
SchemaVersions []int
}

type Link struct {
Expand Down Expand Up @@ -682,6 +687,20 @@ func createResource(schema *openapi.Schema, info *openapi.Info, typeFetcher *Typ
res.TerraformProductName = &scpn
}

// Resource Override: StateUpgrade
stateUpgrade := StateUpgradeDetails{}
stateUpgradeOk, err := overrides.ResourceOverrideWithDetails(StateUpgrade, &stateUpgrade, location)
if err != nil {
return nil, fmt.Errorf("Failed to decode state upgrade details: %v", err)
}
if stateUpgradeOk {
res.SchemaVersion = stateUpgrade.SchemaVersion
res.SchemaVersions = make([]int, res.SchemaVersion)
for i := range res.SchemaVersions {
res.SchemaVersions[i] = i
}
}

res.Samples = res.loadSamples()

return &res, nil
Expand Down
13 changes: 13 additions & 0 deletions tpgtools/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func Resource{{$.PathType}}() *schema.Resource {
),
{{- end }}

{{- if gt $.SchemaVersion 0 }}
SchemaVersion: {{$.SchemaVersion}},
StateUpgraders: []schema.StateUpgrader{
{{- range $v := $.SchemaVersions }}
{
Type: resource{{$.PathType}}ResourceV{{$v}}().CoreConfigSchema().ImpliedType(),
Upgrade: Resource{{$.PathType}}UpgradeV{{$v}},
Version: {{$v}},
},
{{- end }}
},
{{- end }}

Schema: map[string]*schema.Schema{
{{- range $p := $.SchemaProperties }}
"{{$p.Name}}": {
Expand Down

0 comments on commit 1a9a20e

Please sign in to comment.