-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
233 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# linux_script | ||
|
||
Read arbritrary resource by specifying commands that will be uploaded and executed remotely. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
locals { | ||
package_name = "apache2" | ||
} | ||
resource "linux_script" "install_package" { | ||
lifecycle_commands { | ||
read = "apt-cache policy $PACKAGE_NAME | grep 'Installed:' | grep -v '(none)' | awk '{ print $2 }' | xargs | tr -d '\n'" | ||
} | ||
environment = { | ||
PACKAGE_NAME = local.package_name | ||
PACKAGE_VERSION = "2.4.18-2ubuntu3.4" | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
- `lifecycle_commands` - (Required) see [lifecycle_commands](#lifecycle_commands). | ||
- `interpreter` - (Optional, string list) Interpreter for running each `lifecycle_commands`. Default empty list. | ||
- `working_directory` - (Optional, string) The working directory where each `lifecycle_commands` is executed. Default empty string. | ||
- `environment` - (Optional, string map) A list of linux environment that will be available in each `lifecycle_commands`. Default empty map. | ||
- `sensitive_environment` - (Optional, string map) Just like `environment` except they don't show up in log files. In case of duplication, environment variables defined here will take precedence over the ones in `environment`. Default empty map. | ||
|
||
### lifecycle_commands | ||
|
||
Block that contains commands to be uploaded and remotely executed in Terraform. | ||
|
||
- `read` - (Required, string) Commands that will be executed to obtain data regarding the arbritrary resource. Terraform will record the output of these commands inside `output` attributes. | ||
|
||
## Attribute Reference | ||
|
||
- `output` - (string) The raw output of `read` commands. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package linux | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var schemaScriptDataSource = func() (m map[string]*schema.Schema) { | ||
m = make(map[string]*schema.Schema) | ||
for k, v := range schemaScriptResource { | ||
switch k { | ||
default: | ||
m[k] = v | ||
|
||
case attrScriptLifecycleCommands: | ||
m[k] = new(schema.Schema) | ||
*m[k] = *v | ||
m[k].Elem = &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
attrScriptLifecycleCommandRead: v.Elem.(*schema.Resource).Schema[attrScriptLifecycleCommandRead], | ||
}, | ||
} | ||
|
||
case attrScriptTriggers: | ||
case attrScriptDirtyOutput: | ||
case attrScriptFaultyOutput: | ||
} | ||
} | ||
return | ||
}() | ||
|
||
type handlerScriptDataSource struct { | ||
hsr handlerScriptResource | ||
} | ||
|
||
func (h handlerScriptDataSource) Read(ctx context.Context, rd *schema.ResourceData, meta interface{}) (d diag.Diagnostics) { | ||
err := h.hsr.read(ctx, rd, meta.(*linux)) | ||
if err != nil { | ||
d = diag.FromErr(err) | ||
} | ||
rd.SetId("static") | ||
return | ||
} | ||
|
||
func scriptDataSource() *schema.Resource { | ||
h := handlerScriptDataSource{hsr: handlerScriptResource{}} | ||
return &schema.Resource{ | ||
Schema: schemaScriptDataSource, | ||
ReadContext: h.Read, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package linux | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAccLinuxDataScriptBasic(t *testing.T) { | ||
file := fmt.Sprintf(`"/tmp/linux-%s.yml"`, acctest.RandString(16)) | ||
conf := tfConf{ | ||
Provider: testAccProvider, | ||
Script: tfScript{ | ||
Interpreter: tfList{`"sh"`, `"-c"`}, | ||
LifecycleCommands: tfmap{ | ||
attrScriptLifecycleCommandRead: `"cat $FILE"`, | ||
attrScriptLifecycleCommandCreate: `"echo -n $CONTENT > $FILE"`, | ||
attrScriptLifecycleCommandUpdate: `"echo -n $CONTENT > $FILE"`, | ||
attrScriptLifecycleCommandDelete: `"rm $FILE"`, | ||
}, | ||
Environment: tfmap{ | ||
"FILE": file, | ||
"CONTENT": `"helloworld"`, | ||
}, | ||
}, | ||
DataScript: tfScript{ | ||
Interpreter: tfList{`"sh"`, `"-c"`}, | ||
LifecycleCommands: tfmap{ | ||
attrScriptLifecycleCommandRead: `"cat $FILE"`, | ||
}, | ||
Environment: tfmap{ | ||
"FILE": file, | ||
}, | ||
}, | ||
} | ||
failedScripte := conf.Copy(func(tc *tfConf) { | ||
tc.DataScript.LifecycleCommands.With(attrScriptLifecycleCommandRead, `"cat $FILE.notexist"`) | ||
}) | ||
updatedContent := conf.Copy(func(tc *tfConf) { | ||
tc.Script.Environment.With("CONTENT", `"helloworld1"`) | ||
}) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ExternalProviders: map[string]resource.ExternalProvider{"null": {}}, | ||
PreCheck: testAccPreCheckConnection(t), | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLinuxDataScriptBasicConfig(t, conf), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("null_resource.output", "triggers.output", strings.Trim(conf.Script.Environment["CONTENT"], `"`)), | ||
), | ||
}, | ||
{ | ||
Config: testAccLinuxDataScriptBasicConfig(t, failedScripte), | ||
ExpectError: regexp.MustCompile("No such file or directory"), | ||
}, | ||
{ | ||
Config: testAccLinuxDataScriptBasicConfig(t, updatedContent), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("null_resource.output", "triggers.output", strings.Trim(updatedContent.Script.Environment["CONTENT"], `"`)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLinuxDataScriptBasicConfig(t *testing.T, conf tfConf) (s string) { | ||
tf := heredoc.Doc(` | ||
provider "linux" { | ||
{{- .Provider.Serialize | nindent 4 }} | ||
} | ||
resource "linux_script" "linux_script" { | ||
{{ .Script.Serialize | nindent 4 }} | ||
} | ||
data "linux_script" "linux_script" { | ||
depends_on = [ linux_script.linux_script ] | ||
{{ .DataScript.Serialize | nindent 4 }} | ||
} | ||
resource "null_resource" "output" { | ||
triggers = { | ||
output = data.linux_script.linux_script.output | ||
} | ||
} | ||
`) | ||
|
||
s, err := conf.compile(tf) | ||
t.Log(s) | ||
require.NoError(t, err, "compile template failed") | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters