Skip to content

Commit

Permalink
use new dataflow templates, fix labels
Browse files Browse the repository at this point in the history
  • Loading branch information
emilymye committed Dec 17, 2019
1 parent 0e40826 commit d7a83db
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 250 deletions.
50 changes: 46 additions & 4 deletions third_party/terraform/resources/resource_dataflow_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"google.golang.org/api/googleapi"
)

const resourceDataflowJobGoogleProvidedLabelPrefix = "labels.goog-dataflow-provided"

var dataflowTerminalStatesMap = map[string]struct{}{
"JOB_STATE_DONE": {},
"JOB_STATE_FAILED": {},
Expand All @@ -22,12 +24,51 @@ var dataflowTerminalStatesMap = map[string]struct{}{
"JOB_STATE_DRAINED": {},
}

func resourceDataflowJobLabelDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if !strings.HasPrefix(k, "labels.") {
return false
}

// Example Diff: "labels.goog-dataflow-provided-template-version": "word_count" => ""
if strings.HasPrefix(k, resourceDataflowJobGoogleProvidedLabelPrefix) && new == "" {
// Suppress diff if field is a Google Dataflow-provided label key and has no explicitly set value in Config.
return true
}

// If k is comparing length of maps, compare old and new value.
if strings.HasPrefix(k, "labels.%") {
o, n := d.GetChange("labels")
oldLabels := o.(map[string]interface{})
newLabels := n.(map[string]interface{})

// If new labels includes labels not in old value, don't suppress.
for labelK, newV := range newLabels {
if oldV, ok := oldLabels[labelK]; !ok || oldV.(string) != newV.(string) {
return false
}
}
// If we reached this point, old labels is superset of new labels
// Don't suppress diff if a old-only label appears to be user-provided
for labelK := range oldLabels {
if _, ok := newLabels[labelK]; !ok {
if !strings.HasPrefix(labelK, resourceDataflowJobGoogleProvidedLabelPrefix) {
return false
}
}
}
// All old-only labels appear to be Google-created - ignore diff.
return true
}

// For other keys, don't suppress diff.
return false
}

func resourceDataflowJob() *schema.Resource {
return &schema.Resource{
Create: resourceDataflowJobCreate,
Read: resourceDataflowJobRead,
Delete: resourceDataflowJobDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -72,9 +113,10 @@ func resourceDataflowJob() *schema.Resource {
},

"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
DiffSuppressFunc: resourceDataflowJobLabelDiffSuppress,
},

"on_delete": {
Expand Down
Loading

0 comments on commit d7a83db

Please sign in to comment.