Skip to content

Commit

Permalink
respect dem headers
Browse files Browse the repository at this point in the history
  • Loading branch information
praktiskt committed Nov 3, 2020
1 parent 9a5f417 commit b9e76c3
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 64 deletions.
22 changes: 7 additions & 15 deletions cluster/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cluster
import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/magnusfurugard/flinkctl/tools"
"github.com/parnurzeal/gorequest"
)

type ClusterConfig struct {
Expand All @@ -30,13 +32,8 @@ type ClusterOverview struct {
}

func (c *Cluster) GetConfig() (ClusterConfig, error) {
//TODO: Respect headers
re, err := http.Get(c.ConfigURL.String())
if err != nil {
return ClusterConfig{}, err
}
defer re.Body.Close()

re, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(c.ConfigURL.String())).End()
//TODO: Error management
body, err := ioutil.ReadAll(re.Body)
if err != nil {
return ClusterConfig{}, err
Expand All @@ -51,13 +48,8 @@ func (c *Cluster) GetConfig() (ClusterConfig, error) {
}

func (c *Cluster) GetOverview() (ClusterOverview, error) {
// TODO: Respect headers
re, err := http.Get(c.OverviewURL.String())
if err != nil {
return ClusterOverview{}, err
}
defer re.Body.Close()

re, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(c.OverviewURL.String())).End()
//TODO: Error management
body, err := ioutil.ReadAll(re.Body)
if err != nil {
return ClusterOverview{}, err
Expand Down
13 changes: 5 additions & 8 deletions cluster/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/magnusfurugard/flinkctl/tools"
"github.com/parnurzeal/gorequest"
)

type JobDescription struct {
Expand Down Expand Up @@ -92,16 +94,11 @@ type JobDescription struct {
}

func (c *Cluster) DescribeJob(jid string) (JobDescription, error) {
//TODO: Respect headers
if len(jid) != 32 {
return JobDescription{}, fmt.Errorf("invalid jid: %v", jid)
}
re, err := http.Get(c.Jobs.URL.String() + "/" + jid)
if err != nil {
return JobDescription{}, err
}
defer re.Body.Close()

re, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(c.Jobs.URL.String() + "/" + jid)).End()
// TODO: Error management
body, err := ioutil.ReadAll(re.Body)
if err != nil {
return JobDescription{}, err
Expand Down
22 changes: 7 additions & 15 deletions cluster/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package cluster
import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/magnusfurugard/flinkctl/tools"
"github.com/parnurzeal/gorequest"
)

type Jobs struct {
Expand Down Expand Up @@ -40,13 +42,8 @@ type JobsOverview struct {
}

func (c *Cluster) GetJobs() (Jobs, error) {
//TODO: Respect headers
re, err := http.Get(c.Jobs.URL.String())
if err != nil {
return Jobs{}, err
}
defer re.Body.Close()

re, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(c.Jobs.URL.String())).End()
//TODO: Error management
body, err := ioutil.ReadAll(re.Body)
if err != nil {
return Jobs{}, err
Expand All @@ -60,13 +57,8 @@ func (c *Cluster) GetJobs() (Jobs, error) {
func (c *Cluster) GetJobsOverview() (JobsOverview, error) {
// TODO: Fix .duration to be a duration
// TODO: .last-modification and start-time shuold be datetimes (UTC)
// TODO: Respect headers
re, err := http.Get(c.Jobs.OverviewURL.String())
if err != nil {
return JobsOverview{}, err
}
defer re.Body.Close()

re, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(c.Jobs.OverviewURL.String())).End()
//TODO: Error management
body, err := ioutil.ReadAll(re.Body)
if err != nil {
return JobsOverview{}, err
Expand Down
10 changes: 5 additions & 5 deletions cmd/addCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ flinkctl config add-cluster https://localhost:567 --headers="Authorization: Basi
currentConfig := config.Get()
newConfig := config.ClusterConfig{URL: u.String(), Headers: headers}
if len(currentConfig.Clusters) == 0 {
viper.SetDefault("clusters", newConfig)
viper.SetDefault("current-cluster", u.String())
viper.SafeWriteConfig()
viper.Set("clusters", newConfig)
viper.Set("current-cluster", u.String())
viper.WriteConfig()
viper.ReadInConfig()
fmt.Printf("Config file created: %v\n", viper.ConfigFileUsed())
} else {
viper.SetDefault("clusters", append(currentConfig.Clusters, newConfig))
viper.SetDefault("current-cluster", u.String())
viper.Set("clusters", append(currentConfig.Clusters, newConfig))
viper.Set("current-cluster", u.String())
viper.WriteConfig()
fmt.Printf("current-cluster updated: %v\n", u.String())
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/getJars.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package cmd
import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/magnusfurugard/flinkctl/tools"
"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
)

Expand All @@ -41,12 +42,8 @@ var getJarsCmd = &cobra.Command{
Short: "List all uploaded jars in your cluster",
PreRun: func(cmd *cobra.Command, args []string) { InitCluster() },
RunE: func(cmd *cobra.Command, args []string) error {
//TODO: Respect headers
resp, err := http.Get(cl.Jars.URL.String())
if err != nil {
return err
}
defer resp.Body.Close()
resp, _, _ := tools.ApplyHeadersToRequest(gorequest.New().Get(cl.Jars.URL.String())).End()
//TODO: Error management
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func Print(t interface{}) {
}

func InitCluster() {
host, err := config.GetCurrent()
conf, err := config.GetCurrent()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
hostString := strings.TrimSpace(host.URL)
hostString := strings.TrimSpace(conf.URL)
cl = c.New(hostString)
}

Expand Down
24 changes: 14 additions & 10 deletions cmd/submitJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"strings"

"github.com/magnusfurugard/flinkctl/tools"
"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -63,12 +64,13 @@ var submitJobCmd = &cobra.Command{

u := cl.Jars.UploadURL.String()
for _, file := range args {
// TODO: Respect headers
resp, _, _ := gorequest.
New().
Post(u).
Type("multipart").
SendFile(file).

resp, _, _ := tools.ApplyHeadersToRequest(
gorequest.
New().
Post(u).
Type("multipart").
SendFile(file)).
End()

body, err := ioutil.ReadAll(resp.Body)
Expand All @@ -89,10 +91,12 @@ var submitJobCmd = &cobra.Command{
EntryClass: StringOrNil(entryClass),
})

resp, _, _ = gorequest.New().
Post(fileToPass).
Type("json").
Send(string(appb)).
resp, _, _ = tools.ApplyHeadersToRequest(
gorequest.
New().
Post(fileToPass).
Type("json").
Send(string(appb))).
End()

body, err = ioutil.ReadAll(resp.Body)
Expand Down
7 changes: 7 additions & 0 deletions config/new.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

//TODO: Refactor into more relevant files

import (
"fmt"
"os"
Expand Down Expand Up @@ -61,3 +63,8 @@ func CheckCurrentExists() {
os.Exit(1)
}
}

func GetHeaders() []string {
current, _ := GetCurrent()
return current.Headers
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.14

require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23
github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272 // indirect
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 // indirect
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/parnurzeal/gorequest v0.2.16
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
gopkg.in/yaml.v2 v2.2.4
moul.io/http2curl v1.0.0 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272 h1:Am81SElhR3XCQBunTisljzNkNese2T1FiV8jP79+dqg=
github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -164,6 +168,7 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down
19 changes: 19 additions & 0 deletions tools/applyHeaders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tools

import (
"strings"

"github.com/magnusfurugard/flinkctl/config"
"github.com/parnurzeal/gorequest"
)

func ApplyHeadersToRequest(request *gorequest.SuperAgent) *gorequest.SuperAgent {
for _, header := range config.GetHeaders() {
parts := strings.Split(header, ": ")
if len(parts) != 2 {
panic("could not split header: " + header)
}
request = request.AppendHeader(parts[0], parts[1])
}
return request
}

0 comments on commit b9e76c3

Please sign in to comment.