Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate request id for each provider request. #387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ignored = ["github.com/Azure/go-ansiterm*"]

[[constraint]]
name = "github.com/fnproject/fn_go"
version = "0.2.11"
version = "0.2.12"

[[constraint]]
name = "github.com/giantswarm/semver-bump"
Expand Down
14 changes: 14 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"log"
"os"
"os/exec"
"encoding/base32"
"os/signal"
"path/filepath"
"crypto/rand"
"strings"
"time"
"unicode"
Expand All @@ -32,6 +34,8 @@ const (
FunctionsDockerImage = "fnproject/fnserver"
FuncfileDockerRuntime = "docker"
MinRequiredDockerVersion = "17.5.0"
RequestID = "request-id"
RequestedLength = 16
)

// GetWd returns working directory.
Expand All @@ -55,6 +59,16 @@ func GetDir(c *cli.Context) string {
return dir
}

func GetRequestID() string {
byteArr := make([]byte, RequestedLength)
_, err := rand.Read(byteArr)
if err != nil {
log.Fatalf("failed to generate random number for requestID")
}

return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
}

// BuildFunc bumps version and builds function.
func BuildFunc(c *cli.Context, fpath string, funcfile *FuncFile, buildArg []string, noCache bool) (*FuncFile, error) {
var err error
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ package main

import (
"bytes"
"crypto/rand"
"encoding/base32"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"

"github.com/fnproject/cli/commands"
"github.com/fnproject/cli/common"
"github.com/fnproject/cli/common/color"
"github.com/fnproject/cli/config"
"github.com/spf13/viper"
"github.com/urfave/cli"
)

func getRequestID() string {
Copy link
Member

@msgodf msgodf Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the format of these IDs? It might be worth giving an example in a comment above the function.

byteArr := make([]byte, 16)
_, err := rand.Read(byteArr)
if err != nil {
log.Fatalf("failed to generate random number for requestID")
}

return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
}

func newFn() *cli.App {
app := cli.NewApp()
app.Name = "fn"
Expand All @@ -29,6 +43,8 @@ func newFn() *cli.App {
if err != nil {
return err
}

viper.Set(common.RequestID, common.GetRequestID())
commandArgOverrides(c)
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions objects/app/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

"github.com/fnproject/cli/client"
"github.com/fnproject/cli/common"
fnclient "github.com/fnproject/fn_go/client"
fnclient "github.com/fnproject/fn_go/client"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some trailing whitespace here.

apiapps "github.com/fnproject/fn_go/client/apps"
"github.com/fnproject/fn_go/models"
"github.com/fnproject/fn_go/provider"
"github.com/jmoiron/jsonq"
"github.com/spf13/viper"
"github.com/urfave/cli"
)

Expand All @@ -26,7 +27,8 @@ type appsCmd struct {
}

func (a *appsCmd) list(c *cli.Context) error {
params := &apiapps.GetAppsParams{Context: context.Background()}
ctx := provider.WithRequestID(context.Background(), viper.GetString("request-id"))
params := &apiapps.GetAppsParams{Context: ctx}
var resApps []*models.App
for {
resp, err := a.client.Apps.GetApps(params)
Expand Down