/
secret_get_output.go
120 lines (103 loc) · 3.24 KB
/
secret_get_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// originally generated by 'threeport-sdk codegen api-model' but will not be regenerated - intended for modification
package cmd
import (
"fmt"
"net/http"
"os"
"text/tabwriter"
v0 "github.com/threeport/threeport/pkg/api/v0"
util "github.com/threeport/threeport/pkg/util/v0"
client "github.com/threeport/threeport/pkg/client/v0"
)
// outputGetSecretsCmd produces the tabular output for the
// 'tptctl get secrets' command.
func outputGetSecretsCmd(
secretInstances *[]v0.SecretInstance,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t AGE\t WORKLOAD INSTANCE")
if len(*secretInstances) > 0 {
for _, secretInstance := range *secretInstances {
workloadInstanceName, _ := getWorkloadInstanceName(apiClient, apiEndpoint, secretInstance)
fmt.Fprintln(
writer,
*secretInstance.Name, "\t",
util.GetAge(secretInstance.CreatedAt), "\t",
workloadInstanceName,
)
}
}
writer.Flush()
return nil
}
// outputGetSecretDefinitionsCmd produces the tabular output for the
// 'tptctl get secret-definitions' command.
func outputGetSecretDefinitionsCmd(
secretDefinitions *[]v0.SecretDefinition,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t AGE")
for _, secretDefinition := range *secretDefinitions {
fmt.Fprintln(
writer,
*secretDefinition.Name, "\t",
util.GetAge(secretDefinition.CreatedAt),
)
}
writer.Flush()
return nil
}
// outputGetSecretInstancesCmd produces the tabular output for the
// 'tptctl get secret-instances' command.
func outputGetSecretInstancesCmd(
secretInstances *[]v0.SecretInstance,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t AGE\t WORKLOAD INSTANCE")
if len(*secretInstances) > 0 {
for _, secretInstance := range *secretInstances {
workloadInstanceName, _ := getWorkloadInstanceName(apiClient, apiEndpoint, secretInstance)
fmt.Fprintln(
writer,
*secretInstance.Name, "\t",
util.GetAge(secretInstance.CreatedAt), "\t",
workloadInstanceName,
)
}
}
writer.Flush()
return nil
}
// getWorkloadInstanceName returns the name of the workload instance
// that the secret instance is attached to.
func getWorkloadInstanceName(apiClient *http.Client, apiEndpoint string, secretInstance v0.SecretInstance) (string, error) {
attachedObjectReferences, err := client.GetAttachedObjectReferencesByAttachedObjectID(
apiClient,
apiEndpoint,
*secretInstance.ID,
)
if err != nil {
return "", fmt.Errorf("failed to get attached object references: %v", err)
}
if len(*attachedObjectReferences) == 0 {
return "", fmt.Errorf("expected 1 attached object reference, got %d", len(*attachedObjectReferences))
}
if len(*attachedObjectReferences) > 1 {
return "", fmt.Errorf("expected 1 attached object reference, got %d", len(*attachedObjectReferences))
}
workloadInstance, err := client.GetWorkloadInstanceByID(
apiClient,
apiEndpoint,
*(*attachedObjectReferences)[0].ObjectID,
)
if err != nil {
return "", fmt.Errorf("failed to get workload instance: %v", err)
}
return *workloadInstance.Name, nil
}