|
4 | 4 | "fmt"
|
5 | 5 | "os"
|
6 | 6 | "os/exec"
|
| 7 | + "strings" |
7 | 8 | "time"
|
8 | 9 |
|
9 | 10 | log "github.com/Sirupsen/logrus"
|
@@ -49,21 +50,48 @@ func main() {
|
49 | 50 | standalone := cmd.Flags().Bool("standalone", false, "Set if running standalone, disables manager leadership verification")
|
50 | 51 | // Import options
|
51 | 52 | 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>") |
53 | 54 | importGrpID := cmd.Flags().String("import-group-id", "", "Defines the group ID to import the resource into (optional)")
|
54 | 55 |
|
55 | 56 | cmd.Run = func(c *cobra.Command, args []string) {
|
56 | 57 | mustHaveTerraform()
|
57 | 58 | importInstSpec, err := parseInstanceSpecFromGroup(*importGrpSpecURL, *importGrpID)
|
58 | 59 | 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 |
60 | 61 | // not start since terraform is not managing the resource
|
61 | 62 | log.Error(err)
|
62 | 63 | panic(err)
|
63 | 64 | }
|
| 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 | + } |
64 | 92 | importOpts := terraform.ImportOptions{
|
65 | 93 | InstanceSpec: importInstSpec,
|
66 |
| - InstanceID: importInstID, |
| 94 | + Resources: resources, |
67 | 95 | }
|
68 | 96 | cli.SetLogLevel(*logLevel)
|
69 | 97 | run.Plugin(plugin_base.DefaultTransport(*name), instance_plugin.PluginServer(
|
@@ -118,6 +146,10 @@ func parseInstanceSpecFromGroup(groupSpecURL, groupID string) (*instance.Spec, e
|
118 | 146 | }
|
119 | 147 | tags["infrakit.group"] = groupID
|
120 | 148 | }
|
| 149 | + // Use the first logical ID if set |
| 150 | + if len(groupProps.Allocation.LogicalIDs) > 0 { |
| 151 | + tags["LogicalID"] = string(groupProps.Allocation.LogicalIDs[0]) |
| 152 | + } |
121 | 153 |
|
122 | 154 | spec := instance.Spec{
|
123 | 155 | Properties: groupProps.Instance.Properties,
|
|
0 commit comments