Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #4

Merged
merged 1 commit into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (

"github.com/databus23/goslo.policy"
"github.com/sapcc/hermes/pkg/api"
"github.com/sapcc/hermes/pkg/cmd"
"github.com/sapcc/hermes/pkg/keystone"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
"github.com/sapcc/hermes/pkg/util"
"github.com/spf13/viper"
Expand All @@ -47,18 +46,7 @@ func main() {
keystoneDriver := configuredKeystoneDriver()
storageDriver := configuredStorageDriver()
readPolicy()

// If there are args left over after flag processing, we are a Hermes CLI client
if len(flag.Args()) > 0 {
cmd.RootCmd.SetArgs(flag.Args())
cmd.SetDrivers(keystoneDriver, storageDriver)
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else { // otherwise, we are running a Hermes API server
api.Server(keystoneDriver, storageDriver)
}
api.Server(keystoneDriver, storageDriver)
}

func parseCmdlineFlags() {
Expand Down Expand Up @@ -104,32 +92,39 @@ func readConfig(configPath *string) {
}

// Setup environment variable overrides for OpenStack authentication
for _, osVarName := range cmd.OSVars {
var OSVars = []string{"username", "password", "auth_url", "user_domain_name", "project_name", "project_domain_name"}
for _, osVarName := range OSVars {
viper.BindEnv("keystone."+osVarName, "OS_"+strings.ToUpper(osVarName))
}

}

func configuredKeystoneDriver() keystone.Driver {
var keystoneIdentity = identity.Keystone{}
var mockIdentity = identity.Mock{}

func configuredKeystoneDriver() identity.Identity {
driverName := viper.GetString("hermes.keystone_driver")
switch driverName {
case "keystone":
return keystone.Keystone()
return keystoneIdentity
case "mock":
return keystone.Mock()
return mockIdentity
default:
log.Printf("Couldn't match a keystone driver for configured value \"%s\"", driverName)
return nil
}
}

func configuredStorageDriver() storage.Driver {
var elasticSearchStorage = storage.ElasticSearch{}
var mockStorage = storage.Mock{}

func configuredStorageDriver() storage.Storage {
driverName := viper.GetString("hermes.storage_driver")
switch driverName {
case "elasticsearch":
return storage.ElasticSearch()
return elasticSearchStorage
case "mock":
return storage.Mock()
return mockStorage
default:
log.Printf("Couldn't match a storage driver for configured value \"%s\"", driverName)
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"encoding/json"
"github.com/databus23/goslo.policy"
"github.com/sapcc/hermes/pkg/keystone"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
"github.com/sapcc/hermes/pkg/test"
"github.com/spf13/viper"
Expand All @@ -52,8 +52,8 @@ func setupTest(t *testing.T) http.Handler {
viper.Set("hermes.PolicyEnforcer", policyEnforcer)

//create test driver with the domains and projects from start-data.sql
keystone := keystone.Mock()
storage := storage.Mock()
keystone := identity.Mock{}
storage := storage.Mock{}
router, _ := NewV1Router(keystone, storage)
return router
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"bytes"
"fmt"
"github.com/gorilla/mux"
"github.com/sapcc/hermes/pkg/keystone"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
)

Expand All @@ -47,15 +47,15 @@ type versionLinkData struct {
}

type v1Provider struct {
keystone keystone.Driver
storage storage.Driver
keystone identity.Identity
storage storage.Storage
versionData versionData
}

//NewV1Router creates a http.Handler that serves the Hermes v1 API.
//It also returns the versionData for this API version which is needed for the
//version advertisement on "GET /".
func NewV1Router(keystone keystone.Driver, storage storage.Driver) (http.Handler, versionData) {
func NewV1Router(keystone identity.Identity, storage storage.Storage) (http.Handler, versionData) {
r := mux.NewRouter()
p := &v1Provider{
keystone: keystone,
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/sapcc/hermes/pkg/keystone"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
"github.com/sapcc/hermes/pkg/util"
"github.com/spf13/viper"
)

// Set up and start the API server, hooking it up to the API router
func Server(keystone keystone.Driver, storage storage.Driver) error {
func Server(keystone identity.Identity, storage storage.Storage) error {
fmt.Println("API")
mainRouter := mux.NewRouter()

Expand Down
79 changes: 0 additions & 79 deletions pkg/cmd/auth/token.go

This file was deleted.

73 changes: 0 additions & 73 deletions pkg/cmd/get.go

This file was deleted.

81 changes: 0 additions & 81 deletions pkg/cmd/list.go

This file was deleted.