-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws_test.go
210 lines (193 loc) · 4.77 KB
/
aws_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terraform
import (
"bytes"
"fmt"
"log/slog"
tfjson "github.com/hashicorp/terraform-json"
aws "github.com/golingon/docsterraform/out/aws"
"github.com/golingon/docsterraform/out/aws/aws_subnet"
"github.com/golingon/docsterraform/out/aws/aws_vpc"
"github.com/golingon/lingon/pkg/terra"
)
func Example_awsProvider() {
type AWSStack struct {
terra.Stack
Provider *aws.Provider `validate:"required"`
}
// Initialise a stack with the AWS provider configuration
_ = AWSStack{
Provider: &aws.Provider{
Region: terra.String("eu-north-1"),
},
}
}
func Example_awsVPC() {
type AWSStack struct {
terra.Stack
Provider *aws.Provider `validate:"required"`
VPC *aws_vpc.Resource `validate:"required"`
}
// Initialise a stack with the AWS provider configuration
stack := AWSStack{
Provider: &aws.Provider{
Region: terra.String("eu-north-1"),
},
VPC: &aws_vpc.Resource{
Name: "vpc",
Args: aws_vpc.Args{
CidrBlock: terra.String("10.0.0.0/16"),
EnableDnsSupport: terra.Bool(true),
},
},
}
// Export the stack to Terraform HCL
var b bytes.Buffer
if err := terra.Export(&stack, terra.WithExportWriter(&b)); err != nil {
slog.Error("exporting stack", "err", err)
return
}
fmt.Println(b.String())
// Output:
// terraform {
// required_providers {
// aws = {
// source = "hashicorp/aws"
// version = "5.44.0"
// }
// }
// }
//
// // Provider blocks
// provider "aws" {
// region = "eu-north-1"
// }
//
// // Resource blocks
// resource "aws_vpc" "vpc" {
// cidr_block = "10.0.0.0/16"
// enable_dns_support = true
// }
}
func Example_awsVPCWithSubnet() {
type AWSStack struct {
terra.Stack
Provider *aws.Provider `validate:"required"`
VPC *aws_vpc.Resource `validate:"required"`
Subnet *aws_subnet.Resource `validate:"required"`
}
vpc := aws_vpc.Resource{
Name: "vpc",
Args: aws_vpc.Args{
CidrBlock: terra.String("10.0.0.0/16"),
EnableDnsSupport: terra.Bool(true),
},
}
subnet := aws_subnet.Resource{
Name: "subnet",
Args: aws_subnet.Args{
// Reference the VPC's ID, which will translate into a reference
// in the Terraform configuration
VpcId: vpc.Attributes().Id(),
},
}
// Initialise a stack with the AWS provider configuration
stack := AWSStack{
Provider: &aws.Provider{
Region: terra.String("eu-north-1"),
},
VPC: &vpc,
Subnet: &subnet,
}
// Export the stack to Terraform HCL
var b bytes.Buffer
if err := terra.Export(&stack, terra.WithExportWriter(&b)); err != nil {
slog.Error("exporting stack", "err", err)
return
}
fmt.Println(b.String())
// Output:
// terraform {
// required_providers {
// aws = {
// source = "hashicorp/aws"
// version = "5.44.0"
// }
// }
// }
//
// // Provider blocks
// provider "aws" {
// region = "eu-north-1"
// }
//
// // Resource blocks
// resource "aws_vpc" "vpc" {
// cidr_block = "10.0.0.0/16"
// enable_dns_support = true
// }
//
// resource "aws_subnet" "subnet" {
// vpc_id = aws_vpc.vpc.id
// }
}
func Example_awsVPCImportState() {
type AWSStack struct {
terra.Stack
Provider *aws.Provider `validate:"required"`
VPC *aws_vpc.Resource `validate:"required"`
}
// Initialise a stack with the AWS provider configuration
stack := AWSStack{
Provider: &aws.Provider{
Region: terra.String("eu-north-1"),
},
VPC: &aws_vpc.Resource{
Name: "vpc",
Args: aws_vpc.Args{
CidrBlock: terra.String("10.0.0.0/16"),
EnableDnsSupport: terra.Bool(true),
},
},
}
// At this point, you would invoke the Terraform CLI, and at a minimum
// run the `terraform show` command to get the state back.
// The state can then be decoded back into our stack.
// For this test, we will create some dummy state data
// (don't do this at home!)
state := tfjson.State{
Values: &tfjson.StateValues{
RootModule: &tfjson.StateModule{
Resources: []*tfjson.StateResource{
{
Type: "aws_vpc",
Name: "vpc",
AttributeValues: map[string]interface{}{
"id": "12345",
},
},
},
},
},
}
ok, err := terra.StackImportState(&stack, &state)
if err != nil {
slog.Error("importing stack state", "error", err)
}
if !ok {
// This means the stack includes resources that did not have values
// in the Terraform state.
// This is happens if you have not applied your Terraform configuration.
slog.Info("stack state is not complete")
}
// Access the VPC state. If you know the state is complete, you can also use
// the StateMust() function
vpcState, ok := stack.VPC.State()
if !ok {
slog.Info("vpc does not have state")
return
}
fmt.Println(vpcState.Id)
// Output: 12345
}