diff --git a/runner-manager/cfd/cg/cf_adapter.go b/runner-manager/cfd/cloudgov/cf_adapter.go similarity index 78% rename from runner-manager/cfd/cg/cf_adapter.go rename to runner-manager/cfd/cloudgov/cf_adapter.go index 56585a7..ed07095 100644 --- a/runner-manager/cfd/cg/cf_adapter.go +++ b/runner-manager/cfd/cloudgov/cf_adapter.go @@ -1,4 +1,4 @@ -package cg +package cloudgov import ( "context" @@ -8,11 +8,11 @@ import ( "github.com/cloudfoundry/go-cfclient/v3/resource" ) -type GoCFClientAdapter struct { +type CFClientAPI struct { _con *client.Client } -func (cf *GoCFClientAdapter) connect(url string, creds *Creds) error { +func (cf *CFClientAPI) connect(url string, creds *Creds) error { cfg, err := config.New(url, config.UserPassword(creds.Username, creds.Password)) if err != nil { return err @@ -27,7 +27,7 @@ func (cf *GoCFClientAdapter) connect(url string, creds *Creds) error { return nil } -func (cf *GoCFClientAdapter) conn() *client.Client { +func (cf *CFClientAPI) conn() *client.Client { if cf._con != nil { return cf._con } @@ -42,7 +42,7 @@ func castApps(apps []*resource.App) []*App { return Apps } -func (cf *GoCFClientAdapter) getApps() ([]*App, error) { +func (cf *CFClientAPI) appsGet() ([]*App, error) { apps, err := cf.conn().Applications.ListAll(context.Background(), nil) if err != nil { return nil, err diff --git a/runner-manager/cfd/cg/cg.go b/runner-manager/cfd/cloudgov/cloudgov.go similarity index 62% rename from runner-manager/cfd/cg/cg.go rename to runner-manager/cfd/cloudgov/cloudgov.go index 082436d..88b713b 100644 --- a/runner-manager/cfd/cg/cg.go +++ b/runner-manager/cfd/cloudgov/cloudgov.go @@ -1,10 +1,4 @@ -package cg - -type App struct { - Id string - Name string - State string -} +package cloudgov // Stuff we'll need to implement, for ref // @@ -17,52 +11,53 @@ type App struct { // appCmd() // appPush() // appDelete() -type CloudI interface { - getApps() (apps []*App, err error) +type ClientAPI interface { connect(url string, creds *Creds) error + + appsGet() (apps []*App, err error) } -type CredI interface { +type CredsGetter interface { getCreds() (*Creds, error) } type Opts struct { - CredI + CredsGetter Creds *Creds APIRootURL string } -type CG struct { - CloudI +type Client struct { + ClientAPI *Opts } const apiRootURLDefault = "https://api.fr.cloud.gov" -func New(i CloudI, o *Opts) (*CG, error) { +func New(i ClientAPI, o *Opts) (*Client, error) { if o == nil { - o = &Opts{CredI: EnvCredsGetter{}} + o = &Opts{CredsGetter: EnvCredsGetter{}} } - cg := &CG{i, o} + cg := &Client{i, o} return cg.Connect() } -func (c *CG) apiRootURL() string { +func (c *Client) apiRootURL() string { if c.APIRootURL == "" { return apiRootURLDefault } return c.APIRootURL } -func (c *CG) creds() (*Creds, error) { +func (c *Client) creds() (*Creds, error) { if c.Creds.isEmpty() { return c.getCreds() } return c.Creds, nil } -func (c *CG) Connect() (*CG, error) { +func (c *Client) Connect() (*Client, error) { creds, err := c.creds() if err != nil { return nil, err @@ -73,6 +68,12 @@ func (c *CG) Connect() (*CG, error) { return c, nil } -func (c *CG) GetApps() ([]*App, error) { - return c.getApps() +type App struct { + Id string + Name string + State string +} + +func (c *Client) AppsGet() ([]*App, error) { + return c.appsGet() } diff --git a/runner-manager/cfd/cg/cg_integration_test.go b/runner-manager/cfd/cloudgov/cloudgov_integration_test.go similarity index 71% rename from runner-manager/cfd/cg/cg_integration_test.go rename to runner-manager/cfd/cloudgov/cloudgov_integration_test.go index 722ba85..5f915e4 100644 --- a/runner-manager/cfd/cg/cg_integration_test.go +++ b/runner-manager/cfd/cloudgov/cloudgov_integration_test.go @@ -1,6 +1,6 @@ //go:build integration -package cg_test +package cloudgov_test import ( "bufio" @@ -8,15 +8,15 @@ import ( "os" "testing" - "github.com/GSA-TTS/gitlab-runner-cloudgov/runner/cfd/cg" + "github.com/GSA-TTS/gitlab-runner-cloudgov/runner/cfd/cloudgov" "github.com/google/go-cmp/cmp" ) -func Test_CFAdapter_GetApps(t *testing.T) { +func Test_CFAdapter_AppsGet(t *testing.T) { var u, p, want string var l int - path := "./testdata/.cg_creds" + path := "./testdata/.cloudgov_creds" f, err := os.Open(path) if err != nil { t.Errorf( @@ -60,17 +60,17 @@ scanning: return } - cgClient, err := cg.New(&cg.GoCFClientAdapter{}, &cg.Opts{ - Creds: &cg.Creds{Username: u, Password: p}, + cgClient, err := cloudgov.New(&cloudgov.CFClientAPI{}, &cloudgov.Opts{ + Creds: &cloudgov.Creds{Username: u, Password: p}, }) if err != nil { - t.Errorf("Error getting cgClient = %v", err) + t.Errorf("Error getting cloudgovClient = %v", err) return } - apps, err := cgClient.GetApps() + apps, err := cgClient.AppsGet() if err != nil { - t.Errorf("Error running GetApps() = %v", err) + t.Errorf("Error running AppsGet() = %v", err) return } diff --git a/runner-manager/cfd/cg/cg_test.go b/runner-manager/cfd/cloudgov/cloudgov_test.go similarity index 62% rename from runner-manager/cfd/cg/cg_test.go rename to runner-manager/cfd/cloudgov/cloudgov_test.go index 787936e..37d152a 100644 --- a/runner-manager/cfd/cg/cg_test.go +++ b/runner-manager/cfd/cloudgov/cloudgov_test.go @@ -1,6 +1,4 @@ -//go:build !integration - -package cg +package cloudgov import ( "errors" @@ -9,25 +7,25 @@ import ( "github.com/google/go-cmp/cmp" ) -type adapterStub struct { - CloudI +type stubClientAPI struct { + ClientAPI - StCreds *Creds StURL string + StCreds *Creds StApps []*App - FailGetApps bool + FailAppsGet bool FailConnect bool } -func (a *adapterStub) getApps() (apps []*App, err error) { - if a.FailGetApps { +func (a *stubClientAPI) getApps() (apps []*App, err error) { + if a.FailAppsGet { return nil, errors.New("fail") } return a.StApps, nil } -func (a *adapterStub) connect(url string, creds *Creds) (_ error) { +func (a *stubClientAPI) connect(url string, creds *Creds) (_ error) { if a.FailConnect { return errors.New("fail") } @@ -36,13 +34,13 @@ func (a *adapterStub) connect(url string, creds *Creds) (_ error) { return nil } -type credIStub struct { +type stubCredsGetter struct { U string P string Fail bool } -func (c credIStub) getCreds() (*Creds, error) { +func (c stubCredsGetter) getCreds() (*Creds, error) { if c.Fail { return nil, errors.New("fail") } @@ -50,25 +48,25 @@ func (c credIStub) getCreds() (*Creds, error) { } func TestNew(t *testing.T) { - optsStub := &Opts{CredI: credIStub{"a", "b", false}} - cgStub := &CG{&adapterStub{ + optsStub := &Opts{CredsGetter: stubCredsGetter{"a", "b", false}} + cgStub := &Client{&stubClientAPI{ StURL: apiRootURLDefault, StCreds: &Creds{"a", "b"}, }, optsStub} tests := []struct { - want *CG + want *Client opts *Opts wantErr interface{} name string }{ {name: "fails using default credential getter", wantErr: &syntaxError}, - {name: "returns adapted CG struct", want: cgStub, opts: optsStub}, + {name: "returns adapted Client struct", want: cgStub, opts: optsStub}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := New(&adapterStub{}, tt.opts) + got, err := New(&stubClientAPI{}, tt.opts) if (err == nil) != (tt.wantErr == nil) { t.Errorf("GetCfClient() error = %v, wantErr %v", err, tt.wantErr) return @@ -84,9 +82,9 @@ func TestNew(t *testing.T) { } } -func TestCG_apiRootURL(t *testing.T) { +func TestClient_apiRootURL(t *testing.T) { type fields struct { - CloudI + ClientAPI Opts *Opts } @@ -99,24 +97,24 @@ func TestCG_apiRootURL(t *testing.T) { name: "gets default root API URL", want: apiRootURLDefault, fields: fields{ - CloudI: &adapterStub{}, - Opts: &Opts{CredI: credIStub{}}, + ClientAPI: &stubClientAPI{}, + Opts: &Opts{CredsGetter: stubCredsGetter{}}, }, }, { name: "updates root API URL", want: "foo", fields: fields{ - CloudI: &adapterStub{}, - Opts: &Opts{CredI: credIStub{}, APIRootURL: "foo"}, + ClientAPI: &stubClientAPI{}, + Opts: &Opts{CredsGetter: stubCredsGetter{}, APIRootURL: "foo"}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - c := &CG{ - CloudI: tt.fields.CloudI, - Opts: tt.fields.Opts, + c := &Client{ + ClientAPI: tt.fields.ClientAPI, + Opts: tt.fields.Opts, } got := c.apiRootURL() if diff := cmp.Diff(got, tt.want); diff != "" { @@ -126,9 +124,9 @@ func TestCG_apiRootURL(t *testing.T) { } } -func TestCG_creds(t *testing.T) { +func TestClient_creds(t *testing.T) { type fields struct { - CloudI + ClientAPI Opts *Opts } @@ -142,29 +140,29 @@ func TestCG_creds(t *testing.T) { name: "returns creds when they already exist", want: &Creds{"a", "b"}, fields: fields{ - CloudI: &adapterStub{}, - Opts: &Opts{Creds: &Creds{"a", "b"}}, + ClientAPI: &stubClientAPI{}, + Opts: &Opts{Creds: &Creds{"a", "b"}}, }, }, { name: "returns creds from getter when not supplied", want: &Creds{"foo", "bar"}, fields: fields{ - CloudI: &adapterStub{}, - Opts: &Opts{CredI: credIStub{U: "foo", P: "bar"}}, + ClientAPI: &stubClientAPI{}, + Opts: &Opts{CredsGetter: stubCredsGetter{U: "foo", P: "bar"}}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - c := &CG{ - CloudI: tt.fields.CloudI, - Opts: tt.fields.Opts, + c := &Client{ + ClientAPI: tt.fields.ClientAPI, + Opts: tt.fields.Opts, } got, err := c.creds() if (err != nil) != tt.wantErr { - t.Errorf("CG.creds() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Client.creds() error = %v, wantErr %v", err, tt.wantErr) return } if diff := cmp.Diff(got, tt.want); diff != "" { @@ -174,16 +172,16 @@ func TestCG_creds(t *testing.T) { } } -func TestCG_Connect(t *testing.T) { +func TestClient_Connect(t *testing.T) { type fields struct { - CloudI + ClientAPI Opts *Opts } tests := []struct { fields fields - want *CG - cmpGet func(c *CG) any + want *Client + cmpGet func(c *Client) any name string checks []string wantErr bool @@ -192,47 +190,47 @@ func TestCG_Connect(t *testing.T) { name: "fails with creds() err", wantErr: true, fields: fields{ - CloudI: &adapterStub{}, - Opts: &Opts{CredI: &credIStub{Fail: true}}, + ClientAPI: &stubClientAPI{}, + Opts: &Opts{CredsGetter: &stubCredsGetter{Fail: true}}, }, }, { name: "connect sets URL & creds", - want: &CG{ - CloudI: &adapterStub{ + want: &Client{ + ClientAPI: &stubClientAPI{ StURL: "butter", StCreds: &Creds{Username: "corn", Password: "cob"}, }, }, fields: fields{ - CloudI: &adapterStub{}, + ClientAPI: &stubClientAPI{}, Opts: &Opts{ APIRootURL: "butter", Creds: &Creds{Username: "corn", Password: "cob"}, }, }, - cmpGet: func(c *CG) any { - return c.CloudI + cmpGet: func(c *Client) any { + return c.ClientAPI }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - c := &CG{ - CloudI: tt.fields.CloudI, - Opts: tt.fields.Opts, + c := &Client{ + ClientAPI: tt.fields.ClientAPI, + Opts: tt.fields.Opts, } got, err := c.Connect() if (err != nil) != tt.wantErr { - t.Errorf("CG.Connect() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Client.Connect() error = %v, wantErr %v", err, tt.wantErr) return } if tt.cmpGet == nil { - tt.cmpGet = func(c *CG) any { + tt.cmpGet = func(c *Client) any { return c } } @@ -244,11 +242,11 @@ func TestCG_Connect(t *testing.T) { } } -func TestCG_GetApps(t *testing.T) { +func TestClient_AppsGet(t *testing.T) { testApps := []*App{{Id: "1", Name: "foo"}} type fields struct { - CloudI + ClientAPI Opts *Opts } @@ -262,29 +260,29 @@ func TestCG_GetApps(t *testing.T) { name: "reports errors", wantErr: true, fields: fields{ - CloudI: &adapterStub{StApps: testApps, FailGetApps: true}, - Opts: &Opts{CredI: &credIStub{}}, + ClientAPI: &stubClientAPI{StApps: testApps, FailAppsGet: true}, + Opts: &Opts{CredsGetter: &stubCredsGetter{}}, }, }, { name: "returns available apps list", want: testApps, fields: fields{ - CloudI: &adapterStub{StApps: testApps}, - Opts: &Opts{CredI: &credIStub{}}, + ClientAPI: &stubClientAPI{StApps: testApps}, + Opts: &Opts{CredsGetter: &stubCredsGetter{}}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - c := &CG{ - CloudI: tt.fields.CloudI, - Opts: tt.fields.Opts, + c := &Client{ + ClientAPI: tt.fields.ClientAPI, + Opts: tt.fields.Opts, } - got, err := c.GetApps() + got, err := c.AppsGet() if (err != nil) != tt.wantErr { - t.Errorf("CG.GetApps() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("Client.AppsGet() error = %v, wantErr %v", err, tt.wantErr) return } if diff := cmp.Diff(got, tt.want); diff != "" { diff --git a/runner-manager/cfd/cg/creds.go b/runner-manager/cfd/cloudgov/creds.go similarity index 98% rename from runner-manager/cfd/cg/creds.go rename to runner-manager/cfd/cloudgov/creds.go index 0129221..573c799 100644 --- a/runner-manager/cfd/cg/creds.go +++ b/runner-manager/cfd/cloudgov/creds.go @@ -1,4 +1,4 @@ -package cg +package cloudgov import ( "encoding/json" diff --git a/runner-manager/cfd/cg/creds_test.go b/runner-manager/cfd/cloudgov/creds_test.go similarity index 98% rename from runner-manager/cfd/cg/creds_test.go rename to runner-manager/cfd/cloudgov/creds_test.go index a31168e..0b4c6a9 100644 --- a/runner-manager/cfd/cg/creds_test.go +++ b/runner-manager/cfd/cloudgov/creds_test.go @@ -1,6 +1,4 @@ -//go:build !integration - -package cg +package cloudgov import ( "encoding/json" diff --git a/runner-manager/cfd/cg/testdata/.cg_creds.sample b/runner-manager/cfd/cloudgov/testdata/.cloudgov_creds.sample similarity index 55% rename from runner-manager/cfd/cg/testdata/.cg_creds.sample rename to runner-manager/cfd/cloudgov/testdata/.cloudgov_creds.sample index 4095dca..7611452 100644 --- a/runner-manager/cfd/cg/testdata/.cg_creds.sample +++ b/runner-manager/cfd/cloudgov/testdata/.cloudgov_creds.sample @@ -1,5 +1,5 @@ -# For simple integration test with cg_integration_test.go -# 1. copy this file to `.cg_creds` +# For simple integration test with cloudgov_integration_test.go +# 1. copy this file to `.cloudgov_creds` # 2. replace with real credentials # 3. replace with real output username-1234-asdf