diff --git a/internal/outputs/crossplane/format.go b/internal/outputs/crossplane/format.go index 66379fbd..995efd5a 100644 --- a/internal/outputs/crossplane/format.go +++ b/internal/outputs/crossplane/format.go @@ -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}} @@ -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 diff --git a/internal/outputs/terraform/device.tf.gotmpl b/internal/outputs/terraform/device.tf.gotmpl new file mode 100644 index 00000000..47ab07e6 --- /dev/null +++ b/internal/outputs/terraform/device.tf.gotmpl @@ -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}} +} diff --git a/internal/outputs/terraform/format.go b/internal/outputs/terraform/format.go index 7651ece3..dd2d8a18 100644 --- a/internal/outputs/terraform/format.go +++ b/internal/outputs/terraform/format.go @@ -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 } diff --git a/internal/outputs/terraform/project.tf.gotmpl b/internal/outputs/terraform/project.tf.gotmpl new file mode 100644 index 00000000..bd26da16 --- /dev/null +++ b/internal/outputs/terraform/project.tf.gotmpl @@ -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 +} + diff --git a/internal/projects/retrieve.go b/internal/projects/retrieve.go index a0055df7..513c26a8 100644 --- a/internal/projects/retrieve.go +++ b/internal/projects/retrieve.go @@ -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)