Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit ecd2bea

Browse files
kaufersDavid Chung
authored andcommitted
Add support to import multiple resources into tf plugin (#681)
Signed-off-by: Steven Kaufer <[email protected]>
1 parent 3b6cc3a commit ecd2bea

File tree

8 files changed

+1877
-292
lines changed

8 files changed

+1877
-292
lines changed

pkg/provider/terraform/instance/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ The supported fields are:
116116

117117
The plugin also supports importing an existing resource into terraform; this can be used to import the initial manager into terraform. Once the resource is imported into terraform, a corresponding `.tf.json` file is also created. The following optional fields are used for this purpose:
118118
* `import-group-spec-url`: The group specification URL that contains a nested instance specification; the `.tf.json` file for the imported resource contains the properties in the instance specification
119-
* `import-instance-id`: The ID of the instance to import
119+
* `import-resource`: The terraform resource type, optional resource name, and resource ID to import into in the form of `<type>:[<name>:]<id>`. The `<type>` and `<name>` must match a resource in the instance specification; the `<name>` is required if there is more then one resource of the given type in the specification. This value may be specified multiple times.
120120
* `import-group-id`: Optional group ID that the imported resource should be tagged with
121121

122122
The plugin also checks to make sure it can call `terraform`. Install Terraform [here](https://www.terraform.io/downloads.html) if you haven't done so.

pkg/provider/terraform/instance/cmd/main.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"strings"
78
"time"
89

910
log "github.com/Sirupsen/logrus"
@@ -49,21 +50,48 @@ func main() {
4950
standalone := cmd.Flags().Bool("standalone", false, "Set if running standalone, disables manager leadership verification")
5051
// Import options
5152
importGrpSpecURL := cmd.Flags().String("import-group-spec-url", "", "Defines the group spec that the instance is imported into")
52-
importInstID := cmd.Flags().String("import-instance-id", "", "Defines the instance ID to import ")
53+
importResources := cmd.Flags().StringArray("import-resource", []string{}, "Defines the resource to import in the format <type>:[<name>:]<id>")
5354
importGrpID := cmd.Flags().String("import-group-id", "", "Defines the group ID to import the resource into (optional)")
5455

5556
cmd.Run = func(c *cobra.Command, args []string) {
5657
mustHaveTerraform()
5758
importInstSpec, err := parseInstanceSpecFromGroup(*importGrpSpecURL, *importGrpID)
5859
if err != nil {
59-
// If we cannot prase the group spec then we cannot import the resource, the plugin should
60+
// If we cannot parse the group spec then we cannot import the resource, the plugin should
6061
// not start since terraform is not managing the resource
6162
log.Error(err)
6263
panic(err)
6364
}
65+
resources := []*terraform.ImportResource{}
66+
for _, resourceString := range *importResources {
67+
split := strings.Split(resourceString, ":")
68+
if len(split) < 2 || len(split) > 3 {
69+
err := fmt.Errorf("Imported resource value is not valid: %v", resourceString)
70+
log.Error(err)
71+
panic(err)
72+
}
73+
resType := terraform.TResourceType(split[0])
74+
var resName string
75+
var resID string
76+
if len(split) == 3 {
77+
resName = split[1]
78+
resID = split[2]
79+
} else {
80+
resID = split[1]
81+
}
82+
res := terraform.ImportResource{
83+
ResourceID: &resID,
84+
ResourceType: &resType,
85+
}
86+
if resName != "" {
87+
tResName := terraform.TResourceName(resName)
88+
res.ResourceName = &tResName
89+
}
90+
resources = append(resources, &res)
91+
}
6492
importOpts := terraform.ImportOptions{
6593
InstanceSpec: importInstSpec,
66-
InstanceID: importInstID,
94+
Resources: resources,
6795
}
6896
cli.SetLogLevel(*logLevel)
6997
run.Plugin(plugin_base.DefaultTransport(*name), instance_plugin.PluginServer(
@@ -118,6 +146,10 @@ func parseInstanceSpecFromGroup(groupSpecURL, groupID string) (*instance.Spec, e
118146
}
119147
tags["infrakit.group"] = groupID
120148
}
149+
// Use the first logical ID if set
150+
if len(groupProps.Allocation.LogicalIDs) > 0 {
151+
tags["LogicalID"] = string(groupProps.Allocation.LogicalIDs[0])
152+
}
121153

122154
spec := instance.Spec{
123155
Properties: groupProps.Instance.Properties,

pkg/provider/terraform/instance/cmd/main_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ func TestParseInstanceSpecFromGroup(t *testing.T) {
4848
*instSpec)
4949
}
5050

51+
func TestParseInstanceSpecFromGroupLogicalID(t *testing.T) {
52+
groupSpecURL := `str://
53+
{
54+
"ID": "managers",
55+
"Properties": {
56+
"Allocation": {
57+
"LogicalIDs": ["mgr1", "mgr2", "mgr3"]
58+
},
59+
"instance": {
60+
"Properties": {"resource": {"aws_instance": {}}}
61+
}
62+
}
63+
}`
64+
groupID := "managers"
65+
instSpec, err := parseInstanceSpecFromGroup(groupSpecURL, groupID)
66+
require.NoError(t, err)
67+
require.Equal(t,
68+
instance.Spec{
69+
Properties: types.AnyString(`{"resource": {"aws_instance": {}}}`),
70+
Tags: map[string]string{
71+
"infrakit.config_sha": "bootstrap",
72+
"infrakit.group": groupID,
73+
"LogicalID": "mgr1",
74+
},
75+
},
76+
*instSpec)
77+
}
78+
5179
func TestParseInstanceSpecFromGroupNoGroupIDSpecified(t *testing.T) {
5280
groupSpecURL := `str://
5381
{

0 commit comments

Comments
 (0)