-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding namespaces command with which you can list the namespaces available to the openfaas instance to deploy functions to Signed-off-by: Martin Dekov <[email protected]>
- Loading branch information
1 parent
39a7a0b
commit a58302d
Showing
3 changed files
with
133 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/openfaas/faas-cli/proxy" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
// Setup flags that are used by multiple commands (variables defined in faas.go) | ||
namespacesCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://") | ||
namespacesCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation") | ||
namespacesCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth") | ||
|
||
faasCmd.AddCommand(namespacesCmd) | ||
} | ||
|
||
var namespacesCmd = &cobra.Command{ | ||
Use: `namespaces [--gateway GATEWAY_URL] [--tls-no-verify] [--token JWT_TOKEN]`, | ||
Aliases: []string{"ns"}, | ||
Short: "List OpenFaaS namespaces", | ||
Long: `Lists OpenFaaS namespaces either on a local or remote gateway`, | ||
Example: ` faas-cli namespaces | ||
faas-cli namespaces --gateway https://127.0.0.1:8080`, | ||
RunE: runNamespaces, | ||
} | ||
|
||
func runNamespaces(cmd *cobra.Command, args []string) error { | ||
gatewayAddress := getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment)) | ||
|
||
namespaces, err := proxy.ListNamespacesToken(gatewayAddress, tlsInsecure, token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printNamespaces(namespaces) | ||
|
||
return nil | ||
} | ||
|
||
func printNamespaces(namespaces []string) { | ||
fmt.Print("Namespaces:\n") | ||
for _, v := range namespaces { | ||
fmt.Printf(" - %s\n", v) | ||
} | ||
} |
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,73 @@ | ||
package proxy | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// ListNamespaces lists available function namespaces | ||
func ListNamespaces(gateway string, tlsInsecure bool) ([]string, error) { | ||
return ListNamespacesToken(gateway, tlsInsecure, "") | ||
} | ||
|
||
// ListNamespacesToken lists available function namespaces with a token as auth | ||
func ListNamespacesToken(gateway string, tlsInsecure bool, token string) ([]string, error) { | ||
var namespaces []string | ||
|
||
gateway = strings.TrimRight(gateway, "/") | ||
client := MakeHTTPClient(&defaultCommandTimeout, tlsInsecure) | ||
client.CheckRedirect = func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
} | ||
|
||
getEndpoint, err := createNamespacesEndpoint(gateway) | ||
if err != nil { | ||
return namespaces, err | ||
} | ||
|
||
getRequest, err := http.NewRequest(http.MethodGet, getEndpoint, nil) | ||
|
||
if len(token) > 0 { | ||
SetToken(getRequest, token) | ||
} else { | ||
SetAuth(getRequest, gateway) | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway) | ||
} | ||
|
||
res, err := client.Do(getRequest) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway) | ||
} | ||
|
||
if res.Body != nil { | ||
defer res.Body.Close() | ||
} | ||
|
||
switch res.StatusCode { | ||
case http.StatusOK: | ||
|
||
bytesOut, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot read namespaces from OpenFaaS on URL: %s", gateway) | ||
} | ||
jsonErr := json.Unmarshal(bytesOut, &namespaces) | ||
if jsonErr != nil { | ||
return nil, fmt.Errorf("cannot parse namespaces from OpenFaaS on URL: %s\n%s", gateway, jsonErr.Error()) | ||
} | ||
case http.StatusUnauthorized: | ||
return nil, fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server") | ||
default: | ||
bytesOut, err := ioutil.ReadAll(res.Body) | ||
if err == nil { | ||
return nil, fmt.Errorf("server returned unexpected status code: %d - %s", res.StatusCode, string(bytesOut)) | ||
} | ||
} | ||
return namespaces, 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