{"action":"created","comment":{"url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/comments/216898105","pull_request_review_id":154485555,"id":216898105,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxNjg5ODEwNQ==","diff_hunk":"@@ -67,85 +68,124 @@ func resourceShellScript() *schema.Resource {\n \t}\n }\n \n-func resourceShellScriptDelete(d *schema.ResourceData, meta interface{}) error {\n-\tlog.Printf(\"[DEBUG] Deleting shell script resource\")\n+func resourceShellScriptCreate(d *schema.ResourceData, meta interface{}) error {\n \tl := d.Get(\"lifecycle_commands\").([]interface{})\n \tc := l[0].(map[string]interface{})\n-\tcommand := c[\"delete\"].(string)\n+\tcommand := c[\"create\"].(string)\n \tvars := d.Get(\"environment\").(map[string]interface{})\n \tenvironment := readEnvironmentVariables(vars)\n \tworkingDirectory := d.Get(\"working_directory\").(string)\n-\tinput := d.Get(\"stdout\").(string)\n \t//obtain exclusive lock\n \tshellMutexKV.Lock(shellScriptMutexKey)\n-\tdefer shellMutexKV.Unlock(shellScriptMutexKey)\n-\n-\t_, _, err := runCommand(command, input, environment, workingDirectory)\n+\t\n+\textraout, stdout, _, err := runCommand(command, \"\", environment, workingDirectory)\n \tif err != nil {\n \t\treturn err\n \t}\n+\tlog.Printf(stdout)\n \n-\td.SetId(\"\")\n-\treturn nil\n+\t//try and parse extraout as JSON to expose, could just be a string\n+\toutput, err := parseJSON(extraout)\n+\tif err != nil {\n+\t\tlog.Printf(\"[DEBUG] error parsing sdout into json: %v\", err)\n+\t\toutput = make(map[string]string)\n+\t\td.Set(\"output\", output)\n+\t} else {\n+\t\td.Set(\"output\", output)\n+\t}\n+\n+\t//create random uuid for the id, changes to inputs will prompt update or recreate so it doesn't need to change\n+\tidBytes := make([]byte, 16)\n+    _, randErr := rand.Read(idBytes)\n+    if randErr != nil {\n+        return randErr\n+    }\n+    d.SetId(hash(string(idBytes[0:])))\n+\n+\t//once creation has finished setup state so update works\n+\tif len(extraout) > 0 {\n+\t\t//if we have some content on extraout (used for diff) use that\n+\t\td.Set(\"state\", extraout)\n+\t\tshellMutexKV.Unlock(shellScriptMutexKey)\n+\t\treturn nil\n+\n+\t} else {\n+\t\tshellMutexKV.Unlock(shellScriptMutexKey)","path":"shell/resource_shell_script.go","position":107,"original_position":107,"commit_id":"a0c6b8fc3d692bab28fec570fa1a9a2b1fe7a12d","original_commit_id":"a0c6b8fc3d692bab28fec570fa1a9a2b1fe7a12d","user":{"login":"nhoughto","id":1276450,"node_id":"MDQ6VXNlcjEyNzY0NTA=","avatar_url":"https://avatars0.githubusercontent.com/u/1276450?v=4","gravatar_id":"","url":"https://api.github.com/users/nhoughto","html_url":"https://github.com/nhoughto","followers_url":"https://api.github.com/users/nhoughto/followers","following_url":"https://api.github.com/users/nhoughto/following{/other_user}","gists_url":"https://api.github.com/users/nhoughto/gists{/gist_id}","starred_url":"https://api.github.com/users/nhoughto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhoughto/subscriptions","organizations_url":"https://api.github.com/users/nhoughto/orgs","repos_url":"https://api.github.com/users/nhoughto/repos","events_url":"https://api.github.com/users/nhoughto/events{/privacy}","received_events_url":"https://api.github.com/users/nhoughto/received_events","type":"User","site_admin":false},"body":"can't defer as then the read() call can't lock the mutex, so need to manually unlock","created_at":"2018-09-12T05:07:51Z","updated_at":"2018-09-12T05:07:52Z","html_url":"https://github.com/scottwinkler/terraform-provider-shell/pull/2#discussion_r216898105","pull_request_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/comments/216898105"},"html":{"href":"https://github.com/scottwinkler/terraform-provider-shell/pull/2#discussion_r216898105"},"pull_request":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2"}},"in_reply_to_id":216774163},"pull_request":{"url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2","id":214171742,"node_id":"MDExOlB1bGxSZXF1ZXN0MjE0MTcxNzQy","html_url":"https://github.com/scottwinkler/terraform-provider-shell/pull/2","diff_url":"https://github.com/scottwinkler/terraform-provider-shell/pull/2.diff","patch_url":"https://github.com/scottwinkler/terraform-provider-shell/pull/2.patch","issue_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/2","number":2,"state":"open","locked":false,"title":"WIP - Update support plus some rejigging","user":{"login":"nhoughto","id":1276450,"node_id":"MDQ6VXNlcjEyNzY0NTA=","avatar_url":"https://avatars0.githubusercontent.com/u/1276450?v=4","gravatar_id":"","url":"https://api.github.com/users/nhoughto","html_url":"https://github.com/nhoughto","followers_url":"https://api.github.com/users/nhoughto/followers","following_url":"https://api.github.com/users/nhoughto/following{/other_user}","gists_url":"https://api.github.com/users/nhoughto/gists{/gist_id}","starred_url":"https://api.github.com/users/nhoughto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhoughto/subscriptions","organizations_url":"https://api.github.com/users/nhoughto/orgs","repos_url":"https://api.github.com/users/nhoughto/repos","events_url":"https://api.github.com/users/nhoughto/events{/privacy}","received_events_url":"https://api.github.com/users/nhoughto/received_events","type":"User","site_admin":false},"body":"Super rough WIP of what we're currently working through as an FYI\r\n\r\nCurrently using it with `kubectl` and is working well, as discussed in #1 it is a bit of a departure from your existing usage. Notably i've rejigged how passing back the state works, so rather than using `stdout` which is hard to control (having to pipe every script cmd somewhere), i've made it opt-in, you pipe important output to an extra fd via `... >3&`.\r\n\r\nProcess terraform goes through:\r\n1. Refreshes data which in this simple example reads the `image` tag in the manifest\r\n2. Checks if resource already exists in state, if it does it will compare the `state` property against the local value calculated in step 1 (image tag comparison)\r\n3. If value has changed then it will trigger an update script, `kubectl` will re-align the image\r\n4. If no value change then apply complete\r\n5. If no resource, then do a create.\r\n\r\nExample usage:\r\n\r\n```\r\ndata \"shell_script\" \"test_state\" {\r\n  lifecycle_commands {\r\n    read = <<EOF\r\nkubectl convert --local -o jsonpath=\"{.spec.template.spec.containers[*].image}\" -f ~/pod.yml\r\nEOF\r\n  }\r\n}\r\n\r\nresource \"shell_script\" \"test\" {\r\n  state = \"${data.shell_script.test_state.stdout}\"\r\n  lifecycle_commands {\r\n    read = <<EOF\r\nkubectl get deploy -o jsonpath=\"{.items[*].spec.template.spec.containers[*].image}\" >&3\r\nEOF\r\n    create = <<EOF\r\nkubectl apply -f ~/pod.yml\r\nEOF\r\n    update = <<EOF\r\nkubectl apply -f ~/pod.yml\r\nEOF\r\n    delete = <<EOF\r\nkubectl delete --ignore-not-found -f ~/pod.yml\r\nEOF\r\n  }\r\n}\r\n```\r\n\r\npod.yml\r\n```\r\napiVersion: apps/v1beta1\r\nkind: Deployment\r\nmetadata:\r\n  name: testdeploy\r\nspec:\r\n  replicas: 1\r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: test\r\n    spec:\r\n      containers:\r\n      - name: pod\r\n        image: alpine@sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0\r\n        command: [\"sleep\"]\r\n        args: [\"1800\"]\r\n```","created_at":"2018-09-09T23:42:44Z","updated_at":"2018-09-12T05:07:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f2e42d512b9683b500e04aba7f8d740c642ef1f3","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2/commits","review_comments_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2/comments","review_comment_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/2/comments","statuses_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/statuses/a0c6b8fc3d692bab28fec570fa1a9a2b1fe7a12d","head":{"label":"bsycorp:master","ref":"master","sha":"a0c6b8fc3d692bab28fec570fa1a9a2b1fe7a12d","user":{"login":"bsycorp","id":41216027,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQxMjE2MDI3","avatar_url":"https://avatars1.githubusercontent.com/u/41216027?v=4","gravatar_id":"","url":"https://api.github.com/users/bsycorp","html_url":"https://github.com/bsycorp","followers_url":"https://api.github.com/users/bsycorp/followers","following_url":"https://api.github.com/users/bsycorp/following{/other_user}","gists_url":"https://api.github.com/users/bsycorp/gists{/gist_id}","starred_url":"https://api.github.com/users/bsycorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsycorp/subscriptions","organizations_url":"https://api.github.com/users/bsycorp/orgs","repos_url":"https://api.github.com/users/bsycorp/repos","events_url":"https://api.github.com/users/bsycorp/events{/privacy}","received_events_url":"https://api.github.com/users/bsycorp/received_events","type":"Organization","site_admin":false},"repo":{"id":148074020,"node_id":"MDEwOlJlcG9zaXRvcnkxNDgwNzQwMjA=","name":"terraform-provider-shell","full_name":"bsycorp/terraform-provider-shell","owner":{"login":"bsycorp","id":41216027,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQxMjE2MDI3","avatar_url":"https://avatars1.githubusercontent.com/u/41216027?v=4","gravatar_id":"","url":"https://api.github.com/users/bsycorp","html_url":"https://github.com/bsycorp","followers_url":"https://api.github.com/users/bsycorp/followers","following_url":"https://api.github.com/users/bsycorp/following{/other_user}","gists_url":"https://api.github.com/users/bsycorp/gists{/gist_id}","starred_url":"https://api.github.com/users/bsycorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsycorp/subscriptions","organizations_url":"https://api.github.com/users/bsycorp/orgs","repos_url":"https://api.github.com/users/bsycorp/repos","events_url":"https://api.github.com/users/bsycorp/events{/privacy}","received_events_url":"https://api.github.com/users/bsycorp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bsycorp/terraform-provider-shell","description":null,"fork":true,"url":"https://api.github.com/repos/bsycorp/terraform-provider-shell","forks_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/forks","keys_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/teams","hooks_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/hooks","issue_events_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/issues/events{/number}","events_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/events","assignees_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/assignees{/user}","branches_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/branches{/branch}","tags_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/tags","blobs_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/git/refs{/sha}","trees_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/statuses/{sha}","languages_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/languages","stargazers_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/stargazers","contributors_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/contributors","subscribers_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/subscribers","subscription_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/subscription","commits_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/commits{/sha}","git_commits_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/git/commits{/sha}","comments_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/comments{/number}","issue_comment_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/issues/comments{/number}","contents_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/contents/{+path}","compare_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/merges","archive_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/downloads","issues_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/issues{/number}","pulls_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/pulls{/number}","milestones_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/milestones{/number}","notifications_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/labels{/name}","releases_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/releases{/id}","deployments_url":"https://api.github.com/repos/bsycorp/terraform-provider-shell/deployments","created_at":"2018-09-09T23:19:56Z","updated_at":"2018-09-09T23:35:43Z","pushed_at":"2018-09-09T23:35:42Z","git_url":"git://github.com/bsycorp/terraform-provider-shell.git","ssh_url":"git@github.com:bsycorp/terraform-provider-shell.git","clone_url":"https://github.com/bsycorp/terraform-provider-shell.git","svn_url":"https://github.com/bsycorp/terraform-provider-shell","homepage":null,"size":9,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"scottwinkler:master","ref":"master","sha":"2eba207020c726b3c720f592a31f10efbfad5cc6","user":{"login":"scottwinkler","id":25599364,"node_id":"MDQ6VXNlcjI1NTk5MzY0","avatar_url":"https://avatars1.githubusercontent.com/u/25599364?v=4","gravatar_id":"","url":"https://api.github.com/users/scottwinkler","html_url":"https://github.com/scottwinkler","followers_url":"https://api.github.com/users/scottwinkler/followers","following_url":"https://api.github.com/users/scottwinkler/following{/other_user}","gists_url":"https://api.github.com/users/scottwinkler/gists{/gist_id}","starred_url":"https://api.github.com/users/scottwinkler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottwinkler/subscriptions","organizations_url":"https://api.github.com/users/scottwinkler/orgs","repos_url":"https://api.github.com/users/scottwinkler/repos","events_url":"https://api.github.com/users/scottwinkler/events{/privacy}","received_events_url":"https://api.github.com/users/scottwinkler/received_events","type":"User","site_admin":false},"repo":{"id":136586484,"node_id":"MDEwOlJlcG9zaXRvcnkxMzY1ODY0ODQ=","name":"terraform-provider-shell","full_name":"scottwinkler/terraform-provider-shell","owner":{"login":"scottwinkler","id":25599364,"node_id":"MDQ6VXNlcjI1NTk5MzY0","avatar_url":"https://avatars1.githubusercontent.com/u/25599364?v=4","gravatar_id":"","url":"https://api.github.com/users/scottwinkler","html_url":"https://github.com/scottwinkler","followers_url":"https://api.github.com/users/scottwinkler/followers","following_url":"https://api.github.com/users/scottwinkler/following{/other_user}","gists_url":"https://api.github.com/users/scottwinkler/gists{/gist_id}","starred_url":"https://api.github.com/users/scottwinkler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottwinkler/subscriptions","organizations_url":"https://api.github.com/users/scottwinkler/orgs","repos_url":"https://api.github.com/users/scottwinkler/repos","events_url":"https://api.github.com/users/scottwinkler/events{/privacy}","received_events_url":"https://api.github.com/users/scottwinkler/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/scottwinkler/terraform-provider-shell","description":null,"fork":false,"url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell","forks_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/forks","keys_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/teams","hooks_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/hooks","issue_events_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/events{/number}","events_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/events","assignees_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/assignees{/user}","branches_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/branches{/branch}","tags_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/tags","blobs_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/git/refs{/sha}","trees_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/statuses/{sha}","languages_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/languages","stargazers_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/stargazers","contributors_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/contributors","subscribers_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/subscribers","subscription_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/subscription","commits_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/commits{/sha}","git_commits_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/git/commits{/sha}","comments_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/comments{/number}","issue_comment_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/comments{/number}","contents_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/contents/{+path}","compare_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/merges","archive_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/downloads","issues_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues{/number}","pulls_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls{/number}","milestones_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/milestones{/number}","notifications_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/labels{/name}","releases_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/releases{/id}","deployments_url":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/deployments","created_at":"2018-06-08T07:56:50Z","updated_at":"2018-09-07T03:35:45Z","pushed_at":"2018-09-09T23:42:45Z","git_url":"git://github.com/scottwinkler/terraform-provider-shell.git","ssh_url":"git@github.com:scottwinkler/terraform-provider-shell.git","clone_url":"https://github.com/scottwinkler/terraform-provider-shell.git","svn_url":"https://github.com/scottwinkler/terraform-provider-shell","homepage":null,"size":8,"stargazers_count":2,"watchers_count":2,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":2,"license":null,"forks":1,"open_issues":2,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2"},"html":{"href":"https://github.com/scottwinkler/terraform-provider-shell/pull/2"},"issue":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/2"},"comments":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/scottwinkler/terraform-provider-shell/statuses/a0c6b8fc3d692bab28fec570fa1a9a2b1fe7a12d"}},"author_association":"NONE"}}
{"actor":{"display_login":"nhoughto"}}