-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
example_test.go
1206 lines (1082 loc) 路 41 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint
package auto
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optrefresh"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func Example() {
ctx := context.Background()
// This stack creates an output
projA := "projA"
stackName := FullyQualifiedStackName("myOrg", projA, "devStack")
stackA, err := NewStackInlineSource(ctx, stackName, projA, func(pCtx *pulumi.Context) error {
pCtx.Export("outputA", pulumi.String("valueA"))
return nil
})
if err != nil {
// return errors.Wrap(err, "failed to create stackA")
}
// deploy the stack
aRes, err := stackA.Up(ctx)
if err != nil {
// return errors.Wrap(err, "failed to update bucket stackA")
}
// this stack creates an uses stackA's output to create a new output
projB := "projB"
stackName = FullyQualifiedStackName("myOrg", projB, "devStack")
stackB, err := NewStackInlineSource(ctx, stackName, projB, func(pCtx *pulumi.Context) error {
// output a new value "valueA/valueB"
pCtx.Export(
"outputB",
pulumi.Sprintf(
"%s/%s",
pulumi.String(aRes.Outputs["outputA"].Value.(string)), pulumi.String("valueB"),
),
)
return nil
})
if err != nil {
// return errors.Wrap(err, "failed to create object stackB")
}
// deploy the stack
bRes, err := stackB.Up(ctx)
if err != nil {
// return errors.Wrap(err, "failed to update stackB")
}
// Success!
fmt.Println(bRes.Summary.Result)
}
func ExampleFullyQualifiedStackName() {
stackName := FullyQualifiedStackName("myOrgName", "myProjectName", "myStackName")
// "myOrgName/myProjectName/myStackName"
ctx := context.Background()
_, _ = NewStackLocalSource(ctx, stackName, filepath.Join(".", "project"))
}
func ExampleIsCompilationError() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "broken", "go", "program"))
_, err := s.Up(ctx)
if err != nil && IsCompilationError(err) {
// time to fix up the program
}
}
func ExampleIsConcurrentUpdateError() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "update", "inprogress", "program"))
_, err := s.Up(ctx)
if err != nil && IsConcurrentUpdateError(err) {
// stack "o/p/s" already has an update in progress
// implement some retry logic here...
}
}
func ExampleIsSelectStack404Error() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "stack", "notfound", "program"))
_, err := s.Up(ctx)
if err != nil && IsSelectStack404Error(err) {
// stack "o/p/s" does not exist
// implement some retry logic here...
}
}
func ExampleIsCreateStack409Error() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "stack", "alreadyexists", "program"))
_, err := s.Up(ctx)
if err != nil && IsCreateStack409Error(err) {
// stack "o/p/s" already exists
// implement some retry logic here...
}
}
func ExampleIsRuntimeError() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "runtime", "error", "program"))
_, err := s.Up(ctx)
if err != nil && IsRuntimeError(err) {
// oops, maybe there's an NPE in the program?
}
}
func ExampleIsUnexpectedEngineError() {
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "engine", "error", "program"))
_, err := s.Up(ctx)
if err != nil && IsUnexpectedEngineError(err) {
// oops, you've probably uncovered a bug in the pulumi engine.
// please file a bug report :)
}
}
func ExampleConfigMap() {
cfg := ConfigMap{
"plaintext": {Value: "unencrypted"},
"secret": {Value: "encrypted", Secret: true},
}
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "project"))
s.SetAllConfig(ctx, cfg)
}
func ExampleConfigValue() {
cfgVal := ConfigValue{
Value: "an secret that will be marked as and stored encrypted",
Secret: true,
}
ctx := context.Background()
s, _ := NewStackLocalSource(ctx, "o/p/s", filepath.Join(".", "project"))
s.SetConfig(ctx, "cfgKey", cfgVal)
}
func ExampleDestroyResult() {
ctx := context.Background()
// select an existing stack
s, _ := SelectStackLocalSource(ctx, "o/p/s", filepath.Join(".", "project"))
destroyRes, _ := s.Destroy(ctx)
// success!
fmt.Println(destroyRes.Summary.Result)
}
func ExampleGitRepo() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
// we'll compile a the program into an executable with the name "examplesBinary"
binName := "examplesBinary"
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
ProjectPath: "goproj",
// this call back will get executed post-clone to allow for additional program setup
Setup: func(ctx context.Context, workspace Workspace) error {
cmd := exec.Command("go", "build", "-o", binName, "main.go")
cmd.Dir = workspace.WorkDir()
return cmd.Run()
},
}
// an override to the project file in the git repo, specifying our pre-built executable
project := workspace.Project{
Name: tokens.PackageName(pName),
Runtime: workspace.NewProjectRuntimeInfo("go", map[string]interface{}{
"binary": binName,
}),
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo, Project(project))
}
func ExampleGitRepo_personalAccessToken() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
// Get the Sourcecode Repository PERSONAL_ACCESS_TOKEN
token, _ := os.LookupEnv("PERSONAL_ACCESS_TOKEN")
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
ProjectPath: "goproj",
Auth: &GitAuth{
PersonalAccessToken: token,
},
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo)
}
func ExampleGitRepo_privateKeyPath() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
repo := GitRepo{
URL: "git@github.com:pulumi/test-repo.git",
ProjectPath: "goproj",
Auth: &GitAuth{
SSHPrivateKeyPath: "/Users/myuser/.ssh/id_rsa",
Password: "PrivateKeyPassword",
},
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo)
}
func ExampleGitRepo_privateKey() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
repo := GitRepo{
URL: "git@github.com:pulumi/test-repo.git",
ProjectPath: "goproj",
Auth: &GitAuth{
SSHPrivateKey: "<PRIVATE KEY FILE CONTENTS HERE>",
Password: "PrivateKeyPassword",
},
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo)
}
func ExampleGitRepo_usernameAndPassword() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
ProjectPath: "goproj",
Auth: &GitAuth{
// This will use a username and password combination for the private repo
Username: "myuser",
Password: "myPassword1234!",
},
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo)
}
func ExampleLocalWorkspace() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// install the pulumi-aws plugin
w.InstallPlugin(ctx, "aws", "v3.2.0")
// fetch the config used with the last update on stack "org/proj/stack"
w.RefreshConfig(ctx, "org/proj/stack")
}
func ExampleLocalWorkspace_CreateStack() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// initialize the stack
err := w.CreateStack(ctx, "org/proj/stack")
if err != nil {
// failed to create the stack
}
}
func ExampleLocalWorkspace_GetAllConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// get all config from the workspace for the stack (read from Pulumi.stack.yaml)
cfg, _ := w.GetAllConfig(ctx, "org/proj/stack")
fmt.Println(cfg["config_key"].Value)
fmt.Println(cfg["config_key"].Secret)
}
func ExampleLocalWorkspace_GetConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// get all config from the workspace for the stack (read from Pulumi.stack.yaml)
cfgVal, _ := w.GetConfig(ctx, "org/proj/stack", "config_key")
fmt.Println(cfgVal.Value)
fmt.Println(cfgVal.Secret)
}
func ExampleLocalWorkspace_GetEnvVars() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// get all environment values from the workspace for the stack
e := w.GetEnvVars()
for k, v := range e {
fmt.Println(k)
fmt.Println(v)
}
}
func ExampleLocalWorkspace_SetEnvVars() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// set the environment values on the workspace for the stack
e := map[string]string{
"FOO": "bar",
}
w.SetEnvVars(e)
}
func ExampleLocalWorkspace_SetEnvVar() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// set the environment value on the workspace for the stack
w.SetEnvVar("FOO", "bar")
}
func ExampleLocalWorkspace_UnsetEnvVar() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// set the environment value on the workspace for the stack
w.SetEnvVar("FOO", "bar")
// unset the environment value on the workspace for the stack
w.UnsetEnvVar("FOO")
}
func ExampleLocalWorkspace_InstallPlugin() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
w.InstallPlugin(ctx, "aws", "v3.2.0")
}
func ExampleLocalWorkspace_InstallPluginFromServer() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
w.InstallPluginFromServer(ctx, "scaleway", "v1.2.0", "github://api.github.com/lbrlabs")
}
func ExampleNewLocalWorkspace() {
ctx := context.Background()
// WorkDir sets the working directory for the LocalWorkspace. The workspace will look for a default
// project settings file (Pulumi.yaml) in this location for information about the Pulumi program.
wd := WorkDir(filepath.Join("..", "path", "to", "pulumi", "project"))
// PulumiHome customizes the location of $PULUMI_HOME where metadata is stored and plugins are installed.
ph := PulumiHome(filepath.Join("~", ".pulumi"))
// Project provides ProjectSettings to set once the workspace is created.
proj := Project(workspace.Project{
Name: tokens.PackageName("myproject"),
Runtime: workspace.NewProjectRuntimeInfo("go", nil),
Backend: &workspace.ProjectBackend{
URL: "https://url.to.custom.saas.backend.com",
},
})
_, _ = NewLocalWorkspace(ctx, wd, ph, proj)
}
func ExampleLocalWorkspace_secretsProvider() {
ctx := context.Background()
// WorkDir sets the working directory for the LocalWorkspace. The workspace will look for a default
// project settings file (Pulumi.yaml) in this location for information about the Pulumi program.
wd := WorkDir(filepath.Join("..", "path", "to", "pulumi", "project"))
// PulumiHome customizes the location of $PULUMI_HOME where metadata is stored and plugins are installed.
ph := PulumiHome(filepath.Join("~", ".pulumi"))
// Project provides ProjectSettings to set once the workspace is created.
proj := Project(workspace.Project{
Name: tokens.PackageName("myproject"),
Runtime: workspace.NewProjectRuntimeInfo("go", nil),
Backend: &workspace.ProjectBackend{
URL: "https://url.to.custom.saas.backend.com",
},
})
// Secrets provider provides a way of passing a non-default secrets provider to the
// workspace and the stacks created from it. Supported secrets providers are:
// `awskms`, `azurekeyvault`, `gcpkms`, `hashivault` and `passphrase`
secretsProvider := SecretsProvider("awskms://alias/mysecretkeyalias")
// EnvVars is a map of environment values scoped to the workspace.
// These values will be passed to all Workspace and Stack level commands.
envvars := EnvVars(map[string]string{
"PULUMI_CONFIG_PASSPHRASE": "password",
})
_, _ = NewLocalWorkspace(ctx, wd, ph, proj, secretsProvider, envvars)
}
func ExampleLocalWorkspace_ListPlugins() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
ps, _ := w.ListPlugins(ctx)
fmt.Println(ps[0].Size)
}
func ExampleLocalWorkspace_ListStacks() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
ss, _ := w.ListStacks(ctx)
fmt.Println(ss[0].UpdateInProgress)
}
func ExampleLocalWorkspace_ProjectSettings() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// read existing project settings if any
ps, err := w.ProjectSettings(ctx)
if err != nil {
// no Pulumi.yaml was found, so create a default
ps = &workspace.Project{
Name: tokens.PackageName("myproject"),
Runtime: workspace.NewProjectRuntimeInfo("go", nil),
}
}
// make some changes
author := "pulumipus"
ps.Author = &author
// save the settings back to the Workspace
w.SaveProjectSettings(ctx, ps)
}
func ExampleLocalWorkspace_SaveProjectSettings() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
// read existing project settings if any
ps, err := w.ProjectSettings(ctx)
if err != nil {
// no Pulumi.yaml was found, so create a default
ps = &workspace.Project{
Name: tokens.PackageName("myproject"),
Runtime: workspace.NewProjectRuntimeInfo("go", nil),
}
}
// make some changes
author := "pulumipus"
ps.Author = &author
// save the settings back to the Workspace
w.SaveProjectSettings(ctx, ps)
}
func ExampleLocalWorkspace_RefreshConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackNameA := FullyQualifiedStackName("org", "proj", "stackA")
// get the last deployed config from stack A
// this overwrites config in pulumi.stackA.yaml
cfg, _ := w.RefreshConfig(ctx, stackNameA)
// add a key to the ConfigMap
cfg["addition_config_key"] = ConfigValue{Value: "additional_config_value"}
// create a new stack
stackNameB := FullyQualifiedStackName("org", "proj", "stackB")
w.CreateStack(ctx, stackNameB)
// save the modified config to stackB
w.SetAllConfig(ctx, stackNameB, cfg)
}
func ExampleLocalWorkspace_RemoveAllConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackName := FullyQualifiedStackName("org", "proj", "stackA")
// get all config currently set in the workspace
cfg, _ := w.GetAllConfig(ctx, stackName)
var keys []string
for k := range cfg {
keys = append(keys, k)
}
// remove those config values
_ = w.RemoveAllConfig(ctx, stackName, keys)
}
func ExampleLocalWorkspace_RemoveConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackName := FullyQualifiedStackName("org", "proj", "stackA")
_ = w.RemoveConfig(ctx, stackName, "key_to_remove")
}
func ExampleLocalWorkspace_RemovePlugin() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
_ = w.RemovePlugin(ctx, "aws", "v3.2.0")
}
func ExampleLocalWorkspace_RemoveStack() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackName := FullyQualifiedStackName("org", "proj", "stack")
_ = w.RemoveStack(ctx, stackName)
}
func ExampleLocalWorkspace_SelectStack() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackName := FullyQualifiedStackName("org", "proj", "existing_stack")
_ = w.SelectStack(ctx, stackName)
}
func ExampleLocalWorkspace_SetAllConfig() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackNameA := FullyQualifiedStackName("org", "proj", "stackA")
// get the last deployed config from stack A
// this overwrites config in pulumi.stackA.yaml
cfg, _ := w.RefreshConfig(ctx, stackNameA)
// add a key to the ConfigMap
cfg["addition_config_key"] = ConfigValue{Value: "additional_config_value"}
// create a new stack
stackNameB := FullyQualifiedStackName("org", "proj", "stackB")
w.CreateStack(ctx, stackNameB)
// save the modified config to stackB
w.SetAllConfig(ctx, stackNameB, cfg)
}
func ExampleLocalWorkspace_SetProgram() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
program := func(pCtx *pulumi.Context) error {
pCtx.Export("an output", pulumi.String("an output value"))
return nil
}
// this program will be used for all Stack.Update and Stack.Preview operations going forward
w.SetProgram(program)
}
func ExampleLocalWorkspace_Stack() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
s, _ := w.Stack(ctx)
fmt.Println(s.UpdateInProgress)
}
func ExampleLocalWorkspace_StackSettings() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackNameA := FullyQualifiedStackName("org", "proj", "stackA")
// read existing stack settings for stackA if any
ss, err := w.StackSettings(ctx, stackNameA)
if err != nil {
// no pulumi.stackA.yaml was found, so create a default
ss = &workspace.ProjectStack{}
}
stackNameB := FullyQualifiedStackName("org", "proj", "stackB")
// copy the settings to stackB
w.SaveStackSettings(ctx, stackNameB, ss)
}
func ExampleLocalWorkspace_SaveStackSettings() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
stackNameA := FullyQualifiedStackName("org", "proj", "stackA")
// read existing stack settings for stackA if any
ss, err := w.StackSettings(ctx, stackNameA)
if err != nil {
// no pulumi.stackA.yaml was found, so create a default
ss = &workspace.ProjectStack{}
}
stackNameB := FullyQualifiedStackName("org", "proj", "stackB")
// copy the settings to stackB
w.SaveStackSettings(ctx, stackNameB, ss)
}
func ExampleLocalWorkspace_WhoAmI() {
ctx := context.Background()
// create a workspace from a local project
w, _ := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "program")))
user, _ := w.WhoAmI(ctx)
fmt.Println(user)
}
func ExampleSetupFn() {
ctx := context.Background()
stackName := FullyQualifiedStackName("myOrg", "nodejs_project", "myStack")
repo := GitRepo{
URL: "https://some.example.repo.git",
ProjectPath: "goproj",
// this call back will get executed post-clone to allow for additional program setup
Setup: func(ctx context.Context, workspace Workspace) error {
// restore node dependencies required to run the program
cmd := exec.Command("npm", "install")
cmd.Dir = workspace.WorkDir()
return cmd.Run()
},
}
// initialize a stack from the git repo
NewStackRemoteSource(ctx, stackName, repo)
}
func ExampleStack() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "dev_stack")
cfg := ConfigMap{
"bar": ConfigValue{
Value: "abc",
},
"buzz": ConfigValue{
Value: "secret",
Secret: true,
},
}
// initialize
pDir := filepath.Join(".", "project")
s, err := NewStackLocalSource(ctx, stackName, pDir)
if err != nil {
// Handle failure creating stack.
}
defer func() {
// Workspace operations can be accessed via Stack.Workspace()
// -- pulumi stack rm --
err = s.Workspace().RemoveStack(ctx, s.Name())
}()
err = s.SetAllConfig(ctx, cfg)
if err != nil {
// Handle failure setting configurations.
}
// -- pulumi up --
res, err := s.Up(ctx)
if err != nil {
// Handle failure updating stack.
}
fmt.Println(len(res.Outputs))
// -- pulumi preview --
prev, err := s.Preview(ctx)
if err != nil {
// Handle failure previewing stack.
}
// no changes after the update
fmt.Println(prev.ChangeSummary["same"])
// -- pulumi refresh --
ref, err := s.Refresh(ctx)
if err != nil {
// Handle error during refresh.
}
// Success!
fmt.Println(ref.Summary.Result)
// -- pulumi destroy --
_, err = s.Destroy(ctx)
if err != nil {
// Handle error during destroy.
}
}
func ExampleNewStack() {
ctx := context.Background()
w, err := NewLocalWorkspace(ctx)
if err != nil {
// Handle error creating workspace.
}
stackName := FullyQualifiedStackName("org", "proj", "stack")
stack, err := NewStack(ctx, stackName, w)
if err != nil {
// Handle error creating stack.
}
_, err = stack.Up(ctx)
if err != nil {
// Handle error updating stack.
}
}
func ExampleUpsertStack() {
ctx := context.Background()
w, err := NewLocalWorkspace(ctx)
if err != nil {
// Handle error creating workspace.
}
stackName := FullyQualifiedStackName("org", "proj", "stack")
stack, err := UpsertStack(ctx, stackName, w)
if err != nil {
// Handle error creating/updating stack.
}
_, err = stack.Up(ctx)
if err != nil {
// Handle error during update.
}
}
func ExampleNewStackInlineSource() {
ctx := context.Background()
projName := "proj"
stackName := FullyQualifiedStackName("myOrg", projName, "stack")
// create a new Stack with a default ProjectSettings file, and a temporary WorkDir created in a new
// LocalWorkspace on behalf of the user.
stack, _ := NewStackInlineSource(ctx, stackName, projName, func(pCtx *pulumi.Context) error {
pCtx.Export("outputA", pulumi.String("valueA"))
return nil
})
// Stack.Up runs the inline pulumi.RunFunc specified above.
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
stack.Up(ctx)
}
func ExampleUpsertStackInlineSource() {
ctx := context.Background()
projName := "proj"
stackName := FullyQualifiedStackName("myOrg", "proj", "stack")
// create or select a new Stack with a default ProjectSettings file, and a temporary WorkDir created in a new
// LocalWorkspace on behalf of the user.
stack, _ := UpsertStackInlineSource(ctx, stackName, projName, func(pCtx *pulumi.Context) error {
pCtx.Export("outputA", pulumi.String("valueA"))
return nil
})
// Stack.Up runs the inline pulumi.RunFunc specified above.
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
stack.Up(ctx)
}
func ExampleSelectStackInlineSource() {
ctx := context.Background()
projName := "proj"
stackName := FullyQualifiedStackName("myOrg", projName, "existing_stack")
// selects an existing stack with a default ProjectSettings file, and a temporary WorkDir in a new LocalWorkspace
// created on behalf of the user.
stack, _ := SelectStackInlineSource(ctx, stackName, projName, func(pCtx *pulumi.Context) error {
pCtx.Export("outputA", pulumi.String("valueA"))
return nil
})
// Stack.Up runs the inline pulumi.RunFunc specified above.
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
stack.Up(ctx)
}
func ExampleNewStackLocalSource() {
ctx := context.Background()
stackName := FullyQualifiedStackName("myOrg", "proj", "stack")
workDir := filepath.Join(".", "program", "dir")
// creates a new stack with a new LocalWorkspace created from provided WorkDir. This Workspace will pick up
// any available Settings files (Pulumi.yaml, Pulumi.<stack>.yaml)
stack, _ := NewStackLocalSource(ctx, stackName, workDir)
// Stack.Up runs the program in workDir
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
// Stack.Up now runs our inline program
stack.Up(ctx)
}
func ExampleUpsertStackLocalSource() {
ctx := context.Background()
stackName := FullyQualifiedStackName("myOrg", "proj", "stack")
workDir := filepath.Join(".", "program", "dir")
// create or select a new stack with a new LocalWorkspace created from provided WorkDir. This Workspace will pick up
// any available Settings files (Pulumi.yaml, Pulumi.<stack>.yaml)
stack, _ := UpsertStackLocalSource(ctx, stackName, workDir)
// Stack.Up runs the program in workDir
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
// Stack.Up now runs our inline program
stack.Up(ctx)
}
func ExampleSelectStackLocalSource() {
ctx := context.Background()
stackName := FullyQualifiedStackName("myOrg", "proj", "stack")
workDir := filepath.Join(".", "program", "dir")
// selects an existing stack with a new LocalWorkspace created from provided WorkDir. This Workspace will pick up
// any available Settings files (Pulumi.yaml, Pulumi.<stack>.yaml)
stack, _ := SelectStackLocalSource(ctx, stackName, workDir)
// Stack.Up runs the program in workDir
stack.Up(ctx)
// we can update the Workspace program for subsequent updates if desired
stack.Workspace().SetProgram(func(pCtx *pulumi.Context) error {
pCtx.Export("outputAA", pulumi.String("valueAA"))
return nil
})
// Stack.Up now runs our inline program
stack.Up(ctx)
}
func ExampleNewStackRemoteSource() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
// we'll compile a the program into an executable with the name "examplesBinary"
binName := "examplesBinary"
// a description of the git repo to clone
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
// the subdirectory relative to the root of the repo
ProjectPath: "goproj",
// this call back will get executed post-clone to allow for additional program setup
Setup: func(ctx context.Context, workspace Workspace) error {
cmd := exec.Command("go", "build", "-o", binName, "main.go")
cmd.Dir = workspace.WorkDir()
return cmd.Run()
},
}
// an override to the project file in the git repo, specifying our pre-built executable
project := workspace.Project{
Name: tokens.PackageName(pName),
Runtime: workspace.NewProjectRuntimeInfo("go", map[string]interface{}{
"binary": binName,
}),
}
// initialize a stack from the git repo, specifying our project override
NewStackRemoteSource(ctx, stackName, repo, Project(project))
}
func ExampleUpsertStackRemoteSource() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
// we'll compile a the program into an executable with the name "examplesBinary"
binName := "examplesBinary"
// a description of the git repo to clone
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
// the subdirectory relative to the root of the repo
ProjectPath: "goproj",
// this call back will get executed post-clone to allow for additional program setup
Setup: func(ctx context.Context, workspace Workspace) error {
cmd := exec.Command("go", "build", "-o", binName, "main.go")
cmd.Dir = workspace.WorkDir()
return cmd.Run()
},
}
// an override to the project file in the git repo, specifying our pre-built executable
project := workspace.Project{
Name: tokens.PackageName(pName),
Runtime: workspace.NewProjectRuntimeInfo("go", map[string]interface{}{
"binary": binName,
}),
}
// initialize or select a stack from the git repo, specifying our project override
UpsertStackRemoteSource(ctx, stackName, repo, Project(project))
}
func ExampleSelectStackRemoteSource() {
ctx := context.Background()
pName := "go_remote_proj"
stackName := FullyQualifiedStackName("myOrg", pName, "myStack")
// we'll compile a the program into an executable with the name "examplesBinary"
binName := "examplesBinary"
// a description of the git repo to clone
repo := GitRepo{
URL: "https://github.com/pulumi/test-repo.git",
// the subdirectory relative to the root of the repo
ProjectPath: "goproj",
// this call back will get executed post-clone to allow for additional program setup
Setup: func(ctx context.Context, workspace Workspace) error {
cmd := exec.Command("go", "build", "-o", binName, "main.go")
cmd.Dir = workspace.WorkDir()
return cmd.Run()
},
}
// an override to the project file in the git repo, specifying our pre-built executable
project := workspace.Project{
Name: tokens.PackageName(pName),
Runtime: workspace.NewProjectRuntimeInfo("go", map[string]interface{}{
"binary": binName,
}),
}
// select an existing stack using a LocalWorkspace created from the git repo, specifying our project override
SelectStackRemoteSource(ctx, stackName, repo, Project(project))
}
func ExampleStack_Destroy() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "stack")
// select an existing stack to destroy
stack, _ := SelectStackLocalSource(ctx, stackName, filepath.Join(".", "program"))
stack.Destroy(ctx, optdestroy.Message("a message to save with the destroy operation"))
}
func ExampleStack_Destroy_streamingProgress() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "stack")
// select an existing stack to destroy
stack, _ := SelectStackLocalSource(ctx, stackName, filepath.Join(".", "program"))
// create a temp file that we can tail during while our program runs
tmp, _ := ioutil.TempFile(os.TempDir(), "")
// optdestroy.ProgressStreams allows us to stream incremental output to stdout, a file to tail, etc.
// this gives us incremental status over time
progressStreams := []io.Writer{os.Stdout, tmp}
// this destroy will incrementally stream unstructured progress messages to stdout and our temp file
stack.Destroy(ctx, optdestroy.ProgressStreams(progressStreams...))
}
func ExampleStack_Up() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "stack")
// create a new stack to update
stack, _ := NewStackLocalSource(ctx, stackName, filepath.Join(".", "program"))
result, _ := stack.Up(ctx, optup.Message("a message to save with the up operation"), optup.Parallel(10000))
permalink, _ := result.GetPermalink()
fmt.Println(permalink)
}
func ExampleStack_Up_streamingProgress() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "stack")
// create a new stack to update
stack, _ := NewStackLocalSource(ctx, stackName, filepath.Join(".", "program"))
// create a temp file that we can tail during while our program runs
tmp, _ := ioutil.TempFile(os.TempDir(), "")
// optup.ProgressStreams allows us to stream incremental output to stdout, a file to tail, etc.
// this gives us incremental status over time
progressStreams := []io.Writer{os.Stdout, tmp}
// this update will incrementally stream unstructured progress messages to stdout and our temp file
stack.Up(ctx, optup.ProgressStreams(progressStreams...))
}
func ExampleStack_Preview() {
ctx := context.Background()
stackName := FullyQualifiedStackName("org", "project", "stack")
// create a new stack and preview changes
stack, _ := NewStackLocalSource(ctx, stackName, filepath.Join(".", "program"))
stack.Preview(ctx, optpreview.Message("a message to save with the preive operation"))
}
func ExampleStack_Refresh() {