Skip to content

Commit 0eaf348

Browse files
committed
use latest TF+crossplane provider, use go embeds for templating
1 parent 883c8a7 commit 0eaf348

5 files changed

Lines changed: 41 additions & 17 deletions

File tree

internal/outputs/crossplane/format.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
const (
1111
deviceFormat = `
1212
---
13-
apiVersion: server.metal.equinix.com/v1alpha2
13+
apiVersion: devices.metal.equinix.jet.crossplane.io/v1alpha1
1414
kind: Device
1515
metadata:
1616
name: {{.Hostname}}
@@ -47,8 +47,9 @@ spec:
4747
func many(s string) string {
4848
return `{{range .}}` + s + `{{end}}`
4949
}
50+
5051
func Marshal(i interface{}) ([]byte, error) {
51-
var f = ""
52+
f := ""
5253
switch i.(type) {
5354
case *packngo.Device:
5455
f = deviceFormat
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# terraform import equinix_metal_device.{{.Hostname}} {{.ID}}
2+
resource "equinix_metal_device" "{{.Hostname}}" {
3+
plan = "{{.Plan.Slug}}"
4+
hostname = "{{.Hostname}}"
5+
billing_cycle = "{{.BillingCycle}}"
6+
metro = "{{.Metro.Code}}"
7+
operating_system = "{{.OS.Slug}}"
8+
project_id = "{{.Project.ID}}"
9+
10+
tags = {{.Tags}}
11+
}

internal/outputs/terraform/format.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,43 @@ package terraform
22

33
import (
44
"bytes"
5+
_ "embed"
56
"html/template"
7+
"path"
68

79
"github.com/packethost/packngo"
810
)
911

10-
const deviceFormat = `
11-
# terraform import metal_device.{{.Hostname}} {{.ID}}
12-
resource "metal_device" "{{.Hostname}}" {
13-
plan = "{{.Plan.Slug}}"
14-
hostname = "{{.Hostname}}"
15-
billing_cycle = "{{.BillingCycle}}"
16-
metro = "{{.Metro.Code}}"
17-
operating_system = "{{.OS.Slug}}"
18-
project_id = "{{.Project.ID}}"
12+
var (
13+
//go:embed device.tf.gotmpl
14+
deviceFormat string
1915

20-
tags = {{.Tags}}
21-
}
22-
`
16+
//go:embed project.tf.gotmpl
17+
projectFormat string
18+
)
2319

2420
func many(s string) string {
2521
return `{{range .}}` + s + `{{end}}`
2622
}
23+
2724
func Marshal(i interface{}) ([]byte, error) {
28-
var f = ""
25+
f := ""
2926
switch i.(type) {
3027
case *packngo.Device:
3128
f = deviceFormat
3229
case []packngo.Device:
3330
f = many(deviceFormat)
31+
case *packngo.Project:
32+
f = projectFormat
33+
case []packngo.Project:
34+
f = many(projectFormat)
3435
}
35-
tmpl, err := template.New("terraform").Parse(f)
36+
37+
tmpl, err := template.New("terraform").Funcs(template.FuncMap{
38+
"hrefToID": func(href string) string {
39+
return path.Base(href)
40+
},
41+
}).Parse(f)
3642
if err != nil {
3743
return nil, err
3844
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# terraform import equinix_metal_project.{{.Name}} {{.ID}}
2+
resource "equinix_metal_project" "{{.Name}}" {
3+
name = "{{.Name}}"
4+
organization_id = "{{.Organization.URL | hrefToID}}"
5+
# TODO: bgp_config
6+
}
7+

internal/projects/retrieve.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func (c *Client) Retrieve() *cobra.Command {
9696
}
9797

9898
data := make([][]string, 1)
99-
10099
data[0] = []string{p.ID, p.Name, p.Created}
101100
header := []string{"ID", "Name", "Created"}
102101
return c.Out.Output(p, header, &data)

0 commit comments

Comments
 (0)