Skip to content

Commit 2cb0d09

Browse files
committed
feat(terraform): fetch parents and children
1 parent 1e98790 commit 2cb0d09

File tree

7 files changed

+260
-45
lines changed

7 files changed

+260
-45
lines changed

internal/core/bootstrap.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
150150
isClientFromBootstrapConfig = false
151151
client, err = createAnonymousClient(httpClient, config.BuildInfo)
152152
if err != nil {
153-
printErr := printer.Print(err, nil)
153+
printErr := printer.Print(client, err, nil)
154154
if printErr != nil {
155155
_, _ = fmt.Fprintln(config.Stderr, printErr)
156156
}
@@ -201,7 +201,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
201201
// Load CLI config
202202
cliCfg, err := cliConfig.LoadConfig(ExtractCliConfigPath(ctx))
203203
if err != nil {
204-
printErr := printer.Print(err, nil)
204+
printErr := printer.Print(meta.Client, err, nil)
205205
if printErr != nil {
206206
_, _ = fmt.Fprintln(config.Stderr, printErr)
207207
}
@@ -270,15 +270,15 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
270270
if cliErr, ok := err.(*CliError); ok && cliErr.Code != 0 {
271271
errorCode = cliErr.Code
272272
}
273-
printErr := printer.Print(err, nil)
273+
printErr := printer.Print(meta.Client, err, nil)
274274
if printErr != nil {
275275
_, _ = fmt.Fprintln(os.Stderr, err)
276276
}
277277
return errorCode, nil, err
278278
}
279279

280280
if meta.command != nil {
281-
printErr := printer.Print(meta.result, meta.command.getHumanMarshalerOpt())
281+
printErr := printer.Print(meta.Client, meta.result, meta.command.getHumanMarshalerOpt())
282282
if printErr != nil {
283283
_, _ = fmt.Fprintln(config.Stderr, printErr)
284284
}

internal/core/printer.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/scaleway/scaleway-cli/v2/internal/gofields"
1414
"github.com/scaleway/scaleway-cli/v2/internal/human"
1515
"github.com/scaleway/scaleway-cli/v2/internal/terraform"
16+
"github.com/scaleway/scaleway-sdk-go/scw"
1617
)
1718

1819
// Type defines an formatter format.
@@ -44,8 +45,12 @@ const (
4445
// Option to enable pretty output on json printer.
4546
PrinterOptJSONPretty = "pretty"
4647

47-
// Option to enable pretty output on json printer.
48-
PrinterOptTerraformWithChildren = "with-children"
48+
// Option to disable parents output on terraform printer.
49+
PrinterOptTerraformSkipParents = "skip-parents"
50+
// Option to disable children output on terraform printer.
51+
PrinterOptTerraformSkipChildren = "skip-children"
52+
// Option to disable parents and children output on terraform printer.
53+
PrinterOptTerraformSkipParentsAndChildren = "skip-parents-and-children"
4954
)
5055

5156
type PrinterConfig struct {
@@ -115,11 +120,16 @@ func setupJSONPrinter(printer *Printer, opts string) error {
115120
func setupTerraformPrinter(printer *Printer, opts string) error {
116121
printer.printerType = PrinterTypeTerraform
117122
switch opts {
118-
case PrinterOptTerraformWithChildren:
119-
printer.terraformWithChildren = true
123+
case PrinterOptTerraformSkipParents:
124+
printer.terraformSkipParents = true
125+
case PrinterOptTerraformSkipChildren:
126+
printer.terraformSkipChildren = true
127+
case PrinterOptTerraformSkipParentsAndChildren:
128+
printer.terraformSkipParents = true
129+
printer.terraformSkipChildren = true
120130
case "":
121131
default:
122-
return fmt.Errorf("invalid option %s for terraform outout. Valid options are: %s", opts, PrinterOptTerraformWithChildren)
132+
return fmt.Errorf("invalid option %s for terraform outout. Valid options are: %s and %s", opts, PrinterOptTerraformSkipParents, PrinterOptTerraformSkipChildren)
123133
}
124134

125135
terraformVersion, err := terraform.GetLocalClientVersion()
@@ -173,8 +183,10 @@ type Printer struct {
173183
// Enable pretty print on json output
174184
jsonPretty bool
175185

176-
// Enable children fetching on terraform output
177-
terraformWithChildren bool
186+
// Disable children fetching on terraform output
187+
terraformSkipParents bool
188+
// Disable children fetching on terraform output
189+
terraformSkipChildren bool
178190

179191
// go template to use on template output
180192
template *template.Template
@@ -183,7 +195,7 @@ type Printer struct {
183195
humanFields []string
184196
}
185197

186-
func (p *Printer) Print(data interface{}, opt *human.MarshalOpt) error {
198+
func (p *Printer) Print(client *scw.Client, data interface{}, opt *human.MarshalOpt) error {
187199
// No matter the printer type if data is a RawResult we should print it as is.
188200
if rawResult, isRawResult := data.(RawResult); isRawResult {
189201
_, err := p.stdout.Write(rawResult)
@@ -201,7 +213,7 @@ func (p *Printer) Print(data interface{}, opt *human.MarshalOpt) error {
201213
case PrinterTypeYAML:
202214
err = p.printYAML(data)
203215
case PrinterTypeTerraform:
204-
err = p.printTerraform(data)
216+
err = p.printTerraform(client, data)
205217
case PrinterTypeTemplate:
206218
err = p.printTemplate(data)
207219
default:
@@ -322,13 +334,18 @@ func (p *Printer) printYAML(data interface{}) error {
322334
return encoder.Encode(data)
323335
}
324336

325-
func (p *Printer) printTerraform(data interface{}) error {
337+
func (p *Printer) printTerraform(client *scw.Client, data interface{}) error {
326338
writer := p.stdout
327339
if _, isError := data.(error); isError {
328340
return p.printHuman(data, nil)
329341
}
330342

331-
hcl, err := terraform.GetHCL(data)
343+
hcl, err := terraform.GetHCL(&terraform.GetHCLConfig{
344+
Client: client,
345+
Data: data,
346+
SkipParents: p.terraformSkipParents,
347+
SkipChildren: p.terraformSkipChildren,
348+
})
332349
if err != nil {
333350
return err
334351
}

internal/core/shell.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func shellExecutor(rootCmd *cobra.Command, printer *Printer, meta *meta) func(s
267267
return
268268
}
269269

270-
printErr := printer.Print(err, nil)
270+
printErr := printer.Print(meta.Client, err, nil)
271271
if printErr != nil {
272272
_, _ = fmt.Fprintln(os.Stderr, err)
273273
}
@@ -283,7 +283,7 @@ func shellExecutor(rootCmd *cobra.Command, printer *Printer, meta *meta) func(s
283283

284284
autoCompleteCache.Update(meta.command.Namespace)
285285

286-
printErr := printer.Print(meta.result, meta.command.getHumanMarshalerOpt())
286+
printErr := printer.Print(meta.Client, meta.result, meta.command.getHumanMarshalerOpt())
287287
if printErr != nil {
288288
_, _ = fmt.Fprintln(os.Stderr, printErr)
289289
}

internal/core/shell_disabled.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func RunShell(ctx context.Context, printer *Printer, meta *meta, rootCmd *cobra.Command, args []string) {
14-
err := printer.Print(fmt.Errorf("shell is currently disabled on freebsd"), nil)
14+
err := printer.Print(meta.Client, fmt.Errorf("shell is currently disabled on freebsd"), nil)
1515
if err != nil {
1616
_, _ = fmt.Fprintln(os.Stderr, err)
1717
}

internal/core/testing.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,11 @@ func marshalGolden(t *testing.T, ctx *CheckFuncCtx) string {
717717
require.NoError(t, err)
718718

719719
if ctx.Err != nil {
720-
err = jsonPrinter.Print(ctx.Err, nil)
720+
err = jsonPrinter.Print(nil, ctx.Err, nil)
721721
require.NoError(t, err)
722722
}
723723
if ctx.Result != nil {
724-
err = jsonPrinter.Print(ctx.Result, nil)
724+
err = jsonPrinter.Print(nil, ctx.Result, nil)
725725
require.NoError(t, err)
726726
}
727727

internal/terraform/association.go

+83-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@ package terraform
33
import (
44
"reflect"
55

6+
"github.com/scaleway/scaleway-sdk-go/api/account/v2"
67
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
78
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
89
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
911
)
1012

11-
type associationSubResource struct {
12-
TerraformAttributeName string
13-
Command string
14-
AsDataSource bool
13+
type associationParent struct {
14+
Fetcher func(client *scw.Client, data interface{}) (interface{}, error)
15+
AsDataSource bool
16+
}
17+
18+
type associationChild struct {
19+
// {
20+
// [<child attribute>]: <parent attribute>
21+
// }
22+
ParentFieldMap map[string]string
23+
24+
Fetcher func(client *scw.Client, data interface{}) (interface{}, error)
1525
}
1626

1727
type association struct {
1828
ResourceName string
1929
ImportFormat string
20-
SubResources map[string]*associationSubResource
30+
Parents map[string]*associationParent
31+
Children []*associationChild
2132
}
2233

2334
// const importFormatID = "{{ .Region }}/{{ .ID }}"
@@ -36,21 +47,79 @@ var associations = map[interface{}]*association{
3647
&container.Container{}: {
3748
ResourceName: "scaleway_container",
3849
ImportFormat: importFormatRegionID,
39-
SubResources: map[string]*associationSubResource{
40-
"NamespaceID": {
41-
TerraformAttributeName: "namespace_id",
42-
Command: "container namespace get {{ .NamespaceID }}",
50+
Parents: map[string]*associationParent{
51+
"namespace_id": {
52+
Fetcher: func(client *scw.Client, raw interface{}) (interface{}, error) {
53+
api := container.NewAPI(client)
54+
data := raw.(*container.Container)
55+
56+
return api.GetNamespace(&container.GetNamespaceRequest{
57+
NamespaceID: data.NamespaceID,
58+
Region: data.Region,
59+
})
60+
},
4361
},
4462
},
4563
},
4664
&container.Namespace{}: {
4765
ResourceName: "scaleway_container_namespace",
4866
ImportFormat: importFormatRegionID,
49-
SubResources: map[string]*associationSubResource{
50-
"ProjectID": {
51-
TerraformAttributeName: "project_id",
52-
Command: "container project get project-id={{ .ProjectID }}",
53-
AsDataSource: true,
67+
Parents: map[string]*associationParent{
68+
"project_id": {
69+
AsDataSource: true,
70+
Fetcher: func(client *scw.Client, raw interface{}) (interface{}, error) {
71+
api := account.NewAPI(client)
72+
data := raw.(*container.Namespace)
73+
74+
return api.GetProject(&account.GetProjectRequest{
75+
ProjectID: data.ProjectID,
76+
})
77+
},
78+
},
79+
},
80+
Children: []*associationChild{
81+
{
82+
ParentFieldMap: map[string]string{
83+
"namespace_id": "id",
84+
},
85+
Fetcher: func(client *scw.Client, raw interface{}) (interface{}, error) {
86+
api := container.NewAPI(client)
87+
data := raw.(*container.Namespace)
88+
89+
res, err := api.ListContainers(&container.ListContainersRequest{
90+
NamespaceID: data.ID,
91+
Region: data.Region,
92+
})
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
return res.Containers, nil
98+
},
99+
},
100+
},
101+
},
102+
&account.Project{}: {
103+
ResourceName: "scaleway_account_project",
104+
ImportFormat: "{{ .ID }}",
105+
Children: []*associationChild{
106+
{
107+
ParentFieldMap: map[string]string{
108+
"project_id": "id",
109+
},
110+
Fetcher: func(client *scw.Client, raw interface{}) (interface{}, error) {
111+
api := container.NewAPI(client)
112+
data := raw.(*account.Project)
113+
114+
res, err := api.ListNamespaces(&container.ListNamespacesRequest{
115+
ProjectID: &data.ID,
116+
})
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
return res.Namespaces, nil
122+
},
54123
},
55124
},
56125
},

0 commit comments

Comments
 (0)