Skip to content

Commit 7c2e473

Browse files
authored
Merge pull request #25 from ckaznocha/add_dcos_api_token
Add dcos api token
2 parents a4aec47 + a08944c commit 7c2e473

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ A [Concourse](https://concourse.ci/) resource to deploy applications to [Maratho
1818

1919
* `basic_auth`: *Optional.* Use if you are using HTTP Basic Auth to protect your Marathon instance. Takes `user_name` and `password`
2020

21+
* `api_token`: *Optional.* Use if you are using DC/OS and need to set an HTTP API token.
22+
2123
## Behavior
2224

2325
### `check`: Extract versions of an app from Marathon.

cmd/marathon-resource/behaviors/behaviors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Source struct {
2222
AppID string `json:"app_id"`
2323
URI string `json:"uri"`
2424
BasicAuth *marathon.AuthCreds `json:"basic_auth"`
25+
APIToken string `json:"api_token"`
2526
}
2627

2728
//Version maps to a concousre version

cmd/marathon-resource/behaviors/behaviors_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ func TestOut(t *testing.T) {
2929
mockMarathoner.EXPECT().CheckDeployment("foo").Times(3).Return(false, nil),
3030
mockMarathoner.EXPECT().CheckDeployment("bing").Times(1).Return(false, nil),
3131
mockMarathoner.EXPECT().CheckDeployment("foo").Times(3).Return(false, nil),
32-
mockMarathoner.EXPECT().CheckDeployment("bing").Times(1).Return(false, errors.New("Something bad happened")),
32+
mockMarathoner.EXPECT().CheckDeployment("bing").Times(1).Return(false, errors.New("something bad happened")),
3333
mockMarathoner.EXPECT().CheckDeployment("baz").Times(2).Return(true, nil),
34-
mockMarathoner.EXPECT().CheckDeployment("quux").Times(1).Return(false, errors.New("Something bad happened")),
34+
mockMarathoner.EXPECT().CheckDeployment("quux").Times(1).Return(false, errors.New("something bad happened")),
3535
mockMarathoner.EXPECT().CheckDeployment("zork").Times(2).Return(true, nil),
3636
)
3737
gomock.InOrder(
3838
mockMarathoner.EXPECT().DeleteDeployment("baz").Times(1).Return(nil),
39-
mockMarathoner.EXPECT().DeleteDeployment("zork").Times(1).Return(errors.New("No way!")),
39+
mockMarathoner.EXPECT().DeleteDeployment("zork").Times(1).Return(errors.New("no way")),
4040
)
4141
gomock.InOrder(
4242
mockMarathoner.EXPECT().RestartApp(gomock.Any()).Times(1).Return(gomarathon.DeploymentID{DeploymentID: "bing", Version: "bar"}, nil),
43-
mockMarathoner.EXPECT().RestartApp(gomock.Any()).Times(1).Return(gomarathon.DeploymentID{}, errors.New("No way!")),
43+
mockMarathoner.EXPECT().RestartApp(gomock.Any()).Times(1).Return(gomarathon.DeploymentID{}, errors.New("no way")),
4444
mockMarathoner.EXPECT().RestartApp(gomock.Any()).Times(1).Return(gomarathon.DeploymentID{DeploymentID: "bing", Version: "bar"}, nil),
4545
)
4646
gomock.InOrder(
4747
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(1).Return([]string{"bar"}, nil),
4848
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(2).Return([]string{"baz"}, nil),
49-
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(1).Return([]string{}, errors.New("No way!")),
49+
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(1).Return([]string{}, errors.New("no way")),
5050
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(1).Return([]string{"baz"}, nil),
5151
mockMarathoner.EXPECT().LatestVersions(gomock.Any(), "").Times(1).Return([]string{"baz"}, nil),
5252
)

cmd/marathon-resource/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
logger.WithError(err).Fatalf("Malformed URI %s", input.Source.URI)
4343
}
4444

45-
m := marathon.NewMarathoner(&http.Client{}, uri, input.Source.BasicAuth, logger)
45+
m := marathon.NewMarathoner(&http.Client{}, uri, input.Source.BasicAuth, input.Source.APIToken, logger)
4646

4747
switch os.Args[1] {
4848
case check:

cmd/marathon-resource/marathon/marathon.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ type (
3939
DeleteDeployment(deploymentID string) error
4040
}
4141
marathon struct {
42-
client doer
43-
url *url.URL
44-
auth *AuthCreds
45-
logger logrus.FieldLogger
42+
client doer
43+
url *url.URL
44+
auth *AuthCreds
45+
apiToken string
46+
logger logrus.FieldLogger
4647
}
4748

4849
//AuthCreds will be used for HTTP basic auth
@@ -57,8 +58,15 @@ func NewMarathoner(
5758
client doer,
5859
uri *url.URL,
5960
auth *AuthCreds,
61+
apiToken string,
6062
logger logrus.FieldLogger) Marathoner {
61-
return &marathon{client: client, url: uri, logger: logger}
63+
return &marathon{
64+
client: client,
65+
url: uri,
66+
auth: auth,
67+
apiToken: apiToken,
68+
logger: logger,
69+
}
6270
}
6371

6472
func (m *marathon) handleReq(
@@ -78,6 +86,10 @@ func (m *marathon) handleReq(
7886
if m.auth != nil {
7987
req.SetBasicAuth(m.auth.UserName, m.auth.Password)
8088
}
89+
if m.apiToken != "" {
90+
req.Header.Set("Authorization", fmt.Sprintf("token=%s", m.apiToken))
91+
}
92+
8193
m.logger.WithFields(
8294
logrus.Fields{
8395
"Method": req.Method,

cmd/marathon-resource/marathon/marathon_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,10 @@ func Test_newMarathoner(t *testing.T) {
472472
args args
473473
want Marathoner
474474
}{
475-
{"Works", args{http.DefaultClient, &url.URL{}, logger}, &marathon{http.DefaultClient, &url.URL{}, nil, logger}},
475+
{"Works", args{http.DefaultClient, &url.URL{}, logger}, &marathon{http.DefaultClient, &url.URL{}, nil, "", logger}},
476476
}
477477
for _, tt := range tests {
478-
if got := NewMarathoner(tt.args.client, tt.args.uri, nil, logger); !reflect.DeepEqual(got, tt.want) {
478+
if got := NewMarathoner(tt.args.client, tt.args.uri, nil, "", logger); !reflect.DeepEqual(got, tt.want) {
479479
t.Errorf("%q. NewMarathoner() = %v, want %v", tt.name, got, tt.want)
480480
}
481481
}

0 commit comments

Comments
 (0)