~mna/bungee

0610c44ce8cd96ddc9371b7b45470d028846940a — Martin Angers 1 year, 6 months ago a162538 master
pkg/commands/generate: test api
M pkg/commands/generate/resources.go => pkg/commands/generate/resources.go +49 -0
@@ 168,6 168,55 @@ func (b *bucketObject) encode(w io.Writer, indent string) error {
	return err
}

type api struct {
	ID   string
	Name string

	EndpointConfiguration *apiEndpointConfiguration
}

func (a *api) encode(w io.Writer, indent string) error {
	var buf bytes.Buffer

	fmt.Fprintf(&buf, "%sresource \"aws_api_gateway_rest_api\" %q {\n", indent, a.ID)
	if a.Name != "" {
		fmt.Fprintf(&buf, "%s\tname = %q\n", indent, a.Name)
	}
	if a.EndpointConfiguration != nil {
		if err := a.EndpointConfiguration.encode(&buf, indent+"\t"); err != nil {
			return err
		}
	}
	fmt.Fprintf(&buf, "%s}\n", indent)

	_, err := io.Copy(w, &buf)
	return err
}

type apiEndpointConfiguration struct {
	Types []string
}

func (a *apiEndpointConfiguration) encode(w io.Writer, indent string) error {
	var buf bytes.Buffer

	fmt.Fprintf(&buf, "%sendpoint_configuration {\n", indent)
	if len(a.Types) > 0 {
		fmt.Fprintf(&buf, "%s\ttypes = [", indent)
		for i, t := range a.Types {
			if i > 0 {
				fmt.Fprint(&buf, ",")
			}
			fmt.Fprintf(&buf, "%q", t)
		}
		fmt.Fprint(&buf, "]\n")
	}
	fmt.Fprintf(&buf, "%s}\n", indent)

	_, err := io.Copy(w, &buf)
	return err
}

type nullBool func() bool

func nullTrue() nullBool {

M pkg/commands/generate/resources_test.go => pkg/commands/generate/resources_test.go +11 -0
@@ 41,6 41,17 @@ func TestObject(t *testing.T) {
	}
}

func TestAPI(t *testing.T) {
	cases := []*api{
		{ID: "a1", Name: "a1"},
		{ID: "a2", Name: "a2", EndpointConfiguration: &apiEndpointConfiguration{Types: []string{"REGIONAL"}}},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("api", c.ID, c))
	}
}

func TestQueue(t *testing.T) {
	cases := []*queue{
		{ID: "q1", Name: "q1"},

A pkg/commands/generate/testdata/api/a1.tf => pkg/commands/generate/testdata/api/a1.tf +3 -0
@@ 0,0 1,3 @@
resource "aws_api_gateway_rest_api" "a1" {
	name = "a1"
}

A pkg/commands/generate/testdata/api/a2.tf => pkg/commands/generate/testdata/api/a2.tf +6 -0
@@ 0,0 1,6 @@
resource "aws_api_gateway_rest_api" "a2" {
	name = "a2"
	endpoint_configuration {
		types = ["REGIONAL"]
	}
}