Skip to content

Commit

Permalink
use latest TF+crossplane provider, use go embeds for templating
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Mar 6, 2023
1 parent 890a9e7 commit 0633c2e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
5 changes: 3 additions & 2 deletions internal/outputs/crossplane/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const (
deviceFormat = `
---
apiVersion: server.metal.equinix.com/v1alpha2
apiVersion: devices.metal.equinix.jet.crossplane.io/v1alpha1
kind: Device
metadata:
name: {{.Hostname}}
Expand Down Expand Up @@ -47,8 +47,9 @@ spec:
func many(s string) string {
return `{{range .}}` + s + `{{end}}`
}

func Marshal(i interface{}) ([]byte, error) {
var f = ""
f := ""
switch i.(type) {
case *packngo.Device:
f = deviceFormat
Expand Down
11 changes: 11 additions & 0 deletions internal/outputs/terraform/device.tf.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# terraform import equinix_metal_device.{{.Hostname}} {{.ID}}
resource "equinix_metal_device" "{{.Hostname}}" {
plan = "{{.Plan.Slug}}"
hostname = "{{.Hostname}}"
billing_cycle = "{{.BillingCycle}}"
metro = "{{.Metro.Code}}"
operating_system = "{{.OS.Slug}}"
project_id = "{{.Project.ID}}"

tags = {{.Tags}}
}
34 changes: 20 additions & 14 deletions internal/outputs/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@ package terraform

import (
"bytes"
_ "embed"
"html/template"
"path"

"github.com/packethost/packngo"
)

const deviceFormat = `
# terraform import metal_device.{{.Hostname}} {{.ID}}
resource "metal_device" "{{.Hostname}}" {
plan = "{{.Plan.Slug}}"
hostname = "{{.Hostname}}"
billing_cycle = "{{.BillingCycle}}"
metro = "{{.Metro.Code}}"
operating_system = "{{.OS.Slug}}"
project_id = "{{.Project.ID}}"
var (
//go:embed device.tf.gotmpl
deviceFormat string

tags = {{.Tags}}
}
`
//go:embed project.tf.gotmpl
projectFormat string
)

func many(s string) string {
return `{{range .}}` + s + `{{end}}`
}

func Marshal(i interface{}) ([]byte, error) {
var f = ""
f := ""
switch i.(type) {
case *packngo.Device:
f = deviceFormat
case []packngo.Device:
f = many(deviceFormat)
case *packngo.Project:
f = projectFormat
case []packngo.Project:
f = many(projectFormat)
}
tmpl, err := template.New("terraform").Parse(f)

tmpl, err := template.New("terraform").Funcs(template.FuncMap{
"hrefToID": func(href string) string {
return path.Base(href)
},
}).Parse(f)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions internal/outputs/terraform/project.tf.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# terraform import equinix_metal_project.{{.Name}} {{.ID}}
resource "equinix_metal_project" "{{.Name}}" {
name = "{{.Name}}"
organization_id = "{{.Organization.URL | hrefToID}}"
# TODO: bgp_config
}

1 change: 0 additions & 1 deletion internal/projects/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func (c *Client) Retrieve() *cobra.Command {
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
header := []string{"ID", "Name", "Created"}
return c.Out.Output(p, header, &data)
Expand Down

0 comments on commit 0633c2e

Please sign in to comment.