Skip to content

Commit 4a1c32f

Browse files
committed
Added more comments for resource imports
1 parent 241011a commit 4a1c32f

File tree

8 files changed

+84
-0
lines changed

8 files changed

+84
-0
lines changed

cmd/internal/converters/azure_cloud_service_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
octopus2 "github.com/mcasperson/OctopusTerraformExport/cmd/internal/model/octopus"
@@ -107,6 +108,17 @@ func (c AzureCloudServiceTargetConverter) toHcl(target octopus2.AzureCloudServic
107108
},
108109
}
109110
file := hclwrite.NewEmptyFile()
111+
112+
// Add a comment with the import command
113+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
114+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
115+
Type: hclsyntax.TokenComment,
116+
Bytes: []byte("# Import existing resources with the following commands:\n" +
117+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + target.Name + "\") | .Id')\n" +
118+
"# terraform import octopusdeploy_azure_cloud_service_deployment_target." + targetName + " ${RESOURCE_ID}\n"),
119+
SpacesBefore: 0,
120+
}})
121+
110122
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
111123

112124
return string(file.Bytes()), nil

cmd/internal/converters/azure_service_fabric_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/hcl"
@@ -110,6 +111,17 @@ func (c AzureServiceFabricTargetConverter) toHcl(target octopus2.AzureServiceFab
110111
Endpoint: nil,
111112
}
112113
file := hclwrite.NewEmptyFile()
114+
115+
// Add a comment with the import command
116+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
117+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
118+
Type: hclsyntax.TokenComment,
119+
Bytes: []byte("# Import existing resources with the following commands:\n" +
120+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + target.Name + "\") | .Id')\n" +
121+
"# terraform import octopusdeploy_azure_service_fabric_cluster_deployment_target." + targetName + " ${RESOURCE_ID}\n"),
122+
SpacesBefore: 0,
123+
}})
124+
113125
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
114126

115127
secretVariableResource := terraform2.TerraformVariable{

cmd/internal/converters/azure_web_app_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
octopus2 "github.com/mcasperson/OctopusTerraformExport/cmd/internal/model/octopus"
@@ -105,6 +106,17 @@ func (c AzureWebAppTargetConverter) toHcl(target octopus2.AzureWebAppResource, r
105106
},
106107
}
107108
file := hclwrite.NewEmptyFile()
109+
110+
// Add a comment with the import command
111+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
112+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
113+
Type: hclsyntax.TokenComment,
114+
Bytes: []byte("# Import existing resources with the following commands:\n" +
115+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + target.Name + "\") | .Id')\n" +
116+
"# terraform import octopusdeploy_azure_web_app_deployment_target." + targetName + " ${RESOURCE_ID}\n"),
117+
SpacesBefore: 0,
118+
}})
119+
108120
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
109121

110122
return string(file.Bytes()), nil

cmd/internal/converters/certificate_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/hcl"
@@ -100,6 +101,17 @@ func (c CertificateConverter) toHcl(certificate octopus2.Certificate, recursive
100101
//Version: certificate.Version,
101102
}
102103
file := hclwrite.NewEmptyFile()
104+
105+
// Add a comment with the import command
106+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
107+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
108+
Type: hclsyntax.TokenComment,
109+
Bytes: []byte("# Import existing resources with the following commands:\n" +
110+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + certificate.Name + "\") | .Id')\n" +
111+
"# terraform import octopusdeploy_certificate." + certificateName + " ${RESOURCE_ID}\n"),
112+
SpacesBefore: 0,
113+
}})
114+
103115
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
104116

105117
defaultPassword := ""

cmd/internal/converters/channel_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/hcl"
@@ -83,6 +84,17 @@ func (c ChannelConverter) toHcl(channel octopus2.Channel, recursive bool, terraf
8384
TenantTags: channel.TenantTags,
8485
}
8586
file := hclwrite.NewEmptyFile()
87+
88+
// Add a comment with the import command
89+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
90+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
91+
Type: hclsyntax.TokenComment,
92+
Bytes: []byte("# Import existing resources with the following commands:\n" +
93+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + channel.Name + "\") | .Id')\n" +
94+
"# terraform import octopusdeploy_channel." + resourceName + " ${RESOURCE_ID}\n"),
95+
SpacesBefore: 0,
96+
}})
97+
8698
block := gohcl.EncodeAsBlock(terraformResource, "resource")
8799

88100
/* Channels reference steps and packages by text without terraform understanding

cmd/internal/converters/cloud_target_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
octopus2 "github.com/mcasperson/OctopusTerraformExport/cmd/internal/model/octopus"
@@ -97,6 +98,17 @@ func (c CloudRegionTargetConverter) toHcl(target octopus2.CloudRegionResource, r
9798
DefaultWorkerPoolId: &target.Endpoint.DefaultWorkerPoolId,
9899
}
99100
file := hclwrite.NewEmptyFile()
101+
102+
// Add a comment with the import command
103+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
104+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
105+
Type: hclsyntax.TokenComment,
106+
Bytes: []byte("# Import existing resources with the following commands:\n" +
107+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + target.Name + "\") | .Id')\n" +
108+
"# terraform import octopusdeploy_cloud_region_deployment_target." + targetName + " ${RESOURCE_ID}\n"),
109+
SpacesBefore: 0,
110+
}})
111+
100112
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
101113

102114
return string(file.Bytes()), nil

cmd/internal/converters/environment_converter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"github.com/hashicorp/hcl2/gohcl"
5+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
56
"github.com/hashicorp/hcl2/hclwrite"
67
"github.com/mcasperson/OctopusTerraformExport/cmd/internal/client"
78
octopus2 "github.com/mcasperson/OctopusTerraformExport/cmd/internal/model/octopus"
@@ -81,6 +82,17 @@ func (c EnvironmentConverter) toHcl(environment octopus2.Environment, recursive
8182
},
8283
}
8384
file := hclwrite.NewEmptyFile()
85+
86+
// Add a comment with the import command
87+
baseUrl, _ := c.Client.GetSpaceBaseUrl()
88+
file.Body().AppendUnstructuredTokens([]*hclwrite.Token{{
89+
Type: hclsyntax.TokenComment,
90+
Bytes: []byte("# Import existing resources with the following commands:\n" +
91+
"# RESOURCE_ID=$(curl -H \"X-Octopus-ApiKey: API-REPLACEME\" " + baseUrl + "/" + c.GetResourceType() + " | jq -r '.Items[] | select(.name=\"" + environment.Name + "\") | .Id')\n" +
92+
"# terraform import octopusdeploy_environment." + resourceName + " ${RESOURCE_ID}\n"),
93+
SpacesBefore: 0,
94+
}})
95+
8496
file.Body().AppendBlock(gohcl.EncodeAsBlock(terraformResource, "resource"))
8597

8698
return string(file.Bytes()), nil

wasm/convert_project.wasm

19 KB
Binary file not shown.

0 commit comments

Comments
 (0)