-
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.
Feature: Add Registry ops to query private Providers and Modules (#46) (
#62) * Feature: Add Registry operations to query private Providers and Modules (#46) * Fix: typo in filter flag in policy_set.go * Fix: "failedd" typo in README * Fix: simplify WorkspaceDetail struct, remove comments * Fix: remove comment * Fix: bump go version * Fix: Quote go version
- Loading branch information
1 parent
481d17e
commit e6ea793
Showing
32 changed files
with
845 additions
and
119 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
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,115 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/AGLEnergyPublic/tfectl/resources" | ||
tfe "github.com/hashicorp/go-tfe" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type RegistryModule struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Provider string `json:"provider"` | ||
RegistryName tfe.RegistryName `json:"registry_name"` | ||
Namespace string `json:"namespace"` | ||
PublishingMechanism tfe.PublishingMechanism `json:"publishing_mechanism"` | ||
Status tfe.RegistryModuleStatus `json:"status"` | ||
TestConfig bool `json:"test_config"` | ||
VCSRepo string `json:"vcs_repo"` | ||
ModuleLatestVersion string `json:"module_latest_version"` | ||
} | ||
|
||
var registryModuleCmd = &cobra.Command{ | ||
Use: "registry-module", | ||
Short: "Query/Manage TFE private module registry", | ||
Long: `Query/Manage TFE private module regsistry.`, | ||
} | ||
|
||
var registryModuleListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List private modules in TFE Organization", | ||
Long: `List private modules in TFE Organization.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
organization, client, err := resources.Setup(cmd) | ||
check(err) | ||
|
||
query, _ := cmd.Flags().GetString("query") | ||
|
||
moduleList, err := listPrivateModules(client, organization) | ||
check(err) | ||
|
||
moduleListJson, _ := json.MarshalIndent(moduleList, "", " ") | ||
|
||
if query != "" { | ||
resources.JqRun(moduleListJson, query) | ||
} else { | ||
fmt.Println(string(moduleListJson)) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(registryModuleCmd) | ||
registryModuleCmd.AddCommand(registryModuleListCmd) | ||
} | ||
|
||
func listPrivateModules(client *tfe.Client, organization string) ([]RegistryModule, error) { | ||
results := []RegistryModule{} | ||
result := RegistryModule{} | ||
currentPage := 1 | ||
|
||
for { | ||
log.Debugf("Processing page %d\n", currentPage) | ||
options := &tfe.RegistryModuleListOptions{ | ||
ListOptions: tfe.ListOptions{ | ||
PageNumber: currentPage, | ||
PageSize: 50, | ||
}, | ||
} | ||
|
||
rms, err := client.RegistryModules.List(context.Background(), organization, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, rmItem := range rms.Items { | ||
result.RegistryName = rmItem.RegistryName | ||
result.ID = rmItem.ID | ||
result.Name = rmItem.Name | ||
result.Namespace = rmItem.Namespace | ||
result.VCSRepo = rmItem.VCSRepo.DisplayIdentifier | ||
result.PublishingMechanism = rmItem.PublishingMechanism | ||
result.Provider = rmItem.Provider | ||
result.Status = rmItem.Status | ||
|
||
if rmItem.TestConfig != nil { | ||
result.TestConfig = rmItem.TestConfig.TestsEnabled | ||
} | ||
|
||
for _, rmvs := range rmItem.VersionStatuses { | ||
if rmvs.Status == "ok" { | ||
// get latest module version which published without errors | ||
result.ModuleLatestVersion = rmvs.Version | ||
break | ||
} | ||
} | ||
|
||
results = append(results, result) | ||
} | ||
|
||
if rms.NextPage == 0 { | ||
break | ||
} | ||
|
||
currentPage++ | ||
} | ||
|
||
return results, nil | ||
} |
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,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRegistryModulesListCmd(t *testing.T) { | ||
|
||
tt := []struct { | ||
args []string | ||
err error | ||
}{ | ||
{ | ||
args: []string{"registry-module", "list"}, | ||
err: nil, | ||
}, | ||
} | ||
|
||
r := rootCmd | ||
c1 := registryModuleCmd | ||
c2 := registryModuleListCmd | ||
r.AddCommand(c1, c2) | ||
|
||
runTestCasesNoOutput(t, r, tt) | ||
|
||
r.RemoveCommand(c1, c2) | ||
r.AddCommand(c1) | ||
} |
Oops, something went wrong.