Skip to content

Commit

Permalink
add docs validation and command output test
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallford committed Aug 17, 2024
1 parent 4bdd677 commit bb0378f
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 15 deletions.
8 changes: 6 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ endif
DOCKER := docker
DOCKER_RUN := $(DOCKER) run $(DOCKER_FLAGS)

GOPATH ?= $(shell go env GOPATH)
TERRAFORM_VERSION ?= 1.9.4

EDITORCONFIG_CHECKER_VERSION ?= 3.0.3
Expand All @@ -26,7 +27,10 @@ VENV := .venv
VENV_STAMP := $(VENV)/stamp
ACTIVATE := . $(VENV)/bin/activate

lint: lint/terraform lint/editorconfig lint/shellcheck lint/yamllint lint/go lint/ansible
lint: lint/docs lint/terraform lint/editorconfig lint/shellcheck lint/yamllint lint/go lint/ansible

lint/docs: docs
TFENV_TERRAFORM_VERSION=$(TERRAFORM_VERSION) $(GOPATH)/bin/tfplugindocs validate

lint/terraform:
terraform fmt -recursive -check
Expand Down Expand Up @@ -70,4 +74,4 @@ $(VENV_STAMP): requirements.txt
$(ACTIVATE); pip install -qr requirements.txt
touch $(VENV_STAMP)

.PHONY: lint lint/terraform lint/editorconfig lint/shellcheck lint/yamllint lint/go lint/ansible install test test/pkg test/acc docs deps bin/ansible-navigator
.PHONY: lint lint/docs lint/terraform lint/editorconfig lint/shellcheck lint/yamllint lint/go lint/ansible install test test/pkg test/acc docs deps bin/ansible-navigator
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/marshallford/terraform-provider-ansible)](https://goreportcard.com/report/github.com/marshallford/terraform-provider-ansible)
[![Acceptance Coverage](https://marshallford.github.io/terraform-provider-ansible/badge.svg)](https://marshallford.github.io/terraform-provider-ansible/cover.html)

Run Ansible playbooks using Terraform.
Run [Ansible](https://github.com/ansible/ansible) playbooks using Terraform.

```terraform
resource "ansible_navigator_run" "webservers_example" {
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
# tfplugindocs generate --website-temp-dir /tmp/docs
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "ansible Provider"
subcategory: ""
description: |-
Interact with Ansible https://github.com/ansible/ansible.
Run Ansible https://github.com/ansible/ansible playbooks using Terraform.
---

# ansible Provider

Interact with [Ansible](https://github.com/ansible/ansible).
Run [Ansible](https://github.com/ansible/ansible) playbooks using Terraform.

## Example Usage

Expand Down
16 changes: 16 additions & 0 deletions internal/provider/navigator_run_resource_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccNavigatorRunResource_errors_command_output(t *testing.T) {
t.Setenv("ANSIBLE_NAVIGATOR_CONTAINER_ENGINE", "test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run_resource", "errors", "command_output")),
ConfigVariables: testDefaultConfigVariables(t),
ExpectError: regexp.MustCompile("Ansible navigator run failed"),
},
},
})
}

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

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (p *AnsibleProvider) Metadata(ctx context.Context, req provider.MetadataReq

func (p *AnsibleProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Interact with Ansible.",
MarkdownDescription: "Interact with [Ansible](https://github.com/ansible/ansible).",
Description: "Run Ansible playbooks using Terraform.",
MarkdownDescription: "Run [Ansible](https://github.com/ansible/ansible) playbooks using Terraform.",
Attributes: map[string]schema.Attribute{
"base_run_directory": schema.StringAttribute{
Description: "Base directory in which to create run directories. On Unix systems this defaults to '$TMPDIR' if non-empty, else '/tmp'.",
Expand Down
16 changes: 9 additions & 7 deletions internal/provider/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ func run(ctx context.Context, diags *diag.Diagnostics, timeout time.Duration, op
}
}

err = ansible.QueryPlaybookArtifact(run.dir, run.artifactQueries)
addPathError(diags, path.Root("artifact_queries"), "Playbook artifact queries failed", err)

if run.options.KnownHosts {
knownHosts, err := ansible.GetKnownHosts(run.dir)
addPathError(diags, path.Root("ansible_options").AtMapKey("known_hosts"), "Failed to get known hosts", err)
run.knownHosts = knownHosts
if !diags.HasError() {
err = ansible.QueryPlaybookArtifact(run.dir, run.artifactQueries)
addPathError(diags, path.Root("artifact_queries"), "Playbook artifact queries failed", err)

if run.options.KnownHosts {
knownHosts, err := ansible.GetKnownHosts(run.dir)
addPathError(diags, path.Root("ansible_options").AtMapKey("known_hosts"), "Failed to get known hosts", err)
run.knownHosts = knownHosts
}
}

if !run.persistDir {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# appease linter
variable "ansible_navigator_binary" {
type = string
nullable = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# appease linter
variable "ansible_navigator_binary" {
type = string
nullable = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# appease linter
variable "ansible_navigator_binary" {
type = string
nullable = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# appease linter
variable "ansible_navigator_binary" {
type = string
nullable = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "ansible_navigator_run" "test" {
ansible_navigator_binary = var.ansible_navigator_binary
playbook = <<-EOT
- hosts: localhost
become: false
EOT
inventory = "# localhost"
}
19 changes: 19 additions & 0 deletions templates/index.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
# tfplugindocs generate --website-temp-dir /tmp/docs
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "{{.ProviderShortName}} Provider"
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---

# {{.ProviderShortName}} Provider

{{ .Description | trimspace }}

{{ if .HasExample -}}
## Example Usage

{{tffile .ExampleFile }}
{{- end }}

{{ .SchemaMarkdown | trimspace }}

0 comments on commit bb0378f

Please sign in to comment.