Skip to content

Commit

Permalink
Merge pull request #321 from asteris-llc/feature/lists-in-params
Browse files Browse the repository at this point in the history
Refactor deserializers
  • Loading branch information
BrianHicks authored Sep 29, 2016
2 parents 6c3ebb0 + 68ac323 commit 36b2192
Show file tree
Hide file tree
Showing 36 changed files with 1,185 additions and 751 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ docs_source/public

# examples
terraform.tfstate*
.vagrant
.vagrant
62 changes: 43 additions & 19 deletions docs_source/content/resource-authors-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,55 @@ type Preparer struct {
Apply string `hcl:"apply"`
}

func (p *Preparer) Render(r *resource.Renderer) (resource.Task, error) {
check, err := render.Render("check", p.Check)
if err != nil {
return nil, err
}

apply, err := render.Render("apply", p.Apply)
if err != nil {
return nil, err
}

func (p *Preparer) Prepare(resource.Renderer) (resource.Task, error) {
return &MyShellTask{CheckStmt: check, ApplyStmt: apply}, nil
}
```

"What's the renderer doing there," you may ask, "and how can I accept non-string
values?" If you need to accept non-string parameters directly, the fields should
be `interface{}`s, and you should do all your type casting, conversion, and
validation in Prepare before the values are passed down to your Task.
To get values other than strings (bools, ints, et cetera), you just need to
specify them. Converge will render the values and parse them from strings, if
necessary.

### Struct Tags

Other than `hcl` (which is used to specify the field name you'll accept) the
following struct tags control the values you get:

- `doc_type`: control the exact printed type in the documentation. Example:
fields that accept
a [duration string](https://golang.org/pkg/time/#ParseDuration) (such
as [task.timeout]({{< ref "resources/task.md" >}})) are commonly strings
with a `doc_type` of "duration string"

- `base`: used with numeric types to indicate a base for parsing. Does not work
with floats. Example: [file.mode]({{< ref "resources/file.mode.md" >}})
needs an octal number, and specifies that in this tag.

We can also do some basic validation tasks with tags:

- `required`: one valid value: `true`. If set, this field must be set in the
HCL, but may still have a zero value (for example, `int` can still be `0`.)
Example: [docker.container]{{< ref "resources/docker.container.md" >}} uses
this to require an image for the container.

- `mutually_exclusive`: a comma-separated list of fields that cannot be set
together. Example: [user]({{< ref "resources/user.user.md" >}}) uses this
to disallow setting both `groupname` and `gid`.

- `valid_values`: a comma-separated list of values that will be accepted for
this field.
Example: [docker.container]({{< ref "resources/docker.container.md" >}}) uses
this to enforce status is only `running` or `created`.

### The Renderer

The renderer is what allows your values to take input from the environment (like
calls to `param` or `lookup`.) Normally you'll get a string value back, but when
you get an error it is very important to return it exactly as received. Converge
will automatically wrap the errors from Render to provide context to the user
and to defer rendering of lookup nodes until they're resolved.
calls to `param` or `lookup`.) Normally you won't need to use this, but if
you're doing something extremely custom it will be handy. If you get an error
while using the `Renderer`, return it exactly as received or wrap it with
[`errors.Wrap` or `errors.Wrapf`](https://github.com/pkg/errors). Converge uses
these signals to calculate execution order, so it needs to be able to inspect
the returned error value.

## Registering

Expand Down
13 changes: 8 additions & 5 deletions docs_source/content/resources/docker.container.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "docker.container"
slug: "docker-container"
date: "2016-09-20T14:45:55-05:00"
date: "2016-09-28T10:46:11-05:00"
menu:
main:
parent: resources
Expand Down Expand Up @@ -34,11 +34,11 @@ docker.container "nginx" {

## Parameters

- `name` (string)
- `name` (required string)

name of the container

- `image` (string)
- `image` (required string)

the image name or ID to use for the container

Expand All @@ -56,7 +56,7 @@ docker.container "nginx" {

- `env` (map of string to string)

set environmnet variables in the container
set environment variables in the container

- `expose` (list of strings)

Expand Down Expand Up @@ -94,7 +94,10 @@ Specified as a boolean value

- `status` (string)

the desired status of the container. running|created

Valid values: `running` and `created`

the desired status of the container.

- `force` (bool)

Expand Down
4 changes: 2 additions & 2 deletions docs_source/content/resources/file.mode.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "file.mode"
slug: "file-mode"
date: "2016-09-08T23:18:02-07:00"
date: "2016-09-27T18:19:08-05:00"
menu:
main:
parent: resources
Expand Down Expand Up @@ -34,7 +34,7 @@ file.mode "render" {
file must exist on the system (for example, having been created with
`file.content`.)

- `mode` (octal string)
- `mode` (base 8 uint32)

Mode is the mode of the file, specified in octal.

Expand Down
4 changes: 2 additions & 2 deletions docs_source/content/resources/param.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "param"
slug: "param"
date: "2016-09-20T14:45:55-05:00"
date: "2016-09-27T18:19:08-05:00"
menu:
main:
parent: resources
Expand Down Expand Up @@ -34,7 +34,7 @@ task "render" {

## Parameters

- `default` (anything scalar)
- `default` (anything)

Default is an optional field that provides a default value if none is
provided to this parameter. If this field is not set, this param will be
Expand Down
5 changes: 4 additions & 1 deletion docs_source/content/resources/task.query.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "task.query"
slug: "task-query"
date: "2016-09-08T23:26:25-07:00"
date: "2016-09-27T18:19:08-05:00"
menu:
main:
parent: resources
Expand Down Expand Up @@ -46,3 +46,6 @@ file.content "hostname data" {


- `env` (map of string to string)



10 changes: 6 additions & 4 deletions docs_source/content/resources/user.group.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "user.group"
slug: "user-group"
date: "2016-09-21T14:04:24-05:00"
date: "2016-09-28T10:46:11-05:00"
menu:
main:
parent: resources
Expand All @@ -24,17 +24,19 @@ user.group "group" {

## Parameters

- `gid` (string)
- `gid` (uint32)

Gid is the group gid.

- `name` (string)
- `name` (required string)

Name is the group name.

- `state` (string)


Valid values: `present` and `absent`

State is whether the group should be present.
Options are present and absent; default is present.


20 changes: 14 additions & 6 deletions docs_source/content/resources/user.user.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "user.user"
slug: "user-user"
date: "2016-09-26T09:00:07-05:00"
date: "2016-09-28T10:46:11-05:00"
menu:
main:
parent: resources
Expand All @@ -24,20 +24,26 @@ user.user "user" {

## Parameters

- `username` (string)
- `username` (required string)

Username is the user login name.

- `uid` (string)
- `uid` (uint32)

UID is the user ID.

- `groupname` (string)

Groupname is the primary group for user and must already exist.

Only one of `gid` or `groupname` may be set.

GroupName is the primary group for user and must already exist.
Only one of GID or Groupname may be indicated.

- `gid` (string)
- `gid` (uint32)


Only one of `gid` or `groupname` may be set.

Gid is the primary group ID for user and must refer to an existing group.
Only one of GID or Groupname may be indicated.
Expand All @@ -53,7 +59,9 @@ name is appended to the home directory.

- `state` (string)


Valid values: `present` and `absent`

State is whether the user should be present.
Options are present and absent; default is present.


Binary file removed docs_source/extract
Binary file not shown.
Loading

0 comments on commit 36b2192

Please sign in to comment.