Skip to content

Commit

Permalink
add crossplane outputter format (with basic device external-name supp…
Browse files Browse the repository at this point in the history
…ort)

Signed-off-by: Marques Johansson <mjohansson@equinix.com>
  • Loading branch information
displague committed Jul 28, 2021
1 parent 47cc2c8 commit de58436
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (c *Client) Format() outputPkg.Format {
break
case outputPkg.FormatTable,
outputPkg.FormatTerraform,
outputPkg.FormatCrossplane,
outputPkg.FormatJSON,
outputPkg.FormatYAML:
format = f
Expand Down
68 changes: 68 additions & 0 deletions internal/outputs/crossplane/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package crossplane

import (
"bytes"
"html/template"

"github.com/packethost/packngo"
)

const (
deviceFormat = `
---
apiVersion: server.metal.equinix.com/v1alpha2
kind: Device
metadata:
name: {{.Hostname}}
annotations:
crossplane.io/external-name: {{.ID}}
spec:
## Late-Initialization will provide the current values
## so we don't specify them here.
forProvider:
hostname: {{.Hostname}}
plan: {{.Plan.Slug}}
metro: {{.Metro.Code}}
operatingSystem: {{.OS.Slug}}
billingCycle: {{.BillingCycle}}
locked: {{.Locked}}
tags: {{.Tags}}
## The "default" provider will be used unless named here.
# providerConfigRef:
# name: equinix-metal-provider
## EM devices do not persist passwords in the API long. This
## optional secret will not get the root pass for devices > 24h
# writeConnectionSecretToRef:
# name: crossplane-example
# namespace: crossplane-system
## Do not delete devices that have been imported.
# reclaimPolicy: Retain
`
)

func many(s string) string {
return `{{range .}}` + s + `{{end}}`
}
func Marshal(i interface{}) ([]byte, error) {
var f = ""
switch i.(type) {
case *packngo.Device:
f = deviceFormat
case []packngo.Device:
f = many(deviceFormat)
}
tmpl, err := template.New("crossplane").Parse(f)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, i)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
21 changes: 17 additions & 4 deletions internal/outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
"github.com/olekukonko/tablewriter"
"sigs.k8s.io/yaml"

"github.com/equinix/metal-cli/internal/outputs/crossplane"
"github.com/equinix/metal-cli/internal/outputs/terraform"
)

type Format string

const (
FormatTable Format = "table"
FormatJSON Format = "json"
FormatYAML Format = "yaml"
FormatTerraform Format = "tf"
FormatTable Format = "table"
FormatJSON Format = "json"
FormatYAML Format = "yaml"
FormatTerraform Format = "tf"
FormatCrossplane Format = "crossplane"
)

type Outputer interface {
Expand All @@ -29,6 +31,15 @@ type Standard struct {
Format Format
}

func outputCrossplane(in interface{}) error {
output, err := crossplane.Marshal(in)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}

func outputTerraform(in interface{}) error {
output, err := terraform.Marshal(in)
if err != nil {
Expand Down Expand Up @@ -63,6 +74,8 @@ func (o *Standard) Output(in interface{}, header []string, data *[][]string) err
return outputYAML(in)
} else if o.Format == FormatTerraform {
return outputTerraform(in)
} else if o.Format == FormatCrossplane {
return outputCrossplane(in)
} else {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
Expand Down

0 comments on commit de58436

Please sign in to comment.