Skip to content

Commit

Permalink
Merge pull request #148 from hairyhenderson/aws-namespace
Browse files Browse the repository at this point in the history
Namespacing the aws funcs
  • Loading branch information
hairyhenderson authored Jun 9, 2017
2 parents 3837f48 + d8f2a93 commit cac0bc6
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 170 deletions.
20 changes: 0 additions & 20 deletions aws/ec2info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)

// test doubles
type DummyInstanceDescriber struct {
tags []*ec2.Tag
}

func (d DummyInstanceDescriber) DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
output := &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
Tags: d.tags,
},
},
},
},
}
return output, nil
}

func TestTag_MissingKey(t *testing.T) {
server, ec2meta := MockServer(200, `"i-1234"`)
defer server.Close()
Expand Down
37 changes: 37 additions & 0 deletions aws/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"net/http/httptest"
"net/url"

"github.com/aws/aws-sdk-go/service/ec2"
)

// MockServer -
Expand All @@ -24,3 +26,38 @@ func MockServer(code int, body string) (*httptest.Server, *Ec2Meta) {
client := &Ec2Meta{server.URL + "/", httpClient, false, make(map[string]string)}
return server, client
}

// NewDummyEc2Info -
func NewDummyEc2Info(metaClient *Ec2Meta) *Ec2Info {
i := &Ec2Info{
metaClient: metaClient,
describer: func() InstanceDescriber { return DummyInstanceDescriber{} },
}
return i
}

// NewDummyEc2Meta -
func NewDummyEc2Meta() *Ec2Meta {
return &Ec2Meta{nonAWS: true}
}

// DummyInstanceDescriber - test doubles
type DummyInstanceDescriber struct {
tags []*ec2.Tag
}

// DescribeInstances -
func (d DummyInstanceDescriber) DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
output := &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
Tags: d.tags,
},
},
},
},
}
return output, nil
}
31 changes: 0 additions & 31 deletions docs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ title = "gomplate documentation"

theme = "hugo-material-docs"


[params]
author = "hairyhenderson"
description = "gomplate documentation"
Expand All @@ -19,33 +18,3 @@ theme = "hugo-material-docs"
[social]
twitter = "hairyhenderson"
github = "hairyhenderson"

[[menu.main]]
name = "About"
url = "/"
weight = 1
pre = ""

[[menu.main]]
name = "Installing"
url = "installing/"
weight = 10
pre = ""

[[menu.main]]
name = "Usage"
url = "usage/"
weight = 11
pre = ""

[[menu.main]]
name = "Syntax"
url = "syntax/"
weight = 12
pre = ""

[[menu.main]]
name = "Built-in Functions"
url = "functions/"
weight = 13
pre = ""
75 changes: 75 additions & 0 deletions docs/content/functions/aws.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: aws functions
menu:
main:
parent: functions
---


## `aws.EC2Meta`

**Alias:** _(to be deprecated)_ `ec2meta`

