-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from GSA-TTS/go/cf-client
Connect to Cloud Foundry through go-cfclient
- Loading branch information
Showing
14 changed files
with
793 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
go.work.sum | ||
|
||
# env file | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug main.go", | ||
"type": "go", | ||
"request": "launch", | ||
"program": "main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.DEFAULT_GOAL := build | ||
|
||
.PHONY:fmt vet build | ||
fmt: | ||
go fmt ./... | ||
|
||
vet: fmt | ||
go vet ./... | ||
|
||
test: vet | ||
go test ./... | ||
|
||
build: vet | ||
go build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# CloudFoundry (cloud.gov) driver | ||
|
||
This is a _driver_ developed for use with `gitlab-runner`’s [Custom executor](https://docs.gitlab.com/runner/executors/custom.html) to prepare, run, and clean up runner managers, workers, and services. | ||
|
||
## Go setup | ||
|
||
### Install Go | ||
|
||
You can use Homebrew to install Go on MacOS (it is generally unnecessary to use a version manager). You could also download Go packages for Linux, Mac, and Windows from [Go's website](https://go.dev/doc/install). | ||
|
||
```sh | ||
brew install go | ||
``` | ||
|
||
### Manage dependencies | ||
|
||
Manage project dependencies with `go` & `go mod`. Go programs are divided first into _modules_ and then into _packages_. Dependencies are listed in `./go.mod`. | ||
|
||
#### To download dependencies | ||
|
||
To download deps as listed in `./go.mod`, run: | ||
|
||
```sh | ||
go mod download | ||
``` | ||
|
||
Or, to walk the project and as needed install & remove modules while updating `./go.mod` & `./go.sum` accordingly: | ||
|
||
```sh | ||
go mod tidy | ||
``` | ||
|
||
#### To `get` new dependencies | ||
|
||
While `go mod tidy` can install dependencies you've already imported in your packages, you can also install them explicitly: | ||
|
||
```sh | ||
go get github.com/google/some-mod/pkg | ||
``` | ||
|
||
## Running tests | ||
|
||
The simplest way to run tests—or the one with the least typing, at least—is with `make`. | ||
|
||
```sh | ||
make test | ||
``` | ||
|
||
## Builds | ||
|
||
You can also run a build with `make` (`make build`, or simply `make`), but it won't do you much good because we aren't set up to do anything with an executable yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cg | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudfoundry/go-cfclient/v3/client" | ||
"github.com/cloudfoundry/go-cfclient/v3/config" | ||
"github.com/cloudfoundry/go-cfclient/v3/resource" | ||
) | ||
|
||
type GoCFClientAdapter struct { | ||
_con *client.Client | ||
} | ||
|
||
func (cf *GoCFClientAdapter) connect(url string, creds *Creds) error { | ||
cfg, err := config.New(url, config.UserPassword(creds.Username, creds.Password)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
con, err := client.New(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cf._con = con | ||
return nil | ||
} | ||
|
||
func (cf *GoCFClientAdapter) conn() *client.Client { | ||
if cf._con != nil { | ||
return cf._con | ||
} | ||
panic("go-cfclient adapter is not connected") | ||
} | ||
|
||
func castApps(apps []*resource.App) []*App { | ||
Apps := make([]*App, len(apps)) | ||
for idx, app := range apps { | ||
Apps[idx] = &(App{app.GUID, app.Name, app.State}) | ||
} | ||
return Apps | ||
} | ||
|
||
func (cf *GoCFClientAdapter) getApps() ([]*App, error) { | ||
apps, err := cf.conn().Applications.ListAll(context.Background(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return castApps(apps), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cg | ||
|
||
type App struct { | ||
Id string | ||
Name string | ||
State string | ||
} | ||
|
||
// Stuff we'll need to implement, for ref | ||
// | ||
// mapRoute() | ||
// | ||
// addNetworkPolicy() | ||
// removeNetworkPolicy() | ||
// | ||
// appGet() | ||
// appCmd() | ||
// appPush() | ||
// appDelete() | ||
type CloudI interface { | ||
getApps() (apps []*App, err error) | ||
connect(url string, creds *Creds) error | ||
} | ||
|
||
type CredI interface { | ||
getCreds() (*Creds, error) | ||
} | ||
|
||
type Opts struct { | ||
CredI | ||
Creds *Creds | ||
|
||
APIRootURL string | ||
} | ||
|
||
type CG struct { | ||
CloudI | ||
*Opts | ||
} | ||
|
||
const apiRootURLDefault = "https://api.fr.cloud.gov" | ||
|
||
func New(i CloudI, o *Opts) (*CG, error) { | ||
if o == nil { | ||
o = &Opts{CredI: EnvCredsGetter{}} | ||
} | ||
cg := &CG{i, o} | ||
return cg.Connect() | ||
} | ||
|
||
func (c *CG) apiRootURL() string { | ||
if c.APIRootURL == "" { | ||
return apiRootURLDefault | ||
} | ||
return c.APIRootURL | ||
} | ||
|
||
func (c *CG) creds() (*Creds, error) { | ||
if c.Creds.isEmpty() { | ||
return c.getCreds() | ||
} | ||
return c.Creds, nil | ||
} | ||
|
||
func (c *CG) Connect() (*CG, error) { | ||
creds, err := c.creds() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := c.connect(c.apiRootURL(), creds); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
func (c *CG) GetApps() ([]*App, error) { | ||
return c.getApps() | ||
} |
Oops, something went wrong.