-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 1. Support querying PolicySets and PolicyChecks 2. Returns detailed PolicyCheck output for a given TFC run 3. Vendoring * Update root_test * chore: update dependency * feat: Use native go-tfe to retrieve sentinel body of policy check * feat: More details in policy-set list, improve README * feat: List all runs * chore: vendoring * fix: Bump to go 1.19 * fix: bump go version * fix: spurious declaration in vendored go-tfe (?)
- Loading branch information
1 parent
5612360
commit 3af6f36
Showing
32 changed files
with
1,406 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/AGLEnergyPublic/tfectl/resources" | ||
"github.com/hashicorp/go-tfe" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type PolicyCheckResult struct { | ||
AdvisoryFailed int `json:"advisory_failed"` | ||
HardFailed int `json:"hard_failed"` | ||
Passed int `json:"passed"` | ||
Result bool `json:"result"` | ||
SoftFailed int `json:"soft_failed"` | ||
TotalFailed int `json:"total_failed"` | ||
Sentinel any `json:"sentinel"` | ||
} | ||
|
||
type PolicyCheck struct { | ||
ID string `json:"id"` | ||
Result PolicyCheckResult `json:"result"` | ||
Status tfe.PolicyStatus `json:"status"` | ||
Scope tfe.PolicyScope `json:"scope"` | ||
} | ||
|
||
var policyCheckCmd = &cobra.Command{ | ||
Use: "policy-check", | ||
Short: "Manage policy check workflows of a TFE run", | ||
Long: `Manage policy check workflows of a TFE run.`, | ||
} | ||
|
||
var policyCheckShowCmd = &cobra.Command{ | ||
Use: "show", | ||
Short: "Show details of the policy check in a TFE run", | ||
Long: `Show details of the policy check in a TFE run`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// policy check show function | ||
_, client, err := resources.Setup(cmd) | ||
check(err) | ||
|
||
runId, _ := cmd.Flags().GetString("run-id") | ||
query, _ := cmd.Flags().GetString("query") | ||
|
||
var policyCheckJson []byte | ||
|
||
policyCheck, _ := showPolicyChecks(client, runId) | ||
|
||
policyCheckJson, _ = json.MarshalIndent(policyCheck, "", " ") | ||
if query != "" { | ||
resources.JqRun(policyCheckJson, query) | ||
} else { | ||
fmt.Println(string(policyCheckJson)) | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(policyCheckCmd) | ||
|
||
// Show sub-command | ||
// Returns the detailed policy check results for a given list of RunIDs | ||
policyCheckCmd.AddCommand(policyCheckShowCmd) | ||
policyCheckShowCmd.Flags().String("run-id", "", "RunId to inspect") | ||
} | ||
|
||
func showPolicyChecks(client *tfe.Client, runID string) (PolicyCheck, error) { | ||
result := PolicyCheck{} | ||
log.Debugf("Retrieving policy checks for run: %s\n", runID) | ||
options := &tfe.PolicyCheckListOptions{} | ||
|
||
pc, err := client.PolicyChecks.List(context.Background(), runID, options) | ||
check(err) | ||
|
||
polchk := pc.Items[0] | ||
|
||
result.ID = polchk.ID | ||
result.Scope = polchk.Scope | ||
result.Status = polchk.Status | ||
result.Result.AdvisoryFailed = polchk.Result.AdvisoryFailed | ||
result.Result.HardFailed = polchk.Result.HardFailed | ||
result.Result.TotalFailed = polchk.Result.TotalFailed | ||
result.Result.SoftFailed = polchk.Result.SoftFailed | ||
result.Result.Passed = polchk.Result.Passed | ||
result.Result.Sentinel = polchk.Result.Sentinel | ||
result.Result.Result = polchk.Result.Result | ||
|
||
return result, nil | ||
} |
Oops, something went wrong.