Skip to content

Commit

Permalink
refactor: clarify & expand names for cloudgov module
Browse files Browse the repository at this point in the history
  • Loading branch information
zjrgov committed Jan 17, 2025
1 parent b4b79b2 commit de4f358
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cg
package cloudgov

import (
"context"
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -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
Expand All @@ -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()
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//go:build integration

package cg_test
package cloudgov_test

import (
"bufio"
"encoding/json"
"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(
Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit de4f358

Please sign in to comment.