Queries AWS [EC2 Instance Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for information. This only retrieves data in the `meta-data` path -- for data in the `dynamic` path use `aws.EC2Dynamic`.

This only works when running `gomplate` on an EC2 instance. If the EC2 instance metadata API isn't available, the tool will timeout and fail.

#### Example

```console
$ echo '{{aws.EC2Meta "instance-id"}}' | gomplate
i-12345678
```

## `aws.EC2Dynamic`

**Alias:** _(to be deprecated)_ `ec2dynamic`

Queries AWS [EC2 Instance Dynamic Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for information. This only retrieves data in the `dynamic` path -- for data in the `meta-data` path use `aws.EC2Meta`.

This only works when running `gomplate` on an EC2 instance. If the EC2 instance metadata API isn't available, the tool will timeout and fail.

#### Example

```console
$ echo '{{ (aws.EC2Dynamic "instance-identity/document" | json).region }}' | ./gomplate
us-east-1
```

## `aws.EC2Region`

**Alias:** _(to be deprecated)_ `ec2region`

Queries AWS to get the region. An optional default can be provided, or returns
`unknown` if it can't be determined for some reason.

#### Example

_In EC2_
```console
$ echo '{{ aws.EC2Region }}' | ./gomplate
us-east-1
```
_Not in EC2_
```console
$ echo '{{ aws.EC2Region }}' | ./gomplate
unknown
$ echo '{{ aws.EC2Region "foo" }}' | ./gomplate
foo
```

## `aws.EC2Tag`

**Alias:** _(to be deprecated)_ `ec2tag`

Queries the AWS EC2 API to find the value of the given [user-defined tag](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html). An optional default
can be provided.

#### Example

```console
$ echo 'This server is in the {{ aws.EC2Tag "Account" }} account.' | ./gomplate
foo
$ echo 'I am a {{ aws.EC2Tag "classification" "meat popsicle" }}.' | ./gomplate
I am a meat popsicle.
```
70 changes: 4 additions & 66 deletions docs/content/functions.md → docs/content/functions/general.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
title: Built-in functions
weight: 0
title: other functions
menu:
main:
parent: functions
---

In addition to all of the functions and operators that the [Go template](https://golang.org/pkg/text/template/)
language provides (`if`, `else`, `eq`, `and`, `or`, `range`, etc...), there are
some additional functions baked in to `gomplate`:

## `contains`

Contains reports whether the second string is contained within the first. Equivalent to
Expand Down Expand Up @@ -814,63 +812,3 @@ $ gomplate -d person.json -f input.tmpl
]
}
```

## `ec2meta`

Queries AWS [EC2 Instance Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for information. This only retrieves data in the `meta-data` path -- for data in the `dynamic` path use `ec2dynamic`.

This only works when running `gomplate` on an EC2 instance. If the EC2 instance metadata API isn't available, the tool will timeout and fail.

#### Example

```console
$ echo '{{ec2meta "instance-id"}}' | gomplate
i-12345678
```

## `ec2dynamic`

Queries AWS [EC2 Instance Dynamic Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for information. This only retrieves data in the `dynamic` path -- for data in the `meta-data` path use `ec2meta`.

This only works when running `gomplate` on an EC2 instance. If the EC2 instance metadata API isn't available, the tool will timeout and fail.

#### Example

```console
$ echo '{{ (ec2dynamic "instance-identity/document" | json).region }}' | ./gomplate
us-east-1
```

## `ec2region`

Queries AWS to get the region. An optional default can be provided, or returns
`unknown` if it can't be determined for some reason.

#### Example

_In EC2_
```console
$ echo '{{ ec2region }}' | ./gomplate
us-east-1
```
_Not in EC2_
```console
$ echo '{{ ec2region }}' | ./gomplate
unknown
$ echo '{{ ec2region "foo" }}' | ./gomplate
foo
```

## `ec2tag`

Queries the AWS EC2 API to find the value of the given [user-defined tag](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html). An optional default
can be provided.

#### Example

```console
$ echo 'This server is in the {{ ec2tag "Account" }} account.' | ./gomplate
foo
$ echo 'I am a {{ ec2tag "classification" "meat popsicle" }}.' | ./gomplate
I am a meat popsicle.
```
5 changes: 4 additions & 1 deletion docs/content/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
---
title: gomplate
type: index
weight: 0
weight: 1
menu:
main:
name: About
---

A [Go template](https://golang.org/pkg/text/template/)-based CLI tool. `gomplate` can be used as an alternative to
Expand Down
1 change: 1 addition & 0 deletions docs/content/installing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Installing
weight: 10
menu: main
---
# Installing

Expand Down
9 changes: 8 additions & 1 deletion docs/content/syntax.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Syntax
weight: 0
weight: 12
menu: main
---

## About `.Env`
Expand All @@ -13,3 +14,9 @@ Sometimes, this behaviour is desired; if the output is unusable without certain
strings, this is a sure way to know that variables are missing!

If you want different behaviour, try [`getenv`](../functions/#getenv).

## Built-in functions

In addition to all of the functions and operators that the [Go template](https://golang.org/pkg/text/template/)
language provides (`if`, `else`, `eq`, `and`, `or`, `range`, etc...), there are
some additional functions baked in to `gomplate`. See the links on the left.
3 changes: 2 additions & 1 deletion docs/content/usage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Usage
weight: 0
weight: 11
menu: main
---

The simplest usage of `gomplate` is to just replace environment
Expand Down
56 changes: 56 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"net/url"
"strings"
"text/template"

"github.com/hairyhenderson/gomplate/funcs"
)

// initFuncs - The function mappings are defined here!
func initFuncs(data *Data) template.FuncMap {
env := &Env{}
typeconv := &TypeConv{}
stringfunc := &stringFunc{}

f := template.FuncMap{
"getenv": env.Getenv,
"bool": typeconv.Bool,
"has": typeconv.Has,
"json": typeconv.JSON,
"jsonArray": typeconv.JSONArray,
"yaml": typeconv.YAML,
"yamlArray": typeconv.YAMLArray,
"toml": typeconv.TOML,
"csv": typeconv.CSV,
"csvByRow": typeconv.CSVByRow,
"csvByColumn": typeconv.CSVByColumn,
"slice": typeconv.Slice,
"indent": typeconv.indent,
"join": typeconv.Join,
"toJSON": typeconv.ToJSON,
"toJSONPretty": typeconv.toJSONPretty,
"toYAML": typeconv.ToYAML,
"toTOML": typeconv.ToTOML,
"toCSV": typeconv.ToCSV,
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replaceAll": stringfunc.replaceAll,
"split": strings.Split,
"splitN": strings.SplitN,
"title": strings.Title,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"trim": strings.Trim,
"trimSpace": strings.TrimSpace,
"urlParse": url.Parse,
"datasource": data.Datasource,
"ds": data.Datasource,
"datasourceExists": data.DatasourceExists,
"include": data.include,
}
funcs.AWSFuncs(f)
return f
}
Loading

0 comments on commit cac0bc6

Please sign in to comment.