forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
224 lines (192 loc) · 4.97 KB
/
example_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"crypto/md5"
"fmt"
"log"
"os"
slug "github.com/hashicorp/go-slug"
)
func ExampleOrganizations() {
config := &Config{
Token: "insert-your-token-here",
RetryServerErrors: true,
}
client, err := NewClient(config)
if err != nil {
log.Fatal(err)
}
// Create a context
ctx := context.Background()
// Create a new organization
options := OrganizationCreateOptions{
Name: String("example"),
Email: String("[email protected]"),
}
org, err := client.Organizations.Create(ctx, options)
if err != nil {
log.Fatal(err)
}
// Delete an organization
err = client.Organizations.Delete(ctx, org.Name)
if err != nil {
log.Fatal(err)
}
}
func ExampleWorkspaces() {
config := &Config{
Token: "insert-your-token-here",
RetryServerErrors: true,
}
client, err := NewClient(config)
if err != nil {
log.Fatal(err)
}
// Create a context
ctx := context.Background()
// Create a new workspace
w, err := client.Workspaces.Create(ctx, "org-name", WorkspaceCreateOptions{
Name: String("my-app-tst"),
})
if err != nil {
log.Fatal(err)
}
// Update the workspace
w, err = client.Workspaces.Update(ctx, "org-name", w.Name, WorkspaceUpdateOptions{
AutoApply: Bool(false),
TerraformVersion: String("0.11.1"),
WorkingDirectory: String("my-app/infra"),
})
if err != nil {
log.Fatal(err)
}
}
func ExampleConfigurationVersions_UploadTarGzip() {
ctx := context.Background()
client, err := NewClient(&Config{
Token: "insert-your-token-here",
RetryServerErrors: true,
})
if err != nil {
log.Fatal(err)
}
packer, err := slug.NewPacker(
slug.DereferenceSymlinks(), // dereferences symlinks
slug.ApplyTerraformIgnore(), // ignores paths specified in .terraformignore
slug.AllowSymlinkTarget("/some/path"), // allow certain symlink target paths
)
if err != nil {
log.Fatal(err)
}
rawConfig := bytes.NewBuffer(nil)
// Pass in a path
_, err = packer.Pack("test-fixtures/config", rawConfig)
if err != nil {
log.Fatal(err)
}
// Create a configuration version
cv, err := client.ConfigurationVersions.Create(ctx, "ws-12345678", ConfigurationVersionCreateOptions{
AutoQueueRuns: Bool(false),
})
if err != nil {
log.Fatal(err)
}
// Upload the buffer
err = client.ConfigurationVersions.UploadTarGzip(ctx, cv.UploadURL, rawConfig)
if err != nil {
log.Fatal(err)
}
}
func ExampleRegistryModules_UploadTarGzip() {
ctx := context.Background()
client, err := NewClient(&Config{
Token: "insert-your-token-here",
RetryServerErrors: true,
})
if err != nil {
log.Fatal(err)
}
packer, err := slug.NewPacker(
slug.DereferenceSymlinks(), // dereferences symlinks
slug.ApplyTerraformIgnore(), // ignores paths specified in .terraformignore
slug.AllowSymlinkTarget("/some/path"), // allow certain symlink target paths
)
if err != nil {
log.Fatal(err)
}
rawConfig := bytes.NewBuffer(nil)
// Pass in a path
_, err = packer.Pack("test-fixtures/config", rawConfig)
if err != nil {
log.Fatal(err)
}
// Create a registry module
rm, err := client.RegistryModules.Create(ctx, "hashicorp", RegistryModuleCreateOptions{
Name: String("my-module"),
Provider: String("provider"),
RegistryName: PrivateRegistry,
})
if err != nil {
log.Fatal(err)
}
opts := RegistryModuleCreateVersionOptions{
Version: String("1.1.0"),
}
// Create a registry module version
rmv, err := client.RegistryModules.CreateVersion(ctx, RegistryModuleID{
Organization: "hashicorp",
Name: rm.Name,
Provider: rm.Provider,
}, opts)
if err != nil {
log.Fatal(err)
}
uploadURL, ok := rmv.Links["upload"].(string)
if !ok {
log.Fatal("upload url must be a valid string")
}
// Upload the buffer
err = client.RegistryModules.UploadTarGzip(ctx, uploadURL, rawConfig)
if err != nil {
log.Fatal(err)
}
}
func ExampleStateVersions_Upload() {
ctx := context.Background()
client, err := NewClient(&Config{
Token: "insert-your-token-here",
RetryServerErrors: true,
})
if err != nil {
log.Fatal(err)
}
// Lock the workspace
if _, err = client.Workspaces.Lock(ctx, "ws-12345678", WorkspaceLockOptions{}); err != nil {
log.Fatal(err)
}
state, err := os.ReadFile("state.json")
if err != nil {
log.Fatal(err)
}
// Create upload options that does not contain a State attribute within the create options
options := StateVersionUploadOptions{
StateVersionCreateOptions: StateVersionCreateOptions{
Lineage: String("493f7758-da5e-229e-7872-ea1f78ebe50a"),
Serial: Int64(int64(2)),
MD5: String(fmt.Sprintf("%x", md5.Sum(state))),
Force: Bool(false),
},
RawState: state,
}
// Upload a state version
if _, err = client.StateVersions.Upload(ctx, "ws-12345678", options); err != nil {
log.Fatal(err)
}
// Unlock the workspace
if _, err = client.Workspaces.Unlock(ctx, "ws-12345678"); err != nil {
log.Fatal(err)
}
}