Skip to content

Commit

Permalink
Merge pull request #107 from bcgov/feature/gwa-gateway
Browse files Browse the repository at this point in the history
Feature/gwa gateway
  • Loading branch information
rustyjux authored Jun 27, 2024
2 parents bc18d23 + 7f85408 commit 000ac3f
Show file tree
Hide file tree
Showing 24 changed files with 581 additions and 512 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
env:
GWA_API_HOST: api.gov.bc.ca
GWA_CLIENT_ID: gwa-cli
GWA_VERSION: v2
GWA_VERSION: v3

jobs:
release-tag:
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Currently documentation is limited to the APS [USER-JOURNEY.md](https://github.c
To run help on any command while using the CLI:

```sh
$ gwa-cli login --help
$ gwa login --help
You can login via device login or by using client credentials

To use device login, simply run the command like so:
Expand Down Expand Up @@ -66,8 +66,8 @@ Tools:
Test any commmands by running `just run` in the `cwd`. You can also use `$ just test` to run all tests.
```sh
$ just run namespace current
your-namespace
$ just run gateway current
your-gateway
$ just test
? github.com/bcgov/gwa-cli [no test files]
ok github.com/bcgov/gwa-cli/cmd
Expand Down Expand Up @@ -106,7 +106,7 @@ To install locally you can follow the first 2 steps in Development above, then r
```sh
$ just install
...
$ gwa-cli
$ gwa
gwa version 2.0.0-beta
```
Expand All @@ -117,8 +117,7 @@ Shell completions for all the commands ships with each version. Completions allo
To install completions, run this after installing, using `zsh` for example:
```sh
$ gwa-cli completion zsh --help
$ gwa-cli completion zsh | pbcopy
$ gwa completion zsh --help
```
Then follow the instructions from the help command and paste the output where it needs to live. Bash, Fish and Powershell are also supported.
Then follow the brief instructions from the help command. Bash, Fish and Powershell are also supported.
34 changes: 29 additions & 5 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (o *ApplyOptions) Parse() error {
}
}
}
o.output = append([]interface{}{gatewayService}, o.output...)

// Only append gatewayService if it has configurations
if len(gatewayService.Config) > 0 {
o.output = append([]interface{}{gatewayService}, o.output...)
}
return nil
}

Expand Down Expand Up @@ -142,25 +146,31 @@ $ gwa apply --input gw-config.yaml
if err != nil {
return err
}
pkg.Info("Namespace:" + ctx.Namespace)
pkg.Info("Gateway:" + ctx.Gateway)

counter := &PublishCounter{}
printBlankLine := false
var errors []string // Collect error messages here

