This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
tf.go
110 lines (95 loc) · 3.1 KB
/
tf.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
// Copyright 2018 the Service Broker Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"fmt"
"log"
"os"
"text/tabwriter"
"time"
"github.com/GoogleCloudPlatform/gcp-service-broker/brokerapi/brokers/models"
"github.com/GoogleCloudPlatform/gcp-service-broker/db_service"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/providers/tf"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/providers/tf/wrapper"
"github.com/GoogleCloudPlatform/gcp-service-broker/utils"
"github.com/jinzhu/gorm"
"github.com/spf13/cobra"
)
func init() {
var jobRunner *tf.TfJobRunner
var db *gorm.DB
tfCmd := &cobra.Command{
Use: "tf",
Short: "Interact with the Terraform backend",
Long: `Interact with the Terraform backend`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
logger := utils.NewLogger("tf")
db = db_service.New(logger)
jobRunner, err = tf.NewTfJobRunerFromEnv()
return err
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
rootCmd.AddCommand(tfCmd)
tfCmd.AddCommand(&cobra.Command{
Use: "dump",
Short: "dump a Terraform workspace",
Run: func(cmd *cobra.Command, args []string) {
deployment, err := db_service.GetTerraformDeploymentById(context.Background(), args[0])
if err != nil {
log.Fatal(err)
}
ws, err := wrapper.DeserializeWorkspace(deployment.Workspace)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
log.Fatal(err)
}
fmt.Println(ws)
},
})
tfCmd.AddCommand(&cobra.Command{
Use: "wait",
Short: "wait for a Terraform job",
Run: func(cmd *cobra.Command, args []string) {
err := jobRunner.Wait(context.Background(), args[0])
if err != nil {
log.Fatal(err)
}
},
})
tfCmd.AddCommand(&cobra.Command{
Use: "list",
Short: "show the list of Terraform workspaces",
Run: func(cmd *cobra.Command, args []string) {
results := []models.TerraformDeployment{}
if err := db.Find(&results).Error; err != nil {
log.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.StripEscape)
fmt.Fprintln(w, "ID\tLast Operation\tState\tLast Updated\tElapsed\tMessage")
for _, result := range results {
lastUpdate := result.UpdatedAt.Format(time.RFC822)
elapsed := ""
if result.LastOperationState == tf.InProgress {
elapsed = time.Now().Sub(result.UpdatedAt).Truncate(time.Second).String()
}
fmt.Fprintf(w, "%q\t%s\t%s\t%s\t%s\t%q\n", result.ID, result.LastOperationType, result.LastOperationState, lastUpdate, elapsed, result.LastOperationMessage)
}
w.Flush()
},
})
}