Skip to content

Commit

Permalink
Merge pull request #60 from GSA-TTS/go/cf-client
Browse files Browse the repository at this point in the history
Connect to Cloud Foundry through go-cfclient
  • Loading branch information
zjrgov authored Dec 16, 2024
2 parents c92a54d + e4854ce commit f9819e0
Show file tree
Hide file tree
Showing 14 changed files with 793 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ override.tf.json
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# example: *tfplan*
22 changes: 22 additions & 0 deletions runner/cf-driver-go/.gitignore
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
11 changes: 11 additions & 0 deletions runner/cf-driver-go/.vscode/launch.json
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"
}
]
}
14 changes: 14 additions & 0 deletions runner/cf-driver-go/Makefile
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
51 changes: 51 additions & 0 deletions runner/cf-driver-go/README.md
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.
51 changes: 51 additions & 0 deletions runner/cf-driver-go/cg/cf_adapter.go
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
}
78 changes: 78 additions & 0 deletions runner/cf-driver-go/cg/cg.go
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()
}
Loading

0 comments on commit f9819e0

Please sign in to comment.