Skip to content

Commit

Permalink
Use workflow short name rather than long name, state migrate (#4669) (#…
Browse files Browse the repository at this point in the history
…8869)

* Use workflow short name rather than long name, state migrate

* Fmt

* Fmt again

* Format

* Fmt problems

* Format for the last time!

* Update mmv1/third_party/terraform/tests/resource_workflows_workflow_test.go

Co-authored-by: Scott Suarez <[email protected]>

Co-authored-by: Scott Suarez <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Scott Suarez <[email protected]>
  • Loading branch information
modular-magician and ScottSuarez authored Apr 7, 2021
1 parent 1413a2d commit 64cbb7a
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .changelog/4669.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
workflows: fixed a bug in `google_workflows_workflow` that could cause inconsistent final plan errors when using the `name` field in other resources
```
127 changes: 115 additions & 12 deletions google/resource_workflows_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package google

import (
"context"
"fmt"
"log"
"reflect"
Expand All @@ -38,6 +39,15 @@ func resourceWorkflowsWorkflow() *schema.Resource {
Delete: schema.DefaultTimeout(6 * time.Minute),
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceWorkflowsWorkflowResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceWorkflowsWorkflowUpgradeV0,
Version: 0,
},
},

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Expand All @@ -52,12 +62,11 @@ func resourceWorkflowsWorkflow() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `Name of the Workflow.`,
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Name of the Workflow.`,
},
"region": {
Type: schema.TypeString,
Expand Down Expand Up @@ -189,7 +198,7 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
}

// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -212,7 +221,7 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
}

// This may have caused the ID to update - update it if so.
id, err = replaceVars(d, config, "{{name}}")
id, err = replaceVars(d, config, "projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -230,7 +239,7 @@ func resourceWorkflowsWorkflowRead(d *schema.ResourceData, meta interface{}) err
return err
}

url, err := replaceVars(d, config, "{{WorkflowsBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{WorkflowsBasePath}}projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -322,7 +331,7 @@ func resourceWorkflowsWorkflowUpdate(d *schema.ResourceData, meta interface{}) e
return err
}

url, err := replaceVars(d, config, "{{WorkflowsBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{WorkflowsBasePath}}projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -383,7 +392,7 @@ func resourceWorkflowsWorkflowDelete(d *schema.ResourceData, meta interface{}) e
}
billingProject = project

url, err := replaceVars(d, config, "{{WorkflowsBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{WorkflowsBasePath}}projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -414,7 +423,10 @@ func resourceWorkflowsWorkflowDelete(d *schema.ResourceData, meta interface{}) e
}

func flattenWorkflowsWorkflowName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
if v == nil {
return v
}
return NameFromSelfLinkStateFunc(v)
}

func flattenWorkflowsWorkflowDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand Down Expand Up @@ -492,3 +504,94 @@ func resourceWorkflowsWorkflowEncoder(d *schema.ResourceData, meta interface{},

return obj, nil
}

func resourceWorkflowsWorkflowResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `Description of the workflow provided by the user. Must be at most 1000 unicode characters long.`,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `A set of key/value label pairs to assign to this Workflow.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Name of the Workflow.`,
},
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The region of the workflow.`,
},
"service_account": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `Name of the service account associated with the latest workflow version. This service
account represents the identity of the workflow and determines what permissions the workflow has.
Format: projects/{project}/serviceAccounts/{account}.`,
},
"source_contents": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Workflow code to be executed. The size limit is 32KB.`,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `The timestamp of when the workflow was created in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits.`,
},
"revision_id": {
Type: schema.TypeString,
Computed: true,
Description: `The revision of the workflow. A new one is generated if the service account or source contents is changed.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `State of the workflow deployment.`,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: `The timestamp of when the workflow was last updated in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits.`,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func resourceWorkflowsWorkflowUpgradeV0(_ context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)

rawState["name"] = GetResourceNameFromSelfLink(rawState["name"].(string))

log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
return rawState, nil
}
2 changes: 1 addition & 1 deletion google/resource_workflows_workflow_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func testAccCheckWorkflowsWorkflowDestroyProducer(t *testing.T) func(s *terrafor

config := googleProviderConfig(t)

url, err := replaceVarsForTest(config, rs, "{{WorkflowsBasePath}}{{name}}")
url, err := replaceVarsForTest(config, rs, "{{WorkflowsBasePath}}projects/{{project}}/locations/{{region}}/workflows/{{name}}")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion google/resource_workflows_workflow_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func testSweepWorkflowsWorkflow(region string) error {
continue
}

deleteTemplate := "https://workflows.googleapis.com/v1/{{name}}"
deleteTemplate := "https://workflows.googleapis.com/v1/projects/{{project}}/locations/{{region}}/workflows/{{name}}"
deleteUrl, err := replaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
Expand Down
46 changes: 46 additions & 0 deletions google/resource_workflows_workflow_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -105,3 +106,48 @@ EOF
}
`, name, name)
}

func TestWorkflowsWorkflowStateUpgradeV0(t *testing.T) {
t.Parallel()

cases := map[string]struct {
Attributes map[string]interface{}
Expected map[string]string
Meta interface{}
}{
"shorten long name": {
Attributes: map[string]interface{}{
"name": "projects/my-project/locations/us-central1/workflows/my-workflow",
},
Expected: map[string]string{
"name": "my-workflow",
},
Meta: &Config{},
},
"short name stays": {
Attributes: map[string]interface{}{
"name": "my-workflow",
},
Expected: map[string]string{
"name": "my-workflow",
},
Meta: &Config{},
},
}
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
actual, err := resourceWorkflowsWorkflowUpgradeV0(context.Background(), tc.Attributes, tc.Meta)

if err != nil {
t.Error(err)
}

for _, expectedName := range tc.Expected {
if actual["name"] != expectedName {
t.Errorf("expected: name -> %#v\n got: name -> %#v\n in: %#v",
expectedName, actual["name"], actual)
}
}
})
}
}
2 changes: 1 addition & 1 deletion website/docs/r/workflows_workflow.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ The following arguments are supported:

In addition to the arguments listed above, the following computed attributes are exported:

* `id` - an identifier for the resource with format `{{name}}`
* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{region}}/workflows/{{name}}`

* `create_time` -
The timestamp of when the workflow was created in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits.
Expand Down

0 comments on commit 64cbb7a

Please sign in to comment.