for _, config := range opts.output {
switch c := config.(type) {
case GatewayService:
printBlankLine = true
fmt.Println()
fmt.Printf("↑ Publishing Gateway Services")
res, err := PublishGatewayService(ctx, c.Config)
if err != nil {
counter.AddFailed()
fmt.Print("\r")
fmt.Printf("%s Gateway Services publish failed\n", pkg.Times())
pkg.Error(fmt.Sprintf("Publish Error: %v", err))
errorMessage := fmt.Sprintf("[GatewayService]: %v", err)
pkg.Error(errorMessage)
errors = append(errors, errorMessage)
break
}

counter.AddSuccess()
fmt.Println()
fmt.Printf("%s Gateway Services published\n", pkg.Checkmark())
fmt.Println(res.Results)
fmt.Print("\r")
Expand All @@ -172,13 +182,19 @@ $ gwa apply --input gw-config.yaml
break

case Resource:
if !printBlankLine {
fmt.Println()
printBlankLine = true
}
fmt.Printf("↑ [%s] %s", c.Kind, c.Config["name"])
result, err := PublishResource(ctx, c.Config, c.GetAction())
if err != nil {
counter.AddFailed()
fmt.Print("\r")
fmt.Printf("%s [%s] %s failed\n", pkg.Times(), c.Kind, c.Config["name"])
pkg.Error(fmt.Sprintf("Resource Error: %v", err))
errorMessage := fmt.Sprintf("Resource [%s] %s: %v", c.Kind, c.Config["name"], err)
pkg.Error(errorMessage)
errors = append(errors, errorMessage)
break
}

Expand All @@ -192,6 +208,14 @@ $ gwa apply --input gw-config.yaml
fmt.Println()
fmt.Println(counter.Print())

if len(errors) > 0 {
fmt.Println()
fmt.Println(pkg.Times(), pkg.PrintError("Errors encountered"))
for _, errMsg := range errors {
fmt.Println(errMsg)
}
}

return nil
},
}
Expand All @@ -216,7 +240,7 @@ func PublishResource(ctx *pkg.AppContext, doc map[string]interface{}, arg string
if err != nil {
return "", err
}
route := fmt.Sprintf("/ds/api/%s/namespaces/%s/%ss", ctx.ApiVersion, ctx.Namespace, arg)
route := fmt.Sprintf("/ds/api/%s/gateways/%s/%ss", ctx.ApiVersion, ctx.Gateway, arg)
URL, _ := ctx.CreateUrl(route, nil)
request, err := pkg.NewApiPut[PutResponse](ctx, URL, bytes.NewBuffer(body))
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions cmd/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var kongConfig = `services:
tags: [ ns.aps-moh-proto ]
`

var input = `kind: Namespace
var input = `kind: Gateway
name: ns-sampler
displayName: ns-sampler Display Name
---
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestApplyOptions(t *testing.T) {
"host": "api.co2.com",
},
}},
Skipped{Name: "ns-sampler", Kind: "Namespace"},
Skipped{Name: "ns-sampler", Kind: "Gateway"},
Resource{Kind: "CredentialIssuer", Config: map[string]interface{}{"name": "aps-moh-proto default"}},
Resource{Kind: "DraftDataset", Config: map[string]interface{}{"name": "my-service-dataset"}},
Resource{Kind: "Product", Config: map[string]interface{}{"name": "my-service API"}},
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestPublishResource(t *testing.T) {
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(
"PUT",
"https://aps.gov.bc.ca/ds/api/v2/namespaces/ns-sampler/issuers",
"https://aps.gov.bc.ca/ds/api/v2/gateways/ns-sampler/issuers",
func(r *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, map[string]interface{}{
"result": "Issuer published",
Expand All @@ -180,7 +180,7 @@ func TestPublishResource(t *testing.T) {
)
ctx := &pkg.AppContext{
ApiVersion: "v2",
Namespace: "ns-sampler",
Gateway: "ns-sampler",
Host: "aps.gov.bc.ca",
}
doc := map[string]interface{}{
Expand All @@ -198,7 +198,7 @@ func TestPublishGatewayService(t *testing.T) {
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(
"PUT",
"https://aps.gov.bc.ca/gw/api/v2/namespaces/ns-sampler/gateway",
"https://aps.gov.bc.ca/gw/api/v2/gateways/ns-sampler/gateway",
func(r *http.Request) (*http.Response, error) {
err := r.ParseMultipartForm(10 << 20)
if err != nil {
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestPublishGatewayService(t *testing.T) {
}
ctx := &pkg.AppContext{
ApiVersion: "v2",
Namespace: "ns-sampler",
Gateway: "ns-sampler",
Host: "aps.gov.bc.ca",
}
res, err := PublishGatewayService(ctx, doc)
Expand All @@ -260,7 +260,7 @@ func TestApplyStdout(t *testing.T) {
"✓ Gateway Services published",
"Pubished: 2\nSkipped: 1",
"4/4 Published, 1 Skipped",
"- [Namespace] ns-sampler",
"- [Gateway] ns-sampler",
"↑ [CredentialIssuer] aps-moh-proto default",
"✓ [CredentialIssuer] aps-moh-proto default: Published",
"↑ [DraftDataset] my-service-dataset",
Expand All @@ -276,7 +276,7 @@ func TestApplyStdout(t *testing.T) {
"↑ Publishing Gateway Services",
"x Gateway Services publish failed",
"0/4 Published, 1 Skipped",
"- [Namespace] ns-sampler",
"- [Gateway] ns-sampler",
"↑ [CredentialIssuer] aps-moh-proto default",
"x [CredentialIssuer] aps-moh-proto default failed",
"↑ [DraftDataset] my-service-dataset",
Expand All @@ -292,7 +292,7 @@ func TestApplyStdout(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

regexPattern := `=~^https://api\.gov\.bc\.ca/ds/api/v2/namespaces/ns-sampler/\w+$`
regexPattern := `=~^https://api\.gov\.bc\.ca/ds/api/v2/gateways/ns-sampler/\w+$`
httpmock.RegisterResponder("PUT", regexPattern, func(_ *http.Request) (*http.Response, error) {
fmt.Println("Resource publish")
return httpmock.NewJsonResponse(tt.responseCode, map[string]interface{}{
Expand All @@ -301,7 +301,7 @@ func TestApplyStdout(t *testing.T) {
})
httpmock.RegisterResponder(
"PUT",
"https://api.gov.bc.ca/gw/api/v2/namespaces/ns-sampler/gateway",
"https://api.gov.bc.ca/gw/api/v2/gateways/ns-sampler/gateway",
func(_ *http.Request) (*http.Response, error) {
fmt.Println("gateway publish")
return httpmock.NewJsonResponse(tt.responseCode, map[string]interface{}{
Expand All @@ -314,7 +314,7 @@ func TestApplyStdout(t *testing.T) {
cwd := t.TempDir()
ctx := &pkg.AppContext{
Cwd: cwd,
Namespace: "ns-sampler",
Gateway: "ns-sampler",
ApiHost: "api.gov.bc.ca",
ApiVersion: "v2",
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewConfigCmd(ctx *pkg.AppContext) *cobra.Command {
}

func NewConfigGetCmd(ctx *pkg.AppContext) *cobra.Command {
args := []string{"api_key", "host", "namespace"}
args := []string{"api_key", "host", "gateway"}
argsSentence := pkg.ArgumentsSliceToString(args, "and")

var configGetCmd = &cobra.Command{
Expand All @@ -34,7 +34,7 @@ func NewConfigGetCmd(ctx *pkg.AppContext) *cobra.Command {
- api_key
- host
- namespace
- gateway
`),
ValidArgs: args,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Expand All @@ -60,24 +60,24 @@ func NewConfigSetCmd(ctx *pkg.AppContext) *cobra.Command {
Exposes some specific config values that can be defined by the user.
%s
namespace: The default namespace used
gateway: The default gateway used
token: Use only if you have a token you know is authenticated
host: The API host you wish to communicate with
scheme: http or https
`, lipgloss.NewStyle().Bold(true).Render("Configurable Settings:")),
Example: heredoc.Doc(`
$ gwa config set namespace ns-sampler
$ gwa config set --namespace ns-sampler
$ gwa config set gateway ns-sampler
$ gwa config set --gateway ns-sampler
`),
RunE: pkg.WrapError(ctx, func(cmd *cobra.Command, args []string) error {
totalArgs := len(args)
if totalArgs > 1 {
switch args[0] {
case "token":
viper.Set("api_key", args[1])
case "namespace":
viper.Set("namespace", args[1])
case "gateway":
viper.Set("gateway", args[1])
case "host":
viper.Set("host", args[1])
case "scheme":
Expand Down Expand Up @@ -108,10 +108,10 @@ $ gwa config set --namespace ns-sampler
}),
}

configSetCmd.Flags().String("token", "", "set the namespace")
configSetCmd.Flags().String("token", "", "set the gateway")
viper.BindPFlag("api_key", configSetCmd.Flags().Lookup("token"))
configSetCmd.Flags().String("namespace", "", "set the namespace")
viper.BindPFlag("namespace", configSetCmd.Flags().Lookup("namespace"))
configSetCmd.Flags().String("gateway", "", "set the gateway")
viper.BindPFlag("gateway", configSetCmd.Flags().Lookup("gateway"))
configSetCmd.Flags().String("host", "", "set the host")
viper.BindPFlag("host", configSetCmd.Flags().Lookup("host"))
configSetCmd.Flags().String("scheme", "", "set the scheme")
Expand All @@ -128,7 +128,7 @@ const (
func initialSetModel(ctx *pkg.AppContext) pkg.GenerateModel {
var prompts = make([]pkg.PromptField, 2)

prompts[key] = pkg.NewList("Select a config key to set", []string{"host", "namespace", "scheme", "token"})
prompts[key] = pkg.NewList("Select a config key to set", []string{"host", "gateway", "scheme", "token"})
prompts[value] = pkg.NewTextInput("Value", "", true)

m := pkg.GenerateModel{
Expand Down
18 changes: 9 additions & 9 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestSuccessfulConfigCommands(t *testing.T) {
args: []string{"set", "host", "my.local.dev:8000"},
},
{
name: "set namespace",
args: []string{"set", "namespace", "ns-sampler"},
name: "set gateway",
args: []string{"set", "gateway", "ns-sampler"},
},
{
name: "set scheme",
Expand All @@ -56,9 +56,9 @@ func TestSuccessfulConfigCommands(t *testing.T) {
configKey: "host",
},
{
name: "set namespace flag",
args: []string{"set", "--namespace", "ns-sampler"},
configKey: "namespace",
name: "set gateway flag",
args: []string{"set", "--gateway", "ns-sampler"},
configKey: "gateway",
},
{
name: "set scheme flag",
Expand All @@ -71,8 +71,8 @@ func TestSuccessfulConfigCommands(t *testing.T) {
configKey: "api_key",
},
{
name: "get namespace",
args: []string{"get", "namespace"},
name: "get gateway",
args: []string{"get", "gateway"},
expect: `ns-sampler
`,
},
Expand Down Expand Up @@ -127,8 +127,8 @@ func TestErrorConfigCommands(t *testing.T) {
},
{
name: "no flag value set",
args: []string{"set", "--namespace"},
expect: "flag needs an argument: --namespace",
args: []string{"set", "--gateway"},
expect: "flag needs an argument: --gateway",
},
}
for _, tt := range tests {
Expand Down
Loading

0 comments on commit 000ac3f

Please sign in to comment.