forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (97 loc) · 5.07 KB
/
main.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
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
// Licensed under the Mozilla Public License v2.0
package main
import (
"flag"
"log"
"os"
"strings"
"github.com/fatih/color"
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
provider "github.com/terraform-providers/terraform-provider-oci/oci"
)
func main() {
var command = flag.String("command", "", "Command to run. Supported commands include: 'export', 'list_export_resources' and 'list_export_services'. 'list_export_services' supports json format.")
var listExportServicesPath = flag.String("list_export_services_path", "", "[export] Path to output list of supported services in json format")
var compartmentId = flag.String("compartment_id", "", "[export] OCID of a compartment to export. If no compartment id nor name is specified, the root compartment will be used.")
var compartmentName = flag.String("compartment_name", "", "[export] The name of a compartment to export.")
var includeRelatedResources = flag.Bool("include_related_resources", false, "[export] Set this flag to discover related resources for the resource OCIDs specified in `ids` argument.")
var outputPath = flag.String("output_path", "", "[export] Path to output generated configurations and state files of the exported compartment")
var services = flag.String("services", "", "[export] Comma-separated list of service resources to export. By default, all compartment-scope resources are exported.")
var excludeServices = flag.String("exclude_services", "", "[export] [experimental] Comma-separated list of service resources to exclude from export. If a service is present in both 'services' and 'exclude_services' argument, it will be excluded.")
var ids = flag.String("ids", "", "[export] Comma-separated list of tuples <resource Type:resource ID> for resources to export. The ID could either be an OCID or a Terraform import ID. By default, all resources are exported.")
var generateStateFile = flag.Bool("generate_state", false, "[export][experimental] Set this to import the discovered resources into a state file along with the Terraform configuration")
var help = flag.Bool("help", false, "Prints usage options")
var tfVersion = flag.String("tf_version", "0.12", "The version of terraform syntax to generate for configurations. The state file will be written in v0.12 only. The allowed values are :\n * 0.11\n * 0.12")
var retryTimeout = flag.String("retry_timeout", "15s", "[export] The time duration for which API calls will wait and retry operation in case of API errors. By default, the retry timeout duration is 15s")
var parallelism = flag.Int("parallelism", 1, "The number of threads to use for resource discovery. By default the value is 1")
flag.Parse()
provider.PrintVersion()
if help != nil && *help {
flag.PrintDefaults()
os.Exit(0)
}
if command == nil || *command == "" {
log.Println("Executable runs in Terraform plugin mode by default. For additional usage options, please run with the '-help' flag.")
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return provider.Provider()
},
})
} else {
switch *command {
case "export":
var terraformVersion provider.TfHclVersion
if provider.TfVersionEnum(*tfVersion) == provider.TfVersion11 {
terraformVersion = &provider.TfHclVersion11{Value: provider.TfVersionEnum(*tfVersion)}
} else if *tfVersion == "" || provider.TfVersionEnum(*tfVersion) == provider.TfVersion12 {
terraformVersion = &provider.TfHclVersion12{Value: provider.TfVersionEnum(*tfVersion)}
} else {
color.Red("[ERROR]: Invalid tf_version '%s', supported values: 0.11, 0.12\n", *tfVersion)
os.Exit(1)
}
if *parallelism < 1 {
color.Red("[ERROR] parallelism cannot be less than 1, specify at least 1")
os.Exit(1)
}
args := &provider.ExportCommandArgs{
CompartmentId: compartmentId,
CompartmentName: compartmentName,
OutputDir: outputPath,
GenerateState: *generateStateFile,
TFVersion: &terraformVersion,
RetryTimeout: retryTimeout,
IsExportWithRelatedResources: *includeRelatedResources,
Parallelism: *parallelism,
}
if services != nil && *services != "" {
args.Services = strings.Split(*services, ",")
}
if excludeServices != nil && *excludeServices != "" {
args.ExcludeServices = strings.Split(*excludeServices, ",")
}
if ids != nil && *ids != "" {
args.IDs = strings.Split(*ids, ",")
}
err, status := provider.RunExportCommand(args)
if err != nil {
color.Red("%v", err)
}
os.Exit(int(status))
case "list_export_resources":
if err := provider.RunListExportableResourcesCommand(); err != nil {
color.Red("%v", err)
os.Exit(1)
}
case "list_export_services":
if err := provider.RunListExportableServicesCommand(*listExportServicesPath); err != nil {
color.Red("%v", err)
os.Exit(1)
}
default:
log.Printf("[ERROR]: No command '%s' supported\n", *command)
os.Exit(1)
}
}